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

a sequence of items of type T on an implicit treap, the TreapList of Morin More...

#include <ADS/sequence/TreapList.h>

Classes

struct  Shape
 the shape of the tree, for PathIterator 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 type of the distances
using reference = const T &
 a constant reference
using const_reference = const T &
 a constant reference
using const_iterator = PathIterator< TreapList , Shape >
 the constant iterator in the order of the sequence
using iterator = const_iterator
 the constant iterator

Public member functions

 TreapList (std::uint64_t seed=detail::instanceSeed())
 the empty sequence, its priorities drawn from the seed, by default one of its own [see detail::instanceSeed()]
std::size_t size (void) const
 the number \( n \) of items
bool empty (void) const
 true if there is no item
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 descent of get()
void add (std::size_t i, const T &v)
 \( v \) inserted at the index \( i \), 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, in \( O( \log n ) \) expected time, the node rotated down as in Treap::remove()
TreapList truncate (std::size_t i)
 the items of index \( i \) to \( n - 1 \) taken out and returned as a sequence, by the split of the treap at \( i \), in \( O( \log n ) \) expected time
void absorb (TreapList &s)
 the items of \( s \) appended, in order, and \( s \) left empty, by the join of the two treaps, in \( O( \log n ) \) expected time
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
 as begin()
const_iterator cend (void) const
 as end()

Detailed description

template<class T>
class ADS::TreapList< T >

a sequence of items of type T on an implicit treap, the TreapList of Morin

Member typedef documentation

◆ const_iterator

template<class T>
using ADS::TreapList< T >::const_iterator = PathIterator< TreapList , Shape >

the constant iterator in the order of the sequence

◆ const_reference

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

a constant reference

◆ difference_type

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

the type of the distances

◆ Item

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

the type of the items

◆ iterator

template<class T>
using ADS::TreapList< T >::iterator = const_iterator

the constant iterator

◆ reference

template<class T>
using ADS::TreapList< T >::reference = const T &

a constant reference

◆ size_type

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

the type of the sizes

◆ value_type

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

the items

Constructor and destructor documentation

◆ TreapList()

template<class T>
ADS::TreapList< T >::TreapList ( std::uint64_t seed = detail::instanceSeed())
inlineexplicit

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

Member function documentation

◆ absorb()

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

the items of \( s \) appended, in order, and \( s \) left empty, by the join of the two treaps, in \( O( \log n ) \) expected time

In pseudocode (Seidel and Aragon, beyond the book), the two right and left spines merged by the priorities:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{join}( a , b ) \cr 2 & \quad \textbf{if}\ a = \textbf{nil}\ \textbf{or}\ b = \textbf{nil} \ \textbf{then return}\ \text{the other} \cr 3 & \quad \textbf{if}\ p( a ) > p( b )\ \textbf{then}\ a.\mathit{right} \gets \mathrm{join}( a.\mathit{right} , b );\ \textbf{return}\ a \cr 4 & \quad b.\mathit{left} \gets \mathrm{join}( a , b.\mathit{left} ); \ \textbf{return}\ b \end{array}} \]

◆ add()

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

\( v \) inserted at the index \( i \), in \( O( \log n ) \) expected time

In pseudocode (exercise 7.11 of Morin), \( p \) the priorities:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{add}( t , i , v ) \cr 2 & \quad \textbf{if}\ t = \textbf{nil}\ \textbf{then}\ t \gets \text{a node of } v \text{ and a random priority};\ \textbf{return} \cr 3 & \quad \textbf{if}\ i \le s( t.\mathit{left} )\ \textbf{then} \cr 4 & \qquad \mathrm{add}( t.\mathit{left} , i , v );\ \textbf{if}\ p( t.\mathit{left} ) > p( t )\ \textbf{then}\ \mathrm{rotateRight}( t ) \cr 5 & \quad \textbf{else}\ \mathrm{add}( t.\mathit{right} , i - s( t.\mathit{left} ) - 1 , v );\ \text{and the mirror of line 4} \cr 6 & \quad \text{the size of } t \text{ updated} \end{array}} \]

Precondition
\( i \le n \)

◆ begin()

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

the iterator on the first item

◆ cbegin()

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

as begin()

◆ cend()

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

as end()

◆ empty()

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

true if there is no item

◆ end()

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

the iterator past the last item

◆ erase()

template<class T>
void ADS::TreapList< 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::TreapList< T >::get ( std::size_t i) const
nodiscard

the item of index \( i \), in \( O( \log n ) \) expected time

In pseudocode (exercise 7.11 of Morin), \( s \) the sizes:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{get}( i ) \cr 2 & \quad t \gets \mathit{root} \cr 3 & \quad \textbf{while}\ i \ne s( t.\mathit{left} )\ \textbf{do} \cr 4 & \qquad \textbf{if}\ i < s( t.\mathit{left} )\ \textbf{then}\ t \gets t.\mathit{left} \cr 5 & \qquad \textbf{else}\ i \gets i - s( t.\mathit{left} ) - 1;\ t \gets t.\mathit{right} \cr 6 & \quad \textbf{return}\ t.\mathit{item} \end{array}} \]

Precondition
\( i < n \)

◆ insert()

template<class T>
void ADS::TreapList< 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 \)

◆ operator[]() [1/2]

template<class T>
T & ADS::TreapList< 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::TreapList< 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::TreapList< T >::push_back ( const T & v)
inline

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

◆ remove()

template<class T>
T ADS::TreapList< T >::remove ( std::size_t i)

the item of index \( i \) removed and returned, in \( O( \log n ) \) expected time, the node rotated down as in Treap::remove()

Precondition
\( i < n \)

◆ set()

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

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

Precondition
\( i < n \)

◆ size()

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

the number \( n \) of items

◆ truncate()

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

the items of index \( i \) to \( n - 1 \) taken out and returned as a sequence, by the split of the treap at \( i \), in \( O( \log n ) \) expected time

In pseudocode (Seidel and Aragon, beyond the book):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{split}( t , i ) \cr 2 & \quad \textbf{if}\ t = \textbf{nil}\ \textbf{then return} \ ( \textbf{nil} , \textbf{nil} ) \cr 3 & \quad \textbf{if}\ i \le s( t.\mathit{left} )\ \textbf{then} \cr 4 & \qquad ( a , b ) \gets \mathrm{split}( t.\mathit{left} , i ); \ t.\mathit{left} \gets b;\ \textbf{return}\ ( a , t ) \cr 5 & \quad ( a , b ) \gets \mathrm{split}( t.\mathit{right} , i - s( t.\mathit{left} ) - 1 );\ t.\mathit{right} \gets a;\ \textbf{return} \ ( t , b ) \end{array}} \]

Precondition
\( i \le n \)

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