ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
BTreeMap.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File BTreeMap.h -----------------------------*/
3/*--------------------------------------------------------------------------*/
123/*--------------------------------------------------------------------------*/
124/*----------------------------- DEFINITIONS --------------------------------*/
125/*--------------------------------------------------------------------------*/
126
127#ifndef __ADS_BTreeMap
128 #define __ADS_BTreeMap
129/* self-identification: #endif constant */
130
131/*--------------------------------------------------------------------------*/
132/*------------------------------ INCLUDES ----------------------------------*/
133/*--------------------------------------------------------------------------*/
134
135#include "ADS/Contract.h"
136#include "ADS/Iterators.h"
137#include "ADS/Portable.h"
141#include "ADS/memory/Prefetch.h"
142#include "ADS/search/CountLess.h"
143
144#include <algorithm>
145#include <array>
146#include <concepts>
147#include <cstddef>
148#include <cstdint>
149#include <functional>
150#include <iterator>
151#include <limits>
152#include <new>
153#include <numeric>
154#include <optional>
155#include <type_traits>
156#include <utility>
157#include <vector>
158
159/*--------------------------------------------------------------------------*/
160/*--------------------------- NAMESPACE ------------------------------------*/
161/*--------------------------------------------------------------------------*/
162
163namespace ADS
164{
165
166/*--------------------------------------------------------------------------*/
167/*---------------------------- CLASS BTreeMap ------------------------------*/
168/*--------------------------------------------------------------------------*/
169
173template< class K , class V , class Less = std::less< K > ,
174 std::size_t B = 512 ,
175 bool R = false ,
176 class A = HugePageAllocator< std::pair< const K , V > > >
177 requires std::default_initializable< K > && std::default_initializable< V >
179{
182 static constexpr bool PLAIN =
183 detail::alwaysEqualAlloc< A > && std::is_default_constructible_v< A >;
184
185/*--------------------------------------------------------------------------*/
186 public:
187 using Key = K;
188 using Value = V;
189
191 static constexpr std::size_t L =
192 std::clamp< std::size_t >( B / ( sizeof( K ) + sizeof( V ) ) , 4 , 255 );
193
195 static constexpr std::size_t I =
196 std::clamp< std::size_t >( B / ( sizeof( K ) + sizeof( void * ) ) , 4 ,
197 255 );
198
200 explicit BTreeMap( Less less = Less() );
201
204 BTreeMap( Less less , const A & alloc );
205
207 explicit BTreeMap( const A & alloc ) : BTreeMap( Less() , alloc ) { }
208
210 BTreeMap( const BTreeMap & o )
211 requires( PLAIN );
212
215 BTreeMap( const BTreeMap & o )
216 requires( ! PLAIN )
217 : BTreeMap( o , detail::copyAlloc( o.get_allocator() ) ) { }
218
220 BTreeMap( const BTreeMap & o , const A & alloc );
221
223 BTreeMap( BTreeMap && o ) noexcept;
224
227 BTreeMap( BTreeMap && o , const A & alloc );
228
231 requires( PLAIN );
232
235 requires( ! PLAIN )
236 {
237 return( detail::copyAssign( *this , o ) );
238 }
239
243 requires( ! PLAIN )
244 {
245 return( detail::moveAssign( *this , std::move( o ) ) );
246 }
247
250
251/*--------------------------------------------------------------------------*/
252
272 [[nodiscard]] std::optional< V > lookup( const K & k ) const;
273
276 [[nodiscard]] const V * find( const K & k ) const;
277
280 [[nodiscard]] V * find( const K & k );
281
335 bool insert( const K & k , const V & v );
336
372 bool remove( const K & k );
373
375 [[nodiscard]] bool contains( const K & k ) const;
376
378 [[nodiscard]] std::size_t size( void ) const;
379
381 [[nodiscard]] bool empty( void ) const;
382
384 [[nodiscard]] int height( void ) const;
385
387 void clear( void );
388
413 template< std::input_iterator It >
414 void build( It first , It last );
415
418 template< class F >
419 void forEach( F && f ) const;
420
437 template< class F >
438 void forRange( const K & lo , const K & hi , F && f ) const;
439
443 void swap( BTreeMap & o ) noexcept( detail::swapNoexcept< A > );
444
445/*------------------------- THE STANDARD INTERFACE -------------------------*/
446
447 // a constant view of the pairs ( key , value ) by increasing keys,
448 // bidirectional, a leaf and a place in it
449
450 class const_iterator;
451
453 using value_type = std::pair< K , V >;
455 using key_type = K;
457 using mapped_type = V;
458 using size_type = std::size_t;
459 using difference_type = std::ptrdiff_t;
464 using allocator_type = A;
466 [[nodiscard]] allocator_type get_allocator( void ) const {
467 return( allocator_type( f_leaves.get_allocator() ) );
468 }
469
471 [[nodiscard]] const_iterator begin( void ) const;
472
474 [[nodiscard]] const_iterator end( void ) const;
475
477 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
478
480 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
481
484 [[nodiscard]] const_iterator lowerBound( const K & k ) const;
485
489 [[nodiscard]] const K * findKey( const K & k ) const;
490
494 [[nodiscard]] const K * lowerBoundKey( const K & k ) const;
495
499 [[nodiscard]] const_iterator upperBound( const K & k ) const;
500
504
520 [[nodiscard]] std::size_t rank( const K & k ) const
521 requires R;
522
541 [[nodiscard]] const_iterator select( std::size_t i ) const
542 requires R;
543
544/*--------------------------------------------------------------------------*/
545 private:
547 static constexpr bool FAST =
548 std::is_arithmetic_v< K > && ( std::is_same_v< Less , std::less< K > > ||
549 std::is_same_v< Less , std::less<> > );
550
552 [[nodiscard]] static constexpr K pad( void ) {
553 if constexpr( std::numeric_limits< K >::has_infinity )
554 return( std::numeric_limits< K >::infinity() );
555 else
556 return( std::numeric_limits< K >::max() );
557 }
558
560 struct alignas( 64 ) Leaf
561 {
562 K k[ L ];
563 V v[ L ];
564 Leaf * prev = nullptr;
565 Leaf * next = nullptr;
566 std::uint32_t n = 0;
567 };
568
570 struct None
571 { };
572
574 struct alignas( 64 ) Inner
575 {
576 K k[ I ];
577 void * c[ I + 1 ];
578 Inner * prev = nullptr;
579 Inner * next = nullptr;
580 std::uint32_t n = 0;
583 std::conditional_t< R , std::array< std::size_t , I + 1 > , None > cnt;
584 };
585
587 [[nodiscard]] static std::size_t total( const void * x , int h );
588
593 static void recount( Inner * u , int h , std::uint32_t from = 0 ,
594 std::uint32_t to = I + 1 );
595
597 struct Step
598 {
599 Inner * u;
600 std::uint32_t i;
601 };
602
603 static constexpr int DEPTH = 64;
604
605
607 template< class N >
608 [[nodiscard]] N * make( void );
609
611 void free( Leaf * x ) noexcept;
612
614 void free( Inner * x ) noexcept;
615
618 static void clean( K * a , std::size_t from , std::size_t to );
619
621 static void fetch( const void * x , std::size_t bytes );
622
625 template< std::size_t C >
626 [[nodiscard]] std::size_t place( const K * a , std::uint32_t n ,
627 const K & x ) const;
628
632 template< bool Values >
633 [[nodiscard]] const Leaf * descend( const K & x ,
634 const Inner ** up = nullptr ,
635 std::uint32_t * at = nullptr ) const;
636
638 [[nodiscard]] const Inner * edge( bool right ) const;
639
642 [[nodiscard]] const Leaf * descend( const K & x , Step * path ) const;
643
645 [[nodiscard]] bool equal( const K & a , const K & b ) const;
646
649 void split( Leaf * x , std::uint32_t p , const K & k , const V & v ,
650 Step * path ,
651 int d );
652
656 void climb( K s , void * r , Step * path , int d , int edge );
657
660 void shrink( Leaf * x , Step * path , int d );
661
664 void shrink( Inner * x , Step * path , int d );
665
667 void destroy( void * x , int h ) noexcept;
668
670 [[nodiscard]] bool valid( void ) const;
671
674 [[nodiscard]] bool valid( const void * x , int h , const K * lo ,
675 const K * hi ,
676 const Leaf *& last ,
677 std::vector< const Inner * > & left ,
678 std::size_t & count ) const;
679
680 void * f_root = nullptr;
681 Leaf * f_first = nullptr;
682 Leaf * f_last = nullptr;
683 std::size_t f_size = 0;
684 int f_height = 0;
685 ADS_NO_UNIQUE_ADDRESS Less f_less;
687 using Leaves = ObjectPool< Leaf , detail::RebindAlloc< A , Leaf > >;
689 using Inners = ObjectPool< Inner , detail::RebindAlloc< A , Inner > >;
690
693 template< class P >
694 [[nodiscard]] static P emptyPool( const P & p ) {
695 if constexpr( PLAIN )
696 return( P() );
697 else
698 return( P( std::size_t( 1 ) << 24 , p.get_allocator() ) );
699 }
700
702 void exchange( BTreeMap & o ) noexcept;
703
704 Leaves f_leaves;
705 Inners f_inners;
706
707/*--------------------------------------------------------------------------*/
708 public:
712 {
713 public:
715 using value_type = std::pair< K , V >;
718 using difference_type = std::ptrdiff_t;
720 using iterator_concept = std::bidirectional_iterator_tag;
722 using iterator_category = std::input_iterator_tag;
723
725 const_iterator( void ) = default;
726
728 reference operator*( void ) const {
729 return( reference( f_l->k[ f_i ] , f_l->v[ f_i ] ) );
730 }
731
753 if( ++f_i == f_n ) [[unlikely]]
754 step();
755 return( *this );
756 }
757
761 if( ! f_l ) { // from the end
762 f_l = f_t->f_last;
763 f_p = f_t->edge( true );
764 f_c = f_p ? f_p->n : 0;
765 f_i = f_n = f_l->n;
766 }
767 else if( f_i == 0 ) {
768 if( f_p ) {
769 if( f_c == 0 ) {
770 f_p = f_p->prev;
771 f_c = f_p->n;
772 }
773 else
774 --f_c;
775 f_l = static_cast< const Leaf * >( f_p->c[ f_c ] );
776 }
777 else
778 f_l = f_l->prev;
779 f_i = f_n = f_l->n;
780 }
781 --f_i;
782 return( *this );
783 }
784
787 auto t = *this;
788 ++*this;
789 return( t );
790 }
791
794 auto t = *this;
795 --*this;
796 return( t );
797 }
798
800 bool operator==( const const_iterator & o ) const {
801 return( ( f_l == o.f_l ) && ( f_i == o.f_i ) );
802 }
803
804 private:
805 friend class BTreeMap;
806
808 static constexpr std::uint32_t AHEAD = 4;
809
812 const_iterator( const BTreeMap * t , const Leaf * l , std::uint32_t i ,
813 const Inner * p , std::uint32_t c )
814 : f_t( t ) , f_l( l ) , f_p( p ) , f_i( i ) , f_n( l ? l->n : 0 ) ,
815 f_c( c ) {
816 if( f_l && ( f_i == f_n ) ) // past a leaf: the next one
817 step();
818 }
819
823 void step( void ) {
824 f_i = 0;
825 if( f_p ) {
826 if( f_c == f_p->n ) {
827 f_p = f_p->next;
828 f_c = 0;
829 if( f_p && f_p->next )
830 fetch( f_p->next , sizeof( Inner ) );
831 }
832 else
833 ++f_c;
834 if( f_p ) {
835 f_l = static_cast< const Leaf * >( f_p->c[ f_c ] );
836 const Inner * q = f_p;
837 std::uint32_t a = f_c + AHEAD;
838 if( a > q->n ) {
839 a -= q->n + 1;
840 q = q->next;
841 }
842 if( q && ( a <= q->n ) )
843 fetch( q->c[ a ] , sizeof( Leaf ) );
844 }
845 else
846 f_l = nullptr;
847 }
848 else
849 f_l = f_l->next;
850 f_n = f_l ? f_l->n : 0;
851 }
852
853 const BTreeMap * f_t = nullptr;
854 const Leaf * f_l = nullptr;
855 const Inner * f_p = nullptr;
856 std::uint32_t f_i = 0;
857 std::uint32_t f_n = 0;
858 std::uint32_t f_c = 0;
859
860 }; // end( class const_iterator )
861
862 }; // end( class BTreeMap )
863
864/*--------------------------------------------------------------------------*/
865
866} // end( namespace ADS )
867
868/*--------------------------------------------------------------------------*/
869/*-------------------------- IMPLEMENTATION --------------------------------*/
870/*--------------------------------------------------------------------------*/
871
872#include "ADS/tree/impl/BTreeMap.tpp"
873
874/*--------------------------------------------------------------------------*/
875
876#endif /* BTreeMap.h included */
877
878/*--------------------------------------------------------------------------*/
879/*------------------------- End File BTreeMap.h ----------------------------*/
880/*--------------------------------------------------------------------------*/
The rules of the allocator-aware containers of the standard ([container.reqmts], [allocator....
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The count of the items of a short array smaller than a key, the search in a node of the B-trees in ma...
An allocator for the large blocks of a structure, beyond the book: a request of at least one huge pag...
The iterators that the containers of the library have in common, beyond the book: they give a contain...
A pool of objects of one type, beyond the book: make() builds an object in a slot of the pool and fre...
The primitives of the processor that the compilers spell each in their own way, in one form for all: ...
The prefetch of a line of the cache, beyond the book: ADS::prefetch( p ) asks the processor to bring ...
the iterator on the pairs by increasing keys: a leaf, a place in it and the parent of the leaf
Definition BTreeMap.h:712
friend class BTreeMap
Definition BTreeMap.h:805
const_iterator operator++(int)
to the next pair, returning the iterator before the step
Definition BTreeMap.h:786
reference operator*(void) const
the pair of the place
Definition BTreeMap.h:728
const_iterator(void)=default
an iterator on no tree
std::ptrdiff_t difference_type
the type of the distances
Definition BTreeMap.h:718
std::input_iterator_tag iterator_category
input for the older algorithms, since *it is a value
Definition BTreeMap.h:722
const_iterator & operator--(void)
to the previous pair, from the end to the last pair, through the parent at the start of a leaf
Definition BTreeMap.h:760
std::bidirectional_iterator_tag iterator_concept
the category of the iterator for the concepts of C++20, bidirectional
Definition BTreeMap.h:720
const_iterator & operator++(void)
to the next pair, to the next leaf through the parent at the end of a leaf
Definition BTreeMap.h:752
const_iterator operator--(int)
to the previous pair, returning the iterator before the step
Definition BTreeMap.h:793
bool operator==(const const_iterator &o) const
equal on the same place of the same leaf
Definition BTreeMap.h:800
PairRef< const K &, const V & > reference
a pair of references, by value
Definition BTreeMap.h:717
std::pair< K, V > value_type
the pairs ( key , value ) by value, which own their copies
Definition BTreeMap.h:715
BTreeMap & operator=(const BTreeMap &o)
the copy assignment, with the allocator of o if it propagates
Definition BTreeMap.h:234
std::optional< V > lookup(const K &k) const
the value of , if there
BTreeMap(Less less=Less())
the empty map, by the order less
void clear(void)
removes the pairs, the pools keeping their memory
std::size_t rank(const K &k) const
the pairs whose key is smaller than , time
~BTreeMap()
frees the nodes
const_iterator cend(void) const
as end()
Definition BTreeMap.h:480
V mapped_type
the type of the values
Definition BTreeMap.h:457
const_iterator iterator
the constant iterator
Definition BTreeMap.h:462
std::pair< K, V > value_type
the pairs ( key , value ) by value, which own their copies
Definition BTreeMap.h:453
const_iterator select(std::size_t i) const
the pair of rank (from 0), end() if
const_iterator upperBound(const K &k) const
the first pair whose key is larger than , the place after the keys not larger than in the leaf reach...
void forRange(const K &lo, const K &hi, F &&f) const
f( key , value ) on each pair of key in , by increasing keys, for pairs
BTreeMap(BTreeMap &&o, const A &alloc)
the pairs of o, which is left empty, on alloc: its nodes if the allocators are equal,...
int height(void) const
the levels of the tree, 0 if empty, 1 if a single leaf
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...
std::ptrdiff_t difference_type
the type of the distances
Definition BTreeMap.h:459
const V * find(const K &k) const
the value of , to change in place, or nullptr, by the descent of lookup()
bool remove(const K &k)
removes ; true if it was there
const_iterator erase(const_iterator it)
removes the pair of it, and gives the one after it: remove( k ) and then lowerBound( k ),...
const_iterator begin(void) const
the iterator on the smallest key
const_iterator end(void) const
the iterator past the largest key
A allocator_type
the type of the allocator
Definition BTreeMap.h:464
const_iterator cbegin(void) const
as begin()
Definition BTreeMap.h:477
V Value
the type of the values
Definition BTreeMap.h:188
void forEach(F &&f) const
f( key , value ) on each pair, by increasing keys, along the iterators
BTreeMap(const A &alloc)
the empty map, the blocks of its nodes from alloc
Definition BTreeMap.h:207
static constexpr std::size_t I
the keys of an internal node
Definition BTreeMap.h:195
K key_type
the type of the keys
Definition BTreeMap.h:455
void swap(BTreeMap &o) noexcept(detail::swapNoexcept< A >)
exchanges the nodes and the pools with o, each pool with its allocator
static constexpr std::size_t L
the pairs of a leaf
Definition BTreeMap.h:191
V * find(const K &k)
the value of , to change in place, or nullptr, by the descent of lookup()
std::size_t size(void) const
the number of pairs
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...
BTreeMap(Less less, const A &alloc)
the empty map, by the order less, the blocks of its nodes from alloc
bool insert(const K &k, const V &v)
gives the value ; true if was not there
bool contains(const K &k) const
whether is there, by the descent of lookup()
BTreeMap(const BTreeMap &o, const A &alloc)
a copy of o, node by node, on alloc
BTreeMap(BTreeMap &&o) noexcept
the nodes of o, which is left empty
const_iterator lowerBound(const K &k) const
the first pair whose key is not smaller than , the place of in the leaf reached by the descent of lo...
BTreeMap(const BTreeMap &o)
a copy of o, node by node, on the allocator that select_on_container_copy_construction() gives
Definition BTreeMap.h:215
BTreeMap & operator=(BTreeMap o) noexcept
the pairs of o, by copy and swap
bool empty(void) const
true if there is no pair
allocator_type get_allocator(void) const
the allocator of the blocks of the nodes
Definition BTreeMap.h:466
PairRef< const K &, const V & > reference
references, by value
Definition BTreeMap.h:460
BTreeMap & operator=(BTreeMap &&o)
the move assignment: the nodes of o if its allocator propagates or is equal, else a copy of its pairs
Definition BTreeMap.h:242
reference const_reference
the same
Definition BTreeMap.h:461
K Key
the type of the keys
Definition BTreeMap.h:187
std::size_t size_type
the type of the sizes
Definition BTreeMap.h:458
void build(It first, It last)
replaces the pairs with those of [ first , last ), pairs ( key , value ) by strictly increasing keys,...
BTreeMap(const BTreeMap &o)
a copy of o, node by node
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
#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
std::vector< int > path(int r, int s, const std::vector< int > &p)
the path from to in the tree of the parents , empty if there is none (the procedure printPath of th...
@ None
Definition Prefetch.h:64
Definition ADT.h:96