ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
BTree.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------------ File BTree.h ------------------------------*/
3/*--------------------------------------------------------------------------*/
56/*--------------------------------------------------------------------------*/
57/*----------------------------- DEFINITIONS --------------------------------*/
58/*--------------------------------------------------------------------------*/
59
60#ifndef __ADS_BTree
61 #define __ADS_BTree
62/* self-identification: #endif constant */
63
64/*--------------------------------------------------------------------------*/
65/*------------------------------ INCLUDES ----------------------------------*/
66/*--------------------------------------------------------------------------*/
67
68#include "ADS/Contract.h"
69#include "ADS/Iterators.h"
70#include "ADS/Modification.h"
71#include "ADS/Portable.h"
72
73#include <algorithm>
74#include <concepts>
75#include <cstddef>
76#include <iterator>
77#include <memory>
78#include <optional>
79#include <utility>
80#include <vector>
81
82/*--------------------------------------------------------------------------*/
83/*--------------------------- NAMESPACE ------------------------------------*/
84/*--------------------------------------------------------------------------*/
85
86namespace ADS
87{
88
89/*--------------------------------------------------------------------------*/
90/*----------------------------- CLASS BTree --------------------------------*/
91/*--------------------------------------------------------------------------*/
92
95template< std::totally_ordered K , class V , class Policy = NoObservers >
96class BTree
97{
98
99/*--------------------------------------------------------------------------*/
100 public:
101 using Key = K;
102 using Value = V;
103
110
116 using Subject = typename Policy::template bind< modifications , BTree >;
117
121 template< class... O >
122 explicit BTree( int t = 3 , O &... o );
123
143 [[nodiscard]] std::optional< V > lookup( const K & k ) const;
144
185 void insert( const K & k , const V & v );
186
243 void remove( const K & k );
244
246 void clear( void );
247
249 [[nodiscard]] bool contains( const K & k ) const;
250
253 [[nodiscard]] std::optional< std::pair< K , V > >
254 lowerBound( const K & k ) const;
255
259 [[nodiscard]] const K * findKey( const K & k ) const;
260
264 [[nodiscard]] const K * lowerBoundKey( const K & k ) const;
265
267 [[nodiscard]] std::size_t size( void ) const;
268
270 [[nodiscard]] bool empty( void ) const;
271
273 [[nodiscard]] int height( void ) const;
274
278 [[nodiscard]] std::vector< std::vector< std::vector< K > > >
279 levels( void ) const;
280
298 template< class F >
299 void forEach( F && f ) const;
300
301/*--------------------------------------------------------------------------*/
302 private:
304 struct Node
305 {
306 std::vector< K > k;
307 std::vector< V > v;
308 std::vector< std::unique_ptr< Node > > c;
310 [[nodiscard]] bool leaf( void ) const { return( c.empty() ); }
311 };
312
313 public:
314/*--------------------------- THE OBSERVERS --------------------------------*/
315
317 [[nodiscard]] Subject & subject( void ) { return( f_sub ); }
319 [[nodiscard]] const Subject & subject( void ) const { return( f_sub ); }
320
323 template< class O >
324 int attach( O & o )
325 requires( Subject::dynamic )
326 {
327 return( f_sub.attach( o ) );
328 }
329
332 void detach( int id )
333 requires( Subject::dynamic )
334 {
335 f_sub.detach( id );
336 }
337
338/*------------------------- THE STANDARD INTERFACE -------------------------*/
339
340 // a constant view of the pairs ( key , value ) by increasing key,
341 // forward, with the path of ( node , index ) from the root: in a node
342 // above the last, the index is the child the path went into, and the key
343 // of that index comes once the child is done
344
348 {
349 public:
352 using reference =
354 using value_type = std::pair< K , V >;
355 using difference_type = std::ptrdiff_t;
357 using iterator_concept = std::forward_iterator_tag;
359 using iterator_category = std::input_iterator_tag;
360
362 const_iterator( void ) = default;
363
366 explicit const_iterator( const Node * x ) {
367 if( x && ! x->k.empty() )
368 leftmost( x );
369 }
370
372 reference operator*( void ) const {
373 const auto & [ x , i ] = f_path.back();
374 return( reference( x->k[ i ] , x->v[ i ] ) );
375 }
376
395 auto & [ x , i ] = f_path.back();
396 if( ! x->c.empty() ) { // into the child after the key
397 ++i;
398 leftmost( x->c[ i ].get() );
399 return( *this );
400 }
401 if( ++i < x->k.size() )
402 return( *this );
403 f_path.pop_back(); // up to the first ancestor
404 while( ! f_path.empty() && // with a key left
405 ( f_path.back().second >= f_path.back().first->k.size() ) )
406 f_path.pop_back();
407 return( *this );
408 }
409
412 auto t = *this;
413 ++*this;
414 return( t );
415 }
416
418 bool operator==( const const_iterator & o ) const {
419 return( f_path.empty() ? o.f_path.empty()
420 : ( ! o.f_path.empty() &&
421 ( f_path.back() == o.f_path.back() ) ) );
422 }
423
424 private:
426 void leftmost( const Node * x ) {
427 for( ; ! x->c.empty() ; x = x->c[ 0 ].get() )
428 f_path.emplace_back( x , 0 );
429 f_path.emplace_back( x , 0 );
430 }
431
433 std::vector< std::pair< const Node * , std::size_t > > f_path;
434 };
435
437 using value_type = std::pair< K , V >;
439 using key_type = K;
441 using mapped_type = V;
442 using size_type = std::size_t;
443 using difference_type = std::ptrdiff_t;
448
450 [[nodiscard]] const_iterator begin( void ) const {
451 return( const_iterator( f_root.get() ) );
452 }
453
455 [[nodiscard]] const_iterator end( void ) const {
456 return( const_iterator() );
457 }
458
460 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
461
463 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
464
465 private:
467 void insertPlain( const K & key , const V & val );
468
470 [[nodiscard]] std::pair< Node * , std::size_t > slot( const K & key ) const;
471
473 void insertTold( const K & key , const V & val );
474
477 bool removeTold( const K & key );
478
480 ADS_COLD_PATH void insertCold( const K & key , const V & val ) {
481 insertTold( key , val );
482 }
483 ADS_COLD_PATH bool removeCold( const K & key ) {
484 return( removeTold( key ) );
485 }
486
488 static std::size_t place( const Node * x , const K & key );
489
491 void split( Node * x , std::size_t i );
492
495 static void merge( Node * x , std::size_t j );
496
499 std::size_t fill( Node * x , std::size_t i );
500
502 bool remove( Node * x , const K & key );
503
506 template< class F >
507 static void visit( const Node * x , F & f );
508
510 bool valid( const Node * x , const K * lo , const K * hi , int d ,
511 int & leafDepth , bool root ) const;
512
515 [[nodiscard]] std::pair< const Node * , std::size_t >
516 lowerBoundPlace( const K & key ) const;
517
518 int f_t;
519 std::unique_ptr< Node > f_root;
520 std::size_t f_size = 0;
522
523 }; // end( class BTree )
524
525/*--------------------------------------------------------------------------*/
526
527} // end( namespace ADS )
528
529/*--------------------------------------------------------------------------*/
530/*-------------------------- IMPLEMENTATION --------------------------------*/
531/*--------------------------------------------------------------------------*/
532
533#include "ADS/tree/impl/BTree.tpp"
534
535/*--------------------------------------------------------------------------*/
536
537#endif /* BTree.h included */
538
539/*--------------------------------------------------------------------------*/
540/*---------------------------- End File BTree.h ----------------------------*/
541/*--------------------------------------------------------------------------*/
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 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: ...
the iterator on the pairs by increasing keys, with the path from the root
Definition BTree.h:348
const_iterator(const Node *x)
on the first key below , or at the end if is nil or empty
Definition BTree.h:366
std::pair< K, V > value_type
the pairs, by value
Definition BTree.h:354
PairRef< const K &, typename std::vector< V >::const_reference, K, V > reference
a pair of constant references, the key and its value (that of the vector of the values,...
Definition BTree.h:352
const_iterator operator++(int)
to the next key, returning the iterator before the step
Definition BTree.h:411
const_iterator & operator++(void)
to the next key, amortized and at most
Definition BTree.h:394
std::forward_iterator_tag iterator_concept
the category of the iterator for the concepts of C++20, forward
Definition BTree.h:357
reference operator*(void) const
the pair of the key at the end of the path
Definition BTree.h:372
std::ptrdiff_t difference_type
the type of the distances
Definition BTree.h:355
bool operator==(const const_iterator &o) const
equal on the same place of the same node, or both at the end
Definition BTree.h:418
const_iterator(void)=default
the end
std::input_iterator_tag iterator_category
input for the older algorithms, since *it is a value
Definition BTree.h:359
typename Policy::template bind< modifications, BTree > Subject
the subject
Definition BTree.h:116
std::pair< K, V > value_type
the pairs ( key , value ) by value, which own their copies
Definition BTree.h:437
std::vector< std::vector< std::vector< K > > > levels(void) const
the keys of the nodes, level by level from the root, each level from left to right: the shape of the ...
reference const_reference
the same
Definition BTree.h:446
V Value
the type of the values
Definition BTree.h:102
K Key
the type of the keys
Definition BTree.h:101
const_iterator cbegin(void) const
as begin()
Definition BTree.h:460
void forEach(F &&f) const
f( k , v ) on the pairs by increasing keys
const_iterator iterator
the constant iterator
Definition BTree.h:447
K key_type
the type of the keys
Definition BTree.h:439
int attach(O &o)
attaches the observer and gives its number
Definition BTree.h:324
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),...
const_iterator begin(void) const
the iterator on the smallest key
Definition BTree.h:450
const Subject & subject(void) const
the subject
Definition BTree.h:319
std::size_t size(void) const
the number of keys
std::optional< V > lookup(const K &k) const
the value of the key , or nothing
std::ptrdiff_t difference_type
the type of the distances
Definition BTree.h:443
const_iterator end(void) const
the iterator past the largest key
Definition BTree.h:455
void remove(const K &k)
removes the key , if there, in one pass down from the root
bool empty(void) const
true if there is no key
int height(void) const
the height, 0 for a single leaf, -1 if empty
typename const_iterator::reference reference
references, by value
Definition BTree.h:445
void clear(void)
removes all the keys, in time
const_iterator cend(void) const
as end()
Definition BTree.h:463
bool contains(const K &k) const
whether the key is there, lookup() above
BTree(int t=3, O &... o)
the empty B-tree of minimum degree , the static observers o...
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...
mod::Inserted< K, K, V > Inserted
an insertion of a key, the key its handle
Definition BTree.h:105
void insert(const K &k, const V &v)
gives the key the value , in one pass down from the root
mod::Erased< K, K, V > Erased
an erasure of a key, told before the removal
Definition BTree.h:107
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 DictionarySe...
void detach(int id)
detaches the observer number
Definition BTree.h:332
Subject & subject(void)
the subject, for ADS::batch() and ADS::quiet()
Definition BTree.h:317
mod::Assigned< K, V > Assigned
a change of the value of a key
Definition BTree.h:109
mod::list< Inserted, Erased, Assigned, mod::Cleared, mod::Rebuilt > modifications
the events: a key inserted or erased with its value, its value changed, the tree emptied or rebuilt
Definition BTree.h:113
std::size_t size_type
the type of the sizes
Definition BTree.h:442
V mapped_type
the type of the values
Definition BTree.h:441
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
Definition ADT.h:96
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