Renamed the thread-safe "contains" to if_contains.

Add tests to parallel_flat_hash_map_mutex_test
This commit is contained in:
Brian McKinnon
2020-05-02 20:45:46 -05:00
parent 131a84f6ed
commit bf7203b3c8
5 changed files with 11 additions and 14 deletions
+6 -9
View File
@@ -3393,15 +3393,11 @@ public:
return Policy::value(&*it); return Policy::value(&*it);
} }
template <class K = key_type>
bool contains(const key_arg<K>& key) const {
return Base::contains(key);
}
template <class K = key_type, class V = mapped_type> template <class K = key_type, class V = mapped_type>
bool contains(const key_arg<K>& key, V& v) const { std::enable_if_t<std::is_assignable<mapped_type&, V>::value,
bool> if_contains(const key_arg<K>& key, V& v) const {
typename Lockable::SharedLock m; typename Lockable::SharedLock m;
auto it = const_cast<parallel_hash_map *>(this)->find(key, Base::hash(key), m); auto it = const_cast<parallel_hash_map *>(this)->find(key, this->hash(key), m);
if (it == this->end()) if (it == this->end())
return false; return false;
v = Policy::value(&*it); v = Policy::value(&*it);
@@ -3409,9 +3405,10 @@ public:
} }
template <class K = key_type, class F> template <class K = key_type, class F>
bool if_contains(const key_arg<K>& key, F&& f) const { std::enable_if_t<!std::is_assignable<mapped_type&, F>::value && std::is_invocable<F, mapped_type&>::value,
bool> if_contains(const key_arg<K>& key, F&& f) const {
typename Lockable::SharedLock m; typename Lockable::SharedLock m;
auto it = const_cast<parallel_hash_map*>(this)->find(key, Base::hash(key), m); auto it = const_cast<parallel_hash_map*>(this)->find(key, this->hash(key), m);
if (it == this->end()) if (it == this->end())
return false; return false;
std::forward<F>(f)(Policy::value(&*it)); std::forward<F>(f)(Policy::value(&*it));
+1 -1
View File
@@ -9,4 +9,4 @@
#define THIS_EXTRA_TPL_PARAMS , 4, boost::upgrade_mutex #define THIS_EXTRA_TPL_PARAMS , 4, boost::upgrade_mutex
#endif #endif
#include "flat_hash_map_test.cc" #include "parallel_hash_map_test.cc"
-1
View File
@@ -1,5 +1,4 @@
#define THIS_HASH_MAP parallel_flat_hash_map #define THIS_HASH_MAP parallel_flat_hash_map
#define THIS_TEST_NAME ParallelFlatHashMap #define THIS_TEST_NAME ParallelFlatHashMap
#include "flat_hash_map_test.cc"
#include "parallel_hash_map_test.cc" #include "parallel_hash_map_test.cc"
+4 -2
View File
@@ -3,6 +3,8 @@
#define THIS_TEST_NAME ParallelFlatHashMap #define THIS_TEST_NAME ParallelFlatHashMap
#endif #endif
#include "flat_hash_map_test.cc"
namespace phmap { namespace phmap {
namespace container_internal { namespace container_internal {
namespace { namespace {
@@ -12,10 +14,10 @@ TEST(THIS_TEST_NAME, ThreadSafeContains) {
// Test that the nodes have the proper API. // Test that the nodes have the proper API.
ThisMap<int, int> m = { {1, 7}, {2, 9} }; ThisMap<int, int> m = { {1, 7}, {2, 9} };
auto val = 0; auto val = 0;
EXPECT_TRUE(m.contains(1, val)); EXPECT_TRUE(m.if_contains(1, val));
EXPECT_EQ(val, 7); EXPECT_EQ(val, 7);
EXPECT_FALSE(m.contains(3, val)); EXPECT_FALSE(m.if_contains(3, val));
#if __cplusplus > 199711L #if __cplusplus > 199711L
auto func = [&val](int& v) { val = v; }; auto func = [&val](int& v) { val = v; };
-1
View File
@@ -1,5 +1,4 @@
#define THIS_HASH_MAP parallel_node_hash_map #define THIS_HASH_MAP parallel_node_hash_map
#define THIS_TEST_NAME ParallelNodeHashMap #define THIS_TEST_NAME ParallelNodeHashMap
#include "flat_hash_map_test.cc"
#include "parallel_hash_map_test.cc" #include "parallel_hash_map_test.cc"