ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
SkipListSequence.h File Reference

The SkiplistList of Morin, beyond the book: a sequence with access, insertion and removal at any index in \( O( \log n ) \) expected time, on a skip list [see SkipList.h] whose links carry their width, the number of items they jump over. More...

#include "ADS/Contract.h"
#include "ADS/dictionary/RandomHashing.h"
#include <algorithm>
#include <bit>
#include <cstddef>
#include <cstdint>
#include <iterator>
#include <memory>
#include <new>
#include <utility>
#include "ADS/sequence/impl/SkipListSequence.tpp"

Go to the source code of this file.

Classes

class  ADS::SkipListSequence< T >
 a sequence of items of type T on a skip list with the widths of the links, the SkiplistList of Morin More...
class  ADS::SkipListSequence< T >::basic_iterator< Const >
 the forward iterator on the items, along the bottom level More...

Namespaces

namespace  ADS

Detailed description

The SkiplistList of Morin, beyond the book: a sequence with access, insertion and removal at any index in \( O( \log n ) \) expected time, on a skip list [see SkipList.h] whose links carry their width, the number of items they jump over.

The items are in the order of the sequence on the bottom level, each node is in the levels 0 to \( \ell - 1 \) with probability \( 2^{-\ell} \), and a link from the node of index \( i \) (the head has index \( -1 \)) to the node of index \( j \) has width \( j - i \), a link to nil reaching the index \( n \); a search of the index \( i \) goes along a level while the widths summed stay below \( i \) and drops a level otherwise, which is the search by key of a skip list with the positions as keys. An insertion at \( i \) splits the link that it crosses on each of its levels and adds 1 to the width of the links that pass over it on the levels above, a removal the reverse.

truncate( i ) (exercise 4.11 of Morin) cuts the list after the first \( i \) items and returns the rest as a list of its own, and absorb( l ) (exercise 4.12) appends the items of another list, which it leaves empty: both relink one node per level, at the end of the search of the index \( i \) or of the last node of each level, in \( O( \log n ) \) expected time, while a vector would move \( \Theta( n ) \) items. A node is one block of memory, its item and the tower of its links, as in SkipList; the generator is SplitMix64 [see RandomHashing.h], seeded by default with a seed of its own per list, so that the lists absorbed one into another do not repeat their levels; a seed given makes them reproducible.

Where it is meant to be faster than the SkiplistList of Morin, for the benchmarks to check: his node is three blocks of memory (the node, the array of the successors and that of the widths), so that a step of a search touches three lines, while here a link and its width are adjacent in the one block of the node, one line per step; the widths are words, not int, so that a list may pass \( 2^{31} \) items; and the levels come from SplitMix64 instead of rand().

The contracts [see Contract.h]: an index is below size() (at most size() for an insertion and a truncation); the invariant, an \( O( n \log n ) \) check at the level 3, is that the widths of each level sum to \( n + 1 \) and that each level is a sublist of the level below. The operations are defined in SkipListSequence.tpp.

See also
W. Pugh, A skip list cookbook, Technical report CS-TR-2286.1, University of Maryland, 1990 (the widths of the links)
P. Morin, Open Data Structures (in C++), Edition 0.1G, 2013, https://opendatastructures.org/ods-cpp.pdf, section 4.3 and exercises 4.11 and 4.12
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa