|
ADS
Algorithms and Data Structures
|
a DICTIONARY on a binary search tree balanced by the join of B alone, augmented by G, whose changes go to the observers of the subject policy Policy [see Modification.h] More...
#include <ADS/tree/JoinTree.h>
Classes | |
| struct | Node |
| a node: what the navigation below reads More... | |
| struct | Shape |
| the shape of the tree, for PathIterator More... | |
Public types | |
| using | Key = K |
| the type of the keys | |
| using | Value = V |
| the type of the values | |
| using | AugValue = typename G::Value |
| the type of the augmentation | |
| using | Augmented = G |
| the augmentation | |
| using | allocator_type = A |
| the allocator of the nodes | |
| using | Inserted = mod::Inserted< K , K , V > |
| the insertion of a key, whose handle is the key itself | |
| using | Erased = mod::Erased< K , K , V > |
| the removal of a key, told before it is made outside a batch | |
| using | Assigned = mod::Assigned< K , V > |
| the value of a key that is there changed | |
| using | modifications |
| the events: Cleared by clear() and on the trees that a bulk operation consumes, Rebuilt on the tree that it leaves | |
| using | Subject = typename Policy::template bind< modifications , JoinTree > |
| the subject [see Modification.h] | |
| using | value_type = std::pair< K , V > |
| the pairs ( key , value ) by value, which own their copies | |
| using | key_type = K |
| the type of the keys | |
| using | mapped_type = V |
| the type of the values | |
| using | size_type = std::size_t |
| the type of the sizes | |
| using | difference_type = std::ptrdiff_t |
| the type of the distances | |
| using | reference = PairRef< const K & , const V & > |
| references, by value | |
| using | const_reference = reference |
| the same | |
| using | const_iterator = PathIterator< JoinTree , Shape > |
| the constant iterator by increasing key, along the path from the root | |
| using | iterator = const_iterator |
| the constant iterator | |
Public member functions | |
| allocator_type | get_allocator (void) const |
| the allocator of the nodes, stateless | |
| JoinTree (std::uint64_t seed=1) | |
| an empty tree; the seed of the priorities, for a treap | |
| template<class... O> requires ( sizeof...( O ) > 0 ) | |
| JoinTree (std::uint64_t seed, O &... o) | |
| an empty tree whose changes go to the static observers o... of the policy; the seed of the priorities, for a treap | |
| JoinTree (const JoinTree &o) | |
| a copy, node by node with the same shape, in \( O( n ) \) time, with no observer (none of a policy with static observers, by a static assertion) | |
| JoinTree (JoinTree &&o) noexcept | |
| the nodes of o, which is left empty (and tells Cleared), in \( O( 1 ) \) time, with no observer | |
| JoinTree & | operator= (JoinTree o) noexcept |
| the nodes of o, by copy and swap | |
| ~JoinTree () | |
| frees the nodes | |
| void | swap (JoinTree &o) noexcept |
| exchanges the nodes with o; the observers of both get Rebuilt | |
| Subject & | subject (void) |
| the subject, for ADS::batch() and ADS::quiet() | |
| const Subject & | subject (void) const |
| the subject | |
| std::optional< V > | lookup (const K &k) const |
| the value of \( k \), or nothing, by the descent of a binary search tree, in \( O( \log n ) \) time | |
| void | insert (const K &k, const V &v) |
| associates \( v \) to \( k \), in \( O( \log n ) \) time; tells Inserted, or Assigned if \( k \) was there (by one more descent, only when someone listens) | |
| void | remove (const K &k) |
| removes \( k \), if it is there, in \( O( \log n ) \) time; tells Erased (by one more descent, only when someone listens) | |
| bool | assign (const K &k, const V &v) |
| the value of \( k \) set to \( v \), and true, if \( k \) is there, nothing and false otherwise, in \( O( \log n ) \) time; the shape stays, and the augmented values of the path to \( k \) change by one combination each with the difference of the old and the new value when G is an abelian group of exact values (commutative and inverse()), by the recomputation of the value of each node from its children otherwise; tells Assigned | |
| bool | contains (const K &k) const |
| true if \( k \) is there | |
| std::optional< std::pair< K, V > > | lowerBound (const K &k) const |
| the smallest key not smaller than \( k \) and its value, or nothing (the find( x ) of an SSet of Morin), in \( O( \log n ) \) time | |
| const K * | findKey (const K &k) const |
| the address of the key \( k \), nil if absent, in \( O( \log n ) \) time: the find( x ) of a USet of Morin [see DictionarySet]; valid until the key is removed | |
| const K * | lowerBoundKey (const K &k) const |
| the address of the smallest key not smaller than \( k \), nil if none, in \( O( \log n ) \) time: the find( x ) of an SSet of Morin [see DictionarySet]; valid until the key is removed | |
| std::size_t | size (void) const |
| the number \( n \) of keys | |
| bool | empty (void) const |
| true if there is no key | |
| void | clear (void) |
| removes all the pairs; tells Cleared if there was one | |
| int | height (void) const |
| the height of the tree, -1 if empty, in \( O( n ) \) time | |
| template<class F> | |
| void | forEach (F &&f) const |
| f( k , v ) on the pairs by increasing keys | |
| AugValue | augValue (void) const |
| the augmented value of the whole map, in \( O( 1 ) \) time | |
| AugValue | augRange (const K &lo, const K &hi) const |
| the augmented value of the pairs with keys in \( [ \mathit{lo} , \mathit{hi} ] \), in \( O( \log n ) \) calls of combine(), the combination of the pieces of augVisit() | |
| AugValue | augPrefix (const K &k) const |
| the augmented value of the pairs with keys \( \le k \), in \( O( \log n ) \) calls of combine() | |
| template<class F> | |
| void | augVisit (const K &lo, const K &hi, F &&f) const |
| f( a ) on the augmented values \( a \) of the \( O( \log n ) \) pieces (whole subtrees and single pairs) whose union is the set of the keys in \( [ \mathit{lo} , \mathit{hi} ] \), by increasing keys | |
| template<class P> | |
| std::optional< std::pair< K, V > > | augSelect (P &&p) const |
| the first pair, by increasing keys, whose prefix (the augmented value of the pairs up to it) satisfies \( p \), or nothing, in \( O( \log n ) \) calls of combine() and of \( p \): e.g., the \( i \)-th key, \( p( a ) = a \ge i \) on the sums of values 1, and the first prefix whose sum reaches a threshold | |
| template<class Q, class F> | |
| void | augFilter (Q &&q, F &&f) const |
| f( k , v ) on the pairs \( ( k , v ) \) with \( q( \mathrm{from}( k , v ) ) \), by increasing keys, entering only the subtrees whose augmented value satisfies \( q \): for \( m \) such pairs on a tree of height \( h \), at most \( 3 m ( h + 1 ) + 1 \) calls of \( q \), i.e., \( O( 1 + m \log n ) \) | |
| const Node * | root (void) const |
| the root, nullptr if empty; the nodes must not be changed | |
| const_iterator | begin (void) const |
| the iterator on the smallest key | |
| const_iterator | end (void) const |
| the iterator past the largest key | |
| const_iterator | cbegin (void) const |
| as begin() | |
| const_iterator | cend (void) const |
| as end() | |
| Public member functions inherited from ADS::Joinable< JoinTree< K, V, B, G, Policy, A >, JoinTreeNodes< K, V, B, G, Policy, A >, K, V > | |
| std::tuple< JoinTree< K, V, B, G, Policy, A >, std::optional< V >, JoinTree< K, V, B, G, Policy, A > > | split (const K &k) && |
| the keys before \( k \), the value of \( k \) (if there), the keys after \( k \), by JoinAlgorithms::split(); the tree is consumed, in \( O( \log n ) \) time | |
| JoinTree< K, V, B, G, Policy, A > | extractRange (const K &lo, const K &hi) |
| the pairs of the keys in \( [ \mathit{lo} , \mathit{hi} ] \), taken out of the tree by JoinAlgorithms::extract(), in \( O( \log n ) \) time | |
| void | unionWith (JoinTree< K, V, B, G, Policy, A > &&o, F f=F()) |
| the union with o, which is consumed, by JoinAlgorithms::unite(); on a key of both, the value f( mine , its ), by default its; \( O( m \log( n / m + 1 ) ) \) work for sizes \( m \le n \) | |
| void | intersectWith (JoinTree< K, V, B, G, Policy, A > &&o, F f=F()) |
| the intersection with o, which is consumed, by JoinAlgorithms::intersect(); the value f( mine , its ), by default mine; the work of unionWith(), plus the freeing of the nodes that leave | |
| void | differenceWith (JoinTree< K, V, B, G, Policy, A > &&o) |
| the keys not in o, which is consumed, by JoinAlgorithms::subtract(); the work of unionWith(), plus the freeing of the nodes of o | |
| void | multiInsert (std::span< const std::pair< K, V > > B, F f=F()) |
| the pairs of \( B \) inserted, by JoinAlgorithms::multiInsert(); on a key already there, the value f( mine , new ), by default new; \( O( m \log( n / m + 1 ) ) \) work for \( m \) pairs | |
| void | multiRemove (std::span< const K > B) |
| the keys of \( B \) removed, by JoinAlgorithms::multiRemove(), \( O( m \log( n / m + 1 ) ) \) work | |
| void | filter (P p) |
| only the pairs with p( k , v ) kept, by JoinAlgorithms::filter(), \( O( n ) \) work | |
| U | mapReduce (M map, R reduce, U id) const |
| \( \mathrm{reduce}( \ldots \mathrm{reduce}( \mathit{id} , \mathrm{map}(
k_1 , v_1 ) ) \ldots , \mathrm{map}( k_n , v_n ) ) \) by an associative reduce of identity id, by JoinAlgorithms::mapReduce(), \( O( n ) \) work | |
Friends | |
| struct | JoinTreeNodes< K, V, B, G, Policy, A > |
Additional Inherited Members | |
| Static public member functions inherited from ADS::Joinable< JoinTree< K, V, B, G, Policy, A >, JoinTreeNodes< K, V, B, G, Policy, A >, K, V > | |
| static JoinTree< K, V, B, G, Policy, A > | join (JoinTree< K, V, B, G, Policy, A > &&l, const K &k, const V &v, JoinTree< K, V, B, G, Policy, A > &&r) |
| the tree of the keys of \( l \), the pair \( ( k , v ) \) and the keys of \( r \), by the join of the scheme; \( l \) and \( r \) are consumed, in \( O( | \log n_l - \log n_r | + 1 ) \) time | |
| static JoinTree< K, V, B, G, Policy, A > | join2 (JoinTree< K, V, B, G, Policy, A > &&l, JoinTree< K, V, B, G, Policy, A > &&r) |
| the tree of the keys of \( l \) and of \( r \), which are consumed, by JoinAlgorithms::join2(), in \( O( \log n ) \) time | |
a DICTIONARY on a binary search tree balanced by the join of B alone, augmented by G, whose changes go to the observers of the subject policy Policy [see Modification.h]
| using ADS::JoinTree< K, V, B, G, Policy, A >::allocator_type = A |
the allocator of the nodes
| using ADS::JoinTree< K, V, B, G, Policy, A >::Assigned = mod::Assigned< K , V > |
the value of a key that is there changed
| using ADS::JoinTree< K, V, B, G, Policy, A >::Augmented = G |
the augmentation
| using ADS::JoinTree< K, V, B, G, Policy, A >::AugValue = typename G::Value |
the type of the augmentation
| using ADS::JoinTree< K, V, B, G, Policy, A >::const_iterator = PathIterator< JoinTree , Shape > |
the constant iterator by increasing key, along the path from the root
| using ADS::JoinTree< K, V, B, G, Policy, A >::const_reference = reference |
the same
| using ADS::JoinTree< K, V, B, G, Policy, A >::difference_type = std::ptrdiff_t |
the type of the distances
| using ADS::JoinTree< K, V, B, G, Policy, A >::Erased = mod::Erased< K , K , V > |
the removal of a key, told before it is made outside a batch
| using ADS::JoinTree< K, V, B, G, Policy, A >::Inserted = mod::Inserted< K , K , V > |
the insertion of a key, whose handle is the key itself
| using ADS::JoinTree< K, V, B, G, Policy, A >::iterator = const_iterator |
the constant iterator
| using ADS::JoinTree< K, V, B, G, Policy, A >::Key = K |
the type of the keys
| using ADS::JoinTree< K, V, B, G, Policy, A >::key_type = K |
the type of the keys
| using ADS::JoinTree< K, V, B, G, Policy, A >::mapped_type = V |
the type of the values
| using ADS::JoinTree< K, V, B, G, Policy, A >::modifications |
the events: Cleared by clear() and on the trees that a bulk operation consumes, Rebuilt on the tree that it leaves
| using ADS::JoinTree< K, V, B, G, Policy, A >::reference = PairRef< const K & , const V & > |
references, by value
| using ADS::JoinTree< K, V, B, G, Policy, A >::size_type = std::size_t |
the type of the sizes
| using ADS::JoinTree< K, V, B, G, Policy, A >::Subject = typename Policy::template bind< modifications , JoinTree > |
the subject [see Modification.h]
| using ADS::JoinTree< K, V, B, G, Policy, A >::Value = V |
the type of the values
| using ADS::JoinTree< K, V, B, G, Policy, A >::value_type = std::pair< K , V > |
the pairs ( key , value ) by value, which own their copies
|
explicit |
an empty tree; the seed of the priorities, for a treap
| ADS::JoinTree< K, V, B, G, Policy, A >::JoinTree | ( | std::uint64_t | seed, |
| O &... | o ) |
an empty tree whose changes go to the static observers o... of the policy; the seed of the priorities, for a treap
| ADS::JoinTree< K, V, B, G, Policy, A >::JoinTree | ( | const JoinTree< K, V, B, G, Policy, A > & | o | ) |
a copy, node by node with the same shape, in \( O( n ) \) time, with no observer (none of a policy with static observers, by a static assertion)
|
noexcept |
the nodes of o, which is left empty (and tells Cleared), in \( O( 1 ) \) time, with no observer
| ADS::JoinTree< K, V, B, G, Policy, A >::~JoinTree | ( | ) |
frees the nodes
| bool ADS::JoinTree< K, V, B, G, Policy, A >::assign | ( | const K & | k, |
| const V & | v ) |
the value of \( k \) set to \( v \), and true, if \( k \) is there, nothing and false otherwise, in \( O( \log n ) \) time; the shape stays, and the augmented values of the path to \( k \) change by one combination each with the difference of the old and the new value when G is an abelian group of exact values (commutative and inverse()), by the recomputation of the value of each node from its children otherwise; tells Assigned
In pseudocode (beyond the book), \( \otimes \) the operation of G and \( T \) the root:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{assign}( k , v ) \cr 2 & \quad u \gets \text{the node of } k \text{, by the descent from } T;\ \textbf{if}\ u = \textbf{nil}\ \textbf{then return} \ \mathit{false} \cr 3 & \quad \textbf{if}\ G \text{ is an abelian group of exact values} \ \textbf{then} \cr 4 & \qquad d \gets \mathrm{from}( k , v ) \otimes \mathrm{from}( k , u.v )^{-1} \cr 5 & \qquad \textbf{foreach}\ x \text{ on the path from } T \text{ to } u\ \textbf{do}\ \mathit{aug}( x ) \gets \mathit{aug}( x ) \otimes d \cr 6 & \qquad u.v \gets v \cr 7 & \quad \textbf{else} \cr 8 & \qquad u.v \gets v \cr 9 & \qquad \textbf{foreach}\ x \text{ on the path from } u \text{ to } T\ \textbf{do}\ \mathit{aug}( x ) \gets \mathit{aug}( x.\mathit{left} ) \otimes \mathrm{from}( x ) \otimes \mathit{aug}( x.\mathit{right} ) \cr 10 & \quad \textbf{return}\ \mathit{true} \end{array}} \]
| void ADS::JoinTree< K, V, B, G, Policy, A >::augFilter | ( | Q && | q, |
| F && | f ) const |
f( k , v ) on the pairs \( ( k , v ) \) with \( q( \mathrm{from}( k , v ) ) \), by increasing keys, entering only the subtrees whose augmented value satisfies \( q \): for \( m \) such pairs on a tree of height \( h \), at most \( 3 m ( h + 1 ) + 1 \) calls of \( q \), i.e., \( O( 1 + m \log n ) \)
In pseudocode (Sun, Ferizovic and Blelloch, beyond the book), from \( u = T \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{augFilter}( u , q , f ) \cr 2 & \quad \textbf{if}\ u = \textbf{nil}\ \textbf{or not}\ q( \mathit{aug}( u ) )\ \textbf{then return} \cr 3 & \quad \mathrm{augFilter}( u.\mathit{left} , q , f ) \cr 4 & \quad \textbf{if}\ q( \mathrm{from}( u ) )\ \textbf{then}\ f( u.\mathit{key} , u.v ) \cr 5 & \quad \mathrm{augFilter}( u.\mathit{right} , q , f ) \end{array}} \]
|
nodiscard |
the augmented value of the pairs with keys \( \le k \), in \( O( \log n ) \) calls of combine()
In pseudocode (Sun, Ferizovic and Blelloch, beyond the book), on the path to \( k \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{augPrefix}( k ) \cr 2 & \quad a \gets \mathrm{identity}();\ u \gets T \cr 3 & \quad \textbf{while}\ u \ne \textbf{nil}\ \textbf{do} \cr 4 & \qquad \textbf{if}\ k < u.\mathit{key}\ \textbf{then}\ u \gets u.\mathit{left} \cr 5 & \qquad \textbf{else}\ a \gets a \otimes \mathit{aug}( u.\mathit{left} ) \otimes \mathrm{from}( u );\ u \gets u.\mathit{right} \cr 6 & \quad \textbf{return}\ a \end{array}} \]
|
nodiscard |
the augmented value of the pairs with keys in \( [ \mathit{lo} , \mathit{hi} ] \), in \( O( \log n ) \) calls of combine(), the combination of the pieces of augVisit()
|
nodiscard |
the first pair, by increasing keys, whose prefix (the augmented value of the pairs up to it) satisfies \( p \), or nothing, in \( O( \log n ) \) calls of combine() and of \( p \): e.g., the \( i \)-th key, \( p( a ) = a \ge i \) on the sums of values 1, and the first prefix whose sum reaches a threshold
In pseudocode (beyond the book), on one descent:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{augSelect}( p ) \cr 2 & \quad a \gets \mathrm{identity}();\ u \gets T \cr 3 & \quad \textbf{while}\ u \ne \textbf{nil}\ \textbf{do} \cr 4 & \qquad b \gets a \otimes \mathit{aug}( u.\mathit{left} ) \cr 5 & \qquad \textbf{if}\ u.\mathit{left} \ne \textbf{nil}\ \textbf{and} \ p( b )\ \textbf{then}\ u \gets u.\mathit{left}; \ \textbf{continue} \cr 6 & \qquad b \gets b \otimes \mathrm{from}( u ) \cr 7 & \qquad \textbf{if}\ p( b )\ \textbf{then return}\ ( u.\mathit{key} , u.v ) \cr 8 & \qquad a \gets b;\ u \gets u.\mathit{right} \cr 9 & \quad \textbf{return}\ \textbf{nil} \end{array}} \]
|
nodiscard |
the augmented value of the whole map, in \( O( 1 ) \) time
| void ADS::JoinTree< K, V, B, G, Policy, A >::augVisit | ( | const K & | lo, |
| const K & | hi, | ||
| F && | f ) const |
f( a ) on the augmented values \( a \) of the \( O( \log n ) \) pieces (whole subtrees and single pairs) whose union is the set of the keys in \( [ \mathit{lo} , \mathit{hi} ] \), by increasing keys
In pseudocode (Sun, Ferizovic and Blelloch, beyond the book), \( t \) the node where the paths to \( \mathit{lo} \) and to \( \mathit{hi} \) part:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{augVisit}( \mathit{lo} , \mathit{hi} , f ) \cr 2 & \quad t \gets \text{the first node of the descent from the root } \text{with } \mathit{lo} \le t.\mathit{key} \le \mathit{hi} \cr 3 & \quad \textbf{if}\ t = \textbf{nil}\ \textbf{then return} \cr 4 & \quad P \gets \text{the nodes } u \text{ with } u.\mathit{key} \ge \mathit{lo} \text{ on the path to } \mathit{lo} \text{ in } t.\mathit{left} \cr 5 & \quad \textbf{foreach}\ u \in P \text{, by increasing keys} \ \textbf{do}\ f( \mathrm{from}( u ) );\ f( \mathit{aug}( u.\mathit{right} ) ) \cr 6 & \quad f( \mathrm{from}( t ) ) \cr 7 & \quad \textbf{foreach}\ u \text{ with } u.\mathit{key} \le \mathit{hi} \text{ on the path to } \mathit{hi} \text{ in } t.\mathit{right} \text{, in order}\ \textbf{do} \cr 8 & \qquad f( \mathit{aug}( u.\mathit{left} ) );\ f( \mathrm{from}( u ) ) \end{array}} \]
|
inlinenodiscard |
the iterator on the smallest key
|
inlinenodiscard |
as begin()
|
inlinenodiscard |
as end()
| void ADS::JoinTree< K, V, B, G, Policy, A >::clear | ( | void | ) |
removes all the pairs; tells Cleared if there was one
|
nodiscard |
true if \( k \) is there
|
nodiscard |
true if there is no key
|
inlinenodiscard |
the iterator past the largest key
|
nodiscard |
the address of the key \( k \), nil if absent, in \( O( \log n ) \) time: the find( x ) of a USet of Morin [see DictionarySet]; valid until the key is removed
| void ADS::JoinTree< K, V, B, G, Policy, A >::forEach | ( | F && | f | ) | const |
f( k , v ) on the pairs by increasing keys
|
inlinenodiscard |
the allocator of the nodes, stateless
|
nodiscard |
the height of the tree, -1 if empty, in \( O( n ) \) time
| void ADS::JoinTree< K, V, B, G, Policy, A >::insert | ( | const K & | k, |
| const V & | v ) |
associates \( v \) to \( k \), in \( O( \log n ) \) time; tells Inserted, or Assigned if \( k \) was there (by one more descent, only when someone listens)
In pseudocode (Blelloch, Ferizovic and Sun, beyond the book), the insertion by joins of JoinAlgorithms::insert():
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{insert}( k , v ) \cr 2 & \quad T \gets \mathrm{insert}( T , \text{a node alone of } ( k , v ) ) \end{array}} \]
|
nodiscard |
the value of \( k \), or nothing, by the descent of a binary search tree, in \( O( \log n ) \) time
|
nodiscard |
the smallest key not smaller than \( k \) and its value, or nothing (the find( x ) of an SSet of Morin), in \( O( \log n ) \) time
|
nodiscard |
the address of the smallest key not smaller than \( k \), nil if none, in \( O( \log n ) \) time: the find( x ) of an SSet of Morin [see DictionarySet]; valid until the key is removed
|
noexcept |
the nodes of o, by copy and swap
| void ADS::JoinTree< K, V, B, G, Policy, A >::remove | ( | const K & | k | ) |
removes \( k \), if it is there, in \( O( \log n ) \) time; tells Erased (by one more descent, only when someone listens)
In pseudocode (Blelloch, Ferizovic and Sun, beyond the book), the removal by joins of JoinAlgorithms::remove():
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{remove}( k ) \cr 2 & \quad T \gets \mathrm{remove}( T , k ) \end{array}} \]
|
inlinenodiscard |
the root, nullptr if empty; the nodes must not be changed
|
nodiscard |
the number \( n \) of keys
|
inlinenodiscard |
the subject, for ADS::batch() and ADS::quiet()
|
inlinenodiscard |
the subject
|
noexcept |
exchanges the nodes with o; the observers of both get Rebuilt
|
friend |