|
ADS
Algorithms and Data Structures
|
The binary search trees balanced by join alone, and the augmented maps, beyond the book: JoinTree< K , V , B , G > is a DICTIONARY [see ADT.h] of keys K and values V whose only balancing operation is the join of the scheme B [see JoinAlgorithms.h], AVLBalance, RedBlackBalance, WeightBalance or TreapBalance, so that the insertions and the removals are a split or a descent followed by joins, and the bulk operations of Joinable (union, intersection, difference, batches of insertions and of removals, filter, map and reduce, extraction of a range) are those of the four schemes alike. More...
#include "ADS/Algebra.h"#include "ADS/Contract.h"#include "ADS/Iterators.h"#include "ADS/Modification.h"#include "ADS/Portable.h"#include "ADS/memory/AllocatorAware.h"#include "ADS/dictionary/RandomHashing.h"#include "ADS/tree/BinaryTreeAlgorithms.h"#include "ADS/tree/JoinAlgorithms.h"#include <concepts>#include <cstddef>#include <cstdint>#include <limits>#include <optional>#include <type_traits>#include <utility>#include "ADS/tree/impl/JoinTree.tpp"Go to the source code of this file.
Classes | |
| struct | ADS::AVLBalance |
| the AVL trees [see AVLJoin] More... | |
| struct | ADS::RedBlackBalance |
| the Red-Black trees [see RedBlackJoin] More... | |
| struct | ADS::WeightBalance |
| the weight-balanced trees More... | |
| struct | ADS::TreapBalance |
| the treaps [see TreapJoin] More... | |
| struct | ADS::NoAugmentation |
| no augmentation: nothing is kept, nothing computed More... | |
| struct | ADS::NoAugmentation::Value |
| the empty value More... | |
| struct | ADS::SumAugmentation< V > |
| the sum of the values, an abelian group when V has the opposites More... | |
| struct | ADS::MaxAugmentation< V > |
| the largest of the values, whose identity is \( -\infty \) if V has it, its smallest value otherwise: a commutative and selective monoid More... | |
| class | ADS::JoinTree< K, V, B, G, Policy, A > |
| 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... | |
| struct | ADS::JoinTree< K, V, B, G, Policy, A >::Node |
| a node: what the navigation below reads More... | |
| struct | ADS::JoinTree< K, V, B, G, Policy, A >::Shape |
| the shape of the tree, for PathIterator More... | |
Namespaces | |
| namespace | ADS |
Concepts | |
| concept | ADS::Augmentation |
| an augmentation of a map from K to V: a monoid on G::Value, of identity G::identity() and associative operation G::combine( a , b ), and the value G::from( k , v ) of a pair; the augmented value of a subtree is the combination of the values of its pairs by increasing keys | |
Typedefs | |
| template<std::totally_ordered K, class V, class G = NoAugmentation, class Policy = NoObservers, class A = std::allocator< std::pair< const K , V > >> | |
| using | ADS::JoinAVLTree = JoinTree< K , V , AVLBalance , G , Policy , A > |
| the AVL tree whose balance is in join alone | |
| template<std::totally_ordered K, class V, class G = NoAugmentation, class Policy = NoObservers, class A = std::allocator< std::pair< const K , V > >> | |
| using | ADS::JoinRedBlackTree = JoinTree< K , V , RedBlackBalance , G , Policy , A > |
| the Red-Black tree whose balance is in join alone | |
| template<std::totally_ordered K, class V, class G = NoAugmentation, class Policy = NoObservers, class A = std::allocator< std::pair< const K , V > >> | |
| using | ADS::WeightBalancedTree = JoinTree< K , V , WeightBalance , G , Policy , A > |
| the tree balanced by weight, in join alone | |
| template<std::totally_ordered K, class V, class G = NoAugmentation, class Policy = NoObservers, class A = std::allocator< std::pair< const K , V > >> | |
| using | ADS::JoinTreap = JoinTree< K , V , TreapBalance , G , Policy , A > |
| the treap whose balance is in join alone | |
| template<std::totally_ordered K, class V, class G, class Policy = NoObservers, class A = std::allocator< std::pair< const K , V > >> | |
| using | ADS::AugmentedMap = JoinTree< K , V , WeightBalance , G , Policy , A > |
| the map augmented by G, balanced by weight as in PAM | |
The binary search trees balanced by join alone, and the augmented maps, beyond the book: JoinTree< K , V , B , G > is a DICTIONARY [see ADT.h] of keys K and values V whose only balancing operation is the join of the scheme B [see JoinAlgorithms.h], AVLBalance, RedBlackBalance, WeightBalance or TreapBalance, so that the insertions and the removals are a split or a descent followed by joins, and the bulk operations of Joinable (union, intersection, difference, batches of insertions and of removals, filter, map and reduce, extraction of a range) are those of the four schemes alike.
A node keeps the size of its subtree and one word for its scheme: the height plus 1 for AVL, the black height and the color for Red-Black, the priority for the treap (the weight is the size); JoinAVLTree, JoinRedBlackTree, WeightBalancedTree and JoinTreap name the four.
The augmentation G is a monoid on a type G::Value, of identity G::identity() and associative operation G::combine(), and a map G::from( k , v ) of a pair into it [see Augmentation]. A node \( u \) keeps the augmented value of its subtree \( T_u \),
\[ \mathit{aug}( u ) = \mathit{aug}( u.\mathit{left} ) \otimes \mathit{from}( u.k , u.v ) \otimes \mathit{aug}( u.\mathit{right} ) , \]
the combination of the values of its pairs by increasing keys, which need not be commutative; connect() recomputes it, so that every join, and every operation built on join, keeps it at the cost of one combination per node touched. augValue() is that of the root, in \( O( 1 ) \); augRange( lo , hi ) combines the values of the \( O( \log n ) \) pieces that cover the keys of \( [ \mathit{lo} , \mathit{hi} ] \): from the node where the paths to \( \mathit{lo} \) and to \( \mathit{hi} \) part, the pairs met on the path to \( \mathit{lo} \) with the right subtrees it leaves behind, the pair of the node, and the left subtrees and the pairs of the path to \( \mathit{hi} \); augPrefix( k ) does the same on the path to \( k \) alone, and augVisit() gives the pieces themselves, in order, to a function (the range tree counts on them [see RangeTree.h]). augSelect( p ) descends to the first pair whose prefix satisfies a monotone predicate (the \( i \)-th key on the counts, the first prefix whose sum reaches a threshold), and augFilter( q , f ) gives the pairs whose value satisfies a predicate that distributes over the combination, entering only the subtrees whose augmented value satisfies it, in time proportional to the pairs given (see Aggregates that change). SumAugmentation and MaxAugmentation are the sums and the maxima of the values, and AugmentedMap the map augmented by G and balanced by weight, as in PAM; the interval tree [see IntervalTree.h] and the range tree are augmented maps.
An augmentation may declare its algebra by static members, which refine Augmentation by the concepts of Algebra.h: commutative, inverse( a ) (a group), selective (the combination of two values is one of them), as SumAugmentation (an abelian group when its values have opposites) and MaxAugmentation (commutative and selective) do. assign( k , v ) changes the value of a key that is there and keeps the shape: when G is an abelian group of exact values, it combines each augmented value of the path to \( k \) with the difference of the new and the old value, otherwise it recomputes them from the children, from \( k \) up.
The changes go to the observers of the subject policy Policy [see Modification.h], NoObservers by default, which adds neither a member nor an instruction: Inserted, Erased (told before the removal) and Assigned of the key, whose handle is the key itself, by insert(), remove() and assign() (an insertion of a key that is there is an assignment), Cleared by clear() and on a tree whose nodes a bulk operation takes (the second tree of a union, a tree moved from), and Rebuilt on a tree whose nodes it puts back (the first tree of a union, the trees of a split, both trees of a swap). With someone listening, an insertion and a removal make one more descent, to read the old value; an observer that throws from on() of Inserted or Assigned gets the change undone (the strong guarantee). A tree of static observers is not copied nor moved, and has no split; a copy or a move of the others has no observer.
The nodes come from the allocator A, std::allocator by default (by new and delete), which must be stateless, its allocators all equal and default-constructible (e.g., HugePageAllocator or mi_stl_allocator of mimalloc): the algorithms of JoinAlgorithms.h build and free the nodes with no tree at hand, and in parallel by the set operations, so that a stateful allocator, as one of std::pmr, has nowhere to be kept.
The contracts [see Contract.h]: insert() ensures that the key is there, remove() that it is not and assign() that the key has its new value; the predicate of augSelect() is monotone on the prefixes, that of augFilter() false on the identity and distributive (not checked); the operations of a group are total on the values; the invariant is the order of the keys, the sizes, the augmented values (when G::Value is comparable) and the balance of the scheme (the heights of AVL; no red node with a red child and the same black height on every path, the root black, for Red-Black; \( \alpha ( w_l + w_r ) \le \min( w_l , w_r ) \) for the weights of the two children of every node; the heap order of the priorities of the treap), an \( O( n ) \) check at the level 3. The operations are defined in JoinTree.tpp.