Add erase_if custom API - issue #84

This commit is contained in:
greg
2021-03-15 11:20:49 -04:00
parent 8335afbbb6
commit 08c10a02d5
2 changed files with 39 additions and 0 deletions
+29
View File
@@ -3622,6 +3622,17 @@ public:
return modify_if_impl<K, F, typename Lockable::UniqueLock>(key, std::forward<F>(f));
}
// if map contains key, lambda is called with the mapped value (under write lock protection).
// If the lambda returns true, the key is subsequently erased from the map (the write lock
// is only released after erase).
// returns true if key was erased, false otherwise.
// ----------------------------------------------------------------------------------------------------
template <class K = key_type, class F>
bool erase_if(const key_arg<K>& key, F&& f) {
return erase_if_impl<K, F, typename Lockable::UniqueLock>(key, std::forward<F>(f));
}
// if map does not contains key, it is inserted and the mapped value is value-constructed
// with the provided arguments (if any), as with try_emplace.
// if map already contains key, then the lambda is called with the mapped value (under
@@ -3670,6 +3681,24 @@ private:
return true;
}
template <class K = key_type, class F, class L>
bool erase_if_impl(const key_arg<K>& key, F&& f) {
#if __cplusplus >= 201703L
static_assert(std::is_invocable<F, mapped_type&>::value);
#endif
L m;
auto it = this->template find<K, L>(key, this->hash(key), m);
if (it == this->end())
return false;
if (std::forward<F>(f)(Policy::value(&*it)))
{
this->erase(it);
return true;
}
return false;
}
template <class K, class V>
std::pair<iterator, bool> insert_or_assign_impl(K&& k, V&& v) {
typename Lockable::UniqueLock m;