Merge pull request #1468 from serverglen/flat_map_insert

Implement FlatMap insert(const std::pair<key_type, mapped_type>& kv)
This commit is contained in:
Weibing Wang
2022-03-14 10:21:15 +08:00
committed by GitHub
3 changed files with 16 additions and 3 deletions
+5
View File
@@ -167,6 +167,11 @@ public:
// Returns address of the inserted value, NULL on error.
mapped_type* insert(const key_type& key, const mapped_type& value);
// Insert a pair of {key, value}. If size()*100/bucket_count() is
// more than load_factor(), a resize() will be done.
// Returns address of the inserted value, NULL on error.
mapped_type* insert(const std::pair<key_type, mapped_type>& kv);
// Remove |key| and the associated value
// Returns: 1 on erased, 0 otherwise.
// Remove all items. Allocated spaces are NOT returned by system.
+5
View File
@@ -375,6 +375,11 @@ _T* FlatMap<_K, _T, _H, _E, _S>::insert(const key_type& key,
return p;
}
template <typename _K, typename _T, typename _H, typename _E, bool _S>
_T* FlatMap<_K, _T, _H, _E, _S>::insert(const std::pair<key_type, mapped_type>& kv) {
return insert(kv.first, kv.second);
}
template <typename _K, typename _T, typename _H, typename _E, bool _S>
template <typename K2>
size_t FlatMap<_K, _T, _H, _E, _S>::erase(const K2& key, _T* old_value) {
+6 -3
View File
@@ -287,10 +287,13 @@ TEST_F(FlatMapTest, make_sure_all_methods_compile) {
ASSERT_EQ(20, m1[2]);
ASSERT_EQ(2u, m1.size());
m1.insert(1, 100);
m1.insert({3, 30});
ASSERT_EQ(100, m1[1]);
ASSERT_EQ(2u, m1.size());
ASSERT_EQ(NULL, m1.seek(3));
ASSERT_EQ(0u, m1.erase(3));
ASSERT_EQ(3u, m1.size());
ASSERT_TRUE(m1.seek(3));
ASSERT_EQ(NULL, m1.seek(4));
ASSERT_EQ(1u, m1.erase(3));
ASSERT_EQ(0u, m1.erase(4));
ASSERT_EQ(2u, m1.size());
ASSERT_EQ(1u, m1.erase(2));
ASSERT_EQ(1u, m1.size());