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");
183 std::array<std::unique_ptr<Shard>, Shards>
shards_;
227 shard = std::make_unique<Shard>(hash_fct,
cmp);
258 std::unique_lock lock(shard.
mutex);
285 std::unique_lock lock(shard.
mutex);
309 std::unique_lock lock(shard.
mutex);
334 std::unique_lock lock(shard.
mutex);
363 std::shared_lock lock(shard.
mutex);
378 [[nodiscard]] std::optional<T>
find_copy(
const Key &key)
const
379 requires std::is_copy_constructible_v<T>
382 std::shared_lock lock(shard.
mutex);
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);
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);
486 [[nodiscard]]
size_t size()
const
489 for (
const auto &shard :
shards_)
491 std::shared_lock lock(shard->mutex);
492 total += shard->map.size();
507 for (
const auto &shard :
shards_)
509 std::shared_lock lock(shard->mutex);
510 if (not shard->map.is_empty())
534 requires
std::is_copy_constructible_v<Key>
and std::is_copy_constructible_v<
T>
538 for (
const auto &shard :
shards_)
540 std::shared_lock lock(shard->mutex);
545 reserved += shard->map.size();
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 ...
void remove_by_data(Data &data)
Removes from the table the key whose pointer must be the result of a prior insertion or search.
size_t(*)(const Key &) Hash_Fct_Ptr
Pair * insert(const Key &key, const Data &data)
Inserts into the hash map the pair (key, record) indexed by key.
Pair * search(const Key &key) noexcept(nothrow_search< const Key & >)
Searches for the pair whose key is key.
bool contains(const Key &key) const noexcept(nothrow_search< const Key & >)
Checks if a key exists in the map; an alias for has().
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Main namespace for Aleph-w library functions.
std::decay_t< typename HeadC::Item_Type > T
and
Check uniqueness with explicit hash + equality functors.
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.