a sequence of items of type T on an implicit treap, the TreapList of Morin
More...
#include <ADS/sequence/TreapList.h>
|
| | 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()
|
template<class T>
class ADS::TreapList< T >
a sequence of items of type T on an implicit treap, the TreapList of Morin
◆ const_iterator
the constant iterator in the order of the sequence
◆ const_reference
◆ difference_type
the type of the distances
◆ Item
◆ iterator
◆ reference
◆ size_type
◆ value_type
◆ 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()]
◆ absorb()
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()
\( 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()
the iterator on the first item
◆ cbegin()
◆ cend()
◆ empty()
◆ end()
the iterator past the last item
◆ erase()
the item of index \( i \) removed, remove( i ) (the name of IndexedList)
- Precondition
- \( i < n \)
◆ get()
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()
\( v \) inserted at \( i \), add( i , v ) (the name of IndexedList)
- Precondition
- \( i \le n \)
◆ operator[]() [1/2]
the item of index \( i \), to be read or written, by the search of get()
- Precondition
- \( i < n \)
◆ operator[]() [2/2]
the item of index \( i \), as get( i ) (the name of IndexedList)
- Precondition
- \( i < n \)
◆ push_back()
\( v \) appended, add( size() , v )
◆ remove()
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()
the item of index \( i \) replaced by \( v \), by the descent of get()
- Precondition
- \( i < n \)
◆ size()
the number \( n \) of items
◆ truncate()
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: