ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
BlockBTree.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File BlockBTree.h ---------------------------*/
3/*--------------------------------------------------------------------------*/
109/*--------------------------------------------------------------------------*/
110/*----------------------------- DEFINITIONS --------------------------------*/
111/*--------------------------------------------------------------------------*/
112
113#ifndef __ADS_BlockBTree
114 #define __ADS_BlockBTree
115/* self-identification: #endif constant */
116
117/*--------------------------------------------------------------------------*/
118/*------------------------------ INCLUDES ----------------------------------*/
119/*--------------------------------------------------------------------------*/
120
121#include "ADS/Contract.h"
123
124#include <algorithm>
125#include <concepts>
126#include <cstddef>
127#include <cstdint>
128#include <limits>
129#include <optional>
130#include <tuple>
131#include <utility>
132#include <vector>
133
134/*--------------------------------------------------------------------------*/
135/*--------------------------- NAMESPACE ------------------------------------*/
136/*--------------------------------------------------------------------------*/
137
138namespace ADS
139{
140
141/*--------------------------------------------------------------------------*/
142/*--------------------------- CLASS BlockBTree -----------------------------*/
143/*--------------------------------------------------------------------------*/
144
152
154template< std::totally_ordered K >
156{
157
158/*--------------------------------------------------------------------------*/
159 public:
160 using Key = K;
161 using Item = K;
162
167 TwoLevelMemory * mem = nullptr );
168
169/*--------------------------------------------------------------------------*/
170
191 [[nodiscard]] std::optional< K > lowerBound( const K & x ) const;
192
194 [[nodiscard]] bool contains( const K & x ) const;
195
218 bool insert( const K & x );
219
250 bool remove( const K & x );
251
253 [[nodiscard]] std::size_t size( void ) const { return( f_n ); }
254
256 [[nodiscard]] bool empty( void ) const { return( f_n == 0 ); }
257
259 [[nodiscard]] int order( void ) const { return( f_B ); }
260
262 [[nodiscard]] int height( void ) const;
263
265 [[nodiscard]] std::size_t internalNodes( void ) const;
266
268 [[nodiscard]] std::uint64_t reads( void ) const { return( f_bs.reads() ); }
269
271 [[nodiscard]] std::uint64_t writes( void ) const { return( f_bs.writes() ); }
272
274 void resetCounters( void ) { f_bs.resetCounters(); }
275
277 [[nodiscard]] std::size_t nodes( void ) const { return( f_bs.blocks() ); }
278
280 [[nodiscard]] std::size_t restructures( void ) const {
281 return( f_splits + f_merges + f_borrows );
282 }
283
285 [[nodiscard]] std::size_t splits( void ) const { return( f_splits ); }
286
288 [[nodiscard]] std::size_t merges( void ) const { return( f_merges ); }
289
291 [[nodiscard]] std::size_t borrows( void ) const { return( f_borrows ); }
292
294 [[nodiscard]] std::size_t peakNodes( void ) const { return( f_peak ); }
295
297 [[nodiscard]] std::size_t peakPath( void ) const { return( f_peakPath ); }
298
301 [[nodiscard]] std::vector< std::vector< std::vector< K > > >
302 levels( void ) const;
303
307 void assign( const std::vector< std::vector< std::vector< K > > > & L );
308
310 template< class F >
311 void forEach( F && f ) const;
312
313/*--------------------------------------------------------------------------*/
314 private:
316 struct Node
317 {
318 std::vector< K > keys;
319 std::vector< std::size_t > kids;
320 bool operator==( const Node & ) const = default;
321 };
322
323 using Store = BlockStore< Node >;
324
326 struct Held
327 {
328 Node node;
329 const BlockBTree * t;
331 explicit Held( const BlockBTree * tree , Node n = Node() );
333 ~Held();
334 Held( const Held & ) = delete;
335 Held & operator=( const Held & ) = delete;
336 };
337
339 [[nodiscard]] std::size_t maxKeys( void ) const;
340
342 [[nodiscard]] std::size_t minKeys( void ) const {
343 return( std::size_t( f_B - 1 ) );
344 }
345
347 [[nodiscard]] static std::size_t place( const Node & u , const K & x );
348
372 template< class D >
373 void deal( Node & p , std::size_t f , std::size_t k , std::size_t c ,
374 const Node & known , D && decide );
375
411 void fixOverflow( Node & p , std::size_t i , const Node & u );
412
415 void fixUnderflow( Node & p , std::size_t i , const Node & u , bool root );
416
419 [[nodiscard]] std::size_t lowKeys( bool twoLeaves ) const;
420
423 [[nodiscard]] bool valid( std::size_t u , const K * lo , const K * hi ,
424 int d ,
425 int & h , std::size_t & n ) const;
426
429 [[nodiscard]] bool consistent( void ) const;
430
432 void path( std::size_t len ) const;
433
434 Store f_bs;
435 std::size_t f_root;
436 int f_B;
437 BTreePolicy f_policy;
438 std::size_t f_n = 0;
439 std::size_t f_splits = 0;
440 std::size_t f_merges = 0;
441 std::size_t f_borrows = 0;
442 mutable std::size_t f_held = 0;
443 mutable std::size_t f_peak = 0;
444 mutable std::size_t f_peakPath = 0;
445
446 }; // end( class BlockBTree )
447
448/*--------------------------------------------------------------------------*/
449
450} // end( namespace ADS )
451
452/*--------------------------------------------------------------------------*/
453/*-------------------------- IMPLEMENTATION --------------------------------*/
454/*--------------------------------------------------------------------------*/
455
456#include "ADS/external/impl/BlockBTree.tpp"
457
458/*--------------------------------------------------------------------------*/
459
460#endif /* BlockBTree.h included */
461
462/*--------------------------------------------------------------------------*/
463/*------------------------- End File BlockBTree.h --------------------------*/
464/*--------------------------------------------------------------------------*/
The store of blocks of section 14.1 of Morin's book, beyond the book: a disk simulated in memory,...
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
a B-tree of keys whose nodes are the blocks of a BlockStore
Definition BlockBTree.h:156
bool remove(const K &x)
removes ; true if it was there, in I/Os
std::size_t borrows(void) const
the borrows so far
Definition BlockBTree.h:291
BlockBTree(int B, BTreePolicy p=BTreePolicy::STANDARD, TwoLevelMemory *mem=nullptr)
the empty tree of the parameter and of the policy p, whose I/Os are also counted by mem,...
std::size_t internalNodes(void) const
the number of internal nodes
K Key
the type of the keys
Definition BlockBTree.h:160
bool contains(const K &x) const
true if is there, in I/Os
std::optional< K > lowerBound(const K &x) const
the smallest key , or nothing, in I/Os: a copy, since the blocks of the external memory are copied i...
bool insert(const K &x)
adds ; true if it was not there, in I/Os
std::size_t peakPath(void) const
the longest path kept by an operation, in pairs of indices
Definition BlockBTree.h:297
std::size_t peakNodes(void) const
the most nodes held in memory at once by an operation
Definition BlockBTree.h:294
void assign(const std::vector< std::vector< std::vector< K > > > &L)
the tree of the levels , as levels() gives them, in place of this one (the trees of the figures of th...
int height(void) const
the number of levels, 1 for a root that is a leaf
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,...
std::size_t splits(void) const
the splits so far
Definition BlockBTree.h:285
std::uint64_t writes(void) const
the writes of blocks so far
Definition BlockBTree.h:271
std::size_t nodes(void) const
the number of blocks in use, the nodes
Definition BlockBTree.h:277
std::size_t restructures(void) const
the splits, the merges and the borrows so far
Definition BlockBTree.h:280
K Item
the items of the sorted set, the keys
Definition BlockBTree.h:161
std::size_t merges(void) const
the merges so far
Definition BlockBTree.h:288
int order(void) const
the parameter
Definition BlockBTree.h:259
void resetCounters(void)
the counters of the I/Os back to 0
Definition BlockBTree.h:274
void forEach(F &&f) const
f( x ) on the keys in increasing order, with no I/O
std::size_t size(void) const
the number of keys
Definition BlockBTree.h:253
std::uint64_t reads(void) const
the reads of blocks so far
Definition BlockBTree.h:268
bool empty(void) const
true if there are no keys
Definition BlockBTree.h:256
the parameters and of the model, the counters of the I/Os and the occupation of the internal memory
Definition TwoLevelMemory.h:94
BTreePolicy
how a BlockBTree keeps its nodes within their bounds
Definition BlockBTree.h:146
@ THREE
the book's bounds but in a tree of two leaves, by three
Definition BlockBTree.h:149
@ STANDARD
to keys, the book's rules
Definition BlockBTree.h:147
@ WIDE
to keys (exercise 14.6)
Definition BlockBTree.h:148
Definition ADT.h:96