diff --git a/gtsam/nonlinear/Values.h b/gtsam/nonlinear/Values.h index 9833a1a86..f469fe8e5 100644 --- a/gtsam/nonlinear/Values.h +++ b/gtsam/nonlinear/Values.h @@ -124,20 +124,17 @@ namespace gtsam { struct _KeyValuePair { const Key key; ///< The key ValueType& value; ///< The value - const Key& first; ///< For std::pair compatibility, the key - ValueType& second; ///< For std::pair compatibility, the value - _KeyValuePair(Key _key, ValueType& _value) : key(_key), value(_value), first(key), second(value) {} + _KeyValuePair(Key _key, ValueType& _value) : key(_key), value(_value) {} }; template struct _ConstKeyValuePair { const Key key; ///< The key const ValueType& value; ///< The value - const Key& first; ///< For std::pair compatibility, the key - const ValueType& second; ///< For std::pair compatibility, the value - _ConstKeyValuePair(Key _key, const Value& _value) : key(_key), value(_value), first(key), second(value) {} + _ConstKeyValuePair(Key _key, const ValueType& _value) : key(_key), value(_value) {} + _ConstKeyValuePair(const _KeyValuePair& rhs) : key(rhs.key), value(rhs.value) {} }; public: @@ -150,13 +147,6 @@ namespace gtsam { public: typedef _KeyValuePair KeyValuePair; - private: - typedef boost::transformed_range< - KeyValuePair(*)(Values::KeyValuePair key_value), - const boost::filtered_range< - boost::function, - const boost::iterator_range > > Base; - public: /** Returns the number of values in this view */ size_t size() const { typename Base::iterator it = this->begin(); @@ -168,6 +158,12 @@ namespace gtsam { private: + typedef boost::transformed_range< + KeyValuePair(*)(Values::KeyValuePair key_value), + const boost::filtered_range< + boost::function, + const boost::iterator_range > > Base; + Filtered(const Base& base) : Base(base) {} friend class Values; @@ -182,14 +178,16 @@ namespace gtsam { public: typedef _ConstKeyValuePair KeyValuePair; - private: - typedef boost::transformed_range< - KeyValuePair(*)(Values::ConstKeyValuePair key_value), - const boost::filtered_range< - boost::function, - const boost::iterator_range > > Base; + /** Conversion from Filtered to ConstFiltered */ + ConstFiltered(const Filtered& rhs) : Base( + boost::adaptors::transform( + boost::adaptors::filter( + boost::make_iterator_range( + const_iterator(rhs.begin().base().base().base(), &make_const_deref_pair), + const_iterator(rhs.end().base().base().base(), &make_const_deref_pair)), + rhs.begin().base().predicate()), + &castHelper, ConstKeyValuePair>)) {} - public: /** Returns the number of values in this view */ size_t size() const { typename Base::const_iterator it = this->begin(); @@ -201,6 +199,12 @@ namespace gtsam { private: + typedef boost::transformed_range< + KeyValuePair(*)(Values::ConstKeyValuePair key_value), + const boost::filtered_range< + boost::function, + const boost::iterator_range > > Base; + ConstFiltered(const Base& base) : Base(base) {} friend class Values; diff --git a/gtsam/nonlinear/tests/testValues.cpp b/gtsam/nonlinear/tests/testValues.cpp index 5f98e94a0..6ccd3b083 100644 --- a/gtsam/nonlinear/tests/testValues.cpp +++ b/gtsam/nonlinear/tests/testValues.cpp @@ -300,9 +300,9 @@ TEST(Values, filter) { // Filter by type i = 0; - Values::Filtered pose_filtered = values.filter(); + Values::ConstFiltered pose_filtered = values.filter(); EXPECT_LONGS_EQUAL(2, pose_filtered.size()); - BOOST_FOREACH(const Values::Filtered::KeyValuePair& key_value, pose_filtered) { + BOOST_FOREACH(const Values::ConstFiltered::KeyValuePair& key_value, pose_filtered) { if(i == 0) { EXPECT_LONGS_EQUAL(1, key_value.key); EXPECT(assert_equal(pose1, key_value.value));