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

a LIST by rank in three levels, \( O( 1 ) \) access and \( O( n^{1/3} ) \) amortized insertions and removals More...

#include <ADS/sequence/CubishArrayStack.h>

Public types

using value_type = T
 the type of 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 = T &
 a reference to an item
using const_reference = const T &
 a constant reference
using iterator = RankIterator< CubishArrayStack , T , false >
 the iterator on the items
using const_iterator = RankIterator< CubishArrayStack , T , true >
 the iterator on the constant items

Public member functions

 CubishArrayStack (void)=default
 the empty list
 CubishArrayStack (std::initializer_list< T > l)
 the items of l, in order
 CubishArrayStack (const CubishArrayStack &o)
 a copy of o
 CubishArrayStack (CubishArrayStack &&o) noexcept
 the blocks of o, which is left empty
CubishArrayStack & operator= (CubishArrayStack o) noexcept
 the items of o, by copy and swap
void swap (CubishArrayStack &o) noexcept
 exchanges the blocks with those of o
bool empty (void) const
 true if there is no item
std::size_t size (void) const
 the number \( n \) of items
std::size_t block (void) const
 the places \( b \) of a leaf
std::size_t capacity (void) const
 the places of the leaves, those kept for later included
T & operator[] (std::size_t i)
 the item of rank \( i \)
const T & operator[] (std::size_t i) const
 the item of rank \( i \), constant
void insert (std::size_t i, const T &x)
 puts \( x \) at the rank \( i \), in \( O( n^{1/3} ) \) amortized time
void erase (std::size_t i)
 removes the item of rank \( i \), in \( O( n^{1/3} ) \) amortized time
iterator insert (const_iterator p, const T &x)
 \( x \) before the item of \( p \), i.e., insert( i , x ) with \( i \) the rank of \( p \): the members by iterator of a SequenceContainer of the standard library, by which std::flat_set and std::flat_map keep their keys here; an iterator on \( x \)
template<std::input_iterator I, std::sentinel_for< I > E>
iterator insert (const_iterator p, I first, E last)
 the items of \( [ \mathit{first} , \mathit{last} ) \) before the item of \( p \), in their order, one insert( i , x ) each; an iterator on the first of them
template<class... A>
iterator emplace (const_iterator p, A &&... a)
 an item built from \( a \) before the item of \( p \)
iterator erase (const_iterator p)
 the item of \( p \) removed, i.e., erase( i ) with \( i \) its rank; an iterator on the item after it
iterator erase (const_iterator first, const_iterator last)
 the items of \( [ \mathit{first} , \mathit{last} ) \) removed, one erase( i ) each; an iterator on the item after them
void push_back (const T &x)
 \( x \) at the end, \( O( 1 ) \) amortized
void pop_back (void)
 removes the last item
void clear (void)
 removes all the items and frees the leaves
bool operator== (const CubishArrayStack &o) const
 the same items in the same order
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
 the constant iterator on the first item
const_iterator cend (void) const
 the constant iterator past the last item

Detailed description

template<class T>
class ADS::CubishArrayStack< T >

a LIST by rank in three levels, \( O( 1 ) \) access and \( O( n^{1/3} ) \) amortized insertions and removals

Member typedef documentation

◆ const_iterator

template<class T>
using ADS::CubishArrayStack< T >::const_iterator = RankIterator< CubishArrayStack , T , true >

the iterator on the constant items

◆ const_reference

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

a constant reference

◆ difference_type

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

the type of the distances

◆ iterator

template<class T>
using ADS::CubishArrayStack< T >::iterator = RankIterator< CubishArrayStack , T , false >

the iterator on the items

◆ reference

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

a reference to an item

◆ size_type

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

the type of the sizes

◆ value_type

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

the type of the items

Constructor and destructor documentation

◆ CubishArrayStack() [1/4]

template<class T>
ADS::CubishArrayStack< T >::CubishArrayStack ( void )
default

the empty list

◆ CubishArrayStack() [2/4]

template<class T>
ADS::CubishArrayStack< T >::CubishArrayStack ( std::initializer_list< T > l)

the items of l, in order

◆ CubishArrayStack() [3/4]

template<class T>
ADS::CubishArrayStack< T >::CubishArrayStack ( const CubishArrayStack< T > & o)

a copy of o

◆ CubishArrayStack() [4/4]

template<class T>
ADS::CubishArrayStack< T >::CubishArrayStack ( CubishArrayStack< T > && o)
noexcept

the blocks of o, which is left empty

Member function documentation

◆ begin() [1/2]

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

the iterator on the first item

◆ begin() [2/2]

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

the constant iterator on the first item

◆ block()

template<class T>
std::size_t ADS::CubishArrayStack< T >::block ( void ) const
inlinenodiscard

the places \( b \) of a leaf

◆ capacity()

template<class T>
std::size_t ADS::CubishArrayStack< T >::capacity ( void ) const
nodiscard

the places of the leaves, those kept for later included

◆ cbegin()

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

the constant iterator on the first item

◆ cend()

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

the constant iterator past the last item

◆ clear()

template<class T>
void ADS::CubishArrayStack< T >::clear ( void )

removes all the items and frees the leaves

◆ emplace()

template<class T>
template<class... A>
iterator ADS::CubishArrayStack< T >::emplace ( const_iterator p,
A &&... a )
inline

an item built from \( a \) before the item of \( p \)

◆ empty()

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

true if there is no item

◆ end() [1/2]

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

the iterator past the last item

◆ end() [2/2]

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

the constant iterator past the last item

◆ erase() [1/3]

template<class T>
iterator ADS::CubishArrayStack< T >::erase ( const_iterator first,
const_iterator last )
inline

the items of \( [ \mathit{first} , \mathit{last} ) \) removed, one erase( i ) each; an iterator on the item after them

◆ erase() [2/3]

template<class T>
iterator ADS::CubishArrayStack< T >::erase ( const_iterator p)
inline

the item of \( p \) removed, i.e., erase( i ) with \( i \) its rank; an iterator on the item after it

Precondition
\( p \) is an iterator on an item of this sequence

◆ erase() [3/3]

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

removes the item of rank \( i \), in \( O( n^{1/3} ) \) amortized time

In pseudocode (Morin, exercise 2.12):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{erase}( i ) \cr 2 & \quad k \gets \lfloor i / b^2 \rfloor;\ M_k.\mathrm{erase}( i \bmod b^2 ) \cr 3 & \quad \textbf{for}\ j \gets k + 1\ \textbf{to}\ t\ \textbf{do} \ M_{j-1}.\mathrm{pushBack}( M_j.\mathrm{popFront}() ) \cr 4 & \quad \textbf{if}\ M_t = \emptyset\ \textbf{then}\ \text{remove } M_t \cr 5 & \quad n \gets n - 1;\ \textbf{if}\ b > 16\ \textbf{and}\ 8 n < b^3 \ \textbf{then}\ \mathrm{rebuild}( n ) \end{array}} \]

Precondition
\( i < n \)
Postcondition
one item less

◆ insert() [1/3]

template<class T>
iterator ADS::CubishArrayStack< T >::insert ( const_iterator p,
const T & x )
inline

\( x \) before the item of \( p \), i.e., insert( i , x ) with \( i \) the rank of \( p \): the members by iterator of a SequenceContainer of the standard library, by which std::flat_set and std::flat_map keep their keys here; an iterator on \( x \)

Precondition
\( p \) is an iterator of this sequence or its end

◆ insert() [2/3]

template<class T>
template<std::input_iterator I, std::sentinel_for< I > E>
iterator ADS::CubishArrayStack< T >::insert ( const_iterator p,
I first,
E last )
inline

the items of \( [ \mathit{first} , \mathit{last} ) \) before the item of \( p \), in their order, one insert( i , x ) each; an iterator on the first of them

◆ insert() [3/3]

template<class T>
void ADS::CubishArrayStack< T >::insert ( std::size_t i,
const T & x )

puts \( x \) at the rank \( i \), in \( O( n^{1/3} ) \) amortized time

In pseudocode (Morin, exercise 2.12), \( M_0 , \ldots , M_t \) the middle blocks:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{insert}( i , x ) \cr 2 & \quad \textbf{if}\ n + 1 > 8 b^3\ \textbf{then}\ \mathrm{rebuild}( n + 1 ) \cr 3 & \quad \textbf{if}\ |M_t| = b^2\ \textbf{then} \ \text{a new empty middle block } M_{t+1};\ t \gets t + 1 \cr 4 & \quad k \gets \lfloor i / b^2 \rfloor \cr 5 & \quad \textbf{for}\ j \gets t\ \textbf{downto}\ k + 1\ \textbf{do} \ M_j.\mathrm{pushFront}( M_{j-1}.\mathrm{popBack}() ) \cr 6 & \quad M_k.\mathrm{insert}( i \bmod b^2 , x )\quad \triangleright \ \text{as } \mathrm{insert}() \text{ of a tiered vector} \cr 7 & \quad n \gets n + 1 \end{array}} \]

Precondition
\( i \le n \)
Postcondition
one item more, \( x \) at the rank \( i \)

◆ operator=()

template<class T>
CubishArrayStack & ADS::CubishArrayStack< T >::operator= ( CubishArrayStack< T > o)
noexcept

the items of o, by copy and swap

◆ operator==()

template<class T>
bool ADS::CubishArrayStack< T >::operator== ( const CubishArrayStack< T > & o) const
nodiscard

the same items in the same order

◆ operator[]() [1/2]

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

the item of rank \( i \)

Precondition
\( i < n \)

◆ operator[]() [2/2]

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

the item of rank \( i \), constant

Precondition
\( i < n \)

◆ pop_back()

template<class T>
void ADS::CubishArrayStack< T >::pop_back ( void )
inline

removes the last item

Precondition
the list is not empty

◆ push_back()

template<class T>
void ADS::CubishArrayStack< T >::push_back ( const T & x)
inline

\( x \) at the end, \( O( 1 ) \) amortized

◆ size()

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

the number \( n \) of items

◆ swap()

template<class T>
void ADS::CubishArrayStack< T >::swap ( CubishArrayStack< T > & o)
noexcept

exchanges the blocks with those of o


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