ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
PartialRebuildingTree.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------- File PartialRebuildingTree.h ----------------------*/
3/*--------------------------------------------------------------------------*/
79/*--------------------------------------------------------------------------*/
80/*----------------------------- DEFINITIONS --------------------------------*/
81/*--------------------------------------------------------------------------*/
82
83#ifndef __ADS_PartialRebuildingTree
84 #define __ADS_PartialRebuildingTree
85/* self-identification: #endif constant */
86
87/*--------------------------------------------------------------------------*/
88/*------------------------------ INCLUDES ----------------------------------*/
89/*--------------------------------------------------------------------------*/
90
91#include "ADS/Contract.h"
92#include "ADS/Iterators.h"
97
98#include <algorithm>
99#include <concepts>
100#include <cstddef>
101#include <cstdint>
102#include <optional>
103#include <type_traits>
104#include <utility>
105#include <vector>
106
107/*--------------------------------------------------------------------------*/
108/*--------------------------- NAMESPACE ------------------------------------*/
109/*--------------------------------------------------------------------------*/
110
111namespace ADS
112{
113
120
121/*--------------------------------------------------------------------------*/
122/*---------------------- CLASS PartialRebuildingTree -----------------------*/
123/*--------------------------------------------------------------------------*/
124
127template< std::totally_ordered K , class V , RebuildPolicy P >
129{
130
131/*--------------------------------------------------------------------------*/
132 public:
133 using Key = K;
134 using Value = V;
135
137 explicit PartialRebuildingTree( std::uint64_t seed = 1 ) : f_g( seed ) { }
138
141
144
147
150
152 [[nodiscard]] std::optional< V > lookup( const K & k ) const;
153
177 void insert( const K & k , const V & v );
178
184 void remove( const K & k );
185
187 [[nodiscard]] bool contains( const K & k ) const;
188
190 [[nodiscard]] std::size_t size( void ) const {
191 return( f_root ? f_root->s : 0 );
192 }
193
195 [[nodiscard]] long height( void ) const;
196
199 [[nodiscard]] std::optional< std::pair< K , V > >
200 lowerBound( const K & k ) const;
201
205 [[nodiscard]] const K * findKey( const K & k ) const;
206
210 [[nodiscard]] const K * lowerBoundKey( const K & k ) const;
211
213 template< class F >
214 void forEach( F && f ) const;
215
217 [[nodiscard]] std::size_t rebuilds( void ) const { return( f_rebuilds ); }
218
220 [[nodiscard]] std::size_t rebuiltNodes( void ) const { return( f_work ); }
221
222/*--------------------------------------------------------------------------*/
223 private:
225 struct Timer
226 {
227 long t = 0;
228 };
229
231 struct NoTimer
232 { };
233
236 struct Node
237 : std::conditional_t< P == RebuildPolicy::COUNTDOWN , Timer , NoTimer >
238 {
239 K key;
240 V value;
241 Node * left = nullptr;
242 Node * right = nullptr;
243 std::size_t s = 1;
245 Node( const K & k , const V & v ) : key( k ) , value( v ) { }
246 };
247
248 public:
249/*------------------------- THE STANDARD INTERFACE -------------------------*/
250
252 struct Shape
253 {
255 static auto root( const auto & o ) { return( o.f_root ); }
257 static auto left( const auto * n ) { return( n->left ); }
259 static auto right( const auto * n ) { return( n->right ); }
261 static auto read( const auto * n ) {
262 return( PairRef< const K & , const V & >( n->key , n->value ) );
263 }
264 };
265
267 using value_type = std::pair< K , V >;
269 using key_type = K;
271 using mapped_type = V;
272 using size_type = std::size_t;
273 using difference_type = std::ptrdiff_t;
279
281 [[nodiscard]] const_iterator begin( void ) const {
282 return( const_iterator( this , true ) );
283 }
284
285 [[nodiscard]] const_iterator end( void ) const {
286 return( const_iterator( this , false ) );
287 }
288
289 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
291 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
292
293 private:
294 static std::size_t sz( const Node * u ) { return( u ? u->s : 0 ); }
295
298 void rebalance( void );
299
301 void rebuild( std::size_t i );
302
304 [[nodiscard]] bool valid( void ) const;
305
306
307 Node * f_root = nullptr;
308 std::vector< Node * > f_path;
309 std::vector< Node * > f_buf;
310 SplitMix64 f_g;
311 std::size_t f_rebuilds = 0;
312 std::size_t f_work = 0;
313
314 }; // end( class PartialRebuildingTree )
315
317template< std::totally_ordered K , class V >
320
322template< std::totally_ordered K , class V >
325
327template< std::totally_ordered K , class V >
329
330/*--------------------------------------------------------------------------*/
331
332} // end( namespace ADS )
333
334/*--------------------------------------------------------------------------*/
335/*-------------------------- IMPLEMENTATION --------------------------------*/
336/*--------------------------------------------------------------------------*/
337
338#include "ADS/tree/impl/PartialRebuildingTree.tpp"
339
340/*--------------------------------------------------------------------------*/
341
342#endif /* PartialRebuildingTree.h included */
343
344/*--------------------------------------------------------------------------*/
345/*-------------------- End File PartialRebuildingTree.h --------------------*/
346/*--------------------------------------------------------------------------*/
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...
The engineered random numbers, beyond the book: generators of 64 bits that cost a few instructions an...
Hashing with random tables, beyond the book:
The scapegoat tree of Galperin and Rivest, the general balanced tree of Andersson (section 8....
a DICTIONARY on a binary search tree balanced by partial rebuilding by the rule P
Definition PartialRebuildingTree.h:129
void remove(const K &k)
removes , if it is there, in amortized time (for DynamiteTree, expected, the height),...
V Value
the type of the values
Definition PartialRebuildingTree.h:134
std::pair< K, V > value_type
the pairs ( key , value ) by value, which own their copies
Definition PartialRebuildingTree.h:267
PathIterator< PartialRebuildingTree, Shape > const_iterator
the constant iterator by increasing key, along the path from the root
Definition PartialRebuildingTree.h:277
PartialRebuildingTree(const PartialRebuildingTree &)=delete
no copy, since the nodes are linked by pointers
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...
PartialRebuildingTree(std::uint64_t seed=1)
the empty tree, its random choices (if any) drawn from the seed
Definition PartialRebuildingTree.h:137
PartialRebuildingTree(PartialRebuildingTree &&other) noexcept
the nodes of other, which is left empty
PartialRebuildingTree & operator=(const PartialRebuildingTree &)=delete
no copy, since the nodes are linked by pointers
std::ptrdiff_t difference_type
the type of the distances
Definition PartialRebuildingTree.h:273
K Key
the type of the keys
Definition PartialRebuildingTree.h:133
bool contains(const K &k) const
true if is there
V mapped_type
the type of the values
Definition PartialRebuildingTree.h:271
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...
PairRef< const K &, const V & > reference
references, by value
Definition PartialRebuildingTree.h:274
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 iterator
the constant iterator
Definition PartialRebuildingTree.h:278
std::size_t size(void) const
the number of keys
Definition PartialRebuildingTree.h:190
std::size_t rebuilds(void) const
the number of rebuilds so far
Definition PartialRebuildingTree.h:217
reference const_reference
the same
Definition PartialRebuildingTree.h:275
K key_type
the type of the keys
Definition PartialRebuildingTree.h:269
const_iterator end(void) const
the iterator past the largest key
Definition PartialRebuildingTree.h:285
void forEach(F &&f) const
f( k , v ) on the pairs by increasing keys
std::optional< V > lookup(const K &k) const
the value of , or nothing, in time
const_iterator begin(void) const
the iterator on the smallest key
Definition PartialRebuildingTree.h:281
~PartialRebuildingTree()
frees the nodes
std::size_t size_type
the type of the sizes
Definition PartialRebuildingTree.h:272
long height(void) const
the height, if empty, in
const_iterator cbegin(void) const
as begin()
Definition PartialRebuildingTree.h:289
void insert(const K &k, const V &v)
associates to , in amortized time (for DynamiteTree, expected after insertions only,...
std::size_t rebuiltNodes(void) const
the nodes of all the rebuilds so far, the work they took
Definition PartialRebuildingTree.h:220
const_iterator cend(void) const
as end()
Definition PartialRebuildingTree.h:291
a bidirectional iterator on the nodes of a binary tree of the owner O in symmetric order,...
Definition Iterators.h:867
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
PartialRebuildingTree< K, V, RebuildPolicy::WEIGHT > WeightRebuildTree
the tree of exercise 8.8 of Morin, balanced by weight by rebuilds
Definition PartialRebuildingTree.h:318
RebuildPolicy
the rules that select the subtree to rebuild
Definition PartialRebuildingTree.h:115
PartialRebuildingTree< K, V, RebuildPolicy::COUNTDOWN > CountdownTree
the CountdownTree of exercise 8.9 of Morin
Definition PartialRebuildingTree.h:323
PartialRebuildingTree< K, V, RebuildPolicy::DYNAMITE > DynamiteTree
the DynamiteTree of exercise 8.10 of Morin
Definition PartialRebuildingTree.h:328
@ DYNAMITE
an explosion with probability
Definition PartialRebuildingTree.h:118
@ COUNTDOWN
a timer set to that runs out
Definition PartialRebuildingTree.h:117
@ WEIGHT
Definition PartialRebuildingTree.h:116
Definition ADT.h:96
the shape of the tree, for PathIterator
Definition PartialRebuildingTree.h:253
static auto left(const auto *n)
the left child of the node n
Definition PartialRebuildingTree.h:257
static auto root(const auto &o)
the root of the tree o
Definition PartialRebuildingTree.h:255
static auto read(const auto *n)
the pair of the node n
Definition PartialRebuildingTree.h:261
static auto right(const auto *n)
the right child of the node n
Definition PartialRebuildingTree.h:259