ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
JoinTree.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File JoinTree.h -----------------------------*/
3/*--------------------------------------------------------------------------*/
118/*--------------------------------------------------------------------------*/
119/*----------------------------- DEFINITIONS --------------------------------*/
120/*--------------------------------------------------------------------------*/
121
122#ifndef __ADS_JoinTree
123 #define __ADS_JoinTree
124/* self-identification: #endif constant */
125
126/*--------------------------------------------------------------------------*/
127/*------------------------------ INCLUDES ----------------------------------*/
128/*--------------------------------------------------------------------------*/
129
130#include "ADS/Algebra.h"
131#include "ADS/Contract.h"
132#include "ADS/Iterators.h"
133#include "ADS/Modification.h"
134#include "ADS/Portable.h"
139
140#include <concepts>
141#include <cstddef>
142#include <cstdint>
143#include <limits>
144#include <optional>
145#include <type_traits>
146#include <utility>
147
148/*--------------------------------------------------------------------------*/
149/*--------------------------- NAMESPACE ------------------------------------*/
150/*--------------------------------------------------------------------------*/
151
152namespace ADS
153{
154
155/*--------------------------------------------------------------------------*/
156/*------------------------ SCHEMES AND AUGMENTATIONS -----------------------*/
157/*--------------------------------------------------------------------------*/
158
160{ };
162{ };
164{ };
166{ };
167
173template< class G , class K , class V >
175 requires( const K & k , const V & v , const typename G::Value & a )
176{
177 { G::identity() } -> std::convertible_to< typename G::Value >;
178 { G::from( k , v ) } -> std::convertible_to< typename G::Value >;
179 { G::combine( a , a ) } -> std::convertible_to< typename G::Value >;
180 };
181
183namespace detail
184{
185
188template< class P >
189inline constexpr bool unbound_v = std::is_same_v< P , NoObservers >;
190
191template< class L >
192inline constexpr bool unbound_v< Dynamic< L > > = true;
193
194} // end( namespace detail )
196
199{
201 struct Value
202 {
204 bool operator==( const Value & ) const = default;
205 };
206
207 static Value identity( void ) { return( Value() ); }
209 template< class K , class V >
210 static Value from( const K & , const V & ) {
211 return( Value() );
212 }
213
214 static Value combine( const Value & , const Value & ) { return( Value() ); }
215 };
216
218template< class V >
220{
221 using Value = V;
222 static constexpr bool commutative = true;
224 static Value identity( void ) { return( V() ); }
226 template< class K >
227 static Value from( const K & , const V & v ) {
228 return( v );
229 }
230
231 static Value combine( const Value & a , const Value & b ) { return( a + b ); }
233 static Value inverse( const Value & a )
234 requires requires { -a; }
235 {
236 return( -a );
237 }
238 };
239
243template< class V >
245{
246 using Value = V;
247 static constexpr bool commutative = true;
248 static constexpr bool selective = true;
250 static Value identity( void ) {
251 if constexpr( std::numeric_limits< V >::has_infinity )
252 return( -std::numeric_limits< V >::infinity() );
253 else
254 return( std::numeric_limits< V >::lowest() );
255 }
256
257 template< class K >
258 static Value from( const K & , const V & v ) {
259 return( v );
260 }
261
262 static Value combine( const Value & a , const Value & b ) {
263 return( a < b ? b : a );
264 }
265 };
266
267/*--------------------------------------------------------------------------*/
268/*---------------------------- CLASS JoinTree ------------------------------*/
269/*--------------------------------------------------------------------------*/
270
274template< std::totally_ordered K , class V , class B = AVLBalance ,
275 class G = NoAugmentation , class Policy = NoObservers ,
276 class A = std::allocator< std::pair< const K , V > > >
277 requires Augmentation< G , K , V >
278class JoinTree;
279
281template< std::totally_ordered K , class V , class B , class G , class Policy ,
282 class A >
284
285template< std::totally_ordered K , class V , class B , class G , class Policy ,
286 class A >
289 : public Joinable< JoinTree< K , V , B , G , Policy , A > ,
290 JoinTreeNodes< K , V , B , G , Policy , A > , K , V >
291{
292
293 friend struct JoinTreeNodes< K , V , B , G , Policy , A >;
295
296/*--------------------------------------------------------------------------*/
297 public:
298 using Key = K;
299 using Value = V;
300 using AugValue = typename G::Value;
301 using Augmented = G;
302 using allocator_type = A;
303
305 [[nodiscard]] allocator_type get_allocator( void ) const {
306 return( allocator_type() );
307 }
308
320 using Subject = typename Policy::template bind< modifications , JoinTree >;
321
323 struct Node
324 {
325 Node * left = nullptr;
326 Node * right = nullptr;
327 std::size_t size = 1;
330 std::uint64_t meta = 0;
332 K key;
334 };
335
337 explicit JoinTree( std::uint64_t seed = 1 )
338 requires detail::unbound_v< Policy >;
339
342 template< class... O >
343 requires( sizeof...( O ) > 0 )
344 JoinTree( std::uint64_t seed , O &... o );
345
349 JoinTree( const JoinTree & o );
350
353 JoinTree( JoinTree && o ) noexcept
354 requires detail::unbound_v< Policy >;
355
357 JoinTree & operator=( JoinTree o ) noexcept
358 requires detail::unbound_v< Policy >;
359
362
364 void swap( JoinTree & o ) noexcept;
365
367 [[nodiscard]] Subject & subject( void ) { return( f_sub ); }
368
370 [[nodiscard]] const Subject & subject( void ) const { return( f_sub ); }
371
372/*--------------------------------------------------------------------------*/
373/*------------------------- DICTIONARY OPERATIONS --------------------------*/
374/*--------------------------------------------------------------------------*/
375
378 [[nodiscard]] std::optional< V > lookup( const K & k ) const;
379
394 void insert( const K & k , const V & v );
395
408 void remove( const K & k );
409
444 bool assign( const K & k , const V & v );
445
447 [[nodiscard]] bool contains( const K & k ) const;
448
451 [[nodiscard]] std::optional< std::pair< K , V > >
452 lowerBound( const K & k ) const;
453
457 [[nodiscard]] const K * findKey( const K & k ) const;
458
462 [[nodiscard]] const K * lowerBoundKey( const K & k ) const;
463
465 [[nodiscard]] std::size_t size( void ) const;
466
468 [[nodiscard]] bool empty( void ) const;
469
471 void clear( void );
472
474 [[nodiscard]] int height( void ) const;
475
477 template< class F >
478 void forEach( F && f ) const;
479
480/*--------------------------------------------------------------------------*/
481/*------------------------- AUGMENTED QUERIES ------------------------------*/
482/*--------------------------------------------------------------------------*/
483
485 [[nodiscard]] AugValue augValue( void ) const;
486
490 [[nodiscard]] AugValue augRange( const K & lo , const K & hi ) const;
491
509 [[nodiscard]] AugValue augPrefix( const K & k ) const;
510
539 template< class F >
540 void augVisit( const K & lo , const K & hi , F && f ) const;
541
567 template< class P >
568 [[nodiscard]] std::optional< std::pair< K , V > > augSelect( P && p ) const;
569
593 template< class Q , class F >
594 void augFilter( Q && q , F && f ) const;
595
596/*--------------------------------------------------------------------------*/
597/*---------------------------- NAVIGATION ----------------------------------*/
598/*--------------------------------------------------------------------------*/
599
601 [[nodiscard]] const Node * root( void ) const { return( f_root ); }
602
603/*------------------------- THE STANDARD INTERFACE -------------------------*/
604
606 struct Shape
607 {
609 static auto root( const auto & o ) { return( o.f_root ); }
611 static auto left( const auto * n ) { return( n->left ); }
613 static auto right( const auto * n ) { return( n->right ); }
615 static auto read( const auto * n ) {
616 return( PairRef< const K & , const V & >( n->key , n->value ) );
617 }
618 };
619
621 using value_type = std::pair< K , V >;
623 using key_type = K;
625 using mapped_type = V;
626 using size_type = std::size_t;
627 using difference_type = std::ptrdiff_t;
633
635 [[nodiscard]] const_iterator begin( void ) const {
636 return( const_iterator( this , true ) );
637 }
638
639 [[nodiscard]] const_iterator end( void ) const {
640 return( const_iterator( this , false ) );
641 }
642
643 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
645 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
646
647/*--------------------------------------------------------------------------*/
648 private:
651 [[nodiscard]] bool valid( void ) const;
652
653
655 [[nodiscard]] Node * findNode( const K & k ) const;
656
658 static constexpr bool kDelta = AbelianGroup< G > && exact_v< G >;
659
660/*------------------------ THE CHANGES, UNOBSERVED -------------------------*/
661
663 void insertPlain( const K & k , const V & v );
665 void removePlain( const K & k );
667 bool assignPlain( const K & k , const V & v );
668
669/*------------------------- THE CHANGES, OBSERVED --------------------------*/
670
672 void insertTold( const K & k , const V & v );
674 ADS_COLD_PATH void insertCold( const K & k , const V & v ) {
675 insertTold( k , v );
676 }
678 void removeTold( const K & k );
680 ADS_COLD_PATH void removeCold( const K & k ) { removeTold( k ); }
682 bool assignTold( const K & k , const V & v );
684 ADS_COLD_PATH bool assignCold( const K & k , const V & v ) {
685 return( assignTold( k , v ) );
686 }
688 ADS_COLD_PATH void clearTold( void );
690 void tellCleared( void ) noexcept;
692 void tellRebuilt( void ) noexcept;
693
694 Node * f_root = nullptr;
695 SplitMix64 f_g;
697
698 }; // end( class JoinTree )
699
700/*--------------------------------------------------------------------------*/
701
703template< std::totally_ordered K , class V , class G = NoAugmentation ,
704 class Policy = NoObservers ,
705 class A = std::allocator< std::pair< const K , V > > >
707
709template< std::totally_ordered K , class V , class G = NoAugmentation ,
710 class Policy = NoObservers ,
711 class A = std::allocator< std::pair< const K , V > > >
713
715template< std::totally_ordered K , class V , class G = NoAugmentation ,
716 class Policy = NoObservers ,
717 class A = std::allocator< std::pair< const K , V > > >
719
721template< std::totally_ordered K , class V , class G = NoAugmentation ,
722 class Policy = NoObservers ,
723 class A = std::allocator< std::pair< const K , V > > >
725
727template< std::totally_ordered K , class V , class G ,
728 class Policy = NoObservers ,
729 class A = std::allocator< std::pair< const K , V > > >
731
732/*--------------------------------------------------------------------------*/
733
734} // end( namespace ADS )
735
736/*--------------------------------------------------------------------------*/
737/*-------------------------- IMPLEMENTATION --------------------------------*/
738/*--------------------------------------------------------------------------*/
739
740#include "ADS/tree/impl/JoinTree.tpp"
741
742/*--------------------------------------------------------------------------*/
743
744#endif /* JoinTree.h included */
745
746/*--------------------------------------------------------------------------*/
747/*--------------------------- End File JoinTree.h --------------------------*/
748/*--------------------------------------------------------------------------*/
The algebra of the aggregates, beyond the book: the concepts by which a structure that keeps a combin...
The rules of the allocator-aware containers of the standard ([container.reqmts], [allocator....
The classic problems on binary trees and on binary search trees, written once for any representation ...
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The iterators that the containers of the library have in common, beyond the book: they give a contain...
The balanced binary search trees whose only balancing operation is join, beyond the book,...
The typed modifications, beyond the book: the vocabulary by which a structure tells the observers att...
The primitives of the processor that the compilers spell each in their own way, in one form for all: ...
Hashing with random tables, beyond the book:
a DICTIONARY on a binary search tree balanced by the join of B alone, augmented by G,...
Definition JoinTree.h:291
bool contains(const K &k) const
true if is there
AugValue augPrefix(const K &k) const
the augmented value of the pairs with keys , in calls of combine()
const_iterator end(void) const
the iterator past the largest key
Definition JoinTree.h:639
std::size_t size(void) const
the number of keys
typename Policy::template bind< modifications, JoinTree > Subject
Definition JoinTree.h:320
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) satisfie...
bool empty(void) const
true if there is no key
void insert(const K &k, const V &v)
associates to , in time; tells Inserted, or Assigned if was there (by one more descent,...
PathIterator< JoinTree, Shape > const_iterator
Definition JoinTree.h:631
const K * lowerBoundKey(const K &k) const
the address of the smallest key not smaller than , nil if none, in time: the find( x ) of an SSet of...
void clear(void)
removes all the pairs; tells Cleared if there was one
mod::list< Inserted, Erased, Assigned, mod::Cleared, mod::Rebuilt > modifications
Definition JoinTree.h:317
bool assign(const K &k, const V &v)
the value of set to , and true, if is there, nothing and false otherwise, in time; the shape stays...
PairRef< const K &, const std::size_t & > reference
Definition JoinTree.h:628
void remove(const K &k)
removes , if it is there, in time; tells Erased (by one more descent, only when someone listens)
std::ptrdiff_t difference_type
Definition JoinTree.h:627
AugValue augRange(const K &lo, const K &hi) const
the augmented value of the pairs with keys in , in calls of combine(), the combination of the pieces...
const_iterator begin(void) const
the iterator on the smallest key
Definition JoinTree.h:635
const_iterator cend(void) const
as end()
Definition JoinTree.h:645
std::pair< K, std::size_t > value_type
Definition JoinTree.h:621
mod::Assigned< K, std::size_t > Assigned
Definition JoinTree.h:314
const_iterator iterator
Definition JoinTree.h:632
const Node * root(void) const
the root, nullptr if empty; the nodes must not be changed
Definition JoinTree.h:601
std::optional< V > lookup(const K &k) const
the value of , or nothing, by the descent of a binary search tree, in time
Subject & subject(void)
Definition JoinTree.h:367
allocator_type get_allocator(void) const
the allocator of the nodes, stateless
Definition JoinTree.h:305
int height(void) const
the height of the tree, -1 if empty, in time
void augFilter(Q &&q, F &&f) const
f( k , v ) on the pairs with , by increasing keys, entering only the subtrees whose augmented value ...
typename MaxEnd::Value AugValue
Definition JoinTree.h:300
std::optional< std::pair< K, V > > lowerBound(const K &k) const
the smallest key not smaller than and its value, or nothing (the find( x ) of an SSet of Morin),...
JoinTree(std::uint64_t seed=1)
an empty tree; the seed of the priorities, for a treap
const Subject & subject(void) const
the subject
Definition JoinTree.h:370
AugValue augValue(void) const
the augmented value of the whole map, in time
mod::Inserted< K, K, std::size_t > Inserted
Definition JoinTree.h:310
void augVisit(const K &lo, const K &hi, F &&f) const
f( a ) on the augmented values of the pieces (whole subtrees and single pairs) whose union is the s...
mod::Erased< K, K, std::size_t > Erased
Definition JoinTree.h:312
const K * findKey(const K &k) const
the address of the key , nil if absent, in time: the find( x ) of a USet of Morin [see DictionarySet...
void forEach(F &&f) const
f( k , v ) on the pairs by increasing keys
const_iterator cbegin(void) const
as begin()
Definition JoinTree.h:643
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
a bidirectional iterator on the nodes of a binary tree of the owner O in symmetric order,...
Definition Iterators.h:867
a group whose operation commutes
Definition Algebra.h:137
an augmentation of a map from K to V: a monoid on G::Value, of identity G::identity() and associative...
Definition JoinTree.h:174
constexpr bool exact_v
whether the values of G are exact, false for the floating point types, whose groups drift by rounding...
Definition Algebra.h:161
std::conditional_t< detail::plainPair< A, B, VA, VB >, std::pair< A, B >, detail::PairProxy< A, B, VA, VB > > PairRef
the pair that an iterator reads by value, in general two references into the container,...
Definition Iterators.h:648
Changed< Value, H, V > Assigned
the value of an item changed
Definition Modification.h:623
#define ADS_COLD_PATH
a function of a rare path: never inlined, laid out away from the hot code
Definition Portable.h:107
#define ADS_NO_UNIQUE_ADDRESS
an empty member that takes no storage, by the attribute that does so (the standard one is accepted bu...
Definition Portable.h:81
JoinTree< K, V, WeightBalance, G, Policy, A > AugmentedMap
the map augmented by G, balanced by weight as in PAM
Definition JoinTree.h:730
JoinTree< K, V, AVLBalance, G, Policy, A > JoinAVLTree
the AVL tree whose balance is in join alone
Definition JoinTree.h:706
JoinTree< K, V, TreapBalance, G, Policy, A > JoinTreap
the treap whose balance is in join alone
Definition JoinTree.h:724
JoinTree< K, V, RedBlackBalance, G, Policy, A > JoinRedBlackTree
the Red-Black tree whose balance is in join alone
Definition JoinTree.h:712
JoinTree< K, V, WeightBalance, G, Policy, A > WeightBalancedTree
the tree balanced by weight, in join alone
Definition JoinTree.h:718
Definition ADT.h:96
the AVL trees [see AVLJoin]
Definition JoinTree.h:160
the adaptor of the nodes of a JoinTree [see JoinAdaptor]
Definition JoinTree.h:283
a node: what the navigation below reads
Definition JoinTree.h:324
std::size_t size
the nodes of the subtree
Definition JoinTree.h:327
K key
the key
Definition JoinTree.h:332
Node * left
the left child
Definition JoinTree.h:325
V value
the value
Definition JoinTree.h:333
std::uint64_t meta
the height plus 1 (AVL), the black height times 2 plus 1 if red (Red-Black), the priority (treap)
Definition JoinTree.h:330
AugValue aug
the augmented value
Definition JoinTree.h:331
Node * right
the right child
Definition JoinTree.h:326
the shape of the tree, for PathIterator
Definition JoinTree.h:607
static auto root(const auto &o)
the root of the tree o
Definition JoinTree.h:609
static auto right(const auto *n)
the right child of the node n
Definition JoinTree.h:613
static auto left(const auto *n)
the left child of the node n
Definition JoinTree.h:611
static auto read(const auto *n)
the pair of the node n
Definition JoinTree.h:615
the largest of the values, whose identity is if V has it, its smallest value otherwise: a commutativ...
Definition JoinTree.h:245
static constexpr bool commutative
the order does not count
Definition JoinTree.h:247
static Value combine(const Value &a, const Value &b)
Definition JoinTree.h:262
static constexpr bool selective
the maximum is one of two
Definition JoinTree.h:248
V Value
the maxima, of the type of the values
Definition JoinTree.h:246
static Value identity(void)
the smallest value of the type
Definition JoinTree.h:250
static Value from(const K &, const V &v)
the value of the pair
Definition JoinTree.h:258
the empty value
Definition JoinTree.h:202
bool operator==(const Value &) const =default
all equal
no augmentation: nothing is kept, nothing computed
Definition JoinTree.h:199
static Value combine(const Value &, const Value &)
the empty value
Definition JoinTree.h:214
static Value identity(void)
the empty value
Definition JoinTree.h:207
static Value from(const K &, const V &)
the empty value
Definition JoinTree.h:210
nobody listens: the subject is empty and every test is false at compile time, so that the structure c...
Definition Modification.h:1958
the Red-Black trees [see RedBlackJoin]
Definition JoinTree.h:162
the sum of the values, an abelian group when V has the opposites
Definition JoinTree.h:220
static Value from(const K &, const V &v)
the value of the pair
Definition JoinTree.h:227
static Value inverse(const Value &a)
Definition JoinTree.h:233
static Value identity(void)
0, the value made by V()
Definition JoinTree.h:224
static Value combine(const Value &a, const Value &b)
Definition JoinTree.h:231
static constexpr bool commutative
Definition JoinTree.h:222
V Value
the sums, of the type of the values
Definition JoinTree.h:221
the treaps [see TreapJoin]
Definition JoinTree.h:166
the weight-balanced trees
Definition JoinTree.h:164
the item of handle , key and value erased, told before the removal outside a batch
Definition Modification.h:568
the item of handle , key and value inserted
Definition Modification.h:536
a list of types, the events of a structure [see Modification.h]
Definition Modification.h:482