Skip to content

Commit afd3330

Browse files
committed
added VectorValues::sorted()
1 parent 11409b0 commit afd3330

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

gtsam/linear/VectorValues.cpp

+14-8
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ namespace gtsam {
6464
}
6565
}
6666

67+
/* ************************************************************************ */
68+
std::map<Key, Vector> VectorValues::sorted() const {
69+
std::map<Key, Vector> ordered;
70+
for (const auto& kv : *this) ordered.emplace(kv);
71+
return ordered;
72+
}
73+
6774
/* ************************************************************************ */
6875
VectorValues VectorValues::Zero(const VectorValues& other)
6976
{
@@ -130,11 +137,7 @@ namespace gtsam {
130137
GTSAM_EXPORT std::ostream& operator<<(std::ostream& os, const VectorValues& v) {
131138
// Change print depending on whether we are using TBB
132139
#ifdef GTSAM_USE_TBB
133-
std::map<Key, Vector> sorted;
134-
for (const auto& [key, value] : v) {
135-
sorted.emplace(key, value);
136-
}
137-
for (const auto& [key, value] : sorted)
140+
for (const auto& [key, value] : v.sorted())
138141
#else
139142
for (const auto& [key,value] : v)
140143
#endif
@@ -176,7 +179,12 @@ namespace gtsam {
176179
// Copy vectors
177180
Vector result(totalDim);
178181
DenseIndex pos = 0;
182+
#ifdef GTSAM_USE_TBB
183+
// TBB uses un-ordered map, so inefficiently order them:
184+
for (const auto& [key, value] : sorted()) {
185+
#else
179186
for (const auto& [key, value] : *this) {
187+
#endif
180188
result.segment(pos, value.size()) = value;
181189
pos += value.size();
182190
}
@@ -392,9 +400,7 @@ namespace gtsam {
392400
// Print out all rows.
393401
#ifdef GTSAM_USE_TBB
394402
// TBB uses un-ordered map, so inefficiently order them:
395-
std::map<Key, Vector> ordered;
396-
for (const auto& kv : *this) ordered.emplace(kv);
397-
for (const auto& kv : ordered) {
403+
for (const auto& kv : sorted()) {
398404
#else
399405
for (const auto& kv : *this) {
400406
#endif

gtsam/linear/VectorValues.h

+3
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,9 @@ namespace gtsam {
7777
typedef ConcurrentMap<Key, Vector> Values; ///< Collection of Vectors making up a VectorValues
7878
Values values_; ///< Vectors making up this VectorValues
7979

80+
/** Sort by key (primarily for use with TBB, which uses an unordered map)*/
81+
std::map<Key, Vector> sorted() const;
82+
8083
public:
8184
typedef Values::iterator iterator; ///< Iterator over vector values
8285
typedef Values::const_iterator const_iterator; ///< Const iterator over vector values

0 commit comments

Comments
 (0)