ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ScapegoatTree.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*-------------------------- File ScapegoatTree.h --------------------------*/
3/*--------------------------------------------------------------------------*/
96/*--------------------------------------------------------------------------*/
97/*----------------------------- DEFINITIONS --------------------------------*/
98/*--------------------------------------------------------------------------*/
99
100#ifndef __ADS_ScapegoatTree
101 #define __ADS_ScapegoatTree
102/* self-identification: #endif constant */
103
104/*--------------------------------------------------------------------------*/
105/*------------------------------ INCLUDES ----------------------------------*/
106/*--------------------------------------------------------------------------*/
107
108#include "ADS/Contract.h"
109#include "ADS/Iterators.h"
110
111#include <algorithm>
112#include <cmath>
113#include <concepts>
114#include <cstddef>
115#include <optional>
116#include <utility>
117#include <vector>
118
119/*--------------------------------------------------------------------------*/
120/*--------------------------- NAMESPACE ------------------------------------*/
121/*--------------------------------------------------------------------------*/
122
123namespace ADS
124{
125
126namespace detail
127{
128
134struct Rebuild
135{
136
140 template< class N >
141 static void flatten( N * u , std::vector< N * > & B ) {
142 while( u )
143 if( ! u->left ) {
144 B.push_back( u );
145 u = u->right;
146 }
147 else {
148 N * p = u->left;
149 while( p->right && ( p->right != u ) )
150 p = p->right;
151 if( ! p->right ) {
152 p->right = u;
153 u = u->left;
154 }
155 else {
156 p->right = nullptr;
157 B.push_back( u );
158 u = u->right;
159 }
160 }
161 }
162
178 template< class N , class Set >
179 static N * fromArray( N * const * B , std::size_t i , std::size_t j ,
180 Set & set ) {
181 if( i >= j )
182 return( nullptr );
183 const std::size_t m = i + ( j - i - 1 ) / 2;
184 N * u = B[ m ];
185 u->left = fromArray( B , i , m , set );
186 u->right = fromArray( B , m + 1 , j , set );
187 set( u , j - i );
188 return( u );
189 }
190
215 template< class N >
216 static N * toList( N * u , N * r ) {
217 while( u ) { // the left spine by a loop,
218 r = toList( u->right , r ); // the right subtrees by
219 u->right = r; // recursion
220 r = u;
221 N * l = u->left;
222 u->left = nullptr;
223 u = l;
224 }
225 return( r );
226 }
227
230 template< class N , class Set >
231 static N * fromList( std::size_t s , N *& h , Set & set ) {
232 if( s == 0 )
233 return( nullptr );
234 N * l = fromList( ( s - 1 ) / 2 , h , set );
235 N * u = h;
236 h = h->right;
237 u->left = l;
238 u->right = fromList( s - 1 - ( s - 1 ) / 2 , h , set );
239 set( u , s );
240 return( u );
241 }
242
245 template< class N >
246 static std::size_t count( N * u , std::vector< N * > & S ) {
247 std::size_t c = 0;
248 S.clear();
249 if( u )
250 S.push_back( u );
251 while( ! S.empty() ) {
252 N * x = S.back();
253 S.pop_back();
254 ++c;
255 if( x->left )
256 S.push_back( x->left );
257 if( x->right )
258 S.push_back( x->right );
259 }
260 return( c );
261 }
262 };
263
264} // end( namespace detail )
265
266/*--------------------------------------------------------------------------*/
267/*-------------------------- CLASS ScapegoatTree ---------------------------*/
268/*--------------------------------------------------------------------------*/
269
272template< std::totally_ordered K , class V , bool StoredSizes = false >
274{
275
276/*--------------------------------------------------------------------------*/
277 public:
278 using Key = K;
279 using Value = V;
280
282 enum class Sizes {
285 };
286
288 enum class Scapegoats {
291 };
292
294 enum class Rebuilds {
297 };
298
301 explicit ScapegoatTree( double b = 1.5 , Sizes sizes = Sizes::REUSE ,
303 Scapegoats scapegoats = Scapegoats::HEIGHT );
304
306 ScapegoatTree( const ScapegoatTree & ) = delete;
307
309 ScapegoatTree & operator=( const ScapegoatTree & ) = delete;
310
312 ScapegoatTree( ScapegoatTree && other ) noexcept;
313
316
318 [[nodiscard]] std::optional< V > lookup( const K & k ) const;
319
339 void insert( const K & k , const V & v );
340
358 void remove( const K & k );
359
361 [[nodiscard]] bool contains( const K & k ) const;
362
364 [[nodiscard]] std::size_t size( void ) const { return( f_n ); }
365
367 [[nodiscard]] long height( void ) const;
368
371 [[nodiscard]] std::optional< std::pair< K , V > >
372 lowerBound( const K & k ) const;
373
377 [[nodiscard]] const K * findKey( const K & k ) const;
378
382 [[nodiscard]] const K * lowerBoundKey( const K & k ) const;
383
385 template< class F >
386 void forEach( F && f ) const;
387
389 [[nodiscard]] std::size_t rebuilds( void ) const { return( f_rebuilds ); }
390
392 [[nodiscard]] std::size_t rebuiltNodes( void ) const { return( f_work ); }
393
394/*--------------------------------------------------------------------------*/
395 private:
397 struct Size
398 {
399 std::size_t s = 1;
400 };
401
403 struct NoSize
404 { };
405
407 struct Node : std::conditional_t< StoredSizes , Size , NoSize >
408 {
409 K key;
410 V value;
411 Node * left = nullptr;
412 Node * right = nullptr;
414 Node( const K & k , const V & v ) : key( k ) , value( v ) { }
415 };
416
417 public:
418/*------------------------- THE STANDARD INTERFACE -------------------------*/
419
421 struct Shape
422 {
424 static auto root( const auto & o ) { return( o.f_root ); }
426 static auto left( const auto * n ) { return( n->left ); }
428 static auto right( const auto * n ) { return( n->right ); }
430 static auto read( const auto * n ) {
431 return( PairRef< const K & , const V & >( n->key , n->value ) );
432 }
433 };
434
436 using value_type = std::pair< K , V >;
438 using key_type = K;
440 using mapped_type = V;
441 using size_type = std::size_t;
442 using difference_type = std::ptrdiff_t;
448
450 [[nodiscard]] const_iterator begin( void ) const {
451 return( const_iterator( this , true ) );
452 }
453
454 [[nodiscard]] const_iterator end( void ) const {
455 return( const_iterator( this , false ) );
456 }
457
458 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
460 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
461
462 private:
464 std::size_t sizeOf( Node * u );
465
468 void rebuild( Node * u , std::size_t s , Node * p );
469
471 bool tooDeep( std::size_t d );
472
474 [[nodiscard]] bool valid( void ) const;
475
476
477 Node * f_root = nullptr;
478 std::size_t f_n = 0;
479 std::size_t f_q = 0;
480 double f_b;
481 Sizes f_sizes;
482 Rebuilds f_rebuild;
483 Scapegoats f_goat;
484 std::vector< double > f_pow;
485 std::vector< Node * > f_path;
486 std::vector< Node * > f_buf;
487 std::size_t f_rebuilds = 0;
488 std::size_t f_work = 0;
489
490 }; // end( class ScapegoatTree )
491
492/*--------------------------------------------------------------------------*/
493
494} // end( namespace ADS )
495
496/*--------------------------------------------------------------------------*/
497/*-------------------------- IMPLEMENTATION --------------------------------*/
498/*--------------------------------------------------------------------------*/
499
501#include "ADS/tree/impl/ScapegoatTree.tpp"
502
503/*--------------------------------------------------------------------------*/
504
505#endif /* ScapegoatTree.h included */
506
507/*--------------------------------------------------------------------------*/
508/*------------------------ End File ScapegoatTree.h ------------------------*/
509/*--------------------------------------------------------------------------*/
The classic problems on binary trees and on binary search trees, written once for any representation ...
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...
a bidirectional iterator on the nodes of a binary tree of the owner O in symmetric order,...
Definition Iterators.h:867
reference const_reference
the same
Definition ScapegoatTree.h:444
K key_type
the type of the keys
Definition ScapegoatTree.h:438
const_iterator cend(void) const
as end()
Definition ScapegoatTree.h:460
std::ptrdiff_t difference_type
the type of the distances
Definition ScapegoatTree.h:442
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...
std::size_t size(void) const
the number of keys
Definition ScapegoatTree.h:364
PairRef< const K &, const V & > reference
references, by value
Definition ScapegoatTree.h:443
std::optional< V > lookup(const K &k) const
the value of , or nothing, in time
std::size_t rebuilds(void) const
the number of rebuilds so far
Definition ScapegoatTree.h:389
Scapegoats
which ancestor is the scapegoat
Definition ScapegoatTree.h:288
@ WEIGHT
the first with a child too large, as Morin does
Definition ScapegoatTree.h:290
@ HEIGHT
the first too high for its size (Galperin, Rivest)
Definition ScapegoatTree.h:289
const_iterator cbegin(void) const
as begin()
Definition ScapegoatTree.h:458
~ScapegoatTree()
frees the nodes
ScapegoatTree(const ScapegoatTree &)=delete
no copy, since the nodes are linked by pointers
void forEach(F &&f) const
f( k , v ) on the pairs by increasing keys
PathIterator< ScapegoatTree, Shape > const_iterator
the constant iterator by increasing key, along the path from the root
Definition ScapegoatTree.h:446
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)
std::size_t rebuiltNodes(void) const
the nodes of all the rebuilds so far, the work they took
Definition ScapegoatTree.h:392
K Key
the type of the keys
Definition ScapegoatTree.h:278
V mapped_type
the type of the values
Definition ScapegoatTree.h:440
void insert(const K &k, const V &v)
associates to , in amortized time
long height(void) const
the height, if empty, in
V Value
the type of the values
Definition ScapegoatTree.h:279
ScapegoatTree(ScapegoatTree &&other) noexcept
the nodes of other, which is left empty
const_iterator iterator
the constant iterator
Definition ScapegoatTree.h:447
std::pair< K, V > value_type
the pairs ( key , value ) by value, which own their copies
Definition ScapegoatTree.h:436
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...
ScapegoatTree(double b=1.5, Sizes sizes=Sizes::REUSE, Rebuilds rebuilds=Rebuilds::ARRAY, Scapegoats scapegoats=Scapegoats::HEIGHT)
the empty tree of height at most
Rebuilds
how a subtree is rebuilt
Definition ScapegoatTree.h:294
@ LIST
by a list of its nodes, no buffer (exercise 8.7)
Definition ScapegoatTree.h:296
@ ARRAY
by a buffer of its nodes, as Morin does
Definition ScapegoatTree.h:295
std::size_t size_type
the type of the sizes
Definition ScapegoatTree.h:441
ScapegoatTree & operator=(const ScapegoatTree &)=delete
no copy, since the nodes are linked by pointers
const_iterator begin(void) const
the iterator on the smallest key
Definition ScapegoatTree.h:450
bool contains(const K &k) const
true if is there
void remove(const K &k)
removes , if it is there, in amortized time: the removal of a binary search tree,...
const_iterator end(void) const
the iterator past the largest key
Definition ScapegoatTree.h:454
Sizes
how the sizes are found when they are not stored
Definition ScapegoatTree.h:282
@ REUSE
the child on the path known (exercise 8.5)
Definition ScapegoatTree.h:284
@ RECOMPUTE
each one counted, as Morin does
Definition ScapegoatTree.h:283
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
the shape of the tree, for PathIterator
Definition ScapegoatTree.h:422
static auto right(const auto *n)
the right child of the node n
Definition ScapegoatTree.h:428
static auto left(const auto *n)
the left child of the node n
Definition ScapegoatTree.h:426
static auto root(const auto &o)
the root of the tree o
Definition ScapegoatTree.h:424
static auto read(const auto *n)
the pair of the node n
Definition ScapegoatTree.h:430