ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::SkipList< K, V > Class Template Reference

a DICTIONARY on a skip list, an SSet of Morin by lowerBound() More...

#include <ADS/dictionary/SkipList.h>

Classes

class  const_iterator
 the iterator on the pairs, along the bottom level More...

Public types

using Key = K
 the type of the keys
using Value = V
 the type of the values
using value_type = std::pair< K , V >
 the type of the items, for the standard library
using key_type = K
 the type of the keys
using mapped_type = V
 the type of the values
using size_type = std::size_t
 the type of the sizes
using difference_type = std::ptrdiff_t
 the type of the distances between two iterators
using reference = PairRef< const K & , const V & >
 a reference to an item
using const_reference = reference
 a constant reference to an item
using iterator = const_iterator
 the iterator on the items

Public member functions

 SkipList (std::uint64_t seed=1)
 the empty list, its levels drawn by a generator from the seed
 SkipList (const SkipList &)=delete
 no copy, since the nodes are linked by pointers
SkipList & operator= (const SkipList &)=delete
 no copy, since the nodes are linked by pointers
 SkipList (SkipList &&other) noexcept
 the nodes of other, which is left empty
 ~SkipList ()
 frees the memory of the structure
std::optional< V > lookup (const K &k) const
 the value of the key \( k \), or nothing, in \( O( \log n ) \) expected time
void insert (const K &k, const V &v)
 associates the value \( v \) to the key \( k \), in \( O( \log n ) \) expected time
void remove (const K &k)
 removes the key \( k \), if it is there, unlinking its node from each of its levels after before( k ) and dropping the levels left empty, in \( O( \log n ) \) expected time
bool contains (const K &k) const
 true if the key \( k \) is there
std::size_t size (void) const
 the number of keys
bool empty (void) const
 true if there is no key
int levels (void) const
 the number of levels in use
template<class F>
void forEach (F &&f) const
 f( k , v ) on the pairs by increasing keys
const_iterator begin (void) const
 the iterator on the first item
const_iterator end (void) const
 the iterator past the last item
const_iterator cbegin (void) const
 the constant iterator on the first item
const_iterator cend (void) const
 the constant iterator past the last item
const_iterator lowerBound (const K &k) const
 the iterator on the smallest key not smaller than \( k \), end() if there is none (the find(x) of an SSet of Morin), by before( k ), in \( O( \log n ) \) expected time
const K * findKey (const K &k) const
 the address of the key \( k \), nil if absent, in \( O( \log n ) \) expected time: the find( x ) of a USet of Morin [see DictionarySet]; valid until the key is removed
const K * lowerBoundKey (const K &k) const
 the address of the smallest key not smaller than \( k \), nil if none, in \( O( \log n ) \) expected time: the find( x ) of an SSet of Morin [see DictionarySet]; valid until the key is removed
std::optional< V > fingerLookup (const K &k)
 the value of \( k \), or nothing, by a finger search from the key of the previous fingerLookup(), in \( O( 1 + \log r ) \) expected time, \( r \) the keys between the two (exercise 4.10 of Morin); an insertion or a removal brings the finger back to the head

Detailed description

template<std::totally_ordered K, class V>
class ADS::SkipList< K, V >

a DICTIONARY on a skip list, an SSet of Morin by lowerBound()

Member typedef documentation

◆ const_reference

template<std::totally_ordered K, class V>
using ADS::SkipList< K, V >::const_reference = reference

a constant reference to an item

◆ difference_type

template<std::totally_ordered K, class V>
using ADS::SkipList< K, V >::difference_type = std::ptrdiff_t

the type of the distances between two iterators

◆ iterator

template<std::totally_ordered K, class V>
using ADS::SkipList< K, V >::iterator = const_iterator

the iterator on the items

◆ Key

template<std::totally_ordered K, class V>
using ADS::SkipList< K, V >::Key = K

the type of the keys

◆ key_type

template<std::totally_ordered K, class V>
using ADS::SkipList< K, V >::key_type = K

the type of the keys

◆ mapped_type

template<std::totally_ordered K, class V>
using ADS::SkipList< K, V >::mapped_type = V

the type of the values

◆ reference

template<std::totally_ordered K, class V>
using ADS::SkipList< K, V >::reference = PairRef< const K & , const V & >

a reference to an item

◆ size_type

template<std::totally_ordered K, class V>
using ADS::SkipList< K, V >::size_type = std::size_t

the type of the sizes

◆ Value

template<std::totally_ordered K, class V>
using ADS::SkipList< K, V >::Value = V

the type of the values

◆ value_type

template<std::totally_ordered K, class V>
using ADS::SkipList< K, V >::value_type = std::pair< K , V >

the type of the items, for the standard library

Constructor and destructor documentation

◆ SkipList() [1/3]

template<std::totally_ordered K, class V>
ADS::SkipList< K, V >::SkipList ( std::uint64_t seed = 1)
explicit

the empty list, its levels drawn by a generator from the seed

◆ SkipList() [2/3]

template<std::totally_ordered K, class V>
ADS::SkipList< K, V >::SkipList ( const SkipList< K, V > & )
delete

no copy, since the nodes are linked by pointers

◆ SkipList() [3/3]

template<std::totally_ordered K, class V>
ADS::SkipList< K, V >::SkipList ( SkipList< K, V > && other)
noexcept

the nodes of other, which is left empty

◆ ~SkipList()

template<std::totally_ordered K, class V>
ADS::SkipList< K, V >::~SkipList ( )

frees the memory of the structure

Member function documentation

◆ begin()

template<std::totally_ordered K, class V>
const_iterator ADS::SkipList< K, V >::begin ( void ) const
inlinenodiscard

the iterator on the first item

◆ cbegin()

template<std::totally_ordered K, class V>
const_iterator ADS::SkipList< K, V >::cbegin ( void ) const
inlinenodiscard

the constant iterator on the first item

◆ cend()

template<std::totally_ordered K, class V>
const_iterator ADS::SkipList< K, V >::cend ( void ) const
inlinenodiscard

the constant iterator past the last item

◆ contains()

template<std::totally_ordered K, class V>
bool ADS::SkipList< K, V >::contains ( const K & k) const
nodiscard

true if the key \( k \) is there

◆ empty()

template<std::totally_ordered K, class V>
bool ADS::SkipList< K, V >::empty ( void ) const
inlinenodiscard

true if there is no key

◆ end()

template<std::totally_ordered K, class V>
const_iterator ADS::SkipList< K, V >::end ( void ) const
inlinenodiscard

the iterator past the last item

◆ findKey()

template<std::totally_ordered K, class V>
const K * ADS::SkipList< K, V >::findKey ( const K & k) const
nodiscard

the address of the key \( k \), nil if absent, in \( O( \log n ) \) expected time: the find( x ) of a USet of Morin [see DictionarySet]; valid until the key is removed

◆ fingerLookup()

template<std::totally_ordered K, class V>
std::optional< V > ADS::SkipList< K, V >::fingerLookup ( const K & k)
nodiscard

the value of \( k \), or nothing, by a finger search from the key of the previous fingerLookup(), in \( O( 1 + \log r ) \) expected time, \( r \) the keys between the two (exercise 4.10 of Morin); an insertion or a removal brings the finger back to the head

In pseudocode (Pugh, beyond the book), \( F[ l ] \) the last node before the previous key on the level \( l \), the head counting as \( -\infty \); the finger climbs while its node is not before \( k \) or its successor is, and the search goes down from there, updating \( F \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{fingerLookup}( k ) \cr 2 & \quad l \gets 0 \cr 3 & \quad \textbf{while}\ l < L - 1\ \textbf{and}\ ( F[ l ].\mathit{key} \ge k\ \textbf{or}\ F[ l ].\mathit{next}[ l ].\mathit{key} < k ) \ \textbf{do}\ l \gets l + 1 \cr 4 & \quad x \gets F[ l ];\ \textbf{if}\ x.\mathit{key} \ge k \ \textbf{then}\ x \gets \mathit{head} \cr 5 & \quad \textbf{for}\ j \gets l\ \textbf{downto}\ 0\ \textbf{do} \cr 6 & \qquad \textbf{while}\ x.\mathit{next}[ j ] \ne \textbf{nil} \ \textbf{and}\ x.\mathit{next}[ j ].\mathit{key} < k\ \textbf{do} \ x \gets x.\mathit{next}[ j ] \cr 7 & \qquad F[ j ] \gets x \cr 8 & \quad y \gets x.\mathit{next}[ 0 ] \cr 9 & \quad \textbf{if}\ y \ne \textbf{nil}\ \textbf{and}\ y.\mathit{key} = k\ \textbf{then return}\ y.\mathit{value}\ \textbf{else return nil} \end{array}} \]

◆ forEach()

template<std::totally_ordered K, class V>
template<class F>
void ADS::SkipList< K, V >::forEach ( F && f) const

f( k , v ) on the pairs by increasing keys

◆ insert()

template<std::totally_ordered K, class V>
void ADS::SkipList< K, V >::insert ( const K & k,
const V & v )

associates the value \( v \) to the key \( k \), in \( O( \log n ) \) expected time

In pseudocode (Pugh, beyond the book), the level of a new node drawn as the number of trailing 1s of a random word plus one, i.e., \( \ell \) with probability \( 2^{-\ell} \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{insert}( k , v ) \cr 2 & \quad ( y , U ) \gets \mathrm{before}( k ) \cr 3 & \quad \textbf{if}\ y \ne \textbf{nil}\ \textbf{and}\ y.\mathit{key} = k\ \textbf{then}\ y.\mathit{value} \gets v;\ \textbf{return} \cr 4 & \quad \ell \gets \text{a random level};\ \textbf{for}\ l \gets L \ \textbf{to}\ \ell - 1\ \textbf{do}\ U[ l ] \gets \mathit{head} \cr 5 & \quad L \gets \max\{ L , \ell \};\ x \gets \text{a node } ( k , v ) \text{ of } \ell \text{ levels} \cr 6 & \quad \textbf{for}\ l \gets 0\ \textbf{to}\ \ell - 1\ \textbf{do} \ x.\mathit{next}[ l ] \gets U[ l ].\mathit{next}[ l ];\ U[ l ].\mathit{next}[ l ] \gets x \end{array}} \]

Postcondition
\( k \) has the value \( v \)

◆ levels()

template<std::totally_ordered K, class V>
int ADS::SkipList< K, V >::levels ( void ) const
nodiscard

the number of levels in use

◆ lookup()

template<std::totally_ordered K, class V>
std::optional< V > ADS::SkipList< K, V >::lookup ( const K & k) const
nodiscard

the value of the key \( k \), or nothing, in \( O( \log n ) \) expected time

In pseudocode (Pugh, beyond the book, and exercise 4.8 of Morin), \( x.\mathit{next}[ l ] \) the successor of \( x \) on the level \( l \), the head being a node with no key; the search drops a level just before passing \( k \), the last node met on each level is kept in \( U \) for insert() and remove(), and \( s \), the last node found not smaller than \( k \), is never compared again:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{before}( k ) \cr 2 & \quad x \gets \mathit{head};\ s \gets \textbf{nil} \cr 3 & \quad \textbf{for}\ l \gets L - 1\ \textbf{downto}\ 0\ \textbf{do} \cr 4 & \qquad \textbf{while}\ x.\mathit{next}[ l ] \ne s\ \textbf{and} \ x.\mathit{next}[ l ].\mathit{key} < k\ \textbf{do}\ x \gets x.\mathit{next}[ l ] \cr 5 & \qquad s \gets x.\mathit{next}[ l ];\ U[ l ] \gets x \cr 6 & \quad \textbf{return}\ ( x.\mathit{next}[ 0 ] , U ) \cr 7 & \textbf{function}\ \mathrm{lookup}( k ) \cr 8 & \quad ( y , U ) \gets \mathrm{before}( k ) \cr 9 & \quad \textbf{if}\ y \ne \textbf{nil}\ \textbf{and}\ y.\mathit{key} = k\ \textbf{then return}\ y.\mathit{value}\ \textbf{else return nil} \end{array}} \]

◆ lowerBound()

template<std::totally_ordered K, class V>
const_iterator ADS::SkipList< K, V >::lowerBound ( const K & k) const
nodiscard

the iterator on the smallest key not smaller than \( k \), end() if there is none (the find(x) of an SSet of Morin), by before( k ), in \( O( \log n ) \) expected time

◆ lowerBoundKey()

template<std::totally_ordered K, class V>
const K * ADS::SkipList< K, V >::lowerBoundKey ( const K & k) const
nodiscard

the address of the smallest key not smaller than \( k \), nil if none, in \( O( \log n ) \) expected time: the find( x ) of an SSet of Morin [see DictionarySet]; valid until the key is removed

◆ operator=()

template<std::totally_ordered K, class V>
SkipList & ADS::SkipList< K, V >::operator= ( const SkipList< K, V > & )
delete

no copy, since the nodes are linked by pointers

◆ remove()

template<std::totally_ordered K, class V>
void ADS::SkipList< K, V >::remove ( const K & k)

removes the key \( k \), if it is there, unlinking its node from each of its levels after before( k ) and dropping the levels left empty, in \( O( \log n ) \) expected time

In pseudocode (Pugh, beyond the book), \( y.h \) the levels of the node \( y \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{remove}( k ) \cr 2 & \quad ( y , U ) \gets \mathrm{before}( k ) \cr 3 & \quad \textbf{if}\ y = \textbf{nil}\ \textbf{or}\ y.\mathit{key} \ne k\ \textbf{then return} \cr 4 & \quad \textbf{for}\ l \gets 0\ \textbf{to}\ y.h - 1\ \textbf{do} \ U[ l ].\mathit{next}[ l ] \gets y.\mathit{next}[ l ] \cr 5 & \quad \text{free } y \cr 6 & \quad \textbf{while}\ L > 0\ \textbf{and}\ \mathit{head}.\mathit{next} [ L - 1 ] = \textbf{nil}\ \textbf{do}\ L \gets L - 1 \end{array}} \]

Postcondition
\( k \) is not there

◆ size()

template<std::totally_ordered K, class V>
std::size_t ADS::SkipList< K, V >::size ( void ) const
nodiscard

the number of keys


The documentation for this class was generated from the following file: