ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
SplayTree.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File SplayTree.h ----------------------------*/
3/*--------------------------------------------------------------------------*/
32/*--------------------------------------------------------------------------*/
33/*----------------------------- DEFINITIONS --------------------------------*/
34/*--------------------------------------------------------------------------*/
35
36#ifndef __ADS_SplayTree
37 #define __ADS_SplayTree
38/* self-identification: #endif constant */
39
40/*--------------------------------------------------------------------------*/
41/*------------------------------ INCLUDES ----------------------------------*/
42/*--------------------------------------------------------------------------*/
43
44#include "ADS/Contract.h"
45#include "ADS/Iterators.h"
46
47#include <concepts>
48#include <cstddef>
49#include <memory>
50#include <optional>
51#include <utility>
52#include <vector>
53
54/*--------------------------------------------------------------------------*/
55/*--------------------------- NAMESPACE ------------------------------------*/
56/*--------------------------------------------------------------------------*/
57
58namespace ADS
59{
60
61/*--------------------------------------------------------------------------*/
62/*--------------------------- CLASS SplayTree ------------------------------*/
63/*--------------------------------------------------------------------------*/
64
66template< std::totally_ordered K , class V >
68{
69
70/*--------------------------------------------------------------------------*/
71 public:
72 using Key = K;
73 using Value = V;
74
76 SplayTree( void ) = default;
78 SplayTree( const SplayTree & ) = delete;
80 SplayTree & operator=( const SplayTree & ) = delete;
81
83 SplayTree( SplayTree && other ) noexcept;
86
120 [[nodiscard]] std::optional< V > lookup( const K & k );
121
144 void insert( const K & k , const V & v );
145
165 void remove( const K & k );
166
169 [[nodiscard]] bool contains( const K & k );
170
174 [[nodiscard]] std::optional< std::pair< K , V > > lowerBound( const K & k );
175
179 [[nodiscard]] const K * findKey( const K & k );
180
185 [[nodiscard]] const K * lowerBoundKey( const K & k );
186
188 [[nodiscard]] std::size_t size( void ) const;
189
191 [[nodiscard]] std::optional< K > root( void ) const;
192
194 template< class F >
195 void forEach( F && f ) const;
196
197/*--------------------------------------------------------------------------*/
198 private:
200 struct Node
201 {
202 K key;
203 V value;
204 Node *left = nullptr ,
205 *right = nullptr;
206 };
207
208 public:
209/*------------------------- THE STANDARD INTERFACE -------------------------*/
210
211 // a constant view of the pairs ( key , value ) by increasing key,
212 // bidirectional, along the path from the root; a lookup splays the tree
213 // and invalidates it
214
216 struct Shape
217 {
219 static auto root( const auto & o ) { return( o.f_root ); }
221 static auto left( const auto * n ) { return( n->left ); }
223 static auto right( const auto * n ) { return( n->right ); }
225 static auto read( const auto * n ) {
226 return( PairRef< const K & , const V & >( n->key , n->value ) );
227 }
228 };
229
231 using value_type = std::pair< K , V >;
233 using key_type = K;
235 using mapped_type = V;
236 using size_type = std::size_t;
237 using difference_type = std::ptrdiff_t;
243
245 [[nodiscard]] const_iterator begin( void ) const {
246 return( const_iterator( this , true ) );
247 }
248
249 [[nodiscard]] const_iterator end( void ) const {
250 return( const_iterator( this , false ) );
251 }
252
253 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
255 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
256
257 private:
259 static Node * splay( Node * t , const K & k );
260
261 static void destroy( Node * t );
262
263 template< class F >
264 static void visit( const Node * t , F & f );
265
266 static bool ordered( const Node * t , const K * lo , const K * hi );
267
290 Node * lowerBoundNode( const K & k );
291
292 Node * f_root = nullptr;
293 std::size_t f_size = 0;
294
295 }; // end( class SplayTree )
296
297/*--------------------------------------------------------------------------*/
298
299} // end( namespace ADS )
300
301/*--------------------------------------------------------------------------*/
302/*-------------------------- IMPLEMENTATION --------------------------------*/
303/*--------------------------------------------------------------------------*/
304
305#include "ADS/tree/impl/SplayTree.tpp"
306
307/*--------------------------------------------------------------------------*/
308
309#endif /* SplayTree.h included */
310
311/*--------------------------------------------------------------------------*/
312/*-------------------------- End File SplayTree.h --------------------------*/
313/*--------------------------------------------------------------------------*/
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
~SplayTree()
frees the nodes
bool contains(const K &k)
true if is there; , or the last node on its path, goes to the root, in amortized time
void forEach(F &&f) const
f( k , v ) on the pairs by increasing keys
V mapped_type
the type of the values
Definition SplayTree.h:235
const_iterator iterator
the constant iterator
Definition SplayTree.h:242
SplayTree & operator=(const SplayTree &)=delete
no copy
V Value
the type of the values
Definition SplayTree.h:73
K Key
the type of the keys
Definition SplayTree.h:72
reference const_reference
the same
Definition SplayTree.h:239
const K * findKey(const K &k)
the address of the key , nil if absent, splayed as by lookup() in amortized time: the find( x ) of a...
const_iterator end(void) const
the iterator past the largest key
Definition SplayTree.h:249
SplayTree(const SplayTree &)=delete
no copy
void remove(const K &k)
removes , if it is there, in amortized time
std::ptrdiff_t difference_type
the type of the distances
Definition SplayTree.h:237
std::optional< V > lookup(const K &k)
the value of , or nothing; , or the last node on its path, goes to the root, in amortized time
PairRef< const K &, const V & > reference
references, by value
Definition SplayTree.h:238
const K * lowerBoundKey(const K &k)
the address of the smallest key not smaller than , nil if none, by lowerBoundNode() in amortized tim...
void insert(const K &k, const V &v)
associates to , which goes to the root, in amortized time
const_iterator begin(void) const
the iterator on the smallest key
Definition SplayTree.h:245
const_iterator cend(void) const
as end()
Definition SplayTree.h:255
const_iterator cbegin(void) const
as begin()
Definition SplayTree.h:253
std::size_t size_type
the type of the sizes
Definition SplayTree.h:236
PathIterator< SplayTree, Shape > const_iterator
the constant iterator by increasing key, along the path from the root
Definition SplayTree.h:241
SplayTree(void)=default
the empty tree
SplayTree(SplayTree &&other) noexcept
the nodes of other, which is left empty
std::pair< K, V > value_type
the pairs ( key , value ) by value, which own their copies
Definition SplayTree.h:231
std::optional< std::pair< K, V > > lowerBound(const K &k)
the smallest key not smaller than and its value, or nothing (the find( x ) of an SSet of Morin),...
K key_type
the type of the keys
Definition SplayTree.h:233
std::optional< K > root(void) const
the key at the root, if any
std::size_t size(void) const
the number of keys
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 SplayTree.h:217
static auto root(const auto &o)
the root of the tree o
Definition SplayTree.h:219
static auto left(const auto *n)
the left child of the node n
Definition SplayTree.h:221
static auto read(const auto *n)
the pair of the node n
Definition SplayTree.h:225
static auto right(const auto *n)
the right child of the node n
Definition SplayTree.h:223