|
Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
|
Immutable ordered map backed by a path-copying treap. More...
#include <tpl_persistent_treap.H>
Classes | |
| struct | Node |
Public Member Functions | |
| PersistentTreapMap (Compare cmp=Compare()) | |
| Construct an empty persistent map. | |
| bool | is_empty () const noexcept |
| Return true when the map has no bindings. | |
| size_t | size () const noexcept |
| Return the number of key/value bindings in this version. | |
| const T * | find (const Key &key) const |
| Find a mapped value. | |
| bool | contains (const Key &key) const |
Test whether key is present. | |
| template<typename KArg , typename VArg > requires std::constructible_from<Key, KArg &&> | |
| and std::constructible_from< T, VArg && > PersistentTreapMap | insert (KArg &&key, VArg &&value) const |
| Return a new version with a binding inserted if absent. | |
| template<typename KArg , typename VArg > requires std::constructible_from<Key, KArg &&> | |
| and std::constructible_from< T, VArg && > PersistentTreapMap | insert_or_assign (KArg &&key, VArg &&value) const |
Return a new version with key assigned to value. | |
| PersistentTreapMap | erase (const Key &key) const |
Return a new version without key. | |
| std::pair< PersistentTreapMap, PersistentTreapMap > | split (const Key &pivot) const |
Split this version around pivot. | |
| PersistentTreapMap | join (const PersistentTreapMap &right) const |
Join this version with right. | |
| Array< Key > | keys () const |
| Return all keys in sorted order. | |
| Array< std::pair< Key, T > > | items () const |
| Return all key/value bindings in sorted-key order. | |
| bool | verify () const |
| Verify treap, BST and cached-size invariants. | |
Static Public Member Functions | |
| static PersistentTreapMap | join (const PersistentTreapMap &left, const PersistentTreapMap &right) |
| Join two ordered, non-overlapping map versions. | |
Private Types | |
| using | NodePtr = std::shared_ptr< const Node > |
| using | NodeOps = detail::PersistentTreapNodeOps< Node, Key, Compare > |
Private Member Functions | |
| PersistentTreapMap (NodePtr root, Compare cmp, const std::uint64_t next_priority) | |
Static Private Member Functions | |
| static NodePtr | make_node (std::shared_ptr< const Key > key, std::shared_ptr< const T > value, const std::uint64_t priority, NodePtr left, NodePtr right) |
| static NodePtr | rebuild_node (const NodePtr &node, NodePtr left, NodePtr right) |
| static NodePtr | insert_rec (const NodePtr &node, std::shared_ptr< const Key > key, std::shared_ptr< const T > value, const std::uint64_t priority, const Compare &cmp, bool &inserted) |
| static NodePtr | insert_or_assign_rec (const NodePtr &node, std::shared_ptr< const Key > key, std::shared_ptr< const T > value, const std::uint64_t priority, const Compare &cmp, bool &inserted) |
| static void | collect_items (const NodePtr &node, Array< std::pair< Key, T > > &out) |
Private Attributes | |
| NodePtr | root_ |
| Compare | cmp_ |
| std::uint64_t | next_priority_ = 0 |
Immutable ordered map backed by a path-copying treap.
| Key | Key type. |
| T | Mapped value type. |
| Compare | Strict weak ordering used for keys. Defaults to Aleph::less<Key>. |
Definition at line 546 of file tpl_persistent_treap.H.
|
private |
Definition at line 559 of file tpl_persistent_treap.H.
|
private |
Definition at line 558 of file tpl_persistent_treap.H.
|
inlineprivate |
Definition at line 685 of file tpl_persistent_treap.H.
|
inlineexplicit |
Construct an empty persistent map.
| cmp | Comparator used for key ordering. |
| Nothing | unless copying cmp throws. |
Definition at line 696 of file tpl_persistent_treap.H.
|
inlinestaticprivate |
Definition at line 675 of file tpl_persistent_treap.H.
References Aleph::PersistentTreapMap< Key, T, Compare >::collect_items(), and out.
Referenced by Aleph::PersistentTreapMap< Key, T, Compare >::collect_items(), and Aleph::PersistentTreapMap< Key, T, Compare >::items().
|
inline |
Test whether key is present.
| key | Key to search. |
true if an equivalent key is stored. | Whatever | Compare throws. |
Definition at line 738 of file tpl_persistent_treap.H.
References Aleph::PersistentTreapMap< Key, T, Compare >::find().
|
inline |
Return a new version without key.
| key | Key to erase. |
| std::bad_alloc | or whatever Compare throws. |
Definition at line 794 of file tpl_persistent_treap.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::PersistentTreapMap< Key, T, Compare >::cmp_, Aleph::detail::PersistentTreapNodeOps< Node, Key, Compare >::erase_rec(), Aleph::PersistentTreapMap< Key, T, Compare >::next_priority_, Aleph::PersistentTreapMap< Key, T, Compare >::rebuild_node(), root(), and Aleph::PersistentTreapMap< Key, T, Compare >::root_.
Referenced by TEST().
|
inline |
Find a mapped value.
| key | Key to search. |
nullptr when absent. The pointer remains valid while this map version is alive. | Whatever | Compare throws. |
Definition at line 720 of file tpl_persistent_treap.H.
References Aleph::PersistentTreapMap< Key, T, Compare >::cmp_, Aleph::PersistentTreapMap< Key, T, Compare >::Node::key, Aleph::PersistentTreapMap< Key, T, Compare >::Node::left, Aleph::PersistentTreapMap< Key, T, Compare >::Node::right, Aleph::PersistentTreapMap< Key, T, Compare >::root_, and Aleph::PersistentTreapMap< Key, T, Compare >::Node::value.
Referenced by Aleph::PersistentTreapMap< Key, T, Compare >::contains(), and main().
|
inline |
Return a new version with a binding inserted if absent.
| KArg | Type used to construct Key. |
| VArg | Type used to construct T. |
| key | Key to insert. |
| value | Mapped value to insert. |
| std::bad_alloc | or whatever construction/comparison throws. |
Definition at line 754 of file tpl_persistent_treap.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::PersistentTreapMap< Key, T, Compare >::cmp_, Aleph::PersistentTreapMap< Key, T, Compare >::insert_rec(), Aleph::PersistentTreapMap< Key, T, Compare >::next_priority_, Aleph::detail::persistent_treap_priority(), root(), Aleph::PersistentTreapMap< Key, T, Compare >::root_, and value.
|
inline |
Return a new version with key assigned to value.
| KArg | Type used to construct Key when the key is absent. |
| VArg | Type used to construct T. |
| key | Key to insert or update. |
| value | Mapped value for the returned version. |
| std::bad_alloc | or whatever construction/comparison throws. |
Definition at line 775 of file tpl_persistent_treap.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::PersistentTreapMap< Key, T, Compare >::cmp_, Aleph::PersistentTreapMap< Key, T, Compare >::insert_or_assign_rec(), Aleph::PersistentTreapMap< Key, T, Compare >::next_priority_, Aleph::detail::persistent_treap_priority(), root(), Aleph::PersistentTreapMap< Key, T, Compare >::root_, and value.
|
inlinestaticprivate |
Definition at line 636 of file tpl_persistent_treap.H.
References Aleph::and, Aleph::blossom_maximum_cardinality_matching(), cmp(), Aleph::PersistentTreapMap< Key, T, Compare >::insert_or_assign_rec(), Aleph::PersistentTreapMap< Key, T, Compare >::make_node(), Aleph::PersistentTreapMap< Key, T, Compare >::rebuild_node(), Aleph::detail::PersistentTreapNodeOps< Node, Key, Compare >::rotate_left(), Aleph::detail::PersistentTreapNodeOps< Node, Key, Compare >::rotate_right(), and value.
Referenced by Aleph::PersistentTreapMap< Key, T, Compare >::insert_or_assign(), and Aleph::PersistentTreapMap< Key, T, Compare >::insert_or_assign_rec().
|
inlinestaticprivate |
Definition at line 595 of file tpl_persistent_treap.H.
References Aleph::blossom_maximum_cardinality_matching(), cmp(), Aleph::PersistentTreapMap< Key, T, Compare >::insert_rec(), Aleph::PersistentTreapMap< Key, T, Compare >::make_node(), Aleph::PersistentTreapMap< Key, T, Compare >::rebuild_node(), Aleph::detail::PersistentTreapNodeOps< Node, Key, Compare >::rotate_left(), Aleph::detail::PersistentTreapNodeOps< Node, Key, Compare >::rotate_right(), and value.
Referenced by Aleph::PersistentTreapMap< Key, T, Compare >::insert(), and Aleph::PersistentTreapMap< Key, T, Compare >::insert_rec().
|
inlinenoexcept |
Return true when the map has no bindings.
true iff size() == 0. | Nothing. |
Definition at line 706 of file tpl_persistent_treap.H.
References Aleph::PersistentTreapMap< Key, T, Compare >::root_.
|
inline |
Return all key/value bindings in sorted-key order.
| std::bad_alloc | or whatever copying Key or T throws. |
Definition at line 865 of file tpl_persistent_treap.H.
References Aleph::PersistentTreapMap< Key, T, Compare >::collect_items(), out, Aleph::Array< T >::reserve(), Aleph::PersistentTreapMap< Key, T, Compare >::root_, and Aleph::PersistentTreapMap< Key, T, Compare >::size().
|
inlinestatic |
Join two ordered, non-overlapping map versions.
| left | Map whose keys must all be less than every key in right. |
| right | Map whose keys must all be greater than every key in left. |
| std::domain_error | if right is not ordered under left's comparator, or if the key ranges overlap or touch. |
| std::bad_alloc | if node allocation fails. |
| Whatever | Compare throws. |
Definition at line 824 of file tpl_persistent_treap.H.
References ah_domain_error_unless, Aleph::detail::PersistentTreapNodeOps< Node, Key, Compare >::can_join(), Aleph::PersistentTreapMap< Key, T, Compare >::cmp_, Aleph::detail::PersistentTreapNodeOps< Node, Key, Compare >::is_valid_under(), Aleph::detail::PersistentTreapNodeOps< Node, Key, Compare >::join_nodes(), Aleph::PersistentTreapMap< Key, T, Compare >::next_priority_, Aleph::PersistentTreapMap< Key, T, Compare >::rebuild_node(), and Aleph::PersistentTreapMap< Key, T, Compare >::root_.
Referenced by Aleph::PersistentTreapMap< Key, T, Compare >::join(), and TEST().
|
inline |
Join this version with right.
| right | Map whose keys must all be greater than every key in this map. |
| std::domain_error | if right is not ordered under this map's comparator, or if the key ranges overlap or touch. |
| std::bad_alloc | if node allocation fails. |
| Whatever | Compare throws. |
Definition at line 843 of file tpl_persistent_treap.H.
References Aleph::PersistentTreapMap< Key, T, Compare >::join().
|
inline |
Return all keys in sorted order.
| std::bad_alloc | or whatever copying Key throws. |
Definition at line 852 of file tpl_persistent_treap.H.
References Aleph::detail::PersistentTreapNodeOps< Node, Key, Compare >::collect_keys(), out, Aleph::Array< T >::reserve(), Aleph::PersistentTreapMap< Key, T, Compare >::root_, and Aleph::PersistentTreapMap< Key, T, Compare >::size().
|
inlinestaticprivate |
Definition at line 565 of file tpl_persistent_treap.H.
References ah_overflow_error_if, Aleph::blossom_maximum_cardinality_matching(), Aleph::detail::PersistentTreapNodeOps< Node, Key, Compare >::node_size(), and value.
Referenced by Aleph::PersistentTreapMap< Key, T, Compare >::insert_or_assign_rec(), Aleph::PersistentTreapMap< Key, T, Compare >::insert_rec(), and Aleph::PersistentTreapMap< Key, T, Compare >::rebuild_node().
|
inlinestaticprivate |
Definition at line 589 of file tpl_persistent_treap.H.
References Aleph::PersistentTreapMap< Key, T, Compare >::make_node().
Referenced by Aleph::PersistentTreapMap< Key, T, Compare >::erase(), Aleph::PersistentTreapMap< Key, T, Compare >::insert_or_assign_rec(), Aleph::PersistentTreapMap< Key, T, Compare >::insert_rec(), Aleph::PersistentTreapMap< Key, T, Compare >::join(), and Aleph::PersistentTreapMap< Key, T, Compare >::split().
|
inlinenoexcept |
Return the number of key/value bindings in this version.
| Nothing. |
Definition at line 712 of file tpl_persistent_treap.H.
References Aleph::detail::PersistentTreapNodeOps< Node, Key, Compare >::node_size(), and Aleph::PersistentTreapMap< Key, T, Compare >::root_.
Referenced by Aleph::PersistentTreapMap< Key, T, Compare >::items(), Aleph::PersistentTreapMap< Key, T, Compare >::keys(), and Aleph::PersistentTreapMap< Key, T, Compare >::verify().
|
inline |
Split this version around pivot.
| pivot | Split key. |
{less, greater_equal} where the first map contains keys strictly less than pivot and the second contains the rest. | std::bad_alloc | or whatever Compare throws. |
Definition at line 808 of file tpl_persistent_treap.H.
References Aleph::blossom_maximum_cardinality_matching(), Aleph::PersistentTreapMap< Key, T, Compare >::cmp_, Aleph::PersistentTreapMap< Key, T, Compare >::next_priority_, Aleph::PersistentTreapMap< Key, T, Compare >::rebuild_node(), Aleph::PersistentTreapMap< Key, T, Compare >::root_, and Aleph::detail::PersistentTreapNodeOps< Node, Key, Compare >::split_rec().
Referenced by TEST().
|
inline |
Verify treap, BST and cached-size invariants.
true if the internal structure is consistent. | Nothing | unless Compare throws. |
Definition at line 879 of file tpl_persistent_treap.H.
References Aleph::and, Aleph::PersistentTreapMap< Key, T, Compare >::cmp_, Aleph::count(), Aleph::PersistentTreapMap< Key, T, Compare >::root_, Aleph::PersistentTreapMap< Key, T, Compare >::size(), and Aleph::detail::PersistentTreapNodeOps< Node, Key, Compare >::verify_rec().
Referenced by TEST().
|
private |
Definition at line 562 of file tpl_persistent_treap.H.
Referenced by Aleph::PersistentTreapMap< Key, T, Compare >::erase(), Aleph::PersistentTreapMap< Key, T, Compare >::find(), Aleph::PersistentTreapMap< Key, T, Compare >::insert(), Aleph::PersistentTreapMap< Key, T, Compare >::insert_or_assign(), Aleph::PersistentTreapMap< Key, T, Compare >::join(), Aleph::PersistentTreapMap< Key, T, Compare >::split(), and Aleph::PersistentTreapMap< Key, T, Compare >::verify().
|
private |
Definition at line 563 of file tpl_persistent_treap.H.
Referenced by Aleph::PersistentTreapMap< Key, T, Compare >::erase(), Aleph::PersistentTreapMap< Key, T, Compare >::insert(), Aleph::PersistentTreapMap< Key, T, Compare >::insert_or_assign(), Aleph::PersistentTreapMap< Key, T, Compare >::join(), and Aleph::PersistentTreapMap< Key, T, Compare >::split().
|
private |
Definition at line 561 of file tpl_persistent_treap.H.
Referenced by Aleph::PersistentTreapMap< Key, T, Compare >::erase(), Aleph::PersistentTreapMap< Key, T, Compare >::find(), Aleph::PersistentTreapMap< Key, T, Compare >::insert(), Aleph::PersistentTreapMap< Key, T, Compare >::insert_or_assign(), Aleph::PersistentTreapMap< Key, T, Compare >::is_empty(), Aleph::PersistentTreapMap< Key, T, Compare >::items(), Aleph::PersistentTreapMap< Key, T, Compare >::join(), Aleph::PersistentTreapMap< Key, T, Compare >::keys(), Aleph::PersistentTreapMap< Key, T, Compare >::size(), Aleph::PersistentTreapMap< Key, T, Compare >::split(), and Aleph::PersistentTreapMap< Key, T, Compare >::verify().