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

a DICTIONARY on a skip list with the widths of the links, which reaches a key by its rank More...

#include <ADS/dictionary/IndexedSkipList.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 distances
using reference = PairRef< const K & , const V & >
 references, by value
using const_reference = reference
 the same
using iterator = const_iterator
 the constant iterator

Public member functions

 IndexedSkipList (std::uint64_t seed=1)
 the empty list, its levels drawn by a generator from the seed
 IndexedSkipList (const IndexedSkipList &)=delete
 no copy, since the nodes are linked by pointers
IndexedSkipList & operator= (const IndexedSkipList &)=delete
 no copy, since the nodes are linked by pointers
 IndexedSkipList (IndexedSkipList &&other) noexcept
 the nodes of other, which is left empty
 ~IndexedSkipList ()
 frees the memory of the structure
std::optional< V > lookup (const K &k) const
 the value of \( k \), or nothing, by the search of SkipList, in \( O( \log n ) \) expected time
void insert (const K &k, const V &v)
 associates \( v \) to \( k \), in \( O( \log n ) \) expected time
void remove (const K &k)
 removes \( k \), if it is there, in \( O( \log n ) \) expected time: on each level of its node the link that reaches it is replaced by the one that leaves it, their widths summed less 1, and the links that pass over it lose 1
bool contains (const K &k) const
 true if \( k \) is there
std::size_t size (void) const
 the number \( n \) of keys
bool empty (void) const
 true if there is no key
std::size_t rank (const K &k) const
 the number of keys smaller than \( k \), the sum of the widths along the search of \( k \), in \( O( \log n ) \) expected time
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 smallest key
const_iterator end (void) const
 the iterator past the largest key
const_iterator cbegin (void) const
 as begin()
const_iterator cend (void) const
 as end()
const_iterator lowerBound (const K &k) const
 the iterator on the smallest key not smaller than \( k \) (the find(x) of an SSet of Morin), end() if there is none
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
const_iterator select (std::size_t i) const
 the iterator on the key of rank \( i \), i.e., with \( i \) keys smaller than it, in \( O( \log n ) \) expected time (the get(i) of exercise 4.9 of Morin)

Detailed description

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

a DICTIONARY on a skip list with the widths of the links, which reaches a key by its rank

Member typedef documentation

◆ const_reference

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

the same

◆ difference_type

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

the distances

◆ iterator

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

the constant iterator

◆ Key

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

the type of the keys

◆ key_type

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

the type of the keys

◆ mapped_type

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

the type of the values

◆ reference

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

references, by value

◆ size_type

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

the type of the sizes

◆ Value

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

the type of the values

◆ value_type

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

the type of the items, for the standard library

Constructor and destructor documentation

◆ IndexedSkipList() [1/3]

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

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

◆ IndexedSkipList() [2/3]

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

no copy, since the nodes are linked by pointers

◆ IndexedSkipList() [3/3]

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

the nodes of other, which is left empty

◆ ~IndexedSkipList()

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

frees the memory of the structure

Member function documentation

◆ begin()

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

the iterator on the smallest key

◆ cbegin()

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

as begin()

◆ cend()

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

as end()

◆ contains()

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

true if \( k \) is there

◆ empty()

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

true if there is no key

◆ end()

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

the iterator past the largest key

◆ findKey()

template<std::totally_ordered K, class V>
const K * ADS::IndexedSkipList< 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

◆ forEach()

template<std::totally_ordered K, class V>
template<class F>
void ADS::IndexedSkipList< 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::IndexedSkipList< K, V >::insert ( const K & k,
const V & v )

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

In pseudocode (Pugh, beyond the book), \( U[ l ] \) the last node before \( k \) on the level \( l \) and \( r[ l ] \) its rank, the head of rank \( -1 \), \( y \) the new node of \( \ell \) levels:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{insert}( k , v ) \cr 2 & \quad ( U , r ) \gets \text{the search of } k \text{, summing the} \text{ widths} \cr 3 & \quad \textbf{if}\ U[ 0 ].\mathit{next}[ 0 ].\mathit{key} = k \ \textbf{then}\ \text{its value} \gets v;\ \textbf{return} \cr 4 & \quad i \gets r[ 0 ] + 1 \quad \triangleright\ \text{the rank of } k \cr 5 & \quad \textbf{for}\ l \gets 0\ \textbf{to}\ L - 1\ \textbf{do} \cr 6 & \qquad x \gets U[ l ] \cr 7 & \qquad \textbf{if}\ l \ge \ell\ \textbf{then}\ x.w[ l ] \gets x.w[ l ] + 1 \cr 8 & \qquad \textbf{else}\ y.\mathit{next}[ l ] \gets x.\mathit{next}[ l ];\ y.w[ l ] \gets r[ l ] + x.w[ l ] + 1 - i;\ x.\mathit{next}[ l ] \gets y;\ x.w[ l ] \gets i - r[ l ] \end{array}} \]

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

◆ lookup()

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

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

◆ lowerBound()

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

the iterator on the smallest key not smaller than \( k \) (the find(x) of an SSet of Morin), end() if there is none

◆ lowerBoundKey()

template<std::totally_ordered K, class V>
const K * ADS::IndexedSkipList< 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>
IndexedSkipList & ADS::IndexedSkipList< K, V >::operator= ( const IndexedSkipList< K, V > & )
delete

no copy, since the nodes are linked by pointers

◆ rank()

template<std::totally_ordered K, class V>
std::size_t ADS::IndexedSkipList< K, V >::rank ( const K & k) const
nodiscard

the number of keys smaller than \( k \), the sum of the widths along the search of \( k \), in \( O( \log n ) \) expected time

◆ remove()

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

removes \( k \), if it is there, in \( O( \log n ) \) expected time: on each level of its node the link that reaches it is replaced by the one that leaves it, their widths summed less 1, and the links that pass over it lose 1

In pseudocode (Pugh, beyond the book), with \( U \) as in insert() and \( y \) the node of \( k \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{remove}( k ) \cr 2 & \quad U \gets \text{the search of } k;\ y \gets U[ 0 ].\mathit{next}[ 0 ] \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}\ L - 1\ \textbf{do} \cr 5 & \qquad x \gets U[ l ] \cr 6 & \qquad \textbf{if}\ x.\mathit{next}[ l ] = y\ \textbf{then} \ x.w[ l ] \gets x.w[ l ] + y.w[ l ] - 1;\ x.\mathit{next}[ l ] \gets y.\mathit{next}[ l ] \cr 7 & \qquad \textbf{else}\ x.w[ l ] \gets x.w[ l ] - 1 \cr 8 & \quad \textbf{for}\ l \ge L\ \textbf{do}\ \mathit{head}.w[ l ] \gets \mathit{head}.w[ l ] - 1 \cr 9 & \quad \text{free } y \end{array}} \]

Postcondition
\( k \) is not there

◆ select()

template<std::totally_ordered K, class V>
const_iterator ADS::IndexedSkipList< K, V >::select ( std::size_t i) const
nodiscard

the iterator on the key of rank \( i \), i.e., with \( i \) keys smaller than it, in \( O( \log n ) \) expected time (the get(i) of exercise 4.9 of Morin)

In pseudocode (Pugh, beyond the book), \( j \) the rank of \( x \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{select}( i ) \cr 2 & \quad x \gets \mathit{head};\ j \gets -1 \cr 3 & \quad \textbf{for}\ l \gets L - 1\ \textbf{downto}\ 0\ \textbf{do} \cr 4 & \qquad \textbf{while}\ j + x.w[ l ] \le i\ \textbf{do}\ j \gets j + x.w[ l ];\ x \gets x.\mathit{next}[ l ] \cr 5 & \quad \textbf{return}\ x \end{array}} \]

Precondition
\( i < n \)

◆ size()

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

the number \( n \) of keys


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