ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
BPlusTree.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File BPlusTree.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
70/*--------------------------------------------------------------------------*/
71/*----------------------------- DEFINITIONS --------------------------------*/
72/*--------------------------------------------------------------------------*/
73
74#ifndef __ADS_BPlusTree
75 #define __ADS_BPlusTree
76/* self-identification: #endif constant */
77
78/*--------------------------------------------------------------------------*/
79/*------------------------------ INCLUDES ----------------------------------*/
80/*--------------------------------------------------------------------------*/
81
82#include "ADS/Contract.h"
83#include "ADS/Iterators.h"
84
85#include <algorithm>
86#include <concepts>
87#include <cstddef>
88#include <iterator>
89#include <optional>
90#include <stdexcept>
91#include <string>
92#include <utility>
93#include <vector>
94
95/*--------------------------------------------------------------------------*/
96/*--------------------------- NAMESPACE ------------------------------------*/
97/*--------------------------------------------------------------------------*/
98
99namespace ADS
100{
101
102/*--------------------------------------------------------------------------*/
103/*-------------------------- CLASS BPlusTree -------------------------------*/
104/*--------------------------------------------------------------------------*/
105
108template< std::totally_ordered K , class V >
110{
111
112/*--------------------------------------------------------------------------*/
113 public:
114 using Key = K;
115 using Value = V;
116
119 explicit BPlusTree( int m );
120
122 BPlusTree( const BPlusTree & ) = delete;
123
125 BPlusTree & operator=( const BPlusTree & ) = delete;
126
129
130/*--------------------------------------------------------------------------*/
131
153 [[nodiscard]] std::optional< V > lookup( const K & x ) const;
154
192 void insert( const K & x , const V & v );
193
220 void remove( const K & x );
221
223 [[nodiscard]] bool contains( const K & x ) const;
224
227 [[nodiscard]] const K * lowerBound( const K & x ) const;
228
232 [[nodiscard]] const K * findKey( const K & k ) const;
233
237 [[nodiscard]] const K * lowerBoundKey( const K & k ) const;
238
241 [[nodiscard]] std::vector< std::pair< K , V > > findRange( const K & x ,
242 const K & y ) const;
243
245 [[nodiscard]] std::size_t size( void ) const;
246
248 [[nodiscard]] int order( void ) const;
249
271 [[nodiscard]] std::vector< std::pair< K , V > > range( const K & A ,
272 const K & B ) const;
273
277 [[nodiscard]] std::vector< std::vector< std::vector< K > > >
278 levels( void ) const;
279
282 void check( void ) const;
283
284/*--------------------------------------------------------------------------*/
285 private:
287 struct Node
288 {
290 explicit Node( bool l ) : is_leaf( l ) { }
291 bool is_leaf;
292 std::vector< K > k;
293 std::vector< Node * > p;
294 std::vector< V > v;
295 Node * parent = nullptr;
296 Node * prev = nullptr;
297 Node * next = nullptr;
298 };
299
300 public:
301/*------------------------- THE STANDARD INTERFACE -------------------------*/
302
303 // a constant view of the pairs ( key , value ) by increasing key,
304 // bidirectional along the chain of the leaves
305
308 {
309 public:
312 using reference =
314 using value_type = std::pair< K , V >;
315 using difference_type = std::ptrdiff_t;
317 using iterator_concept = std::bidirectional_iterator_tag;
319 using iterator_category = std::input_iterator_tag;
320
322 const_iterator( void ) = default;
323
326 const_iterator( const BPlusTree * t , const Node * f , std::size_t i )
327 : f_t( t ) , f_f( f ) , f_i( i ) { }
328
330 reference operator*( void ) const {
331 return( reference( f_f->k[ f_i ] , f_f->v[ f_i ] ) );
332 }
333
336 if( ++f_i < f_f->k.size() )
337 return( *this );
338 f_f = f_f->next;
339 f_i = 0;
340 while( f_f && f_f->k.empty() )
341 f_f = f_f->next;
342 return( *this );
343 }
344
347 if( f_f && ( f_i > 0 ) ) {
348 --f_i;
349 return( *this );
350 }
351 f_f = f_f ? f_f->prev : f_t->lastLeaf(); // from the end, the last
352 while( f_f->k.empty() )
353 f_f = f_f->prev;
354 f_i = f_f->k.size() - 1;
355 return( *this );
356 }
357
360 auto t = *this;
361 ++*this;
362 return( t );
363 }
364
367 auto t = *this;
368 --*this;
369 return( t );
370 }
371
373 bool operator==( const const_iterator & o ) const {
374 return( ( f_f == o.f_f ) && ( f_i == o.f_i ) );
375 }
376
377 private:
378 const BPlusTree * f_t = nullptr;
379 const Node * f_f = nullptr;
380 std::size_t f_i = 0;
381 };
382
384 using value_type = std::pair< K , V >;
386 using key_type = K;
388 using mapped_type = V;
389 using size_type = std::size_t;
390 using difference_type = std::ptrdiff_t;
395
397 [[nodiscard]] const_iterator begin( void ) const {
398 const Node * f = firstLeaf();
399 while( f && f->k.empty() )
400 f = f->next;
401 return( const_iterator( this , f , 0 ) );
402 }
403
404 [[nodiscard]] const_iterator end( void ) const {
405 return( const_iterator( this , nullptr , 0 ) );
406 }
407
409 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
410
412 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
413
414 private:
416 [[nodiscard]] const Node * firstLeaf( void ) const {
417 const Node * x = f_root;
418 while( x && ! x->is_leaf )
419 x = x->p.front();
420 return( x );
421 }
423 [[nodiscard]] const Node * lastLeaf( void ) const {
424 const Node * x = f_root;
425 while( x && ! x->is_leaf )
426 x = x->p.back();
427 return( x );
428 }
429
431 Node * leaf( const K & x ) const;
432
436 void up( Node * f , Node * g , const K & s );
437
442 [[nodiscard]] bool shaped( void ) const;
443
446 [[nodiscard]] bool shaped( const Node * u , const K * lo , const K * hi ,
447 int d ,
448 int & depth , const Node *& last ) const;
449
451 static void destroy( Node * u );
452
456 Node * fix( Node * u );
457
458 int f_m;
459 Node * f_root;
460 std::size_t f_size = 0;
461
462 }; // end( class BPlusTree )
463
464/*--------------------------------------------------------------------------*/
465
466} // end( namespace ADS )
467
468/*--------------------------------------------------------------------------*/
469/*-------------------------- IMPLEMENTATION --------------------------------*/
470/*--------------------------------------------------------------------------*/
471
472#include "ADS/tree/impl/BPlusTree.tpp"
473
474/*--------------------------------------------------------------------------*/
475
476#endif /* BPlusTree.h included */
477
478/*--------------------------------------------------------------------------*/
479/*-------------------------- End File BPlusTree.h --------------------------*/
480/*--------------------------------------------------------------------------*/
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 iterator on the pairs by increasing keys, a leaf and a place in it
Definition BPlusTree.h:308
std::pair< K, V > value_type
the pairs, by value
Definition BPlusTree.h:314
std::bidirectional_iterator_tag iterator_concept
the category of the iterator for the concepts of C++20, bidirectional
Definition BPlusTree.h:317
std::input_iterator_tag iterator_category
input for the older algorithms, since *it is a value
Definition BPlusTree.h:319
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 BPlusTree.h:312
reference operator*(void) const
the pair of the place
Definition BPlusTree.h:330
bool operator==(const const_iterator &o) const
equal on the same place of the same leaf
Definition BPlusTree.h:373
const_iterator & operator--(void)
to the previous pair, from the end to the last pair
Definition BPlusTree.h:346
const_iterator operator++(int)
to the next pair, returning the iterator before the step
Definition BPlusTree.h:359
std::ptrdiff_t difference_type
the type of the distances
Definition BPlusTree.h:315
const_iterator operator--(int)
to the previous pair, returning the iterator before the step
Definition BPlusTree.h:366
const_iterator(const BPlusTree *t, const Node *f, std::size_t i)
on the place of the leaf of the tree , the end if is nil
Definition BPlusTree.h:326
const_iterator & operator++(void)
to the next pair, along the list of the leaves
Definition BPlusTree.h:335
const_iterator(void)=default
an iterator on no tree
std::ptrdiff_t difference_type
the type of the distances
Definition BPlusTree.h:390
reference const_reference
the same
Definition BPlusTree.h:393
const_iterator cend(void) const
as end()
Definition BPlusTree.h:412
std::pair< K, V > value_type
the pairs ( key , value ) by value, which own their copies
Definition BPlusTree.h:384
std::vector< std::pair< K, V > > findRange(const K &x, const K &y) const
the pairs with keys in , the findRange( x , y ) of exercise 14.8 of Morin's book: range() with the fi...
std::vector< std::pair< K, V > > range(const K &A, const K &B) const
the pairs with keys in , read along the list of the leaves, for pairs
std::optional< V > lookup(const K &x) const
the value of the key , or nothing
~BPlusTree()
frees the nodes
void insert(const K &x, const V &v)
associates the value to the key
BPlusTree & operator=(const BPlusTree &)=delete
no copy, since the nodes are linked by pointers
const K * lowerBound(const K &x) const
the smallest key , nil if none (the find( x ) of exercise 14.8 of Morin's book), in nodes
std::size_t size(void) const
the number of keys
BPlusTree(int m)
an empty B+-tree of order , the root a leaf
const_iterator begin(void) const
the iterator on the smallest key
Definition BPlusTree.h:397
const_iterator cbegin(void) const
as begin()
Definition BPlusTree.h:409
V mapped_type
the type of the values
Definition BPlusTree.h:388
const K * findKey(const K &k) const
the address of the key , nil if absent, in nodes: the find( x ) of a USet of Morin [see DictionarySe...
K key_type
the type of the keys
Definition BPlusTree.h:386
K Key
the type of the keys
Definition BPlusTree.h:114
int order(void) const
the order
const_iterator iterator
the constant iterator
Definition BPlusTree.h:394
const K * lowerBoundKey(const K &k) const
the address of the smallest key not smaller than , nil if none, in nodes: the find( x ) of an SSet o...
V Value
the type of the values
Definition BPlusTree.h:115
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 ...
typename const_iterator::reference reference
references, by value
Definition BPlusTree.h:392
const_iterator end(void) const
the iterator past the largest key
Definition BPlusTree.h:404
void remove(const K &x)
removes the key , if it is there, in time
bool contains(const K &x) const
true if the key is there
void check(void) const
checks the shape [see shaped()] whatever the level of the contracts; it throws contract_violation if ...
BPlusTree(const BPlusTree &)=delete
no copy, since the nodes are linked by pointers
std::size_t size_type
the type of the sizes
Definition BPlusTree.h:389
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
Definition ADT.h:96