From 7b4100794197bf8f0ff9984cd68b80966bbc46fc Mon Sep 17 00:00:00 2001 From: mxie32 Date: Fri, 14 Jun 2019 11:14:01 -0400 Subject: [PATCH 1/7] print key_values in order --- gtsam/linear/VectorValues.cpp | 31 +++++++++++++++++++++++++------ 1 file changed, 25 insertions(+), 6 deletions(-) diff --git a/gtsam/linear/VectorValues.cpp b/gtsam/linear/VectorValues.cpp index e8304e6e7..10556a073 100644 --- a/gtsam/linear/VectorValues.cpp +++ b/gtsam/linear/VectorValues.cpp @@ -128,13 +128,32 @@ namespace gtsam { v.setZero(); } - /* ************************************************************************* */ - void VectorValues::print(const string& str, const KeyFormatter& formatter) const { - cout << str << ": " << size() << " elements\n"; - for(const value_type& key_value: *this) - cout << " " << formatter(key_value.first) << ": " << key_value.second.transpose() << "\n"; - cout.flush(); +/* ************************************************************************* */ +bool compare(std::pair& lhs, std::pair& rhs) { + return lhs.first < rhs.first; +} + +void VectorValues::print(const string& str, + const KeyFormatter& formatter) const { + cout << str << ": " << size() << " elements\n"; + // Change print depending on whether we are using TBB +#ifdef GTSAM_USE_TBB + std::vector> vec; + vec.reserve(size()); + for (const value_type& key_value : *this) { + vec.push_back(std::make_pair(key_value.first, key_value.second)); } + sort(vec.begin(), vec.end(), compare); + for (const auto& key_value : vec) + cout << " " << formatter(key_value.first) << ": " + << key_value.second.transpose() << "\n"; +#else + for (const value_type& key_value : *this) + cout << " " << formatter(key_value.first) << ": " + << key_value.second.transpose() << "\n"; +#endif + cout.flush(); +} /* ************************************************************************* */ bool VectorValues::equals(const VectorValues& x, double tol) const { From 9ac72a017b588e7a734fe4bdede43a229d5ab3eb Mon Sep 17 00:00:00 2001 From: mxie32 Date: Fri, 14 Jun 2019 14:57:56 -0400 Subject: [PATCH 2/7] overload operator <<, and add unittest --- gtsam/linear/VectorValues.cpp | 51 ++++++++++++++----------- gtsam/linear/VectorValues.h | 6 +++ gtsam/linear/tests/testVectorValues.cpp | 20 ++++++++++ 3 files changed, 55 insertions(+), 22 deletions(-) diff --git a/gtsam/linear/VectorValues.cpp b/gtsam/linear/VectorValues.cpp index 10556a073..2da1a12f1 100644 --- a/gtsam/linear/VectorValues.cpp +++ b/gtsam/linear/VectorValues.cpp @@ -128,31 +128,38 @@ namespace gtsam { v.setZero(); } -/* ************************************************************************* */ -bool compare(std::pair& lhs, std::pair& rhs) { - return lhs.first < rhs.first; -} - -void VectorValues::print(const string& str, - const KeyFormatter& formatter) const { - cout << str << ": " << size() << " elements\n"; - // Change print depending on whether we are using TBB -#ifdef GTSAM_USE_TBB - std::vector> vec; - vec.reserve(size()); - for (const value_type& key_value : *this) { - vec.push_back(std::make_pair(key_value.first, key_value.second)); + /* ************************************************************************* */ + bool compare(std::pair& lhs, std::pair& rhs) { + return lhs.first < rhs.first; } - sort(vec.begin(), vec.end(), compare); - for (const auto& key_value : vec) - cout << " " << formatter(key_value.first) << ": " - << key_value.second.transpose() << "\n"; + + ostream& operator<<(ostream& ss, const VectorValues& v) { + ss << "VectorValues: " + << ": " << v.size() << " elements\n"; + // Change print depending on whether we are using TBB +#ifdef GTSAM_USE_TBB + std::vector> vec; + vec.reserve(v.size()); + for (const auto& key_value : v) { + vec.push_back(std::make_pair(key_value.first, key_value.second)); + } + sort(vec.begin(), vec.end(), compare); + for (const auto& key_value : vec) + ss << " " << key_value.first << ": " << key_value.second.transpose() + << "\n"; #else - for (const value_type& key_value : *this) - cout << " " << formatter(key_value.first) << ": " - << key_value.second.transpose() << "\n"; + for (const auto& key_value : v) + ss << " " << key_value.first << ": " << key_value.second.transpose() + << "\n"; #endif - cout.flush(); + return ss; + } + + /* ************************************************************************* */ + void VectorValues::print(const string& str, + const KeyFormatter& formatter) const { + cout << *this; + cout.flush(); } /* ************************************************************************* */ diff --git a/gtsam/linear/VectorValues.h b/gtsam/linear/VectorValues.h index b1d9de585..32a311848 100644 --- a/gtsam/linear/VectorValues.h +++ b/gtsam/linear/VectorValues.h @@ -29,6 +29,7 @@ #include #include +#include namespace gtsam { @@ -228,6 +229,11 @@ namespace gtsam { */ const_iterator find(Key j) const { return values_.find(j); } + /** + * overload operator << to print to stringstream + */ + friend std::ostream& operator<<(std::ostream& ss, const VectorValues& v); + /** print required by Testable for unit testing */ void print(const std::string& str = "VectorValues: ", const KeyFormatter& formatter = DefaultKeyFormatter) const; diff --git a/gtsam/linear/tests/testVectorValues.cpp b/gtsam/linear/tests/testVectorValues.cpp index d1d9990b0..28600ecad 100644 --- a/gtsam/linear/tests/testVectorValues.cpp +++ b/gtsam/linear/tests/testVectorValues.cpp @@ -24,6 +24,8 @@ #include #include +#include + using namespace std; using namespace boost::assign; using boost::adaptors::map_keys; @@ -228,6 +230,24 @@ TEST(VectorValues, vector_sub) EXPECT(assert_equal(expected, vv.vector(dims))); } +/* ************************************************************************* */ +TEST(VectorValues, print) +{ + VectorValues vv; + vv.insert(0, (Vector(1) << 1).finished()); + vv.insert(1, Vector2(2, 3)); + vv.insert(2, Vector2(4, 5)); + vv.insert(5, Vector2(6, 7)); + vv.insert(7, Vector2(8, 9)); + + string expected = + "VectorValues: : 5 elements\n 0: 1\n 1: 2 3\n 2: 4 5\n 5: 6 7\n 7: 8 9\n"; + stringstream actual; + actual << vv; + + EXPECT(expected == actual.str()); +} + /* ************************************************************************* */ int main() { TestResult tr; return TestRegistry::runAllTests(tr); } /* ************************************************************************* */ From 5ba91939c7215ac9392ec4e75d76c7f2c8795dc8 Mon Sep 17 00:00:00 2001 From: mxie32 Date: Fri, 14 Jun 2019 15:55:08 -0400 Subject: [PATCH 3/7] fix issues according to pr comments --- gtsam/linear/VectorValues.cpp | 24 ++++++++---------------- gtsam/linear/VectorValues.h | 10 ++++------ gtsam/linear/tests/testVectorValues.cpp | 3 +-- 3 files changed, 13 insertions(+), 24 deletions(-) diff --git a/gtsam/linear/VectorValues.cpp b/gtsam/linear/VectorValues.cpp index 2da1a12f1..bb0dbf625 100644 --- a/gtsam/linear/VectorValues.cpp +++ b/gtsam/linear/VectorValues.cpp @@ -129,30 +129,22 @@ namespace gtsam { } /* ************************************************************************* */ - bool compare(std::pair& lhs, std::pair& rhs) { - return lhs.first < rhs.first; - } - - ostream& operator<<(ostream& ss, const VectorValues& v) { - ss << "VectorValues: " + ostream& operator<<(ostream& os, const VectorValues& v) { + os << "VectorValues" << ": " << v.size() << " elements\n"; // Change print depending on whether we are using TBB #ifdef GTSAM_USE_TBB - std::vector> vec; - vec.reserve(v.size()); + map sorted; for (const auto& key_value : v) { - vec.push_back(std::make_pair(key_value.first, key_value.second)); + sorted.insert(std::make_pair(key_value.first, key_value.second)); } - sort(vec.begin(), vec.end(), compare); - for (const auto& key_value : vec) - ss << " " << key_value.first << ": " << key_value.second.transpose() - << "\n"; + for (const auto& key_value : sorted) #else for (const auto& key_value : v) - ss << " " << key_value.first << ": " << key_value.second.transpose() - << "\n"; #endif - return ss; + os << " " << key_value.first << ": " << key_value.second.transpose() + << "\n"; + return os; } /* ************************************************************************* */ diff --git a/gtsam/linear/VectorValues.h b/gtsam/linear/VectorValues.h index 32a311848..fe6b5fcb2 100644 --- a/gtsam/linear/VectorValues.h +++ b/gtsam/linear/VectorValues.h @@ -29,7 +29,7 @@ #include #include -#include +#include namespace gtsam { @@ -229,13 +229,11 @@ namespace gtsam { */ const_iterator find(Key j) const { return values_.find(j); } - /** - * overload operator << to print to stringstream - */ - friend std::ostream& operator<<(std::ostream& ss, const VectorValues& v); + /// overload operator << to print to stringstream + friend std::ostream& operator<<(std::ostream&, const VectorValues&); /** print required by Testable for unit testing */ - void print(const std::string& str = "VectorValues: ", + void print(const std::string& str = "VectorValues", const KeyFormatter& formatter = DefaultKeyFormatter) const; /** equals required by Testable for unit testing */ diff --git a/gtsam/linear/tests/testVectorValues.cpp b/gtsam/linear/tests/testVectorValues.cpp index 28600ecad..2f7e0a35c 100644 --- a/gtsam/linear/tests/testVectorValues.cpp +++ b/gtsam/linear/tests/testVectorValues.cpp @@ -241,10 +241,9 @@ TEST(VectorValues, print) vv.insert(7, Vector2(8, 9)); string expected = - "VectorValues: : 5 elements\n 0: 1\n 1: 2 3\n 2: 4 5\n 5: 6 7\n 7: 8 9\n"; + "VectorValues: 5 elements\n 0: 1\n 1: 2 3\n 2: 4 5\n 5: 6 7\n 7: 8 9\n"; stringstream actual; actual << vv; - EXPECT(expected == actual.str()); } From 8e9aa9718d4c5fa8455b24ac0ecc69ddbb7d46e1 Mon Sep 17 00:00:00 2001 From: mxie32 Date: Fri, 14 Jun 2019 16:20:41 -0400 Subject: [PATCH 4/7] use map::emplace instead of insert --- gtsam/linear/VectorValues.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/gtsam/linear/VectorValues.cpp b/gtsam/linear/VectorValues.cpp index bb0dbf625..288b4471a 100644 --- a/gtsam/linear/VectorValues.cpp +++ b/gtsam/linear/VectorValues.cpp @@ -136,14 +136,16 @@ namespace gtsam { #ifdef GTSAM_USE_TBB map sorted; for (const auto& key_value : v) { - sorted.insert(std::make_pair(key_value.first, key_value.second)); + sorted.emplace(std::make_pair(key_value.first, key_value.second)); } for (const auto& key_value : sorted) #else for (const auto& key_value : v) #endif + { os << " " << key_value.first << ": " << key_value.second.transpose() << "\n"; + } return os; } From c087dd336ba2af674d7b561b222af2da14409ebc Mon Sep 17 00:00:00 2001 From: mxie32 Date: Fri, 14 Jun 2019 16:44:45 -0400 Subject: [PATCH 5/7] delete make_pair --- gtsam/linear/VectorValues.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gtsam/linear/VectorValues.cpp b/gtsam/linear/VectorValues.cpp index 288b4471a..976f65971 100644 --- a/gtsam/linear/VectorValues.cpp +++ b/gtsam/linear/VectorValues.cpp @@ -136,7 +136,7 @@ namespace gtsam { #ifdef GTSAM_USE_TBB map sorted; for (const auto& key_value : v) { - sorted.emplace(std::make_pair(key_value.first, key_value.second)); + sorted.emplace(key_value.first, key_value.second); } for (const auto& key_value : sorted) #else From 45244d3ffdfc01537fba88df8f03501088fafc53 Mon Sep 17 00:00:00 2001 From: mxie32 Date: Sun, 16 Jun 2019 10:31:35 -0400 Subject: [PATCH 6/7] move cout string to print function --- gtsam/linear/VectorValues.cpp | 3 +-- gtsam/linear/tests/testVectorValues.cpp | 3 ++- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/gtsam/linear/VectorValues.cpp b/gtsam/linear/VectorValues.cpp index 976f65971..1d2ce3847 100644 --- a/gtsam/linear/VectorValues.cpp +++ b/gtsam/linear/VectorValues.cpp @@ -130,8 +130,6 @@ namespace gtsam { /* ************************************************************************* */ ostream& operator<<(ostream& os, const VectorValues& v) { - os << "VectorValues" - << ": " << v.size() << " elements\n"; // Change print depending on whether we are using TBB #ifdef GTSAM_USE_TBB map sorted; @@ -152,6 +150,7 @@ namespace gtsam { /* ************************************************************************* */ void VectorValues::print(const string& str, const KeyFormatter& formatter) const { + cout << str << ": " << size() << " elements\n"; cout << *this; cout.flush(); } diff --git a/gtsam/linear/tests/testVectorValues.cpp b/gtsam/linear/tests/testVectorValues.cpp index 2f7e0a35c..af6be8aa9 100644 --- a/gtsam/linear/tests/testVectorValues.cpp +++ b/gtsam/linear/tests/testVectorValues.cpp @@ -239,9 +239,10 @@ TEST(VectorValues, print) vv.insert(2, Vector2(4, 5)); vv.insert(5, Vector2(6, 7)); vv.insert(7, Vector2(8, 9)); + vv.print(); string expected = - "VectorValues: 5 elements\n 0: 1\n 1: 2 3\n 2: 4 5\n 5: 6 7\n 7: 8 9\n"; + " 0: 1\n 1: 2 3\n 2: 4 5\n 5: 6 7\n 7: 8 9\n"; stringstream actual; actual << vv; EXPECT(expected == actual.str()); From addbfe82344bd8f97375b9aa587504badcab6b55 Mon Sep 17 00:00:00 2001 From: mxie32 Date: Sun, 16 Jun 2019 11:53:41 -0400 Subject: [PATCH 7/7] use key_formatter in print function --- gtsam/linear/VectorValues.cpp | 4 ++-- gtsam/linear/tests/testVectorValues.cpp | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/gtsam/linear/VectorValues.cpp b/gtsam/linear/VectorValues.cpp index 1d2ce3847..cd3ae815b 100644 --- a/gtsam/linear/VectorValues.cpp +++ b/gtsam/linear/VectorValues.cpp @@ -141,7 +141,7 @@ namespace gtsam { for (const auto& key_value : v) #endif { - os << " " << key_value.first << ": " << key_value.second.transpose() + os << " " << StreamedKey(key_value.first) << ": " << key_value.second.transpose() << "\n"; } return os; @@ -151,7 +151,7 @@ namespace gtsam { void VectorValues::print(const string& str, const KeyFormatter& formatter) const { cout << str << ": " << size() << " elements\n"; - cout << *this; + cout << key_formatter(formatter) << *this; cout.flush(); } diff --git a/gtsam/linear/tests/testVectorValues.cpp b/gtsam/linear/tests/testVectorValues.cpp index af6be8aa9..f97f96aaf 100644 --- a/gtsam/linear/tests/testVectorValues.cpp +++ b/gtsam/linear/tests/testVectorValues.cpp @@ -17,6 +17,7 @@ #include #include +#include #include @@ -239,7 +240,6 @@ TEST(VectorValues, print) vv.insert(2, Vector2(4, 5)); vv.insert(5, Vector2(6, 7)); vv.insert(7, Vector2(8, 9)); - vv.print(); string expected = " 0: 1\n 1: 2 3\n 2: 4 5\n 5: 6 7\n 7: 8 9\n";