55#ifndef TPL_PERSISTENT_HASH_MAP_H
56#define TPL_PERSISTENT_HASH_MAP_H
111template <
typename Key,
typename T,
class Cmp = Aleph::equal_to<Key>>
114 static_assert(std::is_copy_constructible_v<Key>,
115 "PersistentHashMap requires copy-constructible keys");
116 static_assert(std::is_copy_assignable_v<Key>,
117 "PersistentHashMap requires copy-assignable keys");
118 static_assert(std::is_copy_constructible_v<T>,
119 "PersistentHashMap requires copy-constructible mapped values");
120 static_assert(std::is_copy_assignable_v<T>,
121 "PersistentHashMap requires copy-assignable mapped values");
122 static_assert(std::is_copy_constructible_v<Cmp>,
123 "PersistentHashMap requires a copy-constructible comparator");
209 [[nodiscard]]
static size_t bitpos(
const size_t hash,
const size_t shift)
noexcept
214 [[nodiscard]]
static std::uint32_t
bit(
const size_t pos)
noexcept
216 return std::uint32_t{1} << pos;
219 [[nodiscard]]
static size_t index(
const std::uint32_t bitmap,
220 const std::uint32_t bit_value)
noexcept
222 return std::popcount(bitmap & (bit_value - 1));
227 return std::make_shared<LeafNode>(key,
value, hash);
232 return std::make_shared<LeafNode>(key, std::move(
value), hash);
240 return movable_value !=
nullptr
241 ?
make_leaf(key, std::move(*movable_value), hash)
250 if (movable_value !=
nullptr)
251 entries.append(std::pair<Key, T>{key, std::move(*movable_value)});
253 entries.append(std::pair<Key, T>{key, *
value});
258 const size_t shift)
const
261 ?
static_cast<const LeafNode *
>(n1.get())->hash
264 ?
static_cast<const LeafNode *
>(n2.get())->hash
267 const size_t p1 =
bitpos(h1, shift);
268 const size_t p2 =
bitpos(h2, shift);
284 return std::make_shared<BitmapNode>(
bit(p1) |
bit(p2), std::move(children));
289 return std::make_shared<BitmapNode>(
bit(p1), std::move(children));
311 const auto leaf =
static_cast<const LeafNode *
>(node.get());
312 if (
cmp_(key, leaf->key))
325 if (leaf->hash == hash)
329 entries.
append(std::pair<Key, T>{leaf->key, leaf->value});
333 return std::make_shared<CollisionNode>(hash, std::move(entries));
344 const auto bnode =
static_cast<const BitmapNode *
>(node.get());
345 const size_t pos =
bitpos(hash, shift);
346 const std::uint32_t b =
bit(pos);
347 const size_t idx =
index(bnode->bitmap, b);
349 if ((bnode->bitmap & b) == 0)
352 new_children.
reserve(bnode->children.size() + 1);
353 for (
size_t i = 0; i < idx; ++i)
354 new_children.
append(bnode->children[i]);
356 for (
size_t i = idx; i < bnode->children.size(); ++i)
357 new_children.
append(bnode->children[i]);
361 return std::make_shared<BitmapNode>(bnode->bitmap | b, std::move(new_children));
364 const NodePtr child = bnode->children[idx];
371 new_children[idx] = std::move(new_child);
372 return std::make_shared<BitmapNode>(bnode->bitmap, std::move(new_children));
377 const auto cnode =
static_cast<const CollisionNode *
>(node.get());
378 if (hash != cnode->hash)
387 for (
const auto &entry : cnode->entries)
388 if (
cmp_(key, entry.first))
396 new_entries.
reserve(cnode->entries.size() + 1);
398 for (
const auto &entry : cnode->entries)
399 if (
cmp_(key, entry.first)
and not found)
405 new_entries.
append(entry);
416 return std::make_shared<CollisionNode>(hash, std::move(new_entries));
434 const auto leaf =
static_cast<const LeafNode *
>(node.get());
435 if (
cmp_(key, leaf->key))
445 const auto bnode =
static_cast<const BitmapNode *
>(node.get());
446 const size_t pos =
bitpos(hash, shift);
447 const std::uint32_t b =
bit(pos);
448 if ((bnode->bitmap & b) == 0)
451 const size_t idx =
index(bnode->bitmap, b);
452 const NodePtr child = bnode->children[idx];
454 if (new_child == child)
457 if (new_child ==
nullptr)
459 if (bnode->bitmap == b)
462 if (std::popcount(bnode->bitmap) == 2)
464 const size_t other_idx = idx == 0 ? 1 : 0;
465 NodePtr other_child = bnode->children[other_idx];
472 new_children.
reserve(bnode->children.size() - 1);
473 for (
size_t i = 0; i < bnode->children.size(); ++i)
475 new_children.
append(bnode->children[i]);
476 return std::make_shared<BitmapNode>(bnode->bitmap & ~b, std::move(new_children));
480 new_children[idx] = std::move(new_child);
481 if (new_children.
size() == 1
and
484 return new_children[0];
485 return std::make_shared<BitmapNode>(bnode->bitmap, std::move(new_children));
490 const auto cnode =
static_cast<const CollisionNode *
>(node.get());
491 if (hash != cnode->hash)
495 new_entries.
reserve(cnode->entries.size());
496 for (
const auto &entry : cnode->entries)
497 if (
cmp_(key, entry.first))
500 new_entries.
append(entry);
504 if (new_entries.
size() == 1)
505 return std::make_shared<LeafNode>(new_entries[0].first,
506 std::move(new_entries[0].second),
508 return std::make_shared<CollisionNode>(hash, std::move(new_entries));
517 const size_t shift)
const
524 const auto leaf =
static_cast<const LeafNode *
>(node.get());
525 return cmp_(key, leaf->key) ? &leaf->value :
nullptr;
530 const auto bnode =
static_cast<const BitmapNode *
>(node.get());
531 const size_t pos =
bitpos(hash, shift);
532 const std::uint32_t b =
bit(pos);
533 if ((bnode->bitmap & b) == 0)
541 const auto cnode =
static_cast<const CollisionNode *
>(node.get());
542 if (hash != cnode->hash)
544 for (
const auto &entry : cnode->entries)
545 if (
cmp_(key, entry.first))
546 return &entry.second;
554 const size_t pos)
const
563 const auto bnode =
static_cast<const BitmapNode *
>(node.get());
564 for (
size_t i = 0; i < bnode->children.size(); ++i)
573 for (
size_t j = i + 1; j < node.
entries.
size(); ++j)
587 const auto leaf =
static_cast<const LeafNode *
>(node.get());
596 const auto cnode =
static_cast<const CollisionNode *
>(node.get());
599 for (
const auto &entry : cnode->entries)
600 if (
hash_fct_(entry.first) != cnode->hash)
608 const auto bnode =
static_cast<const BitmapNode *
>(node.get());
609 const size_t child_count = std::popcount(bnode->bitmap);
610 if (child_count == 0 or child_count != bnode->children.size())
612 if (bnode->children.size() == 1
and
621 const std::uint32_t b =
bit(pos);
622 if ((bnode->bitmap & b) == 0)
625 if (idx >= bnode->children.size())
627 const NodePtr &child = bnode->children[idx++];
631 size_t subtree_count = 0;
635 <<
"PersistentHashMap::verify(): node count would overflow";
636 total += subtree_count;
639 if (idx != bnode->children.size())
659 const auto cnode =
static_cast<const CollisionNode *
>(node.get());
660 for (
const auto &entry : cnode->entries)
661 out.append(entry.first);
665 const auto bnode =
static_cast<const BitmapNode *
>(node.get());
666 for (
size_t i = 0; i < bnode->children.size(); ++i)
676 const auto leaf =
static_cast<const LeafNode *
>(node.get());
677 out.append(std::pair<Key, T>{leaf->key, leaf->value});
682 const auto cnode =
static_cast<const CollisionNode *
>(node.get());
683 for (
const auto &entry : cnode->entries)
688 const auto bnode =
static_cast<const BitmapNode *
>(node.get());
689 for (
size_t i = 0; i < bnode->children.size(); ++i)
698 <<
"PersistentHashMap: size would overflow";
713 other.
root_.swap(taken_root);
733 <<
"PersistentHashMap: hash function must not be null";
754 noexcept(std::is_nothrow_copy_constructible_v<Cmp>)
796 root_.swap(other.root_);
798 std::swap(
size_, other.size_);
806 [[nodiscard]]
size_t size() const noexcept {
return size_; }
827 bool changed =
false;
829 false, added, changed);
847 bool changed =
false;
849 false, added, changed);
866 bool changed =
false;
868 true, added, changed);
883 bool changed =
false;
885 true, added, changed);
899 bool removed =
false;
915 return find(key) !=
nullptr;
926 [[nodiscard]]
const T *
find(
const Key &key)
const
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_unless(C)
Throws std::domain_error if condition does NOT hold.
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
Standard functor implementations and comparison objects.
size_t size_t int32_t value
size_t size_t int32_t * out
Simple dynamic array with automatic resizing and functional operations.
constexpr size_t size() const noexcept
Return the number of elements stored in the stack.
T & append(const T &data)
Append a copy of data
void reserve(size_t cap)
Reserves cap cells into the array.
Immutable unordered map backed by a Hash Array Mapped Trie (HAMT).
static Cmp predicate_of(PersistentHashMap &other, NodePtr &taken_root)
PersistentHashMap(const Hash_Fct_Ptr hash_fct=dft_hash_ptr_fct< Key >, const Cmp &cmp=Cmp())
Construct an empty persistent hash map.
bool is_empty() const noexcept
Return true when this version has no bindings.
PersistentHashMap(const PersistentHashMap &other)=default
Share the bindings of another version.
bool hashes_match_slot(const NodePtr &node, const size_t shift, const size_t pos) const
static constexpr size_t BRANCHING_FACTOR
NodePtr insert_impl(const NodePtr &node, const Key &key, const T *value, T *movable_value, const size_t hash, const size_t shift, const bool replace, bool &added, bool &changed) const
NodeType
Types of polymorphic nodes in the HAMT.
@ LEAF
Terminal leaf node that stores exactly one (key, value) pair.
@ BITMAP
Compressed internal node that uses a 32-bit bitmap for routing.
@ COLLISION
Terminal node that stores multiple (key, value) pairs with the exact same hash.
static NodePtr make_leaf(const Key &key, T &&value, const size_t hash)
static NodePtr make_leaf_from_value(const Key &key, const T *value, T *movable_value, const size_t hash)
static size_t index(const std::uint32_t bitmap, const std::uint32_t bit_value) noexcept
static constexpr size_t BITS_PER_LEVEL
PersistentHashMap insert(const Key &key, T &&value) const
Return a new version with key inserted if absent, moving value.
size_t size_after_insert(const bool added) const
PersistentHashMap insert_or_assign(const Key &key, const T &value) const
Return a new version with key bound to value.
bool collision_keys_are_unique(const CollisionNode &node) const
bool verify() const
Verify HAMT routing, collision, uniqueness and size invariants.
std::shared_ptr< const Node > NodePtr
static std::uint32_t bit(const size_t pos) noexcept
NodePtr erase_impl(const NodePtr &node, const Key &key, const size_t hash, const size_t shift, bool &removed) const
Array< std::pair< Key, T > > items() const
Return all key/value bindings in unspecified order.
bool verify_rec(const NodePtr &node, const size_t shift, size_t &count) const
size_t(*)(const Key &) Hash_Fct_Ptr
Hash function pointer type used by this map.
static NodePtr make_leaf(const Key &key, const T &value, const size_t hash)
PersistentHashMap insert(const Key &key, const T &value) const
Return a new version with key inserted if absent.
Array< Key > keys() const
Return all keys in unspecified order.
static size_t bitpos(const size_t hash, const size_t shift) noexcept
PersistentHashMap(PersistentHashMap &&other) noexcept(std::is_nothrow_copy_constructible_v< Cmp >)
Take the bindings of another version, which is left empty.
NodePtr merge_leaves(const NodePtr &n1, const NodePtr &n2, const size_t shift) const
PersistentHashMap & operator=(const PersistentHashMap &other)=default
Share the bindings of another version.
static void collect_items(const NodePtr &node, Array< std::pair< Key, T > > &out)
bool contains(const Key &key) const
Test whether key is present.
PersistentHashMap erase(const Key &key) const
Return a new version without key.
static constexpr size_t LEVEL_MASK
const T * find_impl(const NodePtr &node, const Key &key, const size_t hash, const size_t shift) const
PersistentHashMap(NodePtr root, const Hash_Fct_Ptr hash_fct, Cmp cmp, const size_t size)
static void collect_keys(const NodePtr &node, Array< Key > &out)
PersistentHashMap insert_or_assign(const Key &key, T &&value) const
Return a new version with key bound to moved value.
static void append_entry(Array< std::pair< Key, T > > &entries, const Key &key, const T *value, T *movable_value)
const T * find(const Key &key) const
Find a mapped value.
void swap(PersistentHashMap &other) noexcept(std::is_nothrow_swappable_v< Cmp >)
Exchange in O(1) the bindings, the hash functions and the equality predicates of this version and ano...
size_t size() const noexcept
Return the number of bindings stored in this version.
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
int cmp(const __gmp_expr< T, U > &expr1, const __gmp_expr< V, W > &expr2)
Standard hash functions for Aleph types.
Main namespace for Aleph-w library functions.
std::decay_t< typename HeadC::Item_Type > T
and
Check uniqueness with explicit hash + equality functors.
void replace(Itor beg, const Itor &end, const T &old_value, const T &new_value)
Replace elements equal to a value.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
BitmapNode(const std::uint32_t b, Array< NodePtr > c)
Array< NodePtr > children
Array< std::pair< Key, T > > entries
CollisionNode(const size_t h, Array< std::pair< Key, T > > e)
LeafNode(Key k, T v, const size_t h)
Polymorphic base node managed by std::shared_ptr for structural sharing.
Node(const NodeType t) noexcept
Dynamic array container with automatic resizing.