diff --git a/gtsam/nonlinear/Values-inl.h b/gtsam/nonlinear/Values-inl.h index a354e0139..04c6440a2 100644 --- a/gtsam/nonlinear/Values-inl.h +++ b/gtsam/nonlinear/Values-inl.h @@ -163,14 +163,14 @@ namespace gtsam { &ValuesCastHelper::cast)), constBegin_( boost::make_transform_iterator( boost::make_filter_iterator(filter, - ((const Values&) values).begin(), - ((const Values&) values).end()), + values._begin(), + values._end()), &ValuesCastHelper::cast)), constEnd_( boost::make_transform_iterator( boost::make_filter_iterator(filter, - ((const Values&) values).end(), - ((const Values&) values).end()), + values._end(), + values._end()), &ValuesCastHelper::cast)) { } diff --git a/gtsam/nonlinear/Values.h b/gtsam/nonlinear/Values.h index f93323202..48c9bd1ad 100644 --- a/gtsam/nonlinear/Values.h +++ b/gtsam/nonlinear/Values.h @@ -187,23 +187,36 @@ namespace gtsam { /// @name Iterator /// @{ - struct const_iterator { + struct deref_iterator { using const_iterator_type = typename KeyValueMap::const_iterator; const_iterator_type it_; - const_iterator(const_iterator_type it) : it_(it) {} - std::pair operator*() const { - return {it_->first, *(it_->second)}; + deref_iterator(const_iterator_type it) : it_(it) {} + ConstKeyValuePair operator*() const { return {it_->first, *(it_->second)}; } + std::unique_ptr operator->() { + return std::make_unique(it_->first, *(it_->second)); } - bool operator==(const const_iterator& other) const { return it_ == other.it_; } - bool operator!=(const const_iterator& other) const { return it_ != other.it_; } - const_iterator& operator++() { + bool operator==(const deref_iterator& other) const { + return it_ == other.it_; + } + bool operator!=(const deref_iterator& other) const { return it_ != other.it_; } + deref_iterator& operator++() { ++it_; return *this; } }; - const_iterator begin() { return const_iterator(values_.begin()); } - const_iterator end() { return const_iterator(values_.end()); } + deref_iterator begin() const { return deref_iterator(values_.begin()); } + deref_iterator end() const { return deref_iterator(values_.end()); } + + /** Find an element by key, returning an iterator, or end() if the key was + * not found. */ + deref_iterator find(Key j) const { return deref_iterator(values_.find(j)); } + + /** Find the element greater than or equal to the specified key. */ + deref_iterator lower_bound(Key j) const { return deref_iterator(values_.lower_bound(j)); } + + /** Find the lowest-ordered element greater than the specified key. */ + deref_iterator upper_bound(Key j) const { return deref_iterator(values_.upper_bound(j)); } /// @} /// @name Manifold Operations @@ -363,8 +376,8 @@ namespace gtsam { static KeyValuePair make_deref_pair(const KeyValueMap::iterator::value_type& key_value) { return KeyValuePair(key_value.first, *key_value.second); } - const_iterator begin() const { return boost::make_transform_iterator(values_.begin(), &make_const_deref_pair); } - const_iterator end() const { return boost::make_transform_iterator(values_.end(), &make_const_deref_pair); } + const_iterator _begin() const { return boost::make_transform_iterator(values_.begin(), &make_const_deref_pair); } + const_iterator _end() const { return boost::make_transform_iterator(values_.end(), &make_const_deref_pair); } iterator begin() { return boost::make_transform_iterator(values_.begin(), &make_deref_pair); } iterator end() { return boost::make_transform_iterator(values_.end(), &make_deref_pair); } const_reverse_iterator rbegin() const { return boost::make_transform_iterator(values_.rbegin(), &make_const_deref_pair); } @@ -376,22 +389,12 @@ namespace gtsam { * not found. */ iterator find(Key j) { return boost::make_transform_iterator(values_.find(j), &make_deref_pair); } - /** Find an element by key, returning an iterator, or end() if the key was - * not found. */ - const_iterator find(Key j) const { return boost::make_transform_iterator(values_.find(j), &make_const_deref_pair); } - /** Find the element greater than or equal to the specified key. */ iterator lower_bound(Key j) { return boost::make_transform_iterator(values_.lower_bound(j), &make_deref_pair); } - /** Find the element greater than or equal to the specified key. */ - const_iterator lower_bound(Key j) const { return boost::make_transform_iterator(values_.lower_bound(j), &make_const_deref_pair); } - /** Find the lowest-ordered element greater than the specified key. */ iterator upper_bound(Key j) { return boost::make_transform_iterator(values_.upper_bound(j), &make_deref_pair); } - /** Find the lowest-ordered element greater than the specified key. */ - const_iterator upper_bound(Key j) const { return boost::make_transform_iterator(values_.upper_bound(j), &make_const_deref_pair); } - /** A filtered view of a Values, returned from Values::filter. */ template class Filtered; diff --git a/gtsam/nonlinear/tests/testValues.cpp b/gtsam/nonlinear/tests/testValues.cpp index e554a28b1..41bd6aead 100644 --- a/gtsam/nonlinear/tests/testValues.cpp +++ b/gtsam/nonlinear/tests/testValues.cpp @@ -203,7 +203,6 @@ TEST(Values, basic_functions) } EXPECT_LONGS_EQUAL(4, count); -#ifdef GTSAM_ALLOW_DEPRECATED_SINCE_V42 // find EXPECT_LONGS_EQUAL(4, values.find(4)->key); EXPECT_LONGS_EQUAL(4, values_c.find(4)->key); @@ -219,7 +218,6 @@ TEST(Values, basic_functions) EXPECT_LONGS_EQUAL(6, values_c.upper_bound(4)->key); EXPECT_LONGS_EQUAL(4, values.upper_bound(3)->key); EXPECT_LONGS_EQUAL(4, values_c.upper_bound(3)->key); -#endif } /* ************************************************************************* */