You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by bm...@apache.org on 2017/08/23 22:23:54 UTC

[11/13] mesos git commit: Implemented LinkedHashMap copy construction and assignment.

Implemented LinkedHashMap copy construction and assignment.

The defaults being generated were incorrect. No code is relying
on them today, this was discovered when writing new code.

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


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

Branch: refs/heads/master
Commit: a663be9021e2aabe372db7b47696c31d8b7c5436
Parents: d7c62b8
Author: Benjamin Mahler <bm...@apache.org>
Authored: Mon Aug 14 22:08:31 2017 -0700
Committer: Benjamin Mahler <bm...@apache.org>
Committed: Wed Aug 23 15:09:45 2017 -0700

----------------------------------------------------------------------
 3rdparty/stout/include/stout/linkedhashmap.hpp | 29 +++++++++++
 3rdparty/stout/tests/linkedhashmap_tests.cpp   | 57 +++++++++++++++++++++
 2 files changed, 86 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/a663be90/3rdparty/stout/include/stout/linkedhashmap.hpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/include/stout/linkedhashmap.hpp b/3rdparty/stout/include/stout/linkedhashmap.hpp
index 35684e4..33cc86a 100644
--- a/3rdparty/stout/include/stout/linkedhashmap.hpp
+++ b/3rdparty/stout/include/stout/linkedhashmap.hpp
@@ -33,6 +33,35 @@ public:
   typedef std::list<entry> list;
   typedef hashmap<Key, typename list::iterator> map;
 
+  LinkedHashMap() = default;
+
+  LinkedHashMap(const LinkedHashMap<Key, Value>& other)
+    : entries_(other.entries_)
+  {
+    // Build up the index.
+    for (auto it = entries_.begin(); it != entries_.end(); ++it) {
+      keys_[it->first] = it;
+    }
+  }
+
+  LinkedHashMap& operator=(const LinkedHashMap<Key, Value>& other)
+  {
+    clear();
+
+    entries_ = other.entries_;
+
+    // Build up the index.
+    for (auto it = entries_.begin(); it != entries_.end(); ++it) {
+      keys_[it->first] = it;
+    }
+
+    return *this;
+  }
+
+  // TODO(bmahler): Implement move construction / assignment.
+  LinkedHashMap(LinkedHashMap<Key, Value>&&) = delete;
+  LinkedHashMap& operator=(LinkedHashMap&&) = delete;
+
   Value& operator[] (const Key& key)
   {
     if (!keys_.contains(key)) {

http://git-wip-us.apache.org/repos/asf/mesos/blob/a663be90/3rdparty/stout/tests/linkedhashmap_tests.cpp
----------------------------------------------------------------------
diff --git a/3rdparty/stout/tests/linkedhashmap_tests.cpp b/3rdparty/stout/tests/linkedhashmap_tests.cpp
index 267022e..712752a 100644
--- a/3rdparty/stout/tests/linkedhashmap_tests.cpp
+++ b/3rdparty/stout/tests/linkedhashmap_tests.cpp
@@ -182,3 +182,60 @@ TEST(LinkedHashMapTest, ForeachMutate)
   list<string> values = {"foo", "qux", "caz"};
   EXPECT_EQ(values, map.values());
 }
+
+
+// TODO(bmahler): Simplify this test once LinkedHashMap
+// has equality operators.
+TEST(LinkedHashMapTest, CopyConstruction)
+{
+  LinkedHashMap<int, string> map;
+
+  map[1] = "1";
+  map[2] = "2";
+  map[3] = "3";
+
+  LinkedHashMap<int, string> copy(map);
+
+  EXPECT_EQ(map.keys(), copy.keys());
+  EXPECT_EQ(map.values(), copy.values());
+
+  EXPECT_EQ(1u, map.erase(1));
+  EXPECT_EQ(1u, copy.erase(1));
+
+  EXPECT_EQ(map.keys(), copy.keys());
+  EXPECT_EQ(map.values(), copy.values());
+
+  copy[4] = "4";
+
+  EXPECT_NE(map.keys(), copy.keys());
+  EXPECT_NE(map.values(), copy.values());
+}
+
+
+// TODO(bmahler): Simplify this test once LinkedHashMap
+// has equality operators.
+TEST(LinkedHashMapTest, Assignment)
+{
+  LinkedHashMap<int, string> map;
+
+  map[1] = "1";
+  map[2] = "2";
+  map[3] = "3";
+
+  LinkedHashMap<int, string> copy;
+  copy = map;
+
+  EXPECT_EQ(map.keys(), copy.keys());
+  EXPECT_EQ(map.values(), copy.values());
+
+  EXPECT_EQ(1u, map.erase(1));
+  EXPECT_EQ(1u, copy.erase(1));
+
+  EXPECT_EQ(map.keys(), copy.keys());
+  EXPECT_EQ(map.values(), copy.values());
+
+  copy[4] = "4";
+
+  EXPECT_NE(map.keys(), copy.keys());
+  EXPECT_NE(map.values(), copy.values());
+}