ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
SkipList.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*---------------------------- File SkipList.h -----------------------------*/
3/*--------------------------------------------------------------------------*/
64/*--------------------------------------------------------------------------*/
65/*----------------------------- DEFINITIONS --------------------------------*/
66/*--------------------------------------------------------------------------*/
67
68#ifndef __ADS_SkipList
69 #define __ADS_SkipList
70/* self-identification: #endif constant */
71
72/*--------------------------------------------------------------------------*/
73/*------------------------------ INCLUDES ----------------------------------*/
74/*--------------------------------------------------------------------------*/
75
76#include "ADS/Contract.h"
77#include "ADS/Iterators.h"
79
80#include <algorithm>
81#include <bit>
82#include <concepts>
83#include <cstddef>
84#include <cstdint>
85#include <iterator>
86#include <memory>
87#include <new>
88#include <optional>
89#include <utility>
90
91/*--------------------------------------------------------------------------*/
92/*--------------------------- NAMESPACE ------------------------------------*/
93/*--------------------------------------------------------------------------*/
94
95namespace ADS
96{
97
98/*--------------------------------------------------------------------------*/
99/*---------------------------- CLASS SkipList ------------------------------*/
100/*--------------------------------------------------------------------------*/
101
103template< std::totally_ordered K , class V >
105{
106
107/*--------------------------------------------------------------------------*/
108 public:
109 using Key = K;
110 using Value = V;
111
113 explicit SkipList( std::uint64_t seed = 1 );
114
116 SkipList( const SkipList & ) = delete;
117
119 SkipList & operator=( const SkipList & ) = delete;
120
122 SkipList( SkipList && other ) noexcept;
123
126
153 [[nodiscard]] std::optional< V > lookup( const K & k ) const;
154
177 void insert( const K & k , const V & v );
178
199 void remove( const K & k );
200
202 [[nodiscard]] bool contains( const K & k ) const;
203
205 [[nodiscard]] std::size_t size( void ) const;
206
208 [[nodiscard]] bool empty( void ) const { return( f_size == 0 ); }
209
211 [[nodiscard]] int levels( void ) const;
212
214 template< class F >
215 void forEach( F && f ) const;
216
217/*--------------------------------------------------------------------------*/
218 private:
219 static constexpr int kMax = 32;
220
223 struct Node
224 {
225 K key;
226 V value;
227 int height;
228 };
229
231 static constexpr std::size_t kTower =
232 ( sizeof( Node ) + alignof( Node * ) - 1 ) / alignof( Node * ) *
233 alignof( Node * );
234
236 static Node ** tower( Node * x ) {
237 return( std::launder( reinterpret_cast< Node ** >(
238 reinterpret_cast< std::byte * >( x ) + kTower ) ) );
239 }
240
241 public:
242/*------------------------- THE STANDARD INTERFACE -------------------------*/
243
244 // a constant view of the pairs ( key , value ) by increasing key, along
245 // the bottom level, forward
246
249 {
250 public:
254 using value_type = std::pair< K , V >;
256 using difference_type = std::ptrdiff_t;
258 using iterator_concept = std::forward_iterator_tag;
260 using iterator_category = std::input_iterator_tag;
261
263 const_iterator( void ) = default;
264
266 explicit const_iterator( Node * x ) : f_x( x ) { }
267
269 reference operator*( void ) const {
270 return( reference( f_x->key , f_x->value ) );
271 }
272
274 f_x = tower( f_x )[ 0 ];
275 return( *this );
276 }
277
279 auto t = *this;
280 ++*this;
281 return( t );
282 }
283
285 bool operator==( const const_iterator & o ) const = default;
286
287 private:
288 Node * f_x = nullptr;
289 };
290
292 using value_type = std::pair< K , V >;
294 using key_type = K;
296 using mapped_type = V;
298 using size_type = std::size_t;
300 using difference_type = std::ptrdiff_t;
307
309 [[nodiscard]] const_iterator begin( void ) const {
310 return( const_iterator( f_head[ 0 ] ) );
311 }
312
313 [[nodiscard]] const_iterator end( void ) const {
314 return( const_iterator() );
315 }
316
317 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
319 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
320
321/*----------------------------- THE SSET OF MORIN --------------------------*/
322
326 [[nodiscard]] const_iterator lowerBound( const K & k ) const;
327
331 [[nodiscard]] const K * findKey( const K & k ) const;
332
336 [[nodiscard]] const K * lowerBoundKey( const K & k ) const;
337
367 [[nodiscard]] std::optional< V > fingerLookup( const K & k );
368
369 private:
372 Node * before( const K & k , Node * update[ kMax ] ) const;
373
375 Node ** towerOf( Node * x ) const {
376 return( x ? tower( x ) : const_cast< Node ** >( f_head ) );
377 }
378
380 static Node * make( const K & k , const V & v , int L );
381
383 static void dispose( Node * x ) noexcept;
384
386 [[nodiscard]] bool sorted( void ) const;
387
388 Node * f_head[ kMax ] = {};
389 Node * f_finger[ kMax ] = {};
390 int f_levels = 0;
391 std::size_t f_size = 0;
392 SplitMix64 f_g;
393
394 }; // end( class SkipList )
395
396/*--------------------------------------------------------------------------*/
397
398} // end( namespace ADS )
399
400/*--------------------------------------------------------------------------*/
401/*-------------------------- IMPLEMENTATION --------------------------------*/
402/*--------------------------------------------------------------------------*/
403
404#include "ADS/dictionary/impl/SkipList.tpp"
405
406/*--------------------------------------------------------------------------*/
407
408#endif /* SkipList.h included */
409
410/*--------------------------------------------------------------------------*/
411/*-------------------------- End File SkipList.h ---------------------------*/
412/*--------------------------------------------------------------------------*/
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:
the iterator on the pairs, along the bottom level
Definition SkipList.h:249
std::forward_iterator_tag iterator_concept
the concept of the iterator, for C++20
Definition SkipList.h:258
std::pair< K, V > value_type
the type of the items, for the standard library
Definition SkipList.h:254
std::ptrdiff_t difference_type
the type of the distances between two iterators
Definition SkipList.h:256
reference operator*(void) const
the pair of the node
Definition SkipList.h:269
std::input_iterator_tag iterator_category
the category of the iterator
Definition SkipList.h:260
PairRef< const K &, const V & > reference
a reference to an item
Definition SkipList.h:252
const_iterator & operator++(void)
to the next node of the bottom level
Definition SkipList.h:273
const_iterator(Node *x)
on the pair of the node
Definition SkipList.h:266
const_iterator(void)=default
the end
bool operator==(const const_iterator &o) const =default
equal on the same node
const_iterator operator++(int)
to the next node, returning the iterator before the step
Definition SkipList.h:278
~SkipList()
frees the memory of the structure
V Value
the type of the values
Definition SkipList.h:110
PairRef< const K &, const V & > reference
a reference to an item
Definition SkipList.h:302
const_iterator lowerBound(const K &k) const
the iterator on the smallest key not smaller than , end() if there is none (the find(x) of an SSet of...
std::ptrdiff_t difference_type
the type of the distances between two iterators
Definition SkipList.h:300
const_iterator cend(void) const
the constant iterator past the last item
Definition SkipList.h:319
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...
SkipList(const SkipList &)=delete
no copy, since the nodes are linked by pointers
void forEach(F &&f) const
f( k , v ) on the pairs by increasing keys
std::pair< K, V > value_type
the type of the items, for the standard library
Definition SkipList.h:292
V mapped_type
the type of the values
Definition SkipList.h:296
std::size_t size_type
the type of the sizes
Definition SkipList.h:298
bool empty(void) const
true if there is no key
Definition SkipList.h:208
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...
std::optional< V > fingerLookup(const K &k)
the value of , or nothing, by a finger search from the key of the previous fingerLookup(),...
K key_type
the type of the keys
Definition SkipList.h:294
SkipList(SkipList &&other) noexcept
the nodes of other, which is left empty
std::size_t size(void) const
the number of keys
const_iterator begin(void) const
the iterator on the first item
Definition SkipList.h:309
reference const_reference
a constant reference to an item
Definition SkipList.h:304
SkipList & operator=(const SkipList &)=delete
no copy, since the nodes are linked by pointers
void remove(const K &k)
removes the key , if it is there, unlinking its node from each of its levels after before( k ) and dr...
int levels(void) const
the number of levels in use
std::optional< V > lookup(const K &k) const
the value of the key , or nothing, in expected time
void insert(const K &k, const V &v)
associates the value to the key , in expected time
SkipList(std::uint64_t seed=1)
the empty list, its levels drawn by a generator from the seed
const_iterator end(void) const
the iterator past the last item
Definition SkipList.h:313
const_iterator iterator
the iterator on the items
Definition SkipList.h:306
K Key
the type of the keys
Definition SkipList.h:109
const_iterator cbegin(void) const
the constant iterator on the first item
Definition SkipList.h:317
bool contains(const K &k) const
true if the key is there
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