diff options
author | Qingyang Chen <qingyang.chen@10gen.com> | 2015-07-16 14:38:03 -0400 |
---|---|---|
committer | Qingyang Chen <qingyang.chen@10gen.com> | 2015-07-23 10:43:46 -0400 |
commit | 84921ca797830b4adaedae01a4357d2bfccfb0a6 (patch) | |
tree | 483c3a19bbf9d0cceda078c418048278cbdeaf93 /src/mongo | |
parent | 162fb82bed75ede539bbb960f44d232f8b8e5848 (diff) | |
download | mongo-84921ca797830b4adaedae01a4357d2bfccfb0a6.tar.gz |
SERVER-19450 UnorderedFastKeyTable/StringMap construct with std::initializer_list
Diffstat (limited to 'src/mongo')
-rw-r--r-- | src/mongo/util/string_map.h | 16 | ||||
-rw-r--r-- | src/mongo/util/string_map_test.cpp | 10 | ||||
-rw-r--r-- | src/mongo/util/unordered_fast_key_table.h | 2 | ||||
-rw-r--r-- | src/mongo/util/unordered_fast_key_table_internal.h | 13 |
4 files changed, 33 insertions, 8 deletions
diff --git a/src/mongo/util/string_map.h b/src/mongo/util/string_map.h index 724b03b9124..13e93977fcc 100644 --- a/src/mongo/util/string_map.h +++ b/src/mongo/util/string_map.h @@ -55,11 +55,11 @@ struct StringMapDefaultConvertorOther { }; template <typename V> -class StringMap : public UnorderedFastKeyTable<StringData, // K_L - std::string, // K_S - V, // V - StringMapDefaultHash, - StringMapDefaultEqual, - StringMapDefaultConvertor, - StringMapDefaultConvertorOther> {}; -} +using StringMap = UnorderedFastKeyTable<StringData, // K_L + std::string, // K_S + V, + StringMapDefaultHash, + StringMapDefaultEqual, + StringMapDefaultConvertor, + StringMapDefaultConvertorOther>; +} // namespace mongo diff --git a/src/mongo/util/string_map_test.cpp b/src/mongo/util/string_map_test.cpp index 6e49d34bdc5..0fbcfe2d28f 100644 --- a/src/mongo/util/string_map_test.cpp +++ b/src/mongo/util/string_map_test.cpp @@ -205,4 +205,14 @@ TEST(StringMapTest, Assign) { y = m; ASSERT_EQUALS(5, y["eliot"]); } + +TEST(StringMapTest, InitWithInitializerList) { + StringMap<int> smap{ + {"q", 1}, {"coollog", 2}, {"mango", 3}, {"mango", 4}, + }; + + ASSERT_EQ(1, smap["q"]); + ASSERT_EQ(2, smap["coollog"]); + ASSERT_EQ(3, smap["mango"]); +} } diff --git a/src/mongo/util/unordered_fast_key_table.h b/src/mongo/util/unordered_fast_key_table.h index 781cc1e3fe1..aa42b786ce3 100644 --- a/src/mongo/util/unordered_fast_key_table.h +++ b/src/mongo/util/unordered_fast_key_table.h @@ -103,6 +103,8 @@ public: UnorderedFastKeyTable(const UnorderedFastKeyTable& other); + UnorderedFastKeyTable(std::initializer_list<std::pair<key_type, mapped_type>> entries); + UnorderedFastKeyTable& operator=(const UnorderedFastKeyTable& other) { other.copyTo(this); return *this; diff --git a/src/mongo/util/unordered_fast_key_table_internal.h b/src/mongo/util/unordered_fast_key_table_internal.h index bddca264a9f..22ea22561a0 100644 --- a/src/mongo/util/unordered_fast_key_table_internal.h +++ b/src/mongo/util/unordered_fast_key_table_internal.h @@ -122,6 +122,19 @@ inline UnorderedFastKeyTable<K_L, K_S, V, H, E, C, C_LS>::UnorderedFastKeyTable( _convertorOther(other._convertorOther) {} template <typename K_L, typename K_S, typename V, typename H, typename E, typename C, typename C_LS> +inline UnorderedFastKeyTable<K_L, K_S, V, H, E, C, C_LS>::UnorderedFastKeyTable( + std::initializer_list<std::pair<key_type, mapped_type>> entries) + : UnorderedFastKeyTable<K_L, K_S, V, H, E, C, C_LS>() { + for (auto&& entry : entries) { + // Only insert the entry if the key is not equivalent to the key of any other element + // already in the table. + if (find(entry.first) == end()) { + get(entry.first) = entry.second; + } + } +} + +template <typename K_L, typename K_S, typename V, typename H, typename E, typename C, typename C_LS> inline void UnorderedFastKeyTable<K_L, K_S, V, H, E, C, C_LS>::copyTo( UnorderedFastKeyTable* out) const { out->_size = _size; |