ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
HashedTreap.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File HashedTreap.h ---------------------------*/
3/*--------------------------------------------------------------------------*/
58/*--------------------------------------------------------------------------*/
59/*----------------------------- DEFINITIONS --------------------------------*/
60/*--------------------------------------------------------------------------*/
61
62#ifndef __ADS_HashedTreap
63 #define __ADS_HashedTreap
64/* self-identification: #endif constant */
65
66/*--------------------------------------------------------------------------*/
67/*------------------------------ INCLUDES ----------------------------------*/
68/*--------------------------------------------------------------------------*/
69
70#include "ADS/Contract.h"
71#include "ADS/Iterators.h"
73
74#include <algorithm>
75#include <concepts>
76#include <cstddef>
77#include <cstdint>
78#include <memory>
79#include <optional>
80#include <utility>
81
82/*--------------------------------------------------------------------------*/
83/*--------------------------- NAMESPACE ------------------------------------*/
84/*--------------------------------------------------------------------------*/
85
86namespace ADS
87{
88
89/*--------------------------------------------------------------------------*/
90/*--------------------------- CLASS HashedTreap ----------------------------*/
91/*--------------------------------------------------------------------------*/
92
95template< std::totally_ordered K , class V >
97{
98
99/*--------------------------------------------------------------------------*/
100 public:
101 using Key = K;
102 using Value = V;
103
105 explicit HashedTreap( std::uint64_t seed = 1 ) : f_h( seed ) { }
106
108 [[nodiscard]] std::optional< V > lookup( const K & k ) const;
109
136 void insert( const K & k , const V & v );
137
166 void remove( const K & k );
167
169 [[nodiscard]] bool contains( const K & k ) const;
170
172 [[nodiscard]] std::size_t size( void ) const { return( f_size ); }
173
175 [[nodiscard]] int height( void ) const;
176
179 [[nodiscard]] std::optional< std::pair< K , V > >
180 lowerBound( const K & k ) const;
181
185 [[nodiscard]] const K * findKey( const K & k ) const;
186
190 [[nodiscard]] const K * lowerBoundKey( const K & k ) const;
191
193 template< class F >
194 void forEach( F && f ) const;
195
196/*--------------------------------------------------------------------------*/
197 private:
199 struct Node
200 {
201 K key;
202 V value;
203 std::unique_ptr< Node > left ,
204 right;
205 };
206
207 public:
208/*------------------------- THE STANDARD INTERFACE -------------------------*/
209
211 struct Shape
212 {
214 static auto root( const auto & o ) { return( o.f_root.get() ); }
216 static auto left( const auto * n ) { return( n->left.get() ); }
218 static auto right( const auto * n ) { return( n->right.get() ); }
220 static auto read( const auto * n ) {
221 return( PairRef< const K & , const V & >( n->key , n->value ) );
222 }
223 };
224
226 using value_type = std::pair< K , V >;
228 using key_type = K;
230 using mapped_type = V;
231 using size_type = std::size_t;
232 using difference_type = std::ptrdiff_t;
238
240 [[nodiscard]] const_iterator begin( void ) const {
241 return( const_iterator( this , true ) );
242 }
243
244 [[nodiscard]] const_iterator end( void ) const {
245 return( const_iterator( this , false ) );
246 }
247
248 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
250 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
251
252 private:
253 using Ptr = std::unique_ptr< Node >;
254
256 std::uint64_t priority( const Node * u ) const {
257 return( f_h( std::uint64_t( reinterpret_cast< std::uintptr_t >( u ) ) ) );
258 }
259 static void rotateRight( Ptr & t );
260 static void rotateLeft( Ptr & t );
261 int insert( Ptr & t , const K & k , const V & v );
262 bool remove( Ptr & t , const K & k );
263 static int height( const Ptr & t );
264 bool valid( const Ptr & t , const K * lo , const K * hi ) const;
265
266
267 Ptr f_root;
268 std::size_t f_size = 0;
269 TabulationHash f_h;
270
271 }; // end( class HashedTreap )
272
273/*--------------------------------------------------------------------------*/
274
275} // end( namespace ADS )
276
277/*--------------------------------------------------------------------------*/
278/*-------------------------- IMPLEMENTATION --------------------------------*/
279/*--------------------------------------------------------------------------*/
280
282#include "ADS/tree/impl/HashedTreap.tpp"
283
284/*--------------------------------------------------------------------------*/
285
286#endif /* HashedTreap.h included */
287
288/*--------------------------------------------------------------------------*/
289/*------------------------- End File HashedTreap.h -------------------------*/
290/*--------------------------------------------------------------------------*/
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:
const_iterator iterator
the constant iterator
Definition HashedTreap.h:237
const_iterator cend(void) const
as end()
Definition HashedTreap.h:250
void forEach(F &&f) const
f( k , v ) on the pairs by increasing keys
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 > lookup(const K &k) const
the value of , or nothing, in expected time
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...
K key_type
the type of the keys
Definition HashedTreap.h:228
std::ptrdiff_t difference_type
the type of the distances
Definition HashedTreap.h:232
std::pair< K, V > value_type
the pairs ( key , value ) by value, which own their copies
Definition HashedTreap.h:226
void remove(const K &k)
removes , if it is there, in expected time, by the removal of Treap::remove() with
const_iterator cbegin(void) const
as begin()
Definition HashedTreap.h:248
std::size_t size(void) const
the number of keys
Definition HashedTreap.h:172
const_iterator end(void) const
the iterator past the largest key
Definition HashedTreap.h:244
PathIterator< HashedTreap, Shape > const_iterator
the constant iterator by increasing key, along the path from the root
Definition HashedTreap.h:236
K Key
the type of the keys
Definition HashedTreap.h:101
HashedTreap(std::uint64_t seed=1)
the empty treap, the tables of its hash drawn from the seed
Definition HashedTreap.h:105
std::size_t size_type
the type of the sizes
Definition HashedTreap.h:231
V Value
the type of the values
Definition HashedTreap.h:102
int height(void) const
the height, if empty, in
V mapped_type
the type of the values
Definition HashedTreap.h:230
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...
PairRef< const K &, const V & > reference
references, by value
Definition HashedTreap.h:233
bool contains(const K &k) const
true if is there
void insert(const K &k, const V &v)
associates to , in expected time, by the insertion of Treap::insert() with
reference const_reference
the same
Definition HashedTreap.h:234
const_iterator begin(void) const
the iterator on the smallest key
Definition HashedTreap.h:240
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
Definition ADT.h:96
the shape of the tree, for PathIterator
Definition HashedTreap.h:212
static auto root(const auto &o)
the root of the tree o
Definition HashedTreap.h:214
static auto read(const auto *n)
the pair of the node n
Definition HashedTreap.h:220
static auto left(const auto *n)
the left child of the node n
Definition HashedTreap.h:216
static auto right(const auto *n)
the right child of the node n
Definition HashedTreap.h:218