117#ifndef TPL_CONCURRENT_HASH_MAP_H
118#define TPL_CONCURRENT_HASH_MAP_H
124#include <shared_mutex>
125#include <type_traits>
150template <
typename Key,
typename T,
155 static_assert(
Shards > 0,
"ConcurrentHashMap requires at least one shard");
227 shard = std::make_unique<Shard>(hash_fct,
cmp);
258 std::unique_lock lock(
shard.mutex);
259 return shard.map.insert(key,
value) !=
nullptr;
285 std::unique_lock lock(
shard.mutex);
286 return shard.map.insert(key, std::move(
value)) !=
nullptr;
309 std::unique_lock lock(
shard.mutex);
334 std::unique_lock lock(
shard.mutex);
363 std::shared_lock lock(
shard.mutex);
364 return shard.map.contains(key);
379 requires std::is_copy_constructible_v<T>
382 std::shared_lock lock(
shard.mutex);
383 const auto *
pair =
shard.map.search(key);
408 template <
typename F>
411 using Result = std::invoke_result_t<F, const T &>;
412 static_assert(
not std::is_reference_v<Result>,
413 "ConcurrentHashMap::with_value callback must not return a reference");
415 std::shared_lock lock(
shard.mutex);
416 const auto *
pair =
shard.map.search(key);
419 std::invoke(std::forward<F>(f), std::as_const(
pair->second));
441 template <
typename F>
444 using Result = std::invoke_result_t<F, T &>;
445 static_assert(
not std::is_reference_v<Result>,
446 "ConcurrentHashMap::with_value_mut callback must not return a reference");
448 std::unique_lock lock(
shard.mutex);
452 std::invoke(std::forward<F>(f),
pair->second);
471 std::unique_lock lock(
shard->mutex);
491 std::shared_lock lock(
shard->mutex);
509 std::shared_lock lock(
shard->mutex);
540 std::shared_lock lock(
shard->mutex);
547 for (
const auto &entry :
shard->map)
548 result.append(entry);
Exception handling system with formatted messages for Aleph-w.
size_t size_t int32_t value
Simple dynamic array with automatic resizing and functional operations.
void reserve(size_t cap)
Reserves cap cells into the array.
Sharded concurrent hash map: Shards independently-locked DynMapHashTable partitions,...
std::array< std::unique_ptr< Shard >, Shards > shards_
Shard & shard_for(const Key &key) noexcept
Resolve which shard owns key.
bool insert(const Key &key, T &&value)
Insert key with value moved in, only if key is absent.
bool contains(const Key &key) const
Check whether key is present.
void insert_or_assign(const Key &key, T value)
Insert key with value, or overwrite the existing value if key is already present.
bool is_empty() const
Check whether every shard is currently empty.
typename Map::Hash_Fct_Ptr Hash_Fct_Ptr
Hash function pointer type, matching DynMapHashTable's own.
bool with_value_mut(const Key &key, F &&f)
Invoke f with a mutable reference to key's value, while the owning shard is write-locked.
const Shard & shard_for(const Key &key) const noexcept
const overload of shard_for(const Key&).
size_t size() const
Return the total number of entries across all shards.
bool with_value(const Key &key, F &&f) const
Invoke f with a const reference to key's value, while the owning shard is read-locked.
ConcurrentHashMap & operator=(const ConcurrentHashMap &)=delete
Deleted copy assignment operator.
ConcurrentHashMap(const ConcurrentHashMap &)=delete
Deleted copy constructor: shards own non-copyable std::shared_mutex instances.
std::optional< T > find_copy(const Key &key) const
Look up key and return a copy of its value, if present.
ConcurrentHashMap & operator=(ConcurrentHashMap &&)=delete
Deleted move assignment operator.
bool insert(const Key &key, const T &value)
Insert key with a copy of value, only if key is absent.
void clear()
Remove every entry from every shard.
ConcurrentHashMap(Hash_Fct_Ptr hash_fct=dft_hash_ptr_fct< Key >, const Cmp &cmp=Cmp())
Construct an empty map with Shards independently-locked partitions.
bool erase(const Key &key)
Remove key if present.
ConcurrentHashMap(ConcurrentHashMap &&)=delete
Deleted move constructor: no defined ordering exists to safely transfer locked shards out from under ...
size_t(*)(const Key &) Hash_Fct_Ptr
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Main namespace for Aleph-w library functions.
and
Check uniqueness with explicit hash + equality functors.
std::decay_t< typename HeadC::Item_Type > T
std::pair< First, Second > pair
Alias to std::pair kept for backwards compatibility.
const unsigned long DefaultPrime
Default prime number used when no specific size is requested.
Shard(Hash_Fct_Ptr hash_fct, const Cmp &cmp)
Dynamic set implementations based on hash tables.