From 339871041a05395d87ab6c21eb81dd8b0f302832 Mon Sep 17 00:00:00 2001 From: greg Date: Fri, 7 Aug 2020 12:33:23 -0400 Subject: [PATCH 1/4] cleanup some duplicated headers --- parallel_hashmap/btree.h | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/parallel_hashmap/btree.h b/parallel_hashmap/btree.h index 0cb44a4..7c73162 100644 --- a/parallel_hashmap/btree.h +++ b/parallel_hashmap/btree.h @@ -54,25 +54,12 @@ #pragma warning(disable : 5045) // Compiler will insert Spectre mitigation for memory load if /Qspectre switch specified #endif -#include -#include -#include -#include -#include -#include #include #include #include -#include -#include #include #include -#include -#include -#include -#include -#include #include "phmap_fwd_decl.h" #include "phmap_base.h" From 1904f5c8869323b8c17385a69b92d4449f2aa5e2 Mon Sep 17 00:00:00 2001 From: greg Date: Fri, 7 Aug 2020 12:35:21 -0400 Subject: [PATCH 2/4] document that dump() works only for `flat` hash maps - issue #59 --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2f38906..98aa2b9 100644 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ This repository aims to provide a set of excellent **hash map** implementations, - Easy to **forward declare**: just include `phmap_fwd_decl.h` in your header files to forward declare Parallel Hashmap containers [note: this does not work currently for hash maps with pointer keys] -- **Dump/load** feature: when a hash map stores data that is `std::trivially_copyable`, the table can be dumped to disk and restored as a single array, very efficiently, and without requiring any hash computation. This is typically about 10 times faster than doing element-wise serialization to disk, but it will use 10% to 60% extra disk space. See `examples/serialize.cc`. _(hash map/set only)_ +- **Dump/load** feature: when a `flat` hash map stores data that is `std::trivially_copyable`, the table can be dumped to disk and restored as a single array, very efficiently, and without requiring any hash computation. This is typically about 10 times faster than doing element-wise serialization to disk, but it will use 10% to 60% extra disk space. See `examples/serialize.cc`. _(flat hash map/set only)_ - **Tested** on Windows (vs2015 & vs2017, vs2019, Intel compiler 18 and 19), linux (g++ 4.8.4, 5, 6, 7, 8, clang++ 3.9, 4.0, 5.0) and MacOS (g++ and clang++) - click on travis and appveyor icons above for detailed test status. From 7bbf9dc04144247aa8fd862b5fafc3e691a80264 Mon Sep 17 00:00:00 2001 From: greg Date: Fri, 7 Aug 2020 12:58:43 -0400 Subject: [PATCH 3/4] issue #60: add non-const if_contains() which can update the mapped_value. --- parallel_hashmap/phmap.h | 15 +++++++++++++++ tests/parallel_hash_map_test.cc | 15 ++++++++++----- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/parallel_hashmap/phmap.h b/parallel_hashmap/phmap.h index f1b7996..cd98170 100644 --- a/parallel_hashmap/phmap.h +++ b/parallel_hashmap/phmap.h @@ -3431,12 +3431,27 @@ public: #endif typename Lockable::SharedLock m; auto it = const_cast(this)->find(key, this->hash(key), m); + if (it == this->end()) + return false; + std::forward(f)((const mapped_type&)Policy::value(&*it)); + return true; + } + + template + bool if_contains(const key_arg& key, F&& f) { +#if __cplusplus >= 201703L + static_assert(std::is_invocable::value); +#endif + typename Lockable::UniqueLock m; + auto it = this->find(key, this->hash(key), m); if (it == this->end()) return false; std::forward(f)(Policy::value(&*it)); return true; } + + template MappedReference

operator[](key_arg&& key) { return Policy::value(&*try_emplace(std::forward(key)).first); diff --git a/tests/parallel_hash_map_test.cc b/tests/parallel_hash_map_test.cc index fcac843..662a949 100644 --- a/tests/parallel_hash_map_test.cc +++ b/tests/parallel_hash_map_test.cc @@ -13,13 +13,18 @@ TEST(THIS_TEST_NAME, ThreadSafeContains) { // We can't test mutable keys, or non-copyable keys with ThisMap. // Test that the nodes have the proper API. ThisMap m = { {1, 7}, {2, 9} }; - auto val = 0; - - auto func = [&val](int& v) { val = v; }; - EXPECT_TRUE(m.if_contains(2, func)); + const ThisMap& const_m(m); + + auto val = 0; + auto get_value = [&val](const int& v) { val = v; }; + EXPECT_TRUE(const_m.if_contains(2, get_value)); EXPECT_EQ(val, 9); - EXPECT_FALSE(m.if_contains(3, func)); + EXPECT_FALSE(m.if_contains(3, get_value)); + + auto set_value = [&val](int& v) { v = 11; }; + EXPECT_TRUE(m.if_contains(2, set_value)); + EXPECT_EQ(m[2], 11); } } // namespace From 17f90489903aa63874f55538e70cd732ef185d21 Mon Sep 17 00:00:00 2001 From: greg Date: Fri, 7 Aug 2020 13:10:31 -0400 Subject: [PATCH 4/4] change mutable if_contains to modify_if --- parallel_hashmap/phmap.h | 2 +- tests/parallel_hash_map_test.cc | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/parallel_hashmap/phmap.h b/parallel_hashmap/phmap.h index cd98170..6ca41ad 100644 --- a/parallel_hashmap/phmap.h +++ b/parallel_hashmap/phmap.h @@ -3438,7 +3438,7 @@ public: } template - bool if_contains(const key_arg& key, F&& f) { + bool modify_if(const key_arg& key, F&& f) { #if __cplusplus >= 201703L static_assert(std::is_invocable::value); #endif diff --git a/tests/parallel_hash_map_test.cc b/tests/parallel_hash_map_test.cc index 662a949..48241eb 100644 --- a/tests/parallel_hash_map_test.cc +++ b/tests/parallel_hash_map_test.cc @@ -23,8 +23,10 @@ TEST(THIS_TEST_NAME, ThreadSafeContains) { EXPECT_FALSE(m.if_contains(3, get_value)); auto set_value = [&val](int& v) { v = 11; }; - EXPECT_TRUE(m.if_contains(2, set_value)); + EXPECT_TRUE(m. modify_if(2, set_value)); EXPECT_EQ(m[2], 11); + + EXPECT_FALSE(m.modify_if(3, set_value)); } } // namespace