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

a LIST by rank on blocks of \( 1 , 2 , \ldots , r \) places, with \( O( \sqrt{n} ) \) places wasted More...

#include <ADS/sequence/RootishArrayStack.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< RootishArrayStack , T , false >
 the iterator on the items
using const_iterator = RankIterator< RootishArrayStack , T , true >
 the iterator on the constant items
using allocator_type = A
 the type of the allocator

Public member functions

allocator_type get_allocator (void) const
 the allocator of the blocks
 RootishArrayStack (void)=default
 the empty list, with no block
 RootishArrayStack (const A &alloc)
 the empty sequence, its blocks from alloc
 RootishArrayStack (std::initializer_list< T > l)
 the items of l, in order
 RootishArrayStack (const RootishArrayStack &o)
 a copy of o, on the allocator that select_on_container_copy_construction() gives
 RootishArrayStack (const RootishArrayStack &o, const A &alloc)
 a copy of o, on alloc
 RootishArrayStack (RootishArrayStack &&o) noexcept
 the blocks of o, which is left empty
 RootishArrayStack (RootishArrayStack &&o, const A &alloc)
 the items of o, which is left empty, on alloc: its blocks if the allocators are equal, else its items moved one by one
RootishArrayStack & operator= (RootishArrayStack o) noexcept
 the items of o, by copy and swap, when the allocators are all equal
RootishArrayStack & operator= (const RootishArrayStack &o)
 the copy assignment, with the allocator of o if it propagates
RootishArrayStack & operator= (RootishArrayStack &&o)
 the move assignment: the memory of o if its allocator propagates or is equal, else its items moved one by one
 ~RootishArrayStack ()
 destroys the items and frees the blocks
void swap (RootishArrayStack &o) noexcept(detail::swapNoexcept< A >)
 exchanges the blocks with those of o, and the allocators if they propagate on a swap
bool empty (void) const
 true if there is no item
std::size_t size (void) const
 the number \( n \) of items
std::size_t capacity (void) const
 the places of the \( r \) blocks, \( r ( r + 1 ) / 2 \)
std::size_t blocks (void) const
 the number \( r \) of blocks
T & operator[] (std::size_t i)
 the item of rank \( i \), in the block \( b( i ) \) of (1)
const T & operator[] (std::size_t i) const
 the item of rank \( i \), constant
const T & front (void) const
 the first item
const T & back (void) const
 the last item
void insert (std::size_t i, const T &x)
 puts \( x \) at the rank \( i \), the items from \( i \) one rank on, in \( O( 1 + n - i ) \) amortized time
void erase (std::size_t i)
 removes the item of rank \( i \), those after it one rank back, in \( O( 1 + n - i ) \) 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... Args>
iterator emplace (const_iterator p, Args &&... 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, insert( size() , x )
void push_back (T &&x)
 \( x \) at the end, moved
template<class... Args>
T & emplace_back (Args &&... args)
 the item built from args at the end, \( O( 1 ) \) amortized
void pop_back (void)
 removes the last item
void clear (void)
 removes all the items and frees the blocks
bool operator== (const RootishArrayStack &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

Static public member functions

static std::size_t block (std::size_t i)
 the block of the rank \( i \), (1)

Detailed description

template<class T, class A = std::allocator< T >>
class ADS::RootishArrayStack< T, A >

a LIST by rank on blocks of \( 1 , 2 , \ldots , r \) places, with \( O( \sqrt{n} ) \) places wasted

Member typedef documentation

◆ allocator_type

template<class T, class A = std::allocator< T >>
using ADS::RootishArrayStack< T, A >::allocator_type = A

the type of the allocator

◆ const_iterator

template<class T, class A = std::allocator< T >>
using ADS::RootishArrayStack< T, A >::const_iterator = RankIterator< RootishArrayStack , T , true >

the iterator on the constant items

◆ const_reference

template<class T, class A = std::allocator< T >>
using ADS::RootishArrayStack< T, A >::const_reference = const T &

a constant reference

◆ difference_type

template<class T, class A = std::allocator< T >>
using ADS::RootishArrayStack< T, A >::difference_type = std::ptrdiff_t

the type of the distances

◆ iterator

template<class T, class A = std::allocator< T >>
using ADS::RootishArrayStack< T, A >::iterator = RankIterator< RootishArrayStack , T , false >

the iterator on the items

◆ reference

template<class T, class A = std::allocator< T >>
using ADS::RootishArrayStack< T, A >::reference = T &

a reference to an item

◆ size_type

template<class T, class A = std::allocator< T >>
using ADS::RootishArrayStack< T, A >::size_type = std::size_t

the type of the sizes

◆ value_type

template<class T, class A = std::allocator< T >>
using ADS::RootishArrayStack< T, A >::value_type = T

the type of the items

Constructor and destructor documentation

◆ RootishArrayStack() [1/7]

template<class T, class A = std::allocator< T >>
ADS::RootishArrayStack< T, A >::RootishArrayStack ( void )
default

the empty list, with no block

◆ RootishArrayStack() [2/7]

template<class T, class A = std::allocator< T >>
ADS::RootishArrayStack< T, A >::RootishArrayStack ( const A & alloc)
inlineexplicit

the empty sequence, its blocks from alloc

◆ RootishArrayStack() [3/7]

template<class T, class A = std::allocator< T >>
ADS::RootishArrayStack< T, A >::RootishArrayStack ( std::initializer_list< T > l)

the items of l, in order

◆ RootishArrayStack() [4/7]

template<class T, class A = std::allocator< T >>
ADS::RootishArrayStack< T, A >::RootishArrayStack ( const RootishArrayStack< T, A > & o)

a copy of o, on the allocator that select_on_container_copy_construction() gives

◆ RootishArrayStack() [5/7]

template<class T, class A = std::allocator< T >>
ADS::RootishArrayStack< T, A >::RootishArrayStack ( const RootishArrayStack< T, A > & o,
const A & alloc )

a copy of o, on alloc

◆ RootishArrayStack() [6/7]

template<class T, class A = std::allocator< T >>
ADS::RootishArrayStack< T, A >::RootishArrayStack ( RootishArrayStack< T, A > && o)
noexcept

the blocks of o, which is left empty

◆ RootishArrayStack() [7/7]

template<class T, class A = std::allocator< T >>
ADS::RootishArrayStack< T, A >::RootishArrayStack ( RootishArrayStack< T, A > && o,
const A & alloc )

the items of o, which is left empty, on alloc: its blocks if the allocators are equal, else its items moved one by one

◆ ~RootishArrayStack()

template<class T, class A = std::allocator< T >>
ADS::RootishArrayStack< T, A >::~RootishArrayStack ( )

destroys the items and frees the blocks

Member function documentation

◆ back()

template<class T, class A = std::allocator< T >>
const T & ADS::RootishArrayStack< T, A >::back ( void ) const
nodiscard

the last item

Precondition
the list is not empty

◆ begin() [1/2]

template<class T, class A = std::allocator< T >>
iterator ADS::RootishArrayStack< T, A >::begin ( void )
inlinenodiscard

the iterator on the first item

◆ begin() [2/2]

template<class T, class A = std::allocator< T >>
const_iterator ADS::RootishArrayStack< T, A >::begin ( void ) const
inlinenodiscard

the constant iterator on the first item

◆ block()

template<class T, class A = std::allocator< T >>
std::size_t ADS::RootishArrayStack< T, A >::block ( std::size_t i)
staticnodiscard

the block of the rank \( i \), (1)

In pseudocode, the root in floating point corrected by two loops:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{function}\ \mathrm{block}( i ) \cr 2 & \quad b \gets \lceil ( -3 + \sqrt{ 9 + 8 i } ) / 2 \rceil \quad \triangleright\ \text{in } \mathtt{double} \cr 3 & \quad \textbf{while}\ b > 0\ \textbf{and}\ b ( b + 1 ) / 2 > i \ \textbf{do}\ b \gets b - 1 \cr 4 & \quad \textbf{while}\ ( b + 1 ) ( b + 2 ) / 2 \le i \ \textbf{do}\ b \gets b + 1 \cr 5 & \quad \textbf{return}\ b \end{array}} \]

◆ blocks()

template<class T, class A = std::allocator< T >>
std::size_t ADS::RootishArrayStack< T, A >::blocks ( void ) const
inlinenodiscard

the number \( r \) of blocks

◆ capacity()

template<class T, class A = std::allocator< T >>
std::size_t ADS::RootishArrayStack< T, A >::capacity ( void ) const
inlinenodiscard

the places of the \( r \) blocks, \( r ( r + 1 ) / 2 \)

◆ cbegin()

template<class T, class A = std::allocator< T >>
const_iterator ADS::RootishArrayStack< T, A >::cbegin ( void ) const
inlinenodiscard

the constant iterator on the first item

◆ cend()

template<class T, class A = std::allocator< T >>
const_iterator ADS::RootishArrayStack< T, A >::cend ( void ) const
inlinenodiscard

the constant iterator past the last item

◆ clear()

template<class T, class A = std::allocator< T >>
void ADS::RootishArrayStack< T, A >::clear ( void )

removes all the items and frees the blocks

◆ emplace()

template<class T, class A = std::allocator< T >>
template<class... Args>
iterator ADS::RootishArrayStack< T, A >::emplace ( const_iterator p,
Args &&... a )
inline

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

◆ emplace_back()

template<class T, class A = std::allocator< T >>
template<class... Args>
T & ADS::RootishArrayStack< T, A >::emplace_back ( Args &&... args)

the item built from args at the end, \( O( 1 ) \) amortized

◆ empty()

template<class T, class A = std::allocator< T >>
bool ADS::RootishArrayStack< T, A >::empty ( void ) const
inlinenodiscard

true if there is no item

◆ end() [1/2]

template<class T, class A = std::allocator< T >>
iterator ADS::RootishArrayStack< T, A >::end ( void )
inlinenodiscard

the iterator past the last item

◆ end() [2/2]

template<class T, class A = std::allocator< T >>
const_iterator ADS::RootishArrayStack< T, A >::end ( void ) const
inlinenodiscard

the constant iterator past the last item

◆ erase() [1/3]

template<class T, class A = std::allocator< T >>
iterator ADS::RootishArrayStack< T, A >::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, class A = std::allocator< T >>
iterator ADS::RootishArrayStack< T, A >::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, class A = std::allocator< T >>
void ADS::RootishArrayStack< T, A >::erase ( std::size_t i)

removes the item of rank \( i \), those after it one rank back, in \( O( 1 + n - i ) \) amortized time

In pseudocode (Morin, section 2.6):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{erase}( i ) \cr 2 & \quad \textbf{for}\ k \gets i\ \textbf{to}\ n - 2\ \textbf{do} \ \mathrm{set}( k , \mathrm{get}( k + 1 ) ) \cr 3 & \quad n \gets n - 1 \cr 4 & \quad \textbf{while}\ r > 0\ \textbf{and}\ ( r - 2 ) ( r - 1 ) / 2 \ge n\ \textbf{do}\ \text{free } B_{r-1};\ r \gets r - 1 \end{array}} \]

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

◆ front()

template<class T, class A = std::allocator< T >>
const T & ADS::RootishArrayStack< T, A >::front ( void ) const
nodiscard

the first item

Precondition
the list is not empty

◆ get_allocator()

template<class T, class A = std::allocator< T >>
allocator_type ADS::RootishArrayStack< T, A >::get_allocator ( void ) const
inlinenodiscard

the allocator of the blocks

◆ insert() [1/3]

template<class T, class A = std::allocator< T >>
iterator ADS::RootishArrayStack< T, A >::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, class A = std::allocator< T >>
template<std::input_iterator I, std::sentinel_for< I > E>
iterator ADS::RootishArrayStack< T, A >::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, class A = std::allocator< T >>
void ADS::RootishArrayStack< T, A >::insert ( std::size_t i,
const T & x )

puts \( x \) at the rank \( i \), the items from \( i \) one rank on, in \( O( 1 + n - i ) \) amortized time

In pseudocode (Morin, section 2.6), \( B \) the blocks:

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{insert}( i , x ) \cr 2 & \quad \textbf{if}\ r ( r + 1 ) / 2 = n\ \textbf{then} \ \text{a new block } B_r \text{ of } r + 1 \text{ places}; \ r \gets r + 1 \cr 3 & \quad \textbf{for}\ k \gets n\ \textbf{downto}\ i + 1\ \textbf{do} \ \mathrm{set}( k , \mathrm{get}( k - 1 ) ) \cr 4 & \quad \mathrm{set}( i , x );\ n \gets n + 1 \end{array}} \]

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

◆ operator=() [1/3]

template<class T, class A = std::allocator< T >>
RootishArrayStack & ADS::RootishArrayStack< T, A >::operator= ( const RootishArrayStack< T, A > & o)
inline

the copy assignment, with the allocator of o if it propagates

◆ operator=() [2/3]

template<class T, class A = std::allocator< T >>
RootishArrayStack & ADS::RootishArrayStack< T, A >::operator= ( RootishArrayStack< T, A > && o)
inline

the move assignment: the memory of o if its allocator propagates or is equal, else its items moved one by one

◆ operator=() [3/3]

template<class T, class A = std::allocator< T >>
RootishArrayStack & ADS::RootishArrayStack< T, A >::operator= ( RootishArrayStack< T, A > o)
noexcept

the items of o, by copy and swap, when the allocators are all equal

◆ operator==()

template<class T, class A = std::allocator< T >>
bool ADS::RootishArrayStack< T, A >::operator== ( const RootishArrayStack< T, A > & o) const
nodiscard

the same items in the same order

◆ operator[]() [1/2]

template<class T, class A = std::allocator< T >>
T & ADS::RootishArrayStack< T, A >::operator[] ( std::size_t i)
nodiscard

the item of rank \( i \), in the block \( b( i ) \) of (1)

Precondition
\( i < n \)

◆ operator[]() [2/2]

template<class T, class A = std::allocator< T >>
const T & ADS::RootishArrayStack< T, A >::operator[] ( std::size_t i) const
nodiscard

the item of rank \( i \), constant

Precondition
\( i < n \)

◆ pop_back()

template<class T, class A = std::allocator< T >>
void ADS::RootishArrayStack< T, A >::pop_back ( void )
inline

removes the last item

Precondition
the list is not empty

◆ push_back() [1/2]

template<class T, class A = std::allocator< T >>
void ADS::RootishArrayStack< T, A >::push_back ( const T & x)
inline

\( x \) at the end, insert( size() , x )

◆ push_back() [2/2]

template<class T, class A = std::allocator< T >>
void ADS::RootishArrayStack< T, A >::push_back ( T && x)
inline

\( x \) at the end, moved

◆ size()

template<class T, class A = std::allocator< T >>
std::size_t ADS::RootishArrayStack< T, A >::size ( void ) const
inlinenodiscard

the number \( n \) of items

◆ swap()

template<class T, class A = std::allocator< T >>
void ADS::RootishArrayStack< T, A >::swap ( RootishArrayStack< T, A > & o)
noexcept

exchanges the blocks with those of o, and the allocators if they propagate on a swap

Precondition
the allocators are equal, if they do not propagate

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