a DICTIONARY on a skip list with the widths of the links, which reaches a key by its rank
More...
|
| | 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)
|
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
template<std::totally_ordered K, class 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 \)
template<std::totally_ordered K, class V>
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
template<std::totally_ordered K, class V>
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 \)