ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
FingerTreap.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File FingerTreap.h ---------------------------*/
3/*--------------------------------------------------------------------------*/
57/*--------------------------------------------------------------------------*/
58/*----------------------------- DEFINITIONS --------------------------------*/
59/*--------------------------------------------------------------------------*/
60
61#ifndef __ADS_FingerTreap
62 #define __ADS_FingerTreap
63/* self-identification: #endif constant */
64
65/*--------------------------------------------------------------------------*/
66/*------------------------------ INCLUDES ----------------------------------*/
67/*--------------------------------------------------------------------------*/
68
69#include "ADS/Contract.h"
70#include "ADS/Iterators.h"
72
73#include <algorithm>
74#include <concepts>
75#include <cstddef>
76#include <cstdint>
77#include <optional>
78#include <utility>
79#include <vector>
80
81/*--------------------------------------------------------------------------*/
82/*--------------------------- NAMESPACE ------------------------------------*/
83/*--------------------------------------------------------------------------*/
84
85namespace ADS
86{
87
88/*--------------------------------------------------------------------------*/
89/*--------------------------- CLASS FingerTreap ----------------------------*/
90/*--------------------------------------------------------------------------*/
91
94template< std::totally_ordered K , class V >
96{
97
98 struct Node;
99
100/*--------------------------------------------------------------------------*/
101 public:
102 using Key = K;
103 using Value = V;
104
107 class Finger
108 {
109 public:
111 Finger( void ) = default;
112
113 private:
114 friend class FingerTreap;
115 explicit Finger( Node * u ) : f_u( u ) { }
116 Node * f_u = nullptr;
117 };
118
120 explicit FingerTreap( std::uint64_t seed = 1 ) : f_g( seed ) { }
121
123 FingerTreap( const FingerTreap & ) = delete;
124
126 FingerTreap & operator=( const FingerTreap & ) = delete;
127
129 FingerTreap( FingerTreap && other ) noexcept
130 : f_root( other.f_root ) , f_size( other.f_size ) , f_g( other.f_g ) {
131 other.f_root = other.f_finger = nullptr;
132 other.f_size = 0;
133 }
134
137
140 [[nodiscard]] std::optional< V > lookup( const K & k ) const;
141
145 [[nodiscard]] std::optional< V > fingerLookup( const K & k );
146
168 [[nodiscard]] std::optional< V > lookupFrom( const K & k , Finger f ) const;
169
172 [[nodiscard]] Finger finger( const K & k ) const;
173
178 void insert( const K & k , const V & v );
179
185 void remove( const K & k );
186
188 [[nodiscard]] bool contains( const K & k ) const;
189
191 [[nodiscard]] std::size_t size( void ) const { return( f_size ); }
192
194 [[nodiscard]] int height( void ) const;
195
198 [[nodiscard]] std::optional< std::pair< K , V > >
199 lowerBound( const K & k ) const;
200
204 [[nodiscard]] const K * findKey( const K & k ) const;
205
209 [[nodiscard]] const K * lowerBoundKey( const K & k ) const;
210
213 [[nodiscard]] std::pair< K , V > select( std::size_t i ) const;
214
216 [[nodiscard]] std::size_t rank( const K & k ) const;
217
219 template< class F >
220 void forEach( F && f ) const;
221
222/*--------------------------------------------------------------------------*/
223 private:
226 struct Node
227 {
228 K key;
229 V value;
230 std::uint64_t priority;
231 Node * left = nullptr;
232 Node * right = nullptr;
233 Node * parent = nullptr;
234 Node * lo = nullptr;
235 Node * hi = nullptr;
236 std::size_t size = 1;
237 };
238
239 public:
240/*------------------------- THE STANDARD INTERFACE -------------------------*/
241
243 struct Shape
244 {
246 static auto root( const auto & o ) { return( o.f_root ); }
248 static auto left( const auto * n ) { return( n->left ); }
250 static auto right( const auto * n ) { return( n->right ); }
252 static auto read( const auto * n ) {
253 return( PairRef< const K & , const V & >( n->key , n->value ) );
254 }
255 };
256
258 using value_type = std::pair< K , V >;
260 using key_type = K;
262 using mapped_type = V;
263 using size_type = std::size_t;
264 using difference_type = std::ptrdiff_t;
270
272 [[nodiscard]] const_iterator begin( void ) const {
273 return( const_iterator( this , true ) );
274 }
275
276 [[nodiscard]] const_iterator end( void ) const {
277 return( const_iterator( this , false ) );
278 }
279
280 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
282 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
283
284 private:
286 Node * search( const K & k , Node * u ) const;
287
289 static void update( Node * u ) {
290 u->size =
291 1 + ( u->left ? u->left->size : 0 ) + ( u->right ? u->right->size : 0 );
292 u->lo = u->left ? u->left->lo : u;
293 u->hi = u->right ? u->right->hi : u;
294 }
295
297 void rotateRight( Node * u );
298
300 void rotateLeft( Node * u );
301
303 [[nodiscard]] bool valid( void ) const;
304
305
306 Node * f_root = nullptr;
307 Node * f_finger = nullptr;
308 std::size_t f_size = 0;
309 SplitMix64 f_g;
310
311 }; // end( class FingerTreap )
312
313/*--------------------------------------------------------------------------*/
314
315} // end( namespace ADS )
316
317/*--------------------------------------------------------------------------*/
318/*-------------------------- IMPLEMENTATION --------------------------------*/
319/*--------------------------------------------------------------------------*/
320
322#include "ADS/tree/impl/FingerTreap.tpp"
323
324/*--------------------------------------------------------------------------*/
325
326#endif /* FingerTreap.h included */
327
328/*--------------------------------------------------------------------------*/
329/*------------------------- End File FingerTreap.h -------------------------*/
330/*--------------------------------------------------------------------------*/
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...
Hashing with random tables, beyond the book:
a node of the treap, from which a search may start; valid until the node is removed
Definition FingerTreap.h:108
Finger(void)=default
no node: a search from it starts at the root
friend class FingerTreap
Definition FingerTreap.h:114
void remove(const K &k)
removes , if it is there, in expected time: the node rotated down to a leaf, the child of larger pri...
bool contains(const K &k) const
true if is there
void insert(const K &k, const V &v)
associates to , in expected time: a leaf rotated up while its priority beats its parent's,...
const_iterator end(void) const
the iterator past the largest key
Definition FingerTreap.h:276
std::ptrdiff_t difference_type
the type of the distances
Definition FingerTreap.h:264
PathIterator< FingerTreap, Shape > const_iterator
the constant iterator by increasing key, along the path from the root
Definition FingerTreap.h:268
K Key
the type of the keys
Definition FingerTreap.h:102
PairRef< const K &, const V & > reference
references, by value
Definition FingerTreap.h:265
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::optional< V > lookupFrom(const K &k, Finger f) const
the value of , or nothing, by a search from , in expected time, the keys between and that of (the...
std::size_t size_type
the type of the sizes
Definition FingerTreap.h:263
const_iterator cbegin(void) const
as begin()
Definition FingerTreap.h:280
FingerTreap(const FingerTreap &)=delete
no copy, since the nodes are linked by pointers
std::optional< V > lookup(const K &k) const
the value of , or nothing, from the root, in expected time
std::size_t rank(const K &k) const
the number of keys smaller than , by the sizes
std::pair< K, V > value_type
the pairs ( key , value ) by value, which own their copies
Definition FingerTreap.h:258
FingerTreap(std::uint64_t seed=1)
the empty treap, its priorities drawn from the seed
Definition FingerTreap.h:120
FingerTreap & operator=(const FingerTreap &)=delete
no copy, since the nodes are linked by pointers
void forEach(F &&f) const
f( k , v ) on the pairs by increasing keys
reference const_reference
the same
Definition FingerTreap.h:266
std::pair< K, V > select(std::size_t i) const
the pair of rank , by the sizes, as Treap::select()
K key_type
the type of the keys
Definition FingerTreap.h:260
const_iterator begin(void) const
the iterator on the smallest key
Definition FingerTreap.h:272
const_iterator cend(void) const
as end()
Definition FingerTreap.h:282
int height(void) const
the height, if empty, in
~FingerTreap()
frees the nodes
std::optional< V > fingerLookup(const K &k)
the value of , or nothing, by a search from the node of the last fingerLookup(), which becomes the no...
V Value
the type of the values
Definition FingerTreap.h:103
std::size_t size(void) const
the number of keys
Definition FingerTreap.h:191
const_iterator iterator
the constant iterator
Definition FingerTreap.h:269
V mapped_type
the type of the values
Definition FingerTreap.h:262
const K * lowerBoundKey(const K &k) const
the address of the smallest key not smaller than , nil if none, in expected time: the find( x ) of a...
const K * findKey(const K &k) const
the address of the key , nil if absent, in expected time: the find( x ) of a USet of Morin [see Dict...
FingerTreap(FingerTreap &&other) noexcept
the nodes of other, which is left empty
Definition FingerTreap.h:129
Finger finger(const K &k) const
the node of , or the last node of its search from the root, as a finger
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
long search(const S &s, const typename S::Item &v)
the place of counted from 0, or
Definition ADT.h:96
the shape of the tree, for PathIterator
Definition FingerTreap.h:244
static auto read(const auto *n)
the pair of the node n
Definition FingerTreap.h:252
static auto right(const auto *n)
the right child of the node n
Definition FingerTreap.h:250
static auto root(const auto &o)
the root of the tree o
Definition FingerTreap.h:246
static auto left(const auto *n)
the left child of the node n
Definition FingerTreap.h:248