ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ADS::SkipListSequence< T > Class Template Reference

a sequence of items of type T on a skip list with the widths of the links, the SkiplistList of Morin More...

#include <ADS/sequence/SkipListSequence.h>

Classes

class  basic_iterator
 the forward iterator on the items, along the bottom level More...

Public types

using Item = T
 the type of the items
using value_type = T
 the items
using size_type = std::size_t
 the type of the sizes
using difference_type = std::ptrdiff_t
 the distances
using reference = T &
 a reference to an item
using const_reference = const T &
 a constant reference
using iterator = basic_iterator< false >
 the iterator
using const_iterator = basic_iterator< true >
 the constant iterator

Public member functions

 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()

Detailed description

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

Member typedef documentation

◆ const_iterator

template<class T>
using ADS::SkipListSequence< T >::const_iterator = basic_iterator< true >

the constant iterator

◆ const_reference

template<class T>
using ADS::SkipListSequence< T >::const_reference = const T &

a constant reference

◆ difference_type

template<class T>
using ADS::SkipListSequence< T >::difference_type = std::ptrdiff_t

the distances

◆ Item

template<class T>
using ADS::SkipListSequence< T >::Item = T

the type of the items

◆ iterator

template<class T>
using ADS::SkipListSequence< T >::iterator = basic_iterator< false >

the iterator

◆ reference

template<class T>
using ADS::SkipListSequence< T >::reference = T &

a reference to an item

◆ size_type

template<class T>
using ADS::SkipListSequence< T >::size_type = std::size_t

the type of the sizes

◆ value_type

template<class T>
using ADS::SkipListSequence< T >::value_type = T

the items

Constructor and destructor documentation

◆ SkipListSequence() [1/3]

template<class T>
ADS::SkipListSequence< T >::SkipListSequence ( std::uint64_t seed = detail::instanceSeed())
explicit

the empty sequence, its levels drawn by a generator from the seed, by default one of its own [see detail::instanceSeed()]

◆ SkipListSequence() [2/3]

template<class T>
ADS::SkipListSequence< T >::SkipListSequence ( const SkipListSequence< T > & )
delete

no copy, since the nodes are linked by pointers

◆ SkipListSequence() [3/3]

template<class T>
ADS::SkipListSequence< T >::SkipListSequence ( SkipListSequence< T > && other)
noexcept

the nodes of other, which is left empty

◆ ~SkipListSequence()

template<class T>
ADS::SkipListSequence< T >::~SkipListSequence ( )

frees the memory of the structure

Member function documentation

◆ absorb()

template<class T>
void ADS::SkipListSequence< T >::absorb ( SkipListSequence< T > & s)

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}} \]

◆ add()

template<class T>
void ADS::SkipListSequence< T >::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

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 \)

◆ begin() [1/2]

template<class T>
iterator ADS::SkipListSequence< T >::begin ( void )
inlinenodiscard

the iterator on the first item

◆ begin() [2/2]

template<class T>
const_iterator ADS::SkipListSequence< T >::begin ( void ) const
inlinenodiscard

the constant iterator on the first item

◆ cbegin()

template<class T>
const_iterator ADS::SkipListSequence< T >::cbegin ( void ) const
inlinenodiscard

as begin()

◆ cend()

template<class T>
const_iterator ADS::SkipListSequence< T >::cend ( void ) const
inlinenodiscard

as end()

◆ empty()

template<class T>
bool ADS::SkipListSequence< T >::empty ( void ) const
inlinenodiscard

true if there is no item

◆ end() [1/2]

template<class T>
iterator ADS::SkipListSequence< T >::end ( void )
inlinenodiscard

the iterator past the last item

◆ end() [2/2]

template<class T>
const_iterator ADS::SkipListSequence< T >::end ( void ) const
inlinenodiscard

the constant iterator past the last item

◆ erase()

template<class T>
void ADS::SkipListSequence< T >::erase ( std::size_t i)
inline

the item of index \( i \) removed, remove( i ) (the name of IndexedList)

Precondition
\( i < n \)

◆ get()

template<class T>
const T & ADS::SkipListSequence< T >::get ( std::size_t i) const
nodiscard

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 \)

◆ insert()

template<class T>
void ADS::SkipListSequence< T >::insert ( std::size_t i,
const T & v )
inline

\( v \) inserted at \( i \), add( i , v ) (the name of IndexedList)

Precondition
\( i \le n \)

◆ levels()

template<class T>
int ADS::SkipListSequence< T >::levels ( void ) const
inlinenodiscard

the number of levels in use

◆ operator=()

template<class T>
SkipListSequence & ADS::SkipListSequence< T >::operator= ( const SkipListSequence< T > & )
delete

no copy, since the nodes are linked by pointers

◆ operator[]() [1/2]

template<class T>
T & ADS::SkipListSequence< T >::operator[] ( std::size_t i)
inlinenodiscard

the item of index \( i \), to be read or written, by the search of get()

Precondition
\( i < n \)

◆ operator[]() [2/2]

template<class T>
const T & ADS::SkipListSequence< T >::operator[] ( std::size_t i) const
inlinenodiscard

the item of index \( i \), as get( i ) (the name of IndexedList)

Precondition
\( i < n \)

◆ push_back()

template<class T>
void ADS::SkipListSequence< T >::push_back ( const T & v)
inline

\( v \) appended, add( size() , v )

◆ remove()

template<class T>
T ADS::SkipListSequence< 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

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 \)

◆ set()

template<class T>
void ADS::SkipListSequence< T >::set ( std::size_t i,
const T & v )

the item of index \( i \) replaced by \( v \), by the search of get()

Precondition
\( i < n \)

◆ size()

template<class T>
std::size_t ADS::SkipListSequence< T >::size ( void ) const
inlinenodiscard

the number \( n \) of items

◆ truncate()

template<class T>
SkipListSequence ADS::SkipListSequence< T >::truncate ( std::size_t i)
nodiscard

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 \)

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