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

The skip list of Pugh, beyond the book: a sorted list of the keys with express lanes, each node being in the levels 0 to \( L - 1 \), \( L \) drawn with probability \( 2^{-L} \), and each level a sorted list of its nodes; a search goes along the highest level and drops a level before passing the key, in \( O( \log n ) \) expected time, and an insertion or a removal links or unlinks the node on its levels. More...

#include "ADS/Contract.h"
#include "ADS/Iterators.h"
#include "ADS/dictionary/RandomHashing.h"
#include <algorithm>
#include <bit>
#include <concepts>
#include <cstddef>
#include <cstdint>
#include <iterator>
#include <memory>
#include <new>
#include <optional>
#include <utility>
#include "ADS/dictionary/impl/SkipList.tpp"

Go to the source code of this file.

Classes

class  ADS::SkipList< K, V >
 a DICTIONARY on a skip list, an SSet of Morin by lowerBound() More...
class  ADS::SkipList< K, V >::const_iterator
 the iterator on the pairs, along the bottom level More...

Namespaces

namespace  ADS

Detailed description

The skip list of Pugh, beyond the book: a sorted list of the keys with express lanes, each node being in the levels 0 to \( L - 1 \), \( L \) drawn with probability \( 2^{-L} \), and each level a sorted list of its nodes; a search goes along the highest level and drops a level before passing the key, in \( O( \log n ) \) expected time, and an insertion or a removal links or unlinks the node on its levels.

It is a DICTIONARY [see ADT.h] as simple as a list and, with the right levels, as fast as a balanced tree, whose balance is in the random levels rather than in rotations; the generator is SplitMix64 [see RandomHashing.h], seeded, so that a run is reproducible. It is also the SkiplistSSet of Morin (section 4.2): lowerBound() is his find(x), the smallest key not smaller than \( x \).

A node is one block of memory, its key, its value and the tower of its successors one after the other, and the head is a tower with no node, so that a step of a search reads one line and tests no special case. A search compares \( x \) once with each node: when it drops a level and meets the node that stopped it on the level above, it drops again with no comparison (exercise 4.8 of Morin), which saves a quarter or more of the comparisons of the plain search (see Ordered dictionaries and tries). fingerLookup() is the finger search of exercise 4.10: the list keeps the last node before the previous key on each level, climbs them from the bottom to the first one from which the new key is reachable and goes down from there, in \( O( 1 + \log r ) \) expected time, \( r \) the keys between the two.

Where it is meant to be faster than the SkiplistSSet of Morin, for the benchmarks to check: the levels come from SplitMix64 instead of rand(), a few cycles against a call into the C library; a node has exactly \( \ell \) links where his has \( \ell + 1 \); the search skips the comparisons repeated across the levels (16.2 comparisons per search against 22.6 on 3000 keys, in the test of exercise 4.8), which counts when comparing a key costs more than a word, e.g., strings. The layout of a node, one block with its tower, and the removal in one search are as in his code.

The contracts [see Contract.h]: insert() ensures that the key is there and remove() that it is not; the invariant is that each level is sorted and a sublist of the level below, an \( O( n \log n ) \) check at the level 3. The operations are defined in SkipList.tpp.

See also
W. Pugh, Skip lists: a probabilistic alternative to balanced trees, Communications of the ACM 33(6), 668-676, 1990, doi:10.1145/78973.78977
W. Pugh, A skip list cookbook, Technical report CS-TR-2286.1, University of Maryland, 1990 (the finger search)
P. Morin, Open Data Structures (in C++), Edition 0.1G, 2013, https://opendatastructures.org/ods-cpp.pdf, section 4.2 and exercises 4.8 and 4.10
G. L. Steele, D. Lea, C. H. Flood, Fast splittable pseudorandom number generators, Proceedings of OOPSLA 2014, 453-472, 2014, doi:10.1145/2660193.2660195 (SplitMix64)
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa