Merge pull request #1740 from arutkowski/VectorValues_sorted

release/4.3a0
Varun Agrawal 2024-04-04 08:52:33 -04:00 committed by GitHub
commit 4abef9248e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 8 deletions

View File

@ -64,6 +64,13 @@ namespace gtsam {
} }
} }
/* ************************************************************************ */
std::map<Key, Vector> VectorValues::sorted() const {
std::map<Key, Vector> ordered;
for (const auto& kv : *this) ordered.emplace(kv);
return ordered;
}
/* ************************************************************************ */ /* ************************************************************************ */
VectorValues VectorValues::Zero(const VectorValues& other) VectorValues VectorValues::Zero(const VectorValues& other)
{ {
@ -130,11 +137,7 @@ namespace gtsam {
GTSAM_EXPORT std::ostream& operator<<(std::ostream& os, const VectorValues& v) { GTSAM_EXPORT std::ostream& operator<<(std::ostream& os, const VectorValues& v) {
// Change print depending on whether we are using TBB // Change print depending on whether we are using TBB
#ifdef GTSAM_USE_TBB #ifdef GTSAM_USE_TBB
std::map<Key, Vector> sorted; for (const auto& [key, value] : v.sorted())
for (const auto& [key, value] : v) {
sorted.emplace(key, value);
}
for (const auto& [key, value] : sorted)
#else #else
for (const auto& [key,value] : v) for (const auto& [key,value] : v)
#endif #endif
@ -176,7 +179,12 @@ namespace gtsam {
// Copy vectors // Copy vectors
Vector result(totalDim); Vector result(totalDim);
DenseIndex pos = 0; DenseIndex pos = 0;
#ifdef GTSAM_USE_TBB
// TBB uses un-ordered map, so inefficiently order them:
for (const auto& [key, value] : sorted()) {
#else
for (const auto& [key, value] : *this) { for (const auto& [key, value] : *this) {
#endif
result.segment(pos, value.size()) = value; result.segment(pos, value.size()) = value;
pos += value.size(); pos += value.size();
} }
@ -392,9 +400,7 @@ namespace gtsam {
// Print out all rows. // Print out all rows.
#ifdef GTSAM_USE_TBB #ifdef GTSAM_USE_TBB
// TBB uses un-ordered map, so inefficiently order them: // TBB uses un-ordered map, so inefficiently order them:
std::map<Key, Vector> ordered; for (const auto& kv : sorted()) {
for (const auto& kv : *this) ordered.emplace(kv);
for (const auto& kv : ordered) {
#else #else
for (const auto& kv : *this) { for (const auto& kv : *this) {
#endif #endif

View File

@ -77,6 +77,9 @@ namespace gtsam {
typedef ConcurrentMap<Key, Vector> Values; ///< Collection of Vectors making up a VectorValues typedef ConcurrentMap<Key, Vector> Values; ///< Collection of Vectors making up a VectorValues
Values values_; ///< Vectors making up this VectorValues Values values_; ///< Vectors making up this VectorValues
/** Sort by key (primarily for use with TBB, which uses an unordered map)*/
std::map<Key, Vector> sorted() const;
public: public:
typedef Values::iterator iterator; ///< Iterator over vector values typedef Values::iterator iterator; ///< Iterator over vector values
typedef Values::const_iterator const_iterator; ///< Const iterator over vector values typedef Values::const_iterator const_iterator; ///< Const iterator over vector values