ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
IndexedSkipList.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------- File IndexedSkipList.h -------------------------*/
3/*--------------------------------------------------------------------------*/
42/*--------------------------------------------------------------------------*/
43/*----------------------------- DEFINITIONS --------------------------------*/
44/*--------------------------------------------------------------------------*/
45
46#ifndef __ADS_IndexedSkipList
47 #define __ADS_IndexedSkipList
48/* self-identification: #endif constant */
49
50/*--------------------------------------------------------------------------*/
51/*------------------------------ INCLUDES ----------------------------------*/
52/*--------------------------------------------------------------------------*/
53
54#include "ADS/Contract.h"
55#include "ADS/Iterators.h"
57
58#include <algorithm>
59#include <bit>
60#include <concepts>
61#include <cstddef>
62#include <cstdint>
63#include <iterator>
64#include <memory>
65#include <new>
66#include <optional>
67#include <utility>
68
69/*--------------------------------------------------------------------------*/
70/*--------------------------- NAMESPACE ------------------------------------*/
71/*--------------------------------------------------------------------------*/
72
73namespace ADS
74{
75
76/*--------------------------------------------------------------------------*/
77/*------------------------- CLASS IndexedSkipList --------------------------*/
78/*--------------------------------------------------------------------------*/
79
82template< std::totally_ordered K , class V >
84{
85
86/*--------------------------------------------------------------------------*/
87 public:
88 using Key = K;
89 using Value = V;
90
92 explicit IndexedSkipList( std::uint64_t seed = 1 );
93
95 IndexedSkipList( const IndexedSkipList & ) = delete;
96
99
101 IndexedSkipList( IndexedSkipList && other ) noexcept;
102
105
108 [[nodiscard]] std::optional< V > lookup( const K & k ) const;
109
135 void insert( const K & k , const V & v );
136
163 void remove( const K & k );
164
166 [[nodiscard]] bool contains( const K & k ) const;
167
169 [[nodiscard]] std::size_t size( void ) const { return( f_size ); }
170
172 [[nodiscard]] bool empty( void ) const { return( f_size == 0 ); }
173
176 [[nodiscard]] std::size_t rank( const K & k ) const;
177
179 template< class F >
180 void forEach( F && f ) const;
181
182/*--------------------------------------------------------------------------*/
183 private:
184 static constexpr int kMax = 32;
185
186 struct Node;
187
189 struct Link
190 {
191 Node * next;
192 std::size_t w;
193 };
194
196 struct Node
197 {
198 K key;
199 V value;
200 int height;
201 };
202
204 static constexpr std::size_t kTower =
205 ( sizeof( Node ) + alignof( Link ) - 1 ) / alignof( Link ) *
206 alignof( Link );
207
209 static Link * tower( Node * x ) {
210 return( std::launder( reinterpret_cast< Link * >(
211 reinterpret_cast< std::byte * >( x ) + kTower ) ) );
212 }
213
214 public:
215/*------------------------- THE STANDARD INTERFACE -------------------------*/
216
219 {
220 public:
224 using value_type = std::pair< K , V >;
226 using difference_type = std::ptrdiff_t;
228 using iterator_concept = std::forward_iterator_tag;
230 using iterator_category = std::input_iterator_tag;
231
233 const_iterator( void ) = default;
234
236 explicit const_iterator( Node * x ) : f_x( x ) { }
237
239 reference operator*( void ) const {
240 return( reference( f_x->key , f_x->value ) );
241 }
242
244 f_x = tower( f_x )[ 0 ].next;
245 return( *this );
246 }
247
249 auto t = *this;
250 ++*this;
251 return( t );
252 }
253
255 bool operator==( const const_iterator & o ) const = default;
256
257 private:
258 Node * f_x = nullptr;
259 };
260
262 using value_type = std::pair< K , V >;
264 using key_type = K;
266 using mapped_type = V;
267 using size_type = std::size_t;
268 using difference_type = std::ptrdiff_t;
272
274 [[nodiscard]] const_iterator begin( void ) const {
275 return( const_iterator( f_head[ 0 ].next ) );
276 }
277
278 [[nodiscard]] const_iterator end( void ) const {
279 return( const_iterator() );
280 }
281
282 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
284 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
285
288 [[nodiscard]] const_iterator lowerBound( const K & k ) const;
289
293 [[nodiscard]] const K * findKey( const K & k ) const;
294
298 [[nodiscard]] const K * lowerBoundKey( const K & k ) const;
299
316 [[nodiscard]] const_iterator select( std::size_t i ) const;
317
318 private:
320 Link * towerOf( Node * x ) const {
321 return( x ? tower( x ) : const_cast< Link * >( f_head ) );
322 }
323
326 Node * before( const K & k , Node * update[ kMax ] ,
327 std::size_t rank[ kMax ] ) const;
328
330 static Node * make( const K & k , const V & v , int L );
331
333 static void dispose( Node * x ) noexcept;
334
336 [[nodiscard]] bool valid( void ) const;
337
338 Link f_head[ kMax ];
339 int f_levels = 0;
340 std::size_t f_size = 0;
341 SplitMix64 f_g;
342
343 }; // end( class IndexedSkipList )
344
345/*--------------------------------------------------------------------------*/
346
347} // end( namespace ADS )
348
349/*--------------------------------------------------------------------------*/
350/*-------------------------- IMPLEMENTATION --------------------------------*/
351/*--------------------------------------------------------------------------*/
352
353#include "ADS/dictionary/impl/IndexedSkipList.tpp"
354
355/*--------------------------------------------------------------------------*/
356
357#endif /* IndexedSkipList.h included */
358
359/*--------------------------------------------------------------------------*/
360/*----------------------- End File IndexedSkipList.h -----------------------*/
361/*--------------------------------------------------------------------------*/
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 IndexedSkipList.h:219
const_iterator operator++(int)
to the next node, returning the iterator before the step
Definition IndexedSkipList.h:248
std::ptrdiff_t difference_type
the type of the distances between two iterators
Definition IndexedSkipList.h:226
std::input_iterator_tag iterator_category
the category of the iterator
Definition IndexedSkipList.h:230
bool operator==(const const_iterator &o) const =default
equal on the same node
const_iterator(void)=default
the end
reference operator*(void) const
the pair of the node
Definition IndexedSkipList.h:239
std::forward_iterator_tag iterator_concept
the concept of the iterator, for C++20
Definition IndexedSkipList.h:228
const_iterator(Node *x)
on the pair of the node
Definition IndexedSkipList.h:236
PairRef< const K &, const V & > reference
a reference to an item
Definition IndexedSkipList.h:222
std::pair< K, V > value_type
the type of the items, for the standard library
Definition IndexedSkipList.h:224
const_iterator & operator++(void)
to the next node of the bottom level
Definition IndexedSkipList.h:243
PairRef< const K &, const V & > reference
references, by value
Definition IndexedSkipList.h:269
K key_type
the type of the keys
Definition IndexedSkipList.h:264
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...
bool empty(void) const
true if there is no key
Definition IndexedSkipList.h:172
IndexedSkipList(const IndexedSkipList &)=delete
no copy, since the nodes are linked by pointers
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...
std::size_t size_type
the type of the sizes
Definition IndexedSkipList.h:267
void remove(const K &k)
removes , if it is there, in expected time: on each level of its node the link that reaches it is re...
const_iterator cend(void) const
as end()
Definition IndexedSkipList.h:284
IndexedSkipList(std::uint64_t seed=1)
the empty list, its levels drawn by a generator from the seed
IndexedSkipList(IndexedSkipList &&other) noexcept
the nodes of other, which is left empty
const_iterator end(void) const
the iterator past the largest key
Definition IndexedSkipList.h:278
std::size_t size(void) const
the number of keys
Definition IndexedSkipList.h:169
const_iterator begin(void) const
the iterator on the smallest key
Definition IndexedSkipList.h:274
V Value
the type of the values
Definition IndexedSkipList.h:89
bool contains(const K &k) const
true if is there
const_iterator select(std::size_t i) const
the iterator on the key of rank , i.e., with keys smaller than it, in expected time (the get(i) of ...
void forEach(F &&f) const
f( k , v ) on the pairs by increasing keys
std::size_t rank(const K &k) const
the number of keys smaller than , the sum of the widths along the search of , in expected time
std::ptrdiff_t difference_type
the distances
Definition IndexedSkipList.h:268
IndexedSkipList & operator=(const IndexedSkipList &)=delete
no copy, since the nodes are linked by pointers
V mapped_type
the type of the values
Definition IndexedSkipList.h:266
K Key
the type of the keys
Definition IndexedSkipList.h:88
reference const_reference
the same
Definition IndexedSkipList.h:270
const_iterator iterator
the constant iterator
Definition IndexedSkipList.h:271
std::optional< V > lookup(const K &k) const
the value of , or nothing, by the search of SkipList, in expected time
std::pair< K, V > value_type
the type of the items, for the standard library
Definition IndexedSkipList.h:262
~IndexedSkipList()
frees the memory of the structure
void insert(const K &k, const V &v)
associates to , in expected time
const_iterator cbegin(void) const
as begin()
Definition IndexedSkipList.h:282
const_iterator lowerBound(const K &k) const
the iterator on the smallest key not smaller than (the find(x) of an SSet of Morin),...
the generator SplitMix64, a UniformRandomBitGenerator
Definition RandomHashing.h:122
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