diff --git a/gtsam/nonlinear/Values.h b/gtsam/nonlinear/Values.h index 5a8005db4..9833a1a86 100644 --- a/gtsam/nonlinear/Values.h +++ b/gtsam/nonlinear/Values.h @@ -151,11 +151,22 @@ namespace gtsam { typedef _KeyValuePair KeyValuePair; private: - typedef boost::transformed_range< - KeyValuePair(*)(Values::KeyValuePair key_value), - const boost::filtered_range< - boost::function, - const boost::iterator_range > > Base; + 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(); + size_t i = 0; + for (; it!=this->end(); ++it) + ++i; + return i; + } + + private: Filtered(const Base& base) : Base(base) {} @@ -178,6 +189,18 @@ namespace gtsam { boost::function, const boost::iterator_range > > Base; + public: + /** Returns the number of values in this view */ + size_t size() const { + typename Base::const_iterator it = this->begin(); + size_t i = 0; + for (; it!=this->end(); ++it) + ++i; + return i; + } + + private: + ConstFiltered(const Base& base) : Base(base) {} friend class Values; diff --git a/gtsam/nonlinear/tests/testValues.cpp b/gtsam/nonlinear/tests/testValues.cpp index 064e22e58..5f98e94a0 100644 --- a/gtsam/nonlinear/tests/testValues.cpp +++ b/gtsam/nonlinear/tests/testValues.cpp @@ -274,6 +274,7 @@ TEST(Values, filter) { // Filter by key int i = 0; Values::Filtered filtered = values.filter(boost::bind(std::greater_equal(), _1, 2)); + EXPECT_LONGS_EQUAL(2, filtered.size()); BOOST_FOREACH(const Values::Filtered<>::KeyValuePair& key_value, filtered) { if(i == 0) { LONGS_EQUAL(2, key_value.key); @@ -300,6 +301,7 @@ TEST(Values, filter) { // Filter by type i = 0; Values::Filtered pose_filtered = values.filter(); + EXPECT_LONGS_EQUAL(2, pose_filtered.size()); BOOST_FOREACH(const Values::Filtered::KeyValuePair& key_value, pose_filtered) { if(i == 0) { EXPECT_LONGS_EQUAL(1, key_value.key);