|
| | 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
|
template<std::totally_ordered K, class V>
class ADS::SkipList< K, V >
a DICTIONARY on a skip list, an SSet of Morin by lowerBound()
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}}
\]
template<std::totally_ordered K, class 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 \)
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}}
\]
template<std::totally_ordered K, class V>
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