mirror of
https://github.com/greg7mdp/parallel-hashmap.git
synced 2026-08-29 08:34:39 +08:00
Add "if_contains" to parallel_hash_map so the value does not need to be copied to be read in a thread-safe manner
Add tests for parallel_hash_map thread-safe functions.
This commit is contained in:
Vendored
+10
@@ -3408,6 +3408,16 @@ public:
|
||||
return true;
|
||||
}
|
||||
|
||||
template <class K = key_type, class F>
|
||||
bool if_contains(const key_arg<K>& key, F&& f) {
|
||||
typename Lockable::SharedLock m;
|
||||
auto it = const_cast<parallel_hash_map*>(this)->find(key, hash(key), m);
|
||||
if (it == this->end())
|
||||
return false;
|
||||
std::forward<F>(f)(Policy::value(&*it));
|
||||
return true;
|
||||
}
|
||||
|
||||
template <class K = key_type, class P = Policy, K* = nullptr>
|
||||
MappedReference<P> operator[](key_arg<K>&& key) {
|
||||
return Policy::value(&*try_emplace(std::forward<K>(key)).first);
|
||||
|
||||
@@ -244,6 +244,7 @@ TEST(THIS_TEST_NAME, MergeExtractInsert) {
|
||||
m.insert(std::move(node));
|
||||
EXPECT_THAT(m, UnorderedElementsAre(Pair(1, 17), Pair(2, 9)));
|
||||
}
|
||||
|
||||
#if !defined(__ANDROID__) && !defined(__APPLE__) && !defined(__EMSCRIPTEN__) && defined(PHMAP_HAVE_STD_ANY)
|
||||
TEST(THIS_TEST_NAME, Any) {
|
||||
ThisMap<int, std::any> m;
|
||||
|
||||
@@ -2,3 +2,4 @@
|
||||
#define THIS_TEST_NAME ParallelFlatHashMap
|
||||
|
||||
#include "flat_hash_map_test.cc"
|
||||
#include "parallel_hash_map_test.cc"
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
#ifndef THIS_HASH_MAP
|
||||
#define THIS_HASH_MAP parallel_flat_hash_map
|
||||
#define THIS_TEST_NAME ParallelFlatHashMap
|
||||
#endif
|
||||
|
||||
namespace phmap {
|
||||
namespace container_internal {
|
||||
namespace {
|
||||
|
||||
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<int, int> m = { {1, 7}, {2, 9} };
|
||||
auto val = 0;
|
||||
EXPECT_TRUE(m.contains(1, val));
|
||||
EXPECT_EQ(val, 7);
|
||||
|
||||
EXPECT_FALSE(m.contains(3, val));
|
||||
|
||||
#if __cplusplus > 199711L
|
||||
auto func = [&val](int& v) { val = v; };
|
||||
EXPECT_TRUE(m.if_contains(2, func));
|
||||
EXPECT_EQ(val, 9);
|
||||
|
||||
EXPECT_FALSE(m.if_contains(3, func));
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace container_internal
|
||||
} // namespace phmap
|
||||
@@ -2,3 +2,4 @@
|
||||
#define THIS_TEST_NAME ParallelNodeHashMap
|
||||
|
||||
#include "flat_hash_map_test.cc"
|
||||
#include "parallel_hash_map_test.cc"
|
||||
|
||||
Reference in New Issue
Block a user