|
| | SkipListSequence (std::uint64_t seed=detail::instanceSeed()) |
| | the empty sequence, its levels drawn by a generator from the seed, by default one of its own [see detail::instanceSeed()]
|
| | SkipListSequence (const SkipListSequence &)=delete |
| | no copy, since the nodes are linked by pointers
|
| SkipListSequence & | operator= (const SkipListSequence &)=delete |
| | no copy, since the nodes are linked by pointers
|
| | SkipListSequence (SkipListSequence &&other) noexcept |
| | the nodes of other, which is left empty
|
| | ~SkipListSequence () |
| | frees the memory of the structure
|
| std::size_t | size (void) const |
| | the number \( n \) of items
|
| bool | empty (void) const |
| | true if there is no item
|
| int | levels (void) const |
| | the number of levels in use
|
| const T & | get (std::size_t i) const |
| | the item of index \( i \), in \( O( \log n ) \) expected time
|
| void | set (std::size_t i, const T &v) |
| | the item of index \( i \) replaced by \( v \), by the search of get()
|
| void | add (std::size_t i, const T &v) |
| | \( v \) inserted at the index \( i \), the items from \( i \) on moving one place up, in \( O( \log n ) \) expected time
|
| const T & | operator[] (std::size_t i) const |
| | the item of index \( i \), as get( i ) (the name of IndexedList)
|
| T & | operator[] (std::size_t i) |
| | the item of index \( i \), to be read or written, by the search of get()
|
| void | insert (std::size_t i, const T &v) |
| | \( v \) inserted at \( i \), add( i , v ) (the name of IndexedList)
|
| void | erase (std::size_t i) |
| | the item of index \( i \) removed, remove( i ) (the name of IndexedList)
|
| void | push_back (const T &v) |
| | \( v \) appended, add( size() , v )
|
| T | remove (std::size_t i) |
| | the item of index \( i \) removed and returned, the items after it moving one place down, in \( O( \log n ) \) expected time: on each level the link that reaches the node is replaced by the one that leaves it, their widths summed less 1, and the links that pass over it lose 1
|
| SkipListSequence | truncate (std::size_t i) |
| | the items of index \( i \) to \( n - 1 \) taken out and returned as a sequence, in \( O( \log n ) \) expected time (exercise 4.11 of Morin)
|
| void | absorb (SkipListSequence &s) |
| | the items of \( s \) appended, in order, and \( s \) left empty, in \( O( \log n ) \) expected time (exercise 4.12 of Morin)
|
| iterator | begin (void) |
| | the iterator on the first item
|
| iterator | end (void) |
| | the iterator past the last item
|
| const_iterator | begin (void) const |
| | the constant iterator on the first item
|
| const_iterator | end (void) const |
| | the constant iterator past the last item
|
| const_iterator | cbegin (void) const |
| | as begin()
|
| const_iterator | cend (void) const |
| | as end()
|
template<class T>
class ADS::SkipListSequence< T >
a sequence of items of type T on a skip list with the widths of the links, the SkiplistList of Morin
the items of \( s \) appended, in order, and \( s \) left empty, in \( O( \log n ) \) expected time (exercise 4.12 of Morin)
In pseudocode (beyond the book), \( m \) the items of \( s \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{procedure}\ \mathrm{absorb}( s ) \cr
2 & \quad x \gets \mathit{head};\ j \gets -1 \cr
3 & \quad \textbf{for}\ l \gets \max\{ L , L_s \} - 1\ \textbf{downto}\ 0
\ \textbf{do} \cr
4 & \qquad \textbf{while}\ x.\mathit{next}[ l ] \ne \textbf{nil}
\ \textbf{do}\ j \gets j + x.w[ l ];\ x \gets x.\mathit{next}[ l ]
\cr
5 & \qquad x.\mathit{next}[ l ] \gets s.\mathit{head}.\mathit{next}[ l
];\ x.w[ l ] \gets n + s.\mathit{head}.w[ l ] - 1 - j \cr
6 & \quad n \gets n + m;\ s \gets \text{the empty sequence}
\end{array}}
\]
\( v \) inserted at the index \( i \), the items from \( i \) on moving one place up, in \( O( \log n ) \) expected time
In pseudocode (section 4.3 of Morin), the node \( y \) of \( \ell \) levels, \( \ell \) drawn as in SkipList:
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{procedure}\ \mathrm{add}( i , v ) \cr
2 & \quad y \gets \text{a node of } v \text{ and } \ell
\text{ levels};\ 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 ] < i\ \textbf{do}\ j \gets j +
x.w[ l ];\ x \gets x.\mathit{next}[ l ] \cr
5 & \qquad \textbf{if}\ l \ge \ell\ \textbf{then}\ x.w[ l ] \gets x.w[ l
] + 1 \cr
6 & \qquad \textbf{else}\ y.\mathit{next}[ l ] \gets x.\mathit{next}[ l
];\ y.w[ l ] \gets j + x.w[ l ] + 1 - i;\ x.\mathit{next}[ l ]
\gets y;\ x.w[ l ] \gets i - j
\end{array}}
\]
- Precondition
- \( i \le n \)
the item of index \( i \), in \( O( \log n ) \) expected time
In pseudocode (section 4.3 of Morin), \( x.\mathit{next}[ l ] \) and \( x.w[ l ] \) the successor of \( x \) on the level \( l \) and the width of the link, \( j \) the index of \( x \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{function}\ \mathrm{get}( 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.\mathit{item}
\end{array}}
\]
- Precondition
- \( i < n \)
the item of index \( i \) removed and returned, the items after it moving one place down, in \( O( \log n ) \) expected time: on each level the link that reaches the node is replaced by the one that leaves it, their widths summed less 1, and the links that pass over it lose 1
In pseudocode (section 4.3 of Morin), the search of add() for the node \( y \) of index \( i \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{function}\ \mathrm{remove}( 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 ] < i\ \textbf{do}\ j \gets j +
x.w[ l ];\ x \gets x.\mathit{next}[ l ] \cr
5 & \qquad \textbf{if}\ j + x.w[ l ] = i\ \textbf{and}
\ x.\mathit{next}[ l ] \ne \mathrm{nil}\ \textbf{then}
\ y \gets x.\mathit{next}[ l ] \cr
6 & \qquad\quad 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{return}\ y.\mathit{item}
\end{array}}
\]
- Precondition
- \( i < n \)
the items of index \( i \) to \( n - 1 \) taken out and returned as a sequence, in \( O( \log n ) \) expected time (exercise 4.11 of Morin)
In pseudocode (beyond the book), \( r \) the new sequence, whose head takes on each level the link that leaves the last node before the index \( i \):
\[\class{pseudocode}{\begin{array}{r@{\quad}l}
1 & \textbf{function}\ \mathrm{truncate}( 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 ] < i\ \textbf{do}\ j \gets j +
x.w[ l ];\ x \gets x.\mathit{next}[ l ] \cr
5 & \qquad r.\mathit{head}.\mathit{next}[ l ] \gets x.\mathit{next}[ l
];\ r.\mathit{head}.w[ l ] \gets j + x.w[ l ] - i + 1 \cr
6 & \qquad x.\mathit{next}[ l ] \gets \textbf{nil};\ x.w[ l ] \gets i -
j \cr
7 & \quad \text{drop the levels left empty in both};\ \textbf{return}\ r
\end{array}}
\]
- Precondition
- \( i \le n \)