Added helper constructors to hashmap.

Review: https://reviews.apache.org/r/35694


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/f4811eba
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/f4811eba
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/f4811eba

Branch: refs/heads/master
Commit: f4811ebaad7249b63e1f4a37e616487e2460e6aa
Parents: a6483ee
Author: Benjamin Hindman <[email protected]>
Authored: Sat Jun 20 11:51:36 2015 -0700
Committer: Benjamin Hindman <[email protected]>
Committed: Wed Jun 24 17:27:24 2015 -0700

----------------------------------------------------------------------
 .../3rdparty/stout/include/stout/hashmap.hpp    | 36 +++++++++++++++++---
 .../3rdparty/stout/tests/hashmap_tests.cpp      | 31 +++++++++++++++++
 2 files changed, 62 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/f4811eba/3rdparty/libprocess/3rdparty/stout/include/stout/hashmap.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/include/stout/hashmap.hpp 
b/3rdparty/libprocess/3rdparty/stout/include/stout/hashmap.hpp
index 4f90d3d..c967de9 100644
--- a/3rdparty/libprocess/3rdparty/stout/include/stout/hashmap.hpp
+++ b/3rdparty/libprocess/3rdparty/stout/include/stout/hashmap.hpp
@@ -41,17 +41,43 @@ public:
   // 'const hashmap<T> map;' is not an error.
   hashmap() {}
 
+  // An implicit constructor for converting from a std::map.
+  //
+  // TODO(benh): Allow any arbitrary type that supports 'begin()' and
+  // 'end()' passed into the specified 'emplace'?
+  hashmap(const std::map<Key, Value>& map)
+  {
+    boost::unordered_map<Key, Value, Hash, Equal>::reserve(map.size());
+
+    for (auto iterator = map.begin(); iterator != map.end(); ++iterator) {
+      boost::unordered_map<Key, Value, Hash, Equal>::emplace(
+          iterator->first,
+          iterator->second);
+    }
+  }
+
+  // An implicit constructor for converting from an r-value std::map.
+  //
+  // TODO(benh): Allow any arbitrary type that supports 'begin()' and
+  // 'end()' passed into the specified 'insert'?
+  hashmap(std::map<Key, Value>&& map)
+  {
+    // NOTE: We're using 'insert' here with a move iterator in order
+    // to avoid copies because we know we have an r-value paramater.
+    boost::unordered_map<Key, Value, Hash, Equal>::insert(
+        std::make_move_iterator(map.begin()),
+        std::make_move_iterator(map.end()));
+  }
+
   // Allow simple construction via initializer list.
   hashmap(std::initializer_list<std::pair<Key, Value>> list)
   {
     boost::unordered_map<Key, Value, Hash, Equal>::reserve(list.size());
 
-    // TODO(cmaloney): Use 'foreach*' once supported.
-    auto it = list.begin();
-    while (it != list.end()) {
+    for (auto iterator = list.begin(); iterator != list.end(); ++iterator) {
       boost::unordered_map<Key, Value, Hash, Equal>::emplace(
-          it->first, it->second);
-      ++it;
+          iterator->first,
+          iterator->second);
     }
   }
 

http://git-wip-us.apache.org/repos/asf/mesos/blob/f4811eba/3rdparty/libprocess/3rdparty/stout/tests/hashmap_tests.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/libprocess/3rdparty/stout/tests/hashmap_tests.cpp 
b/3rdparty/libprocess/3rdparty/stout/tests/hashmap_tests.cpp
index 6a26d93..19a0f58 100644
--- a/3rdparty/libprocess/3rdparty/stout/tests/hashmap_tests.cpp
+++ b/3rdparty/libprocess/3rdparty/stout/tests/hashmap_tests.cpp
@@ -29,6 +29,37 @@ TEST(HashMapTest, InitializerList)
 }
 
 
+TEST(HashMapTest, FromStdMap)
+{
+  std::map<int, int> map1{{1, 2}, {2, 3}};
+
+  hashmap<int, int> map2(map1);
+
+  EXPECT_EQ(2, map1.size());
+  EXPECT_EQ(2, map2.size());
+
+  EXPECT_EQ(2, map1[1]);
+  EXPECT_SOME_EQ(2, map2.get(1));
+
+  EXPECT_EQ(3, map1[2]);
+  EXPECT_SOME_EQ(3, map2.get(2));
+}
+
+
+TEST(HashMapTest, FromRValueStdMap)
+{
+  std::map<int, int> map1{{1, 2}, {2, 3}};
+
+  hashmap<int, int> map2(std::move(map1));
+
+  EXPECT_EQ(2, map1.size());
+
+  EXPECT_EQ(2, map1[1]);
+
+  EXPECT_EQ(3, map1[2]);
+}
+
+
 TEST(HashMapTest, Insert)
 {
   hashmap<string, int> map;

Reply via email to