ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
SkipListSequence.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*------------------------- File SkipListSequence.h ------------------------*/
3/*--------------------------------------------------------------------------*/
58/*--------------------------------------------------------------------------*/
59/*----------------------------- DEFINITIONS --------------------------------*/
60/*--------------------------------------------------------------------------*/
61
62#ifndef __ADS_SkipListSequence
63 #define __ADS_SkipListSequence
64/* self-identification: #endif constant */
65
66/*--------------------------------------------------------------------------*/
67/*------------------------------ INCLUDES ----------------------------------*/
68/*--------------------------------------------------------------------------*/
69
70#include "ADS/Contract.h"
72
73#include <algorithm>
74#include <bit>
75#include <cstddef>
76#include <cstdint>
77#include <iterator>
78#include <memory>
79#include <new>
80#include <utility>
81
82/*--------------------------------------------------------------------------*/
83/*--------------------------- NAMESPACE ------------------------------------*/
84/*--------------------------------------------------------------------------*/
85
86namespace ADS
87{
88
89/*--------------------------------------------------------------------------*/
90/*------------------------- CLASS SkipListSequence -------------------------*/
91/*--------------------------------------------------------------------------*/
92
95template< class T >
97{
98
99/*--------------------------------------------------------------------------*/
100 public:
101 using Item = T;
102
105 explicit SkipListSequence( std::uint64_t seed = detail::instanceSeed() );
106
109
112
114 SkipListSequence( SkipListSequence && other ) noexcept;
115
118
120 [[nodiscard]] std::size_t size( void ) const { return( f_size ); }
121
123 [[nodiscard]] bool empty( void ) const { return( f_size == 0 ); }
124
126 [[nodiscard]] int levels( void ) const { return( f_levels ); }
127
144 [[nodiscard]] const T & get( std::size_t i ) const;
145
149 void set( std::size_t i , const T & v );
150
172 void add( std::size_t i , const T & v );
173
176 [[nodiscard]] const T & operator[]( std::size_t i ) const {
177 return( get( i ) );
178 }
179
183 [[nodiscard]] T & operator[]( std::size_t i ) {
184 ADS_PRE( i < size() );
185 return( nodeAt( i )->item );
186 }
187
191 void insert( std::size_t i , const T & v ) { add( i , v ); }
192
196 void erase( std::size_t i ) { ( void )remove( i ); }
197
199 void push_back( const T & v ) { add( f_size , v ); }
200
226 T remove( std::size_t i );
227
250 [[nodiscard]] SkipListSequence truncate( std::size_t i );
251
271
272/*--------------------------------------------------------------------------*/
273 private:
274 static constexpr int kMax = 32;
275
276 struct Node;
277
279 struct Link
280 {
281 Node * next;
282 std::size_t w;
283 };
284
286 struct Node
287 {
288 T item;
289 int height;
290 };
291
293 static constexpr std::size_t kTower =
294 ( sizeof( Node ) + alignof( Link ) - 1 ) / alignof( Link ) *
295 alignof( Link );
296
298 static Link * tower( Node * x ) {
299 return( std::launder( reinterpret_cast< Link * >(
300 reinterpret_cast< std::byte * >( x ) + kTower ) ) );
301 }
302
303 public:
304/*------------------------- THE STANDARD INTERFACE -------------------------*/
305
307 template< bool Const >
309 {
310 public:
312 using value_type = T;
314 using reference = std::conditional_t< Const , const T & , T & >;
316 using difference_type = std::ptrdiff_t;
318 using iterator_category = std::forward_iterator_tag;
319
321 basic_iterator( void ) = default;
322
324 explicit basic_iterator( Node * x ) : f_x( x ) { }
325
327 operator basic_iterator< true >( void ) const {
328 return( basic_iterator< true >( f_x ) );
329 }
330
332 reference operator*( void ) const { return( f_x->item ); }
335 f_x = tower( f_x )[ 0 ].next;
336 return( *this );
337 }
338
340 auto t = *this;
341 ++*this;
342 return( t );
343 }
344
346 bool operator==( const basic_iterator & o ) const = default;
347
348 private:
349 Node * f_x = nullptr;
350 };
351
352 using value_type = T;
353 using size_type = std::size_t;
354 using difference_type = std::ptrdiff_t;
355 using reference = T &;
356 using const_reference = const T &;
359
361 [[nodiscard]] iterator begin( void ) {
362 return( iterator( f_head[ 0 ].next ) );
363 }
364
365 [[nodiscard]] iterator end( void ) { return( iterator() ); }
367 [[nodiscard]] const_iterator begin( void ) const {
368 return( const_iterator( f_head[ 0 ].next ) );
369 }
370
371 [[nodiscard]] const_iterator end( void ) const {
372 return( const_iterator() );
373 }
374
375 [[nodiscard]] const_iterator cbegin( void ) const { return( begin() ); }
377 [[nodiscard]] const_iterator cend( void ) const { return( end() ); }
378
379 private:
381 Link * towerOf( Node * x ) const {
382 return( x ? tower( x ) : const_cast< Link * >( f_head ) );
383 }
384
386 Node * nodeAt( std::size_t i ) const;
387
389 static Node * make( const T & v , int L );
390
392 static void dispose( Node * x ) noexcept;
393
395 void clearHead( void );
396
398 [[nodiscard]] bool valid( void ) const;
399
400 Link f_head[ kMax ];
401 int f_levels = 0;
402 std::size_t f_size = 0;
403 SplitMix64 f_g;
404
405 }; // end( class SkipListSequence )
406
407/*--------------------------------------------------------------------------*/
408
409} // end( namespace ADS )
410
411/*--------------------------------------------------------------------------*/
412/*-------------------------- IMPLEMENTATION --------------------------------*/
413/*--------------------------------------------------------------------------*/
414
415#include "ADS/sequence/impl/SkipListSequence.tpp"
416
417/*--------------------------------------------------------------------------*/
418
419#endif /* SkipListSequence.h included */
420
421/*--------------------------------------------------------------------------*/
422/*----------------------- End File SkipListSequence.h ----------------------*/
423/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
Hashing with random tables, beyond the book:
std::conditional_t< Const, const T &, T & > reference
a reference to an item
Definition SkipListSequence.h:314
reference operator*(void) const
the item of the node
Definition SkipListSequence.h:332
basic_iterator(Node *x)
on the node
Definition SkipListSequence.h:324
bool operator==(const basic_iterator &o) const =default
equal on the same node
std::ptrdiff_t difference_type
the type of the distances between two iterators
Definition SkipListSequence.h:316
basic_iterator & operator++(void)
to the next node of the bottom level
Definition SkipListSequence.h:334
std::forward_iterator_tag iterator_category
the concept and the category of the iterator
Definition SkipListSequence.h:318
basic_iterator operator++(int)
to the next node, returning the iterator before the step
Definition SkipListSequence.h:339
T value_type
the type of the items
Definition SkipListSequence.h:312
SkipListSequence(SkipListSequence &&other) noexcept
the nodes of other, which is left empty
SkipListSequence(const SkipListSequence &)=delete
no copy, since the nodes are linked by pointers
std::ptrdiff_t difference_type
the distances
Definition SkipListSequence.h:354
iterator begin(void)
the iterator on the first item
Definition SkipListSequence.h:361
basic_iterator< true > const_iterator
the constant iterator
Definition SkipListSequence.h:358
const T & const_reference
a constant reference
Definition SkipListSequence.h:356
SkipListSequence truncate(std::size_t i)
the items of index to taken out and returned as a sequence, in expected time (exercise 4....
basic_iterator< false > iterator
the iterator
Definition SkipListSequence.h:357
const_iterator end(void) const
the constant iterator past the last item
Definition SkipListSequence.h:371
void push_back(const T &v)
appended, add( size() , v )
Definition SkipListSequence.h:199
T & reference
a reference to an item
Definition SkipListSequence.h:355
const T & get(std::size_t i) const
the item of index , in expected time
iterator end(void)
the iterator past the last item
Definition SkipListSequence.h:365
const_iterator begin(void) const
the constant iterator on the first item
Definition SkipListSequence.h:367
T value_type
the items
Definition SkipListSequence.h:352
const_iterator cend(void) const
as end()
Definition SkipListSequence.h:377
std::size_t size(void) const
the number of items
Definition SkipListSequence.h:120
SkipListSequence(std::uint64_t seed=detail::instanceSeed())
the empty sequence, its levels drawn by a generator from the seed, by default one of its own [see det...
SkipListSequence & operator=(const SkipListSequence &)=delete
no copy, since the nodes are linked by pointers
int levels(void) const
the number of levels in use
Definition SkipListSequence.h:126
void absorb(SkipListSequence &s)
the items of appended, in order, and left empty, in expected time (exercise 4.12 of Morin)
void insert(std::size_t i, const T &v)
inserted at , add( i , v ) (the name of IndexedList)
Definition SkipListSequence.h:191
void add(std::size_t i, const T &v)
inserted at the index , the items from on moving one place up, in expected time
T Item
the type of the items
Definition SkipListSequence.h:101
T remove(std::size_t i)
the item of index removed and returned, the items after it moving one place down,...
std::size_t size_type
the type of the sizes
Definition SkipListSequence.h:353
bool empty(void) const
true if there is no item
Definition SkipListSequence.h:123
~SkipListSequence()
frees the memory of the structure
void erase(std::size_t i)
the item of index removed, remove( i ) (the name of IndexedList)
Definition SkipListSequence.h:196
void set(std::size_t i, const T &v)
the item of index replaced by , by the search of get()
const T & operator[](std::size_t i) const
the item of index , as get( i ) (the name of IndexedList)
Definition SkipListSequence.h:176
const_iterator cbegin(void) const
as begin()
Definition SkipListSequence.h:375
T & operator[](std::size_t i)
the item of index , to be read or written, by the search of get()
Definition SkipListSequence.h:183
#define ADS_PRE(cond)
[check]
Definition Contract.h:121
Definition ADT.h:96