|
ADS
Algorithms and Data Structures
|
the algorithms on the trees whose only balancing is the join of A; each of them consumes the trees it is given and returns the trees it makes More...
#include <ADS/tree/JoinAlgorithms.h>
Classes | |
| struct | Tree |
| a tree: its root and its rank More... | |
| struct | Exposed |
| a tree taken apart at its root More... | |
Public types | |
| using | Node = typename A::Node |
| the type of the nodes | |
| using | Key = typename A::Key |
| the type of the keys | |
| using | Value = typename A::Value |
| the type of the values | |
| using | Rank = typename A::Rank |
| the type of the ranks | |
Static public member functions | |
| static Tree | empty (void) |
| the empty tree | |
| static Tree | whole (Node *t) |
| the tree of root \( t \), its rank computed | |
| static Exposed | expose (Tree x) |
| the subtrees and the root of \( x \) | |
| static Tree | join (Tree l, Node *m, Tree r) |
| the join of the scheme, with the ranks | |
| static Node * | split (Tree x, const Key &k, Tree &l, Tree &r) |
| the keys of \( x \) smaller than \( k \) in \( l \), those larger in \( r \), and the node of \( k \) returned, alone (nil if \( k \) is not in \( x \)), in \( O( \log n ) \) time | |
| static Node * | splitLast (Tree x, Tree &rest) |
| the node of the largest key of \( x \) returned, alone, and the others in rest, in \( O( \log n ) \) time | |
| static Tree | join2 (Tree l, Tree r) |
| the tree of the keys of \( l \) and of \( r \), with no key in between, in \( O( \log n ) \) time | |
| static Tree | insert (Tree x, Node *n, bool &fresh) |
| the node alone \( n \) inserted in \( x \), fresh whether its key was not there (if it was, its value replaces the old one and \( n \) is freed), by joins on the way back, in \( O( \log n ) \) time | |
| static Tree | remove (Tree x, const Key &k, bool &gone) |
| the key \( k \) taken out of \( x \) and freed, gone whether it was there, by joins on the way back and join2 where it was, in \( O( \log n ) \) time | |
| template<Executor E, class F> | |
| static Tree | unite (E &ex, Tree a, Tree b, const F &f) |
| the union of \( a \) and \( b \), on a key of both the value \( f( \text{of } a , \text{of } b ) \), with \( O( m \log( n / m + 1 ) ) \) work and \( O( \log m \log n ) \) span for sizes \( m \le n \) | |
| template<Executor E, class F> | |
| static Tree | intersect (E &ex, Tree a, Tree b, const F &f) |
| the intersection of \( a \) and \( b \), the value \( f( \text{of } a , \text{of } b ) \), with the work and the span of unite(); the nodes that leave are freed | |
| template<Executor E> | |
| static Tree | subtract (E &ex, Tree a, Tree b) |
| the keys of \( a \) not in \( b \), with the work and the span of unite(); the nodes of \( b \) and those of \( a \) that leave are freed | |
| template<Executor E> | |
| static Tree | build (E &ex, Node *const *N, std::size_t n) |
| the tree of the \( n \) nodes alone of \( N \), whose keys increase: the middle one joined with the trees of the two halves, built in parallel, with \( O( n ) \) work and \( O( \log^2 n ) \) span | |
| template<Executor E, class F> | |
| static Tree | multiInsert (E &ex, Tree x, Node *const *N, std::size_t n, const F &f) |
| the \( n \) nodes alone of \( N \), whose keys increase, inserted in \( x \); on a key already there the value \( f( \text{old} , \text{new} ) \), and the node of \( N \) freed. | |
| template<Executor E> | |
| static Tree | multiRemove (E &ex, Tree x, const Key *K, std::size_t n, std::size_t &gone) |
| the \( n \) keys of \( K \), increasing, taken out of \( x \) and freed, gone how many were there: the difference of \( x \) and \( K \) with the split of \( K \) by a binary search, \( O( m \log( n / m + 1 ) ) \) work | |
| template<Executor E, class P> | |
| static Tree | filter (E &ex, Tree x, const P &p) |
| the nodes of \( x \) with \( p( k , v ) \) kept, the others freed: \( O( n ) \) work and \( O( \log^2 n ) \) span | |
| template<Executor E, class M, class R, class U> | |
| static U | mapReduce (E &ex, Node *t, const M &map, const R &reduce, const U &id) |
| the reduction of the values \( \mathrm{map}( k , v ) \) of the pairs of the tree of root \( t \), in the order of the keys, so that \( \mathrm{reduce} \) needs to be associative only, of identity \( \mathit{id} \); \( O( n ) \) work and \( O( \log n ) \) span | |
| static Tree | extract (Tree &x, const Key &lo, const Key &hi) |
| the keys of \( x \) in \( [ \mathit{lo} , \mathit{hi} ] \) taken out into the tree returned, by two splits, two joins and a join2, in \( O( \log n ) \) time | |
| static const Key & | firstKey (Node *t) |
| the smallest key of \( t \) | |
| static const Key & | lastKey (Node *t) |
| the largest key of \( t \) | |
the algorithms on the trees whose only balancing is the join of A; each of them consumes the trees it is given and returns the trees it makes
| using ADS::JoinAlgorithms< A >::Key = typename A::Key |
the type of the keys
| using ADS::JoinAlgorithms< A >::Node = typename A::Node |
the type of the nodes
| using ADS::JoinAlgorithms< A >::Rank = typename A::Rank |
the type of the ranks
| using ADS::JoinAlgorithms< A >::Value = typename A::Value |
the type of the values
|
staticnodiscard |
the tree of the \( n \) nodes alone of \( N \), whose keys increase: the middle one joined with the trees of the two halves, built in parallel, with \( O( n ) \) work and \( O( \log^2 n ) \) span
In pseudocode (Blelloch, Ferizovic and Sun, beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{build}( N[ 0 .. n - 1 ] ) \cr 2 & \quad \textbf{if}\ n = 0\ \textbf{then return}\ \textbf{nil} \cr 3 & \quad h \gets \lfloor n / 2 \rfloor \cr 4 & \quad T_1 \gets \mathrm{build}( N[ 0 .. h - 1 ] ) \text{ and } T_2 \gets \mathrm{build}( N[ h + 1 .. n - 1 ] ) \text{, in parallel} \cr 5 & \quad \textbf{return}\ \mathrm{join}( T_1 , N[ h ] , T_2 ) \end{array}} \]
|
staticnodiscard |
the empty tree
|
staticnodiscard |
the subtrees and the root of \( x \)
|
staticnodiscard |
the keys of \( x \) in \( [ \mathit{lo} , \mathit{hi} ] \) taken out into the tree returned, by two splits, two joins and a join2, in \( O( \log n ) \) time
In pseudocode (Blelloch, Ferizovic and Sun, beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{extract}( T , \mathit{lo} , \mathit{hi} ) \cr 2 & \quad ( A , u , B ) \gets \mathrm{split}( T , \mathit{lo} );\ B \gets \mathrm{join}( \textbf{nil} , u , B ) \text{ if } u \ne \textbf{nil} \cr 3 & \quad ( C , v , D ) \gets \mathrm{split}( B , \mathit{hi} );\ C \gets \mathrm{join}( C , v , \textbf{nil} ) \text{ if } v \ne \textbf{nil} \cr 4 & \quad T \gets \mathrm{join2}( A , D );\ \textbf{return}\ C \end{array}} \]
|
staticnodiscard |
the nodes of \( x \) with \( p( k , v ) \) kept, the others freed: \( O( n ) \) work and \( O( \log^2 n ) \) span
In pseudocode (Blelloch, Ferizovic and Sun, beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{filter}( T , p ) \cr 2 & \quad \textbf{if}\ T = \textbf{nil}\ \textbf{then return}\ T \cr 3 & \quad ( L , k , R ) \gets \mathrm{expose}( T ) \cr 4 & \quad T_1 \gets \mathrm{filter}( L , p ) \text{ and } T_2 \gets \mathrm{filter}( R , p ) \text{, in parallel} \cr 5 & \quad \textbf{if}\ p( k )\ \textbf{then return}\ \mathrm{join}( T_1 , k , T_2 ) \cr 6 & \quad \text{free } k;\ \textbf{return}\ \mathrm{join2}( T_1 , T_2 ) \end{array}} \]
|
staticnodiscard |
the smallest key of \( t \)
|
staticnodiscard |
the node alone \( n \) inserted in \( x \), fresh whether its key was not there (if it was, its value replaces the old one and \( n \) is freed), by joins on the way back, in \( O( \log n ) \) time
In pseudocode (Blelloch, Ferizovic and Sun, beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{insert}( T , n ) \cr 2 & \quad \textbf{if}\ T = \textbf{nil}\ \textbf{then return} \ \mathrm{join}( \textbf{nil} , n , \textbf{nil} ) \cr 3 & \quad ( L , m , R ) \gets \mathrm{expose}( T ) \cr 4 & \quad \textbf{if}\ n < m\ \textbf{then return}\ \mathrm{join}( \mathrm{insert}( L , n ) , m , R ) \cr 5 & \quad \textbf{if}\ m < n\ \textbf{then return}\ \mathrm{join}( L , m , \mathrm{insert}( R , n ) ) \cr 6 & \quad \text{the value of } m \gets \text{that of } n;\ \text{free } n; \ \textbf{return}\ \mathrm{join}( L , m , R ) \end{array}} \]
|
staticnodiscard |
the intersection of \( a \) and \( b \), the value \( f( \text{of } a , \text{of } b ) \), with the work and the span of unite(); the nodes that leave are freed
In pseudocode (Blelloch, Ferizovic and Sun, beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{intersect}( A , B ) \cr 2 & \quad \textbf{if}\ A = \textbf{nil}\ \textbf{or}\ B = \textbf{nil} \ \textbf{then return}\ \textbf{nil} \cr 3 & \quad ( L , k , R ) \gets \mathrm{expose}( \text{the smaller of } A \text{ and } B ) \cr 4 & \quad ( L' , b , R' ) \gets \mathrm{split}( \text{the larger of } A \text{ and } B , k ) \cr 5 & \quad T_1 \gets \mathrm{intersect}( L , L' ) \text{ and } T_2 \gets \mathrm{intersect}( R , R' ) \text{, in parallel} \cr 6 & \quad \textbf{if}\ b = \textbf{nil}\ \textbf{then return} \ \mathrm{join2}( T_1 , T_2 ) \cr 7 & \quad \text{the value of } k \gets f( \text{of } A , \text{of } B ); \ \textbf{return}\ \mathrm{join}( T_1 , k , T_2 ) \end{array}} \]
|
staticnodiscard |
the join of the scheme, with the ranks
|
staticnodiscard |
the tree of the keys of \( l \) and of \( r \), with no key in between, in \( O( \log n ) \) time
In pseudocode (Blelloch, Ferizovic and Sun, beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{join2}( L , R ) \cr 2 & \quad \textbf{if}\ L = \textbf{nil}\ \textbf{then return}\ R \cr 3 & \quad ( L' , k ) \gets \mathrm{splitLast}( L );\ \textbf{return} \ \mathrm{join}( L' , k , R ) \end{array}} \]
|
staticnodiscard |
the largest key of \( t \)
|
staticnodiscard |
the reduction of the values \( \mathrm{map}( k , v ) \) of the pairs of the tree of root \( t \), in the order of the keys, so that \( \mathrm{reduce} \) needs to be associative only, of identity \( \mathit{id} \); \( O( n ) \) work and \( O( \log n ) \) span
In pseudocode (Blelloch, Ferizovic and Sun, beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{mapReduce}( T ) \cr 2 & \quad \textbf{if}\ T = \textbf{nil}\ \textbf{then return}\ \mathit{id} \cr 3 & \quad ( L , k , R ) \gets \text{the children and the pair of } T \cr 4 & \quad a \gets \mathrm{mapReduce}( L ) \text{ and } b \gets \mathrm{mapReduce}( R ) \text{, in parallel} \cr 5 & \quad \textbf{return}\ \mathrm{reduce}( \mathrm{reduce}( a , \mathrm{map}( k ) ) , b ) \end{array}} \]
|
staticnodiscard |
the \( n \) nodes alone of \( N \), whose keys increase, inserted in \( x \); on a key already there the value \( f( \text{old} , \text{new} ) \), and the node of \( N \) freed.
It is the union of \( x \) and of the tree of \( N \), with the split of the batch by a binary search in place of split(), so that the tree of \( N \) is never built: \( O( m \log( n / m + 1 ) ) \) work for \( m = \min( n , |x| ) \)
In pseudocode (Blelloch, Ferizovic and Sun, beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{multiInsert}( T , N[ 1 .. n ] ) \cr 2 & \quad \textbf{if}\ n = 0\ \textbf{then return}\ T \cr 3 & \quad \textbf{if}\ T = \textbf{nil}\ \textbf{then return} \ \mathrm{build}( N ) \cr 4 & \quad ( L , k , R ) \gets \mathrm{expose}( T ) \cr 5 & \quad j \gets \text{the first index with } N[ j ] \ge k \text{, by a } \text{binary search} \cr 6 & \quad j' \gets j + 1 \text{ if } N[ j ] = k \text{, } j \text{ otherwise} \cr 7 & \quad T_1 \gets \mathrm{multiInsert}( L , N[ 1 .. j - 1 ] ) \text{ and } T_2 \gets \mathrm{multiInsert}( R , N[ j' .. n ] ) \text{, in } \cr & \qquad \text{parallel} \cr 8 & \quad \textbf{return}\ \mathrm{join}( T_1 , k , T_2 ) \end{array}} \]
|
staticnodiscard |
the \( n \) keys of \( K \), increasing, taken out of \( x \) and freed, gone how many were there: the difference of \( x \) and \( K \) with the split of \( K \) by a binary search, \( O( m \log( n / m + 1 ) ) \) work
In pseudocode (Blelloch, Ferizovic and Sun, beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{multiRemove}( T , K[ 1 .. n ] ) \cr 2 & \quad \textbf{if}\ n = 0\ \textbf{or}\ T = \textbf{nil}\ \textbf{then return}\ T \cr 3 & \quad ( L , k , R ) \gets \mathrm{expose}( T );\ j \gets \text{the first } \text{index with } K[ j ] \ge k \cr 4 & \quad j' \gets j + 1 \text{ if } K[ j ] = k \text{, } j \text{ otherwise} \cr 5 & \quad T_1 \gets \mathrm{multiRemove}( L , K[ 1 .. j - 1 ] ) \text{ and } T_2 \gets \mathrm{multiRemove}( R , K[ j' .. n ] ) \text{, in } \cr & \qquad \text{parallel} \cr 6 & \quad \textbf{if}\ K[ j ] = k\ \textbf{then}\ \text{free } k; \ \textbf{return}\ \mathrm{join2}( T_1 , T_2 ) \cr 7 & \quad \textbf{return}\ \mathrm{join}( T_1 , k , T_2 ) \end{array}} \]
|
staticnodiscard |
the key \( k \) taken out of \( x \) and freed, gone whether it was there, by joins on the way back and join2 where it was, in \( O( \log n ) \) time
In pseudocode (Blelloch, Ferizovic and Sun, beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{remove}( T , k ) \cr 2 & \quad \textbf{if}\ T = \textbf{nil}\ \textbf{then return}\ T \cr 3 & \quad ( L , m , R ) \gets \mathrm{expose}( T ) \cr 4 & \quad \textbf{if}\ k < m\ \textbf{then return}\ \mathrm{join}( \mathrm{remove}( L , k ) , m , R ) \cr 5 & \quad \textbf{if}\ m < k\ \textbf{then return}\ \mathrm{join}( L , m , \mathrm{remove}( R , k ) ) \cr 6 & \quad \text{free } m;\ \textbf{return}\ \mathrm{join2}( L , R ) \end{array}} \]
|
static |
the keys of \( x \) smaller than \( k \) in \( l \), those larger in \( r \), and the node of \( k \) returned, alone (nil if \( k \) is not in \( x \)), in \( O( \log n ) \) time
In pseudocode (Blelloch, Ferizovic and Sun, beyond the book), the costs of the joins telescoping along the path:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{split}( T , k ) \cr 2 & \quad \textbf{if}\ T = \textbf{nil}\ \textbf{then return}\ ( \textbf{nil} , \textbf{nil} , \textbf{nil} ) \cr 3 & \quad ( L , m , R ) \gets \mathrm{expose}( T ) \cr 4 & \quad \textbf{if}\ k < m\ \textbf{then} \cr 5 & \qquad ( L' , b , R' ) \gets \mathrm{split}( L , k );\ \textbf{return} \ ( L' , b , \mathrm{join}( R' , m , R ) ) \cr 6 & \quad \textbf{if}\ m < k\ \textbf{then} \cr 7 & \qquad ( L' , b , R' ) \gets \mathrm{split}( R , k );\ \textbf{return} \ ( \mathrm{join}( L , m , L' ) , b , R' ) \cr 8 & \quad \textbf{return}\ ( L , m , R ) \end{array}} \]
|
static |
the node of the largest key of \( x \) returned, alone, and the others in rest, in \( O( \log n ) \) time
In pseudocode (Blelloch, Ferizovic and Sun, beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{splitLast}( T ) \cr 2 & \quad ( L , k , R ) \gets \mathrm{expose}( T ) \cr 3 & \quad \textbf{if}\ R = \textbf{nil}\ \textbf{then return}\ ( L , k ) \cr 4 & \quad ( T' , k' ) \gets \mathrm{splitLast}( R );\ \textbf{return}\ ( \mathrm{join}( L , k , T' ) , k' ) \end{array}} \]
|
staticnodiscard |
the keys of \( a \) not in \( b \), with the work and the span of unite(); the nodes of \( b \) and those of \( a \) that leave are freed
In pseudocode (Blelloch, Ferizovic and Sun, beyond the book), the split always of the larger tree:
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{difference}( A , B ) \cr 2 & \quad \textbf{if}\ A = \textbf{nil}\ \textbf{or}\ B = \textbf{nil} \ \textbf{then return}\ A \cr 3 & \quad \textbf{if}\ |A| < |B|\ \textbf{then} \cr 4 & \qquad ( L , k , R ) \gets \mathrm{expose}( A );\ ( L' , b , R' ) \gets \mathrm{split}( B , k ) \cr 5 & \qquad T_1 \gets \mathrm{difference}( L , L' ) \text{ and } T_2 \gets \mathrm{difference}( R , R' ) \text{, in parallel} \cr 6 & \qquad \textbf{if}\ b = \textbf{nil}\ \textbf{then return} \ \mathrm{join}( T_1 , k , T_2 )\ \textbf{else return}\ \mathrm{join2}( T_1 , T_2 ) \cr 7 & \quad ( L , k , R ) \gets \mathrm{expose}( B );\ ( L' , b , R' ) \gets \mathrm{split}( A , k ) \cr 8 & \quad T_1 \gets \mathrm{difference}( L' , L ) \text{ and } T_2 \gets \mathrm{difference}( R' , R ) \text{, in parallel} \cr 9 & \quad \textbf{return}\ \mathrm{join2}( T_1 , T_2 ) \end{array}} \]
|
staticnodiscard |
the union of \( a \) and \( b \), on a key of both the value \( f( \text{of } a , \text{of } b ) \), with \( O( m \log( n / m + 1 ) ) \) work and \( O( \log m \log n ) \) span for sizes \( m \le n \)
In pseudocode (Blelloch, Ferizovic and Sun, beyond the book):
\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{union}( A , B ) \cr 2 & \quad \textbf{if}\ A = \textbf{nil}\ \textbf{then return}\ B \cr 3 & \quad \textbf{if}\ B = \textbf{nil}\ \textbf{then return}\ A \cr 4 & \quad ( L , k , R ) \gets \mathrm{expose}( \text{the smaller of } A \text{ and } B ) \cr 5 & \quad ( L' , b , R' ) \gets \mathrm{split}( \text{the larger of } A \text{ and } B , k ) \cr 6 & \quad T_1 \gets \mathrm{union}( L , L' ) \text{ and } T_2 \gets \mathrm{union}( R , R' ) \text{, in parallel} \cr 7 & \quad \textbf{if}\ b \ne \textbf{nil}\ \textbf{then}\ \text{the value } \text{of } k \gets f( \text{of } A , \text{of } B ) \cr 8 & \quad \textbf{return}\ \mathrm{join}( T_1 , k , T_2 ) \end{array}} \]
|
staticnodiscard |
the tree of root \( t \), its rank computed