Skip to content

Commit

Permalink
implement erase using erase_if_impl (#223)
Browse files Browse the repository at this point in the history
  • Loading branch information
greg7mdp authored Nov 30, 2023
1 parent 4c93cf0 commit 0f0ecf7
Showing 1 changed file with 8 additions and 19 deletions.
27 changes: 8 additions & 19 deletions parallel_hashmap/phmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -3322,11 +3322,11 @@ class parallel_hash_set
// ----------------------------------------------------------------------------------------------------
template <class K = key_type, class F>
bool erase_if(const key_arg<K>& key, F&& f) {
return erase_if_impl<K, F, ReadWriteLock>(key, std::forward<F>(f));
return !!erase_if_impl<K, F, ReadWriteLock>(key, std::forward<F>(f));
}

template <class K = key_type, class F, class L>
bool erase_if_impl(const key_arg<K>& key, F&& f) {
size_type erase_if_impl(const key_arg<K>& key, F&& f) {
#if __cplusplus >= 201703L
static_assert(std::is_invocable<F, value_type&>::value);
#endif
Expand All @@ -3336,19 +3336,19 @@ class parallel_hash_set
L m(inner);
auto it = set.find(key, hashval);
if (it == set.end())
return false;
return 0;
if (m.switch_to_unique()) {
// we did an unlock/lock, need to call `find()` again
it = set.find(key, hashval);
if (it == set.end())
return false;
return 0;
}
if (std::forward<F>(f)(const_cast<value_type &>(*it)))
{
set._erase(it);
return true;
return 1;
}
return false;
return 0;
}

// if map already contains key, the first lambda is called with the mapped value (under
Expand Down Expand Up @@ -3462,19 +3462,8 @@ class parallel_hash_set
// --------------------------------------------------------------------
template <class K = key_type>
size_type erase(const key_arg<K>& key) {
auto hashval = this->hash(key);
Inner& inner = sets_[subidx(hashval)];
auto& set = inner.set_;
typename Lockable::ReadWriteLock m(inner);
auto it = set.find(key, hashval);
if (it == set.end())
return 0;

if (m.switch_to_unique()) {
it = set.find(key, hashval);
}
set._erase(it);
return 1;
auto always_erase = [](const value_type&){ return true; };
return erase_if_impl<K, decltype(always_erase), ReadWriteLock>(key, std::move(always_erase));
}

// --------------------------------------------------------------------
Expand Down

0 comments on commit 0f0ecf7

Please sign in to comment.