ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
JoinAlgorithms.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------- File JoinAlgorithms.h --------------------------*/
3/*--------------------------------------------------------------------------*/
161/*--------------------------------------------------------------------------*/
162/*----------------------------- DEFINITIONS --------------------------------*/
163/*--------------------------------------------------------------------------*/
164
165#ifndef __ADS_JoinAlgorithms
166 #define __ADS_JoinAlgorithms
167/* self-identification: #endif constant */
168
169/*--------------------------------------------------------------------------*/
170/*------------------------------ INCLUDES ----------------------------------*/
171/*--------------------------------------------------------------------------*/
172
173#include "ADS/Contract.h"
175
176#include <algorithm>
177#include <concepts>
178#include <cstddef>
179#include <cstdint>
180#include <optional>
181#include <span>
182#include <tuple>
183#include <type_traits>
184#include <utility>
185#include <vector>
186
187/*--------------------------------------------------------------------------*/
188/*--------------------------- NAMESPACE ------------------------------------*/
189/*--------------------------------------------------------------------------*/
190
191namespace ADS
192{
193
194/*--------------------------------------------------------------------------*/
195/*--------------------------- CONCEPT JoinAdaptor --------------------------*/
196/*--------------------------------------------------------------------------*/
197
200inline constexpr std::size_t kJoinGrain = 2048;
201
215template< class A >
216concept JoinAdaptor =
217 requires( typename A::Node * t , typename A::Node *& l ,
218 const typename A::Key & k , typename A::Rank r )
219{
220 { A::nil() } -> std::same_as< typename A::Node * >;
221 { A::left( t ) } -> std::same_as< typename A::Node * >;
222 { A::right( t ) } -> std::same_as< typename A::Node * >;
223 { A::key( t ) } -> std::same_as< const typename A::Key & >;
224 { A::value( t ) } -> std::same_as< typename A::Value & >;
225 { A::size( t ) } -> std::convertible_to< std::size_t >;
226 A::expose( t , l , l );
227 { A::connect( t , t , t ) } -> std::same_as< typename A::Node * >;
228 A::dispose( t );
229 { A::rank( t ) } -> std::same_as< typename A::Rank >;
230 { A::childRank( t , r , t ) } -> std::same_as< typename A::Rank >;
231 {
232 A::join( t , r , t , t , r )
233 } -> std::same_as< std::pair< typename A::Node * , typename A::Rank > >;
234 { k < k } -> std::convertible_to< bool >;
235 };
236
237/*--------------------------------------------------------------------------*/
238/*----------------------- THE JOINS OF THE SCHEMES -------------------------*/
239/*--------------------------------------------------------------------------*/
240
243template< class A >
245{
246 using Node = typename A::Node;
247
276 static Node * join( Node * l , Node * m , Node * r );
277
280 static Node * joinRight( Node * l , Node * m , Node * r );
281
284 static Node * joinLeft( Node * l , Node * m , Node * r );
285 };
286
289template< class A >
291{
292 using Node = typename A::Node;
293
331 static std::pair< Node * , int > join( Node * l , int bl , Node * m ,
332 Node * r ,
333 int br );
334
337 static Node * joinRight( Node * l , int bl , Node * m , Node * r , int br );
338
341 static Node * joinLeft( Node * l , int bl , Node * m , Node * r , int br );
342 };
343
345template< class A >
347{
348 using Node = typename A::Node;
349
350 static constexpr std::size_t kAlphaNum = 29;
351 static constexpr std::size_t kAlphaDen = 100;
352
389 static Node * join( Node * l , Node * m , Node * r );
390
393 static Node * joinRight( Node * l , Node * m , Node * r );
394
397 static Node * joinLeft( Node * l , Node * m , Node * r );
398
401 static bool like( std::size_t a , std::size_t b );
402
404 static std::size_t weight( const Node * t );
405 };
406
408template< class A >
410{
411 using Node = typename A::Node;
412
433 static Node * join( Node * l , Node * m , Node * r );
434 };
435
436/*--------------------------------------------------------------------------*/
437/*------------------------- CLASS JoinAlgorithms ---------------------------*/
438/*--------------------------------------------------------------------------*/
439
443template< JoinAdaptor A >
445{
446
447 using Node = typename A::Node;
448 using Key = typename A::Key;
449 using Value = typename A::Value;
450 using Rank = typename A::Rank;
451
453 struct Tree
454 {
455 Node * t = nullptr;
456 Rank r{};
457 };
458
460 struct Exposed
461 {
463 Node * m = nullptr;
465 };
466
468 [[nodiscard]] static Tree empty( void );
469
471 [[nodiscard]] static Tree whole( Node * t );
472
475 [[nodiscard]] static Exposed expose( Tree x );
476
478 [[nodiscard]] static Tree join( Tree l , Node * m , Tree r );
479
501 static Node * split( Tree x , const Key & k , Tree & l , Tree & r );
502
518 static Node * splitLast( Tree x , Tree & rest );
519
533 [[nodiscard]] static Tree join2( Tree l , Tree r );
534
554 [[nodiscard]] static Tree insert( Tree x , Node * n , bool & fresh );
555
573 [[nodiscard]] static Tree remove( Tree x , const Key & k , bool & gone );
574
598 template< Executor E , class F >
599 [[nodiscard]] static Tree unite( E & ex , Tree a , Tree b , const F & f );
600
623 template< Executor E , class F >
624 [[nodiscard]] static Tree intersect( E & ex , Tree a , Tree b , const F & f );
625
654 template< Executor E >
655 [[nodiscard]] static Tree subtract( E & ex , Tree a , Tree b );
656
672 template< Executor E >
673 [[nodiscard]] static Tree build( E & ex , Node * const * N , std::size_t n );
674
703 template< Executor E , class F >
704 [[nodiscard]] static Tree multiInsert( E & ex , Tree x , Node * const * N ,
705 std::size_t n , const F & f );
706
733 template< Executor E >
734 [[nodiscard]] static Tree multiRemove( E & ex , Tree x , const Key * K ,
735 std::size_t n , std::size_t & gone );
736
754 template< Executor E , class P >
755 [[nodiscard]] static Tree filter( E & ex , Tree x , const P & p );
756
775 template< Executor E , class M , class R , class U >
776 [[nodiscard]] static U mapReduce( E & ex , Node * t , const M & map ,
777 const R & reduce , const U & id );
778
797 [[nodiscard]] static Tree extract( Tree & x , const Key & lo ,
798 const Key & hi );
799
802 [[nodiscard]] static const Key & firstKey( Node * t );
803
806 [[nodiscard]] static const Key & lastKey( Node * t );
807
808 private:
810 template< Executor E , class G , class H >
811 static void fork( E & ex , G && g , H && h );
812
814 template< class G , class H >
815 static void fork( Sequential & ex , G && g , H && h );
816
818 template< Executor E >
819 [[nodiscard]] static bool small( E & ex , std::size_t n );
820
821 }; // end( struct JoinAlgorithms )
822
823/*--------------------------------------------------------------------------*/
824/*---------------------------- CLASS Joinable ------------------------------*/
825/*--------------------------------------------------------------------------*/
826
829{
831 template< class V >
832 V operator()( const V & , const V & b ) const {
833 return( b );
834 }
835 };
836
839{
841 template< class V >
842 V operator()( const V & a , const V & ) const {
843 return( a );
844 }
845 };
846
858template< class T , class A , class Key , class Value >
860{
861
862/*--------------------------------------------------------------------------*/
863 public:
864/*--------------------------------------------------------------------------*/
865/*------------------------- SPLITTING AND JOINING --------------------------*/
866/*--------------------------------------------------------------------------*/
867
871 [[nodiscard]] std::tuple< T , std::optional< Value > , T >
872 split( const Key & k ) &&;
873
879 [[nodiscard]] static T join( T && l , const Key & k , const Value & v ,
880 T && r );
881
885 [[nodiscard]] static T join2( T && l , T && r );
886
890 [[nodiscard]] T extractRange( const Key & lo , const Key & hi );
891
892/*--------------------------------------------------------------------------*/
893/*--------------------------- BULK OPERATIONS ------------------------------*/
894/*--------------------------------------------------------------------------*/
895
899 template< class F = TakeSecond >
900 void unionWith( T && o , F f = F() ) {
901 Sequential seq;
902 unionWith( seq , std::move( o ) , f );
903 }
904
906 template< Executor E , class F = TakeSecond >
907 void unionWith( E & ex , T && o , F f = F() );
908
913 template< class F = TakeFirst >
914 void intersectWith( T && o , F f = F() ) {
915 Sequential seq;
916 intersectWith( seq , std::move( o ) , f );
917 }
918
920 template< Executor E , class F = TakeFirst >
921 void intersectWith( E & ex , T && o , F f = F() );
922
926 void differenceWith( T && o ) {
927 Sequential seq;
928 differenceWith( seq , std::move( o ) );
929 }
930
932 template< Executor E >
933 void differenceWith( E & ex , T && o );
934
939 template< class F = TakeSecond >
940 void multiInsert( std::span< const std::pair< Key , Value > > B ,
941 F f = F() ) {
942 Sequential seq;
943 multiInsert( seq , B , f );
944 }
945
947 template< Executor E , class F = TakeSecond >
948 void multiInsert( E & ex , std::span< const std::pair< Key , Value > > B ,
949 F f = F() );
950
954 void multiRemove( std::span< const Key > B ) {
955 Sequential seq;
956 multiRemove( seq , B );
957 }
958
960 template< Executor E >
961 void multiRemove( E & ex , std::span< const Key > B );
962
965 template< class P >
966 void filter( P p ) {
967 Sequential seq;
968 filter( seq , p );
969 }
970
972 template< Executor E , class P >
973 void filter( E & ex , P p );
974
979 template< class M , class R , class U >
980 [[nodiscard]] U mapReduce( M map , R reduce , U id ) const {
981 Sequential seq;
982 return( mapReduce( seq , map , reduce , std::move( id ) ) );
983 }
984
986 template< Executor E , class M , class R , class U >
987 [[nodiscard]] U mapReduce( E & ex , M map , R reduce , U id ) const;
988
989 }; // end( class Joinable )
990
991/*--------------------------------------------------------------------------*/
992
993} // end( namespace ADS )
994
995/*--------------------------------------------------------------------------*/
996/*-------------------------- IMPLEMENTATION --------------------------------*/
997/*--------------------------------------------------------------------------*/
998
999#include "ADS/tree/impl/JoinAlgorithms.tpp"
1000
1001/*--------------------------------------------------------------------------*/
1002
1003#endif /* JoinAlgorithms.h included */
1004
1005/*--------------------------------------------------------------------------*/
1006/*----------------------- End File JoinAlgorithms.h ------------------------*/
1007/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The layer of execution of the parallel algorithms of ADS, beyond the book: an executor runs the calls...
the operations based on join of the tree T of keys Key and values Value, whose nodes A adapts [see Jo...
Definition JoinAlgorithms.h:860
void intersectWith(T &&o, F f=F())
the intersection with o, which is consumed, by JoinAlgorithms::intersect(); the value f( mine ,...
Definition JoinAlgorithms.h:914
void multiRemove(E &ex, std::span< const Key > B)
multiRemove( B ) on the threads of ex
void unionWith(T &&o, F f=F())
the union with o, which is consumed, by JoinAlgorithms::unite(); on a key of both,...
Definition JoinAlgorithms.h:900
void multiInsert(E &ex, std::span< const std::pair< Key, Value > > B, F f=F())
multiInsert( B , f ) on the threads of ex
void multiInsert(std::span< const std::pair< Key, Value > > B, F f=F())
the pairs of inserted, by JoinAlgorithms::multiInsert(); on a key already there, the value f( mine ,...
Definition JoinAlgorithms.h:940
void differenceWith(E &ex, T &&o)
differenceWith( o ) on the threads of ex
static T join2(T &&l, T &&r)
the tree of the keys of and of , which are consumed, by JoinAlgorithms::join2(), in time
T extractRange(const Key &lo, const Key &hi)
the pairs of the keys in , taken out of the tree by JoinAlgorithms::extract(), in time
std::tuple< T, std::optional< Value >, T > split(const Key &k) &&
the keys before , the value of (if there), the keys after , by JoinAlgorithms::split(); the tree is ...
void intersectWith(E &ex, T &&o, F f=F())
intersectWith( o , f ) on the threads of ex
void differenceWith(T &&o)
the keys not in o, which is consumed, by JoinAlgorithms::subtract(); the work of unionWith(),...
Definition JoinAlgorithms.h:926
static T join(T &&l, const Key &k, const Value &v, T &&r)
the tree of the keys of , the pair and the keys of , by the join of the scheme; and are consumed,...
void multiRemove(std::span< const Key > B)
the keys of removed, by JoinAlgorithms::multiRemove(), work
Definition JoinAlgorithms.h:954
void unionWith(E &ex, T &&o, F f=F())
unionWith( o , f ) on the threads of ex
void filter(P p)
only the pairs with p( k , v ) kept, by JoinAlgorithms::filter(), work
Definition JoinAlgorithms.h:966
U mapReduce(E &ex, M map, R reduce, U id) const
mapReduce( map , reduce , id ) on the threads of ex
void filter(E &ex, P p)
filter( p ) on the threads of ex
U mapReduce(M map, R reduce, U id) const
by an associative reduce of identity id, by JoinAlgorithms::mapReduce(), work
Definition JoinAlgorithms.h:980
the executor of the calling thread alone, which makes the calls in their order
Definition Executor.h:265
the nodes of a binary search tree as the algorithms based on join see them: nil(),...
Definition JoinAlgorithms.h:216
constexpr std::size_t kJoinGrain
the sum of the sizes of two trees below which the algorithms run on the calling thread alone
Definition JoinAlgorithms.h:200
Definition ADT.h:96
the join of the AVL trees; A gives also height( t ), -1 for nil, which connect() keeps
Definition JoinAlgorithms.h:245
typename A::Node Node
the type of the nodes
Definition JoinAlgorithms.h:246
static Node * joinRight(Node *l, Node *m, Node *r)
the join when is higher than by more than 1, by the right spine of
static Node * joinLeft(Node *l, Node *m, Node *r)
the join when is higher than by more than 1, the mirror of joinRight()
static Node * join(Node *l, Node *m, Node *r)
the join of the AVL trees and with between them, in time
a tree taken apart at its root
Definition JoinAlgorithms.h:461
Tree r
the right subtree
Definition JoinAlgorithms.h:464
Node * m
the root, alone
Definition JoinAlgorithms.h:463
Tree l
the left subtree
Definition JoinAlgorithms.h:462
a tree: its root and its rank
Definition JoinAlgorithms.h:454
Rank r
the rank
Definition JoinAlgorithms.h:456
Node * t
the root, nil if empty
Definition JoinAlgorithms.h:455
the algorithms on the trees whose only balancing is the join of A; each of them consumes the trees it...
Definition JoinAlgorithms.h:445
typename A::Value Value
the type of the values
Definition JoinAlgorithms.h:449
static Tree join2(Tree l, Tree r)
the tree of the keys of and of , with no key in between, in time
typename A::Node Node
the type of the nodes
Definition JoinAlgorithms.h:447
static Tree filter(E &ex, Tree x, const P &p)
the nodes of with kept, the others freed: work and span
static U mapReduce(E &ex, Node *t, const M &map, const R &reduce, const U &id)
the reduction of the values of the pairs of the tree of root , in the order of the keys,...
static Tree insert(Tree x, Node *n, bool &fresh)
the node alone inserted in , fresh whether its key was not there (if it was, its value replaces the ...
static Node * split(Tree x, const Key &k, Tree &l, Tree &r)
the keys of smaller than in , those larger in , and the node of returned, alone (nil if is not in...
static Tree whole(Node *t)
the tree of root , its rank computed
static Tree extract(Tree &x, const Key &lo, const Key &hi)
the keys of in taken out into the tree returned, by two splits, two joins and a join2,...
static Exposed expose(Tree x)
the subtrees and the root of
static Tree multiInsert(E &ex, Tree x, Node *const *N, std::size_t n, const F &f)
the nodes alone of , whose keys increase, inserted in ; on a key already there the value ,...
static Tree intersect(E &ex, Tree a, Tree b, const F &f)
the intersection of and , the value , with the work and the span of unite(); the nodes that leave ar...
static Tree multiRemove(E &ex, Tree x, const Key *K, std::size_t n, std::size_t &gone)
the keys of , increasing, taken out of and freed, gone how many were there: the difference of and ...
static Node * splitLast(Tree x, Tree &rest)
the node of the largest key of returned, alone, and the others in rest, in time
static const Key & firstKey(Node *t)
the smallest key of
static Tree empty(void)
the empty tree
static Tree join(Tree l, Node *m, Tree r)
the join of the scheme, with the ranks
static Tree unite(E &ex, Tree a, Tree b, const F &f)
the union of and , on a key of both the value , with work and span for sizes
static Tree build(E &ex, Node *const *N, std::size_t n)
the tree of the nodes alone of , whose keys increase: the middle one joined with the trees of the tw...
static const Key & lastKey(Node *t)
the largest key of
typename A::Key Key
the type of the keys
Definition JoinAlgorithms.h:448
static Tree subtract(E &ex, Tree a, Tree b)
the keys of not in , with the work and the span of unite(); the nodes of and those of that leave a...
static Tree remove(Tree x, const Key &k, bool &gone)
the key taken out of and freed, gone whether it was there, by joins on the way back and join2 where...
typename A::Rank Rank
the type of the ranks
Definition JoinAlgorithms.h:450
the join of the Red-Black trees, whose ranks are the black heights; A gives also red( t ),...
Definition JoinAlgorithms.h:291
static std::pair< Node *, int > join(Node *l, int bl, Node *m, Node *r, int br)
the join of the Red-Black trees and , of black heights and , with between them: the root and its b...
static Node * joinRight(Node *l, int bl, Node *m, Node *r, int br)
the join when the black height of is larger than , by the right spine of
typename A::Node Node
the type of the nodes
Definition JoinAlgorithms.h:292
static Node * joinLeft(Node *l, int bl, Node *m, Node *r, int br)
the join when the black height of is larger than , the mirror of joinRight()
the value of the first tree on a key in both
Definition JoinAlgorithms.h:839
V operator()(const V &a, const V &) const
Definition JoinAlgorithms.h:842
the value of the second tree, or of the batch, on a key in both
Definition JoinAlgorithms.h:829
V operator()(const V &, const V &b) const
Definition JoinAlgorithms.h:832
the join of the treaps; A gives also priority( t ), of a node
Definition JoinAlgorithms.h:410
static Node * join(Node *l, Node *m, Node *r)
the join of the treaps and with between them, in expected time
typename A::Node Node
the type of the nodes
Definition JoinAlgorithms.h:411
the join of the trees balanced by weight, with
Definition JoinAlgorithms.h:347
static constexpr std::size_t kAlphaNum
, up
Definition JoinAlgorithms.h:350
static std::size_t weight(const Node *t)
the weight of , its size plus 1
static Node * joinLeft(Node *l, Node *m, Node *r)
the join when is heavier than beyond the balance, the mirror of joinRight()
typename A::Node Node
the type of the nodes
Definition JoinAlgorithms.h:348
static bool like(std::size_t a, std::size_t b)
whether the weights and may be the two sides of a node:
static Node * join(Node *l, Node *m, Node *r)
the join of the trees and balanced by weight with between them, in time
static Node * joinRight(Node *l, Node *m, Node *r)
the join when is heavier than beyond the balance, by the right spine of
static constexpr std::size_t kAlphaDen
and down
Definition JoinAlgorithms.h:351