diff --git a/cpp/LieConfig-inl.h b/cpp/LieConfig-inl.h index e1a92aef4..d4e19ed8f 100644 --- a/cpp/LieConfig-inl.h +++ b/cpp/LieConfig-inl.h @@ -67,6 +67,14 @@ namespace gtsam { dim_ += cfg.dim_; } + template + std::list LieConfig::keys() const { + std::list ret; + BOOST_FOREACH(const typename Values::value_type& v, values_) + ret.push_back(v.first); + return ret; + } + template void LieConfig::erase(const J& j) { size_t dim; // unused diff --git a/cpp/LieConfig.h b/cpp/LieConfig.h index 264094b19..ab5678129 100644 --- a/cpp/LieConfig.h +++ b/cpp/LieConfig.h @@ -10,6 +10,7 @@ #pragma once #include +#include #include "Vector.h" #include "Testable.h" @@ -93,6 +94,12 @@ namespace gtsam { */ void erase(const J& j, size_t& dim); + /** + * Returns a set of keys in the config + * Note: by construction, the list is ordered + */ + std::list keys() const; + /** Replace all keys and variables */ LieConfig& operator=(const LieConfig& rhs) { values_ = rhs.values_; diff --git a/cpp/testLieConfig.cpp b/cpp/testLieConfig.cpp index 962b6c2c4..14281f675 100644 --- a/cpp/testLieConfig.cpp +++ b/cpp/testLieConfig.cpp @@ -8,6 +8,9 @@ #include #include +#include // for operator += +using namespace boost::assign; + #define GTSAM_MAGIC_KEY #include @@ -147,6 +150,28 @@ TEST(LieConfig, expmap_d) CHECK(config0.equals(config0)); } +/* ************************************************************************* */ +TEST(LieConfig, extract_keys) +{ + typedef TypedSymbol PoseKey; + LieConfig config; + + config.insert(PoseKey(1), Pose2()); + config.insert(PoseKey(2), Pose2()); + config.insert(PoseKey(4), Pose2()); + config.insert(PoseKey(5), Pose2()); + + list expected, actual; + expected += PoseKey(1), PoseKey(2), PoseKey(4), PoseKey(5); + actual = config.keys(); + + CHECK(actual.size() == expected.size()); + list::const_iterator itAct = actual.begin(), itExp = expected.begin(); + for (; itAct != actual.end() && itExp != expected.end(); ++itAct, ++itExp) { + CHECK(assert_equal(*itExp, *itAct)); + } +} + /* ************************************************************************* */ int main() { TestResult tr; return TestRegistry::runAllTests(tr); } /* ************************************************************************* */