Skip to content

Commit

Permalink
Merge pull request #1740 from arutkowski/VectorValues_sorted
Browse files Browse the repository at this point in the history
  • Loading branch information
varunagrawal authored Apr 4, 2024
2 parents 11409b0 + afd3330 commit 4abef92
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
22 changes: 14 additions & 8 deletions gtsam/linear/VectorValues.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -130,11 +137,7 @@ namespace gtsam {
GTSAM_EXPORT std::ostream& operator<<(std::ostream& os, const VectorValues& v) {
// Change print depending on whether we are using TBB
#ifdef GTSAM_USE_TBB
std::map<Key, Vector> sorted;
for (const auto& [key, value] : v) {
sorted.emplace(key, value);
}
for (const auto& [key, value] : sorted)
for (const auto& [key, value] : v.sorted())
#else
for (const auto& [key,value] : v)
#endif
Expand Down Expand Up @@ -176,7 +179,12 @@ namespace gtsam {
// Copy vectors
Vector result(totalDim);
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) {
#endif
result.segment(pos, value.size()) = value;
pos += value.size();
}
Expand Down Expand Up @@ -392,9 +400,7 @@ namespace gtsam {
// Print out all rows.
#ifdef GTSAM_USE_TBB
// TBB uses un-ordered map, so inefficiently order them:
std::map<Key, Vector> ordered;
for (const auto& kv : *this) ordered.emplace(kv);
for (const auto& kv : ordered) {
for (const auto& kv : sorted()) {
#else
for (const auto& kv : *this) {
#endif
Expand Down
3 changes: 3 additions & 0 deletions gtsam/linear/VectorValues.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ namespace gtsam {
typedef ConcurrentMap<Key, Vector> Values; ///< Collection of Vectors making up a 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:
typedef Values::iterator iterator; ///< Iterator over vector values
typedef Values::const_iterator const_iterator; ///< Const iterator over vector values
Expand Down

0 comments on commit 4abef92

Please sign in to comment.