diff --git a/gtsam/hybrid/HybridJunctionTree.cpp b/gtsam/hybrid/HybridJunctionTree.cpp index 70cfc4be4..b7aefcc92 100644 --- a/gtsam/hybrid/HybridJunctionTree.cpp +++ b/gtsam/hybrid/HybridJunctionTree.cpp @@ -129,9 +129,9 @@ struct HybridConstructorTraversalData { // Check if we should merge the i^th child if (nrParents + nrFrontals == childConditionals[i]->nrParents()) { const bool myType = - data.discreteKeys.exists(conditional->frontals()[0]); + data.discreteKeys.exists(conditional->frontals().front()); const bool theirType = - data.discreteKeys.exists(childConditionals[i]->frontals()[0]); + data.discreteKeys.exists(childConditionals[i]->frontals().front()); if (myType == theirType) { // Increment number of frontal variables diff --git a/gtsam/inference/Conditional.h b/gtsam/inference/Conditional.h index 9f08e5623..a45e3df23 100644 --- a/gtsam/inference/Conditional.h +++ b/gtsam/inference/Conditional.h @@ -70,12 +70,33 @@ namespace gtsam { /// Typedef to this class typedef Conditional This; + public: + /** A mini implementation of an iterator range, to share const + * views of frontals and parents. */ + typedef std::pair ConstFactorRange; + struct ConstFactorRangeIterator { + ConstFactorRange range_; + // Delete default constructor + ConstFactorRangeIterator() = delete; + ConstFactorRangeIterator(ConstFactorRange const& x) : range_(x) {} + // Implement begin and end for iteration + typename FACTOR::const_iterator begin() const { return range_.first; } + typename FACTOR::const_iterator end() const { return range_.second; } + size_t size() const { return std::distance(range_.first, range_.second); } + const auto& front() const { return *begin(); } + // == operator overload for comparison with another iterator + template + bool operator==(const OTHER& rhs) const { + return std::equal(begin(), end(), rhs.begin()); + } + }; + /** View of the frontal keys (call frontals()) */ - typedef boost::iterator_range Frontals; + typedef ConstFactorRangeIterator Frontals; /** View of the separator keys (call parents()) */ - typedef boost::iterator_range Parents; + typedef ConstFactorRangeIterator Parents; protected: /// @name Standard Constructors @@ -121,10 +142,10 @@ namespace gtsam { } /** return a view of the frontal keys */ - Frontals frontals() const { return boost::make_iterator_range(beginFrontals(), endFrontals()); } + Frontals frontals() const { return ConstFactorRangeIterator({beginFrontals(), endFrontals()});} /** return a view of the parent keys */ - Parents parents() const { return boost::make_iterator_range(beginParents(), endParents()); } + Parents parents() const { return ConstFactorRangeIterator({beginParents(), endParents()}); } /** * All conditional types need to implement a `logProbability` function, for which diff --git a/gtsam/linear/tests/testJacobianFactor.cpp b/gtsam/linear/tests/testJacobianFactor.cpp index 5b3be6492..df8aef2bd 100644 --- a/gtsam/linear/tests/testJacobianFactor.cpp +++ b/gtsam/linear/tests/testJacobianFactor.cpp @@ -55,7 +55,7 @@ TEST(JacobianFactor, constructors_and_accessors) { // b vector only constructor JacobianFactor expected( - boost::make_iterator_range(terms.begin(), terms.begin()), b); + std::vector(terms.begin(), terms.begin()), b); JacobianFactor actual(b); EXPECT(assert_equal(expected, actual)); EXPECT(assert_equal(b, expected.getb())); @@ -66,7 +66,7 @@ TEST(JacobianFactor, constructors_and_accessors) { // One term constructor JacobianFactor expected( - boost::make_iterator_range(terms.begin(), terms.begin() + 1), b, noise); + std::vector(terms.begin(), terms.begin() + 1), b, noise); JacobianFactor actual(terms[0].first, terms[0].second, b, noise); EXPECT(assert_equal(expected, actual)); LONGS_EQUAL((long)terms[0].first, (long)actual.keys().back()); @@ -79,7 +79,7 @@ TEST(JacobianFactor, constructors_and_accessors) { // Two term constructor JacobianFactor expected( - boost::make_iterator_range(terms.begin(), terms.begin() + 2), b, noise); + std::vector(terms.begin(), terms.begin() + 2), b, noise); JacobianFactor actual(terms[0].first, terms[0].second, terms[1].first, terms[1].second, b, noise); EXPECT(assert_equal(expected, actual)); @@ -93,7 +93,7 @@ TEST(JacobianFactor, constructors_and_accessors) { // Three term constructor JacobianFactor expected( - boost::make_iterator_range(terms.begin(), terms.begin() + 3), b, noise); + std::vector(terms.begin(), terms.begin() + 3), b, noise); JacobianFactor actual(terms[0].first, terms[0].second, terms[1].first, terms[1].second, terms[2].first, terms[2].second, b, noise); EXPECT(assert_equal(expected, actual)); @@ -107,7 +107,7 @@ TEST(JacobianFactor, constructors_and_accessors) { // Test three-term constructor with std::map JacobianFactor expected( - boost::make_iterator_range(terms.begin(), terms.begin() + 3), b, noise); + std::vector(terms.begin(), terms.begin() + 3), b, noise); map mapTerms; // note order of insertion plays no role: order will be determined by keys mapTerms.insert(terms[2]); @@ -125,7 +125,7 @@ TEST(JacobianFactor, constructors_and_accessors) { // VerticalBlockMatrix constructor JacobianFactor expected( - boost::make_iterator_range(terms.begin(), terms.begin() + 3), b, noise); + std::vector(terms.begin(), terms.begin() + 3), b, noise); VerticalBlockMatrix blockMatrix(Dims{3, 3, 3, 1}, 3); blockMatrix(0) = terms[0].second; blockMatrix(1) = terms[1].second; diff --git a/gtsam_unstable/linear/tests/testLinearEquality.cpp b/gtsam_unstable/linear/tests/testLinearEquality.cpp index db9aefe18..2cd6e8ffd 100644 --- a/gtsam_unstable/linear/tests/testLinearEquality.cpp +++ b/gtsam_unstable/linear/tests/testLinearEquality.cpp @@ -46,7 +46,7 @@ TEST(LinearEquality, constructors_and_accessors) { { // One term constructor LinearEquality expected( - boost::make_iterator_range(terms.begin(), terms.begin() + 1), b, 0); + std::vector(terms.begin(), terms.begin() + 1), b, 0); LinearEquality actual(terms[0].first, terms[0].second, b, 0); EXPECT(assert_equal(expected, actual)); LONGS_EQUAL((long)terms[0].first, (long)actual.keys().back()); @@ -58,7 +58,7 @@ TEST(LinearEquality, constructors_and_accessors) { { // Two term constructor LinearEquality expected( - boost::make_iterator_range(terms.begin(), terms.begin() + 2), b, 0); + std::vector(terms.begin(), terms.begin() + 2), b, 0); LinearEquality actual(terms[0].first, terms[0].second, terms[1].first, terms[1].second, b, 0); EXPECT(assert_equal(expected, actual)); @@ -71,7 +71,7 @@ TEST(LinearEquality, constructors_and_accessors) { { // Three term constructor LinearEquality expected( - boost::make_iterator_range(terms.begin(), terms.begin() + 3), b, 0); + std::vector(terms.begin(), terms.begin() + 3), b, 0); LinearEquality actual(terms[0].first, terms[0].second, terms[1].first, terms[1].second, terms[2].first, terms[2].second, b, 0);