127#ifndef __ADS_BTreeMap
128 #define __ADS_BTreeMap
155#include <type_traits>
173template<
class K ,
class V ,
class Less = std::less< K > ,
174 std::
size_t B = 512 ,
176 class A = HugePageAllocator< std::pair< const K , V > > >
177 requires std::default_initializable< K > && std::default_initializable< V >
182 static constexpr bool PLAIN =
183 detail::alwaysEqualAlloc< A > && std::is_default_constructible_v< A >;
191 static constexpr std::size_t
L =
192 std::clamp< std::size_t >( B / (
sizeof( K ) +
sizeof( V ) ) , 4 , 255 );
195 static constexpr std::size_t
I =
196 std::clamp< std::size_t >( B / (
sizeof( K ) +
sizeof(
void * ) ) , 4 ,
237 return( detail::copyAssign( *
this , o ) );
245 return( detail::moveAssign( *
this , std::move( o ) ) );
272 [[nodiscard]] std::optional< V >
lookup(
const K & k )
const;
276 [[nodiscard]]
const V *
find(
const K & k )
const;
280 [[nodiscard]] V *
find(
const K & k );
335 bool insert(
const K & k ,
const V & v );
378 [[nodiscard]] std::size_t
size(
void )
const;
381 [[nodiscard]]
bool empty(
void )
const;
413 template< std::input_iterator It >
438 void forRange(
const K & lo ,
const K & hi , F && f )
const;
489 [[nodiscard]]
const K *
findKey(
const K & k )
const;
520 [[nodiscard]] std::size_t
rank(
const K & k )
const
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<> > );
552 [[nodiscard]]
static constexpr K pad(
void ) {
553 if constexpr( std::numeric_limits< K >::has_infinity )
554 return( std::numeric_limits< K >::infinity() );
556 return( std::numeric_limits< K >::max() );
560 struct alignas( 64 ) Leaf
564 Leaf * prev =
nullptr;
565 Leaf * next =
nullptr;
574 struct alignas( 64 ) Inner
578 Inner * prev =
nullptr;
579 Inner * next =
nullptr;
583 std::conditional_t< R , std::array< std::size_t , I + 1 > ,
None > cnt;
587 [[nodiscard]]
static std::size_t total(
const void * x ,
int h );
593 static void recount( Inner * u ,
int h , std::uint32_t from = 0 ,
594 std::uint32_t to =
I + 1 );
603 static constexpr int DEPTH = 64;
608 [[nodiscard]] N * make(
void );
611 void free( Leaf * x )
noexcept;
614 void free( Inner * x )
noexcept;
618 static void clean( K * a , std::size_t from , std::size_t to );
621 static void fetch(
const void * x , std::size_t bytes );
625 template< std::
size_t C >
626 [[nodiscard]] std::size_t place(
const K * a , std::uint32_t n ,
632 template<
bool Values >
633 [[nodiscard]]
const Leaf * descend(
const K & x ,
634 const Inner ** up =
nullptr ,
635 std::uint32_t * at =
nullptr )
const;
638 [[nodiscard]]
const Inner * edge(
bool right )
const;
642 [[nodiscard]]
const Leaf * descend(
const K & x , Step *
path )
const;
645 [[nodiscard]]
bool equal(
const K & a ,
const K & b )
const;
649 void split( Leaf * x , std::uint32_t p ,
const K & k ,
const V & v ,
656 void climb( K s ,
void * r , Step *
path ,
int d ,
int edge );
660 void shrink( Leaf * x , Step *
path ,
int d );
664 void shrink( Inner * x , Step *
path ,
int d );
667 void destroy(
void * x ,
int h )
noexcept;
670 [[nodiscard]]
bool valid(
void )
const;
674 [[nodiscard]]
bool valid(
const void * x ,
int h ,
const K * lo ,
677 std::vector< const Inner * > & left ,
678 std::size_t & count )
const;
680 void * f_root =
nullptr;
681 Leaf * f_first =
nullptr;
682 Leaf * f_last =
nullptr;
683 std::size_t f_size = 0;
687 using Leaves = ObjectPool< Leaf , detail::RebindAlloc< A , Leaf > >;
689 using Inners = ObjectPool< Inner , detail::RebindAlloc< A , Inner > >;
694 [[nodiscard]]
static P emptyPool(
const P & p ) {
695 if constexpr( PLAIN )
698 return( P( std::size_t( 1 ) << 24 , p.get_allocator() ) );
702 void exchange(
BTreeMap & o )
noexcept;
729 return(
reference( f_l->k[ f_i ] , f_l->v[ f_i ] ) );
753 if( ++f_i == f_n ) [[unlikely]]
763 f_p = f_t->edge(
true );
764 f_c = f_p ? f_p->n : 0;
767 else if( f_i == 0 ) {
775 f_l =
static_cast< const Leaf *
>( f_p->c[ f_c ] );
801 return( ( f_l == o.f_l ) && ( f_i == o.f_i ) );
808 static constexpr std::uint32_t AHEAD = 4;
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 ) ,
816 if( f_l && ( f_i == f_n ) )
826 if( f_c == f_p->n ) {
829 if( f_p && f_p->next )
830 fetch( f_p->next ,
sizeof( Inner ) );
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;
842 if( q && ( a <= q->n ) )
843 fetch( q->c[ a ] ,
sizeof( Leaf ) );
850 f_n = f_l ? f_l->n : 0;
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;
872#include "ADS/tree/impl/BTreeMap.tpp"
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