100#ifndef __ADS_ScapegoatTree
101 #define __ADS_ScapegoatTree
141 static void flatten( N * u , std::vector< N * > & B ) {
149 while( p->right && ( p->right != u ) )
178 template<
class N ,
class Set >
179 static N * fromArray( N *
const * B , std::size_t i , std::size_t j ,
183 const std::size_t m = i + ( j - i - 1 ) / 2;
185 u->left = fromArray( B , i , m , set );
186 u->right = fromArray( B , m + 1 , j , set );
216 static N * toList( N * u , N * r ) {
218 r = toList( u->right , r );
230 template<
class N ,
class Set >
231 static N * fromList( std::size_t s , N *& h , Set & set ) {
234 N * l = fromList( ( s - 1 ) / 2 , h , set );
238 u->right = fromList( s - 1 - ( s - 1 ) / 2 , h , set );
246 static std::size_t count( N * u , std::vector< N * > & S ) {
251 while( ! S.empty() ) {
256 S.push_back( x->left );
258 S.push_back( x->right );
272template< std::totally_ordered K ,
class V ,
bool StoredSizes = false >
318 [[nodiscard]] std::optional< V >
lookup(
const K & k )
const;
339 void insert(
const K & k ,
const V & v );
364 [[nodiscard]] std::size_t
size(
void )
const {
return( f_n ); }
371 [[nodiscard]] std::optional< std::pair< K , V > >
377 [[nodiscard]]
const K *
findKey(
const K & k )
const;
389 [[nodiscard]] std::size_t
rebuilds(
void )
const {
return( f_rebuilds ); }
392 [[nodiscard]] std::size_t
rebuiltNodes(
void )
const {
return( f_work ); }
407 struct Node : std::conditional_t< StoredSizes , Size , NoSize >
411 Node * left =
nullptr;
412 Node * right =
nullptr;
414 Node(
const K & k ,
const V & v ) : key( k ) , value( v ) { }
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 ) {
464 std::size_t sizeOf( Node * u );
468 void rebuild( Node * u , std::size_t s , Node * p );
471 bool tooDeep( std::size_t d );
474 [[nodiscard]]
bool valid(
void )
const;
477 Node * f_root =
nullptr;
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;
501#include "ADS/tree/impl/ScapegoatTree.tpp"
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
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