You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by be...@apache.org on 2015/02/08 16:51:30 UTC
[03/20] mesos git commit: Add initializer_list constructor to hashmap.
Add initializer_list constructor to hashmap.
Simplified signature from the C++11 standard specification.
Review: https://reviews.apache.org/r/28252
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/dc9be770
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/dc9be770
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/dc9be770
Branch: refs/heads/master
Commit: dc9be770a71ebaaab1cfc2fc4c4a87846faed9bd
Parents: d4dd716
Author: Cody Maloney <co...@mesosphere.io>
Authored: Fri Jan 30 18:02:10 2015 -0800
Committer: Benjamin Hindman <be...@gmail.com>
Committed: Sat Feb 7 14:42:45 2015 -0800
----------------------------------------------------------------------
.../3rdparty/stout/include/stout/hashmap.hpp | 17 ++++++++++++++---
.../3rdparty/stout/tests/hashmap_tests.cpp | 16 ++++++++++++++++
2 files changed, 30 insertions(+), 3 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mesos/blob/dc9be770/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 aa4d9ba..24dc369 100644
--- a/3rdparty/libprocess/3rdparty/stout/include/stout/hashmap.hpp
+++ b/3rdparty/libprocess/3rdparty/stout/include/stout/hashmap.hpp
@@ -39,6 +39,19 @@ public:
// 'const hashmap<T> map;' is not an error.
hashmap() {}
+ // Allow simple construction via initializer list.
+ hashmap(std::initializer_list<std::pair<Key, Value>> list)
+ {
+ boost::unordered_map<Key, Value>::reserve(list.size());
+
+ // TODO(cmaloney): Use 'foreach*' once supported.
+ auto it = list.begin();
+ while (it != list.end()) {
+ boost::unordered_map<Key, Value>::emplace(it->first, it->second);
+ ++it;
+ }
+ }
+
// Checks whether this map contains a binding for a key.
bool contains(const Key& key) const
{
@@ -67,9 +80,7 @@ public:
// Returns an Option for the binding to the key.
Option<Value> get(const Key& key) const
{
- typedef typename boost::unordered_map<Key, Value>::const_iterator
- const_iterator;
- const_iterator it = boost::unordered_map<Key, Value>::find(key);
+ auto it = boost::unordered_map<Key, Value>::find(key);
if (it == boost::unordered_map<Key, Value>::end()) {
return None();
}
http://git-wip-us.apache.org/repos/asf/mesos/blob/dc9be770/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 eb3abfc..e8a932e 100644
--- a/3rdparty/libprocess/3rdparty/stout/tests/hashmap_tests.cpp
+++ b/3rdparty/libprocess/3rdparty/stout/tests/hashmap_tests.cpp
@@ -10,6 +10,22 @@
using std::string;
+TEST(HashMapTest, InitializerList)
+{
+ hashmap<string, int> map{{"hello", 1}};
+ EXPECT_EQ(1, map.size());
+
+ EXPECT_TRUE((hashmap<int, int>{}.empty()));
+
+ hashmap<int, int> map2{{1, 2}, {2, 3}, {3, 4}};
+ EXPECT_EQ(3, map2.size());
+ EXPECT_SOME_EQ(2, map2.get(1));
+ EXPECT_SOME_EQ(3, map2.get(2));
+ EXPECT_SOME_EQ(4, map2.get(3));
+ EXPECT_NONE(map2.get(4));
+}
+
+
TEST(HashMapTest, Insert)
{
hashmap<string, int> map;