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

a sequence with access in \( O( 1 ) \) and insertion and removal anywhere in \( O( \sqrt{n} ) \), in circular blocks of \( b \) items More...

#include <ADS/sequence/TieredVector.h>

Classes

class  Iterator
 the iterator by index, random access, on items const if Const More...

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 to an item
using allocator_type = A
 the type of the allocator
using iterator = Iterator< false >
 the iterator on the items
using const_iterator = Iterator< true >
 the iterator on the constant items

Public member functions

 TieredVector (void)=default
 the empty vector
 TieredVector (const A &alloc)
 the empty sequence, its blocks from alloc
allocator_type get_allocator (void) const
 the allocator of the blocks
bool empty (void) const
 true if there is no item
std::size_t size (void) const
 the number of items, \( n \)
std::size_t block (void) const
 the places \( b \) of a block
std::size_t capacity (void) const
 the places of the blocks, \( n + O( \sqrt{n} ) \)
T & operator[] (std::size_t i)
 the item at the index \( i \), at the place \( ( \mathit{off} + g + i ) \bmod b \) of the block \( \lfloor ( g + i ) / b \rfloor \), \( \mathit{off} \) the offset of the block
const T & operator[] (std::size_t i) const
 the item at the index \( 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)
 \( x \) at the index \( i \), the items from \( i \) one place on, or those before \( i \) one place back if \( i < n / 2 \), in \( O( 1 + \min\{ i , \sqrt{n} , n - i \} ) \) amortized time
void erase (std::size_t i)
 removes the item at the index \( i \), the items after it one place back, or those before it one place on if \( i < n / 2 \), in \( O( 1 + \min\{ i , \sqrt{n} , n - i \} ) \) amortized time
void push_back (const T &x)
 \( x \) at the end, insert( n , x )
void pop_back (void)
 removes the last item, erase( n - 1 )
void push_front (const T &x)
 \( x \) at the front, insert( 0 , x ), \( O( 1 ) \) amortized
void pop_front (void)
 removes the first item, erase( 0 ), \( O( 1 ) \) amortized
void clear (void)
 removes the items
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
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

Detailed description

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

a sequence with access in \( O( 1 ) \) and insertion and removal anywhere in \( O( \sqrt{n} ) \), in circular blocks of \( b \) items

Member typedef documentation

◆ allocator_type

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

the type of the allocator

◆ const_iterator

template<class T, class A = std::allocator< T >>
using ADS::TieredVector< T, A >::const_iterator = Iterator< true >

the iterator on the constant items

◆ const_reference

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

a constant reference to an item

◆ difference_type

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

the type of the distances

◆ iterator

template<class T, class A = std::allocator< T >>
using ADS::TieredVector< T, A >::iterator = Iterator< false >

the iterator on the items

◆ reference

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

a reference to an item

◆ size_type

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

the type of the sizes

◆ value_type

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

the type of the items

Constructor and destructor documentation

◆ TieredVector() [1/2]

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

the empty vector

◆ TieredVector() [2/2]

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

the empty sequence, its blocks from alloc

Member function documentation

◆ back()

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

the last item

Precondition
the vector is not empty

◆ begin() [1/2]

template<class T, class A = std::allocator< T >>
iterator ADS::TieredVector< 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::TieredVector< 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::TieredVector< T, A >::block ( void ) const
inlinenodiscard

the places \( b \) of a block

◆ capacity()

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

the places of the blocks, \( n + O( \sqrt{n} ) \)

◆ cbegin()

template<class T, class A = std::allocator< T >>
const_iterator ADS::TieredVector< 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::TieredVector< T, A >::cend ( void ) const
inlinenodiscard

the constant iterator past the last item

◆ clear()

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

removes the items

◆ emplace()

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

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

◆ empty()

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

removes the item at the index \( i \), the items after it one place back, or those before it one place on if \( i < n / 2 \), in \( O( 1 + \min\{ i , \sqrt{n} , n - i \} ) \) amortized time

In pseudocode (Goodrich and Kloss, beyond the book), the converse of insert() with \( g = 0 \):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{erase}( i ) \cr 2 & \quad k \gets \lfloor i / b \rfloor;\ L \gets \lfloor ( n - 1 ) / b \rfloor \cr 3 & \quad \text{move the items of } B_k \text{ after the place } i \bmod b \text{ one place back} \cr 4 & \quad \textbf{for}\ j \gets k + 1\ \textbf{to}\ L\ \textbf{do}\quad \triangleright\ \text{the first item of } B_j \text{ to the back } \text{of } B_{j-1} \cr 5 & \qquad B_{j-1}[ B_{j-1}.\mathit{off} + b - 1 ] \gets B_j[ B_j.\mathit{off} ];\ B_j.\mathit{off} \gets B_j.\mathit{off} + 1 \cr 6 & \quad n \gets n - 1 \cr 7 & \quad \textbf{if}\ \text{the last block is empty}\ \textbf{then} \ \text{remove it} \cr 8 & \quad \textbf{if}\ b > 64\ \textbf{and}\ 4 n < b^2\ \textbf{then} \ \mathrm{rebuild}( n ) \end{array}} \]

Precondition
\( i < n \)

◆ front()

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

the first item

Precondition
the vector is not empty

◆ get_allocator()

template<class T, class A = std::allocator< T >>
allocator_type ADS::TieredVector< 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::TieredVector< 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::TieredVector< 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::TieredVector< T, A >::insert ( std::size_t i,
const T & x )

\( x \) at the index \( i \), the items from \( i \) one place on, or those before \( i \) one place back if \( i < n / 2 \), in \( O( 1 + \min\{ i , \sqrt{n} , n - i \} ) \) amortized time

In pseudocode (Goodrich and Kloss, beyond the book), \( B_j \) the blocks, \( B_j.\mathit{off} \) their offsets, the places of a block taken modulo \( b \) and the gap \( g = 0 \) (the shift toward the front is the mirror, the first item of each block to the back of the one before, with the gap in place of the free places of the last block):

\[\class{pseudocode}{\begin{array}{r@{\quad}l} 1 & \textbf{procedure}\ \mathrm{insert}( i , x ) \cr 2 & \quad \textbf{if}\ n + 1 > 4 b^2\ \textbf{then}\ \mathrm{rebuild}( n + 1 )\quad \triangleright\ b \text{ the least power of 2 with } b^2 \ge n + 1 \cr 3 & \quad \textbf{if}\ \text{all the blocks are full}\ \textbf{then} \ \text{add an empty block at the end} \cr 4 & \quad k \gets \lfloor i / b \rfloor;\ L \gets \lfloor n / b \rfloor \cr 5 & \quad \textbf{for}\ j \gets L\ \textbf{downto}\ k + 1\ \textbf{do} \quad \triangleright\ \text{the last item of } B_{j-1} \text{ to the } \text{front of } B_j \cr 6 & \qquad B_j.\mathit{off} \gets B_j.\mathit{off} - 1;\ B_j[ B_j.\mathit{off} ] \gets B_{j-1}[ B_{j-1}.\mathit{off} + b - 1 ] \cr 7 & \quad \text{move the items of } B_k \text{ from the place } i \bmod b \text{ one place on} \cr 8 & \quad B_k[ B_k.\mathit{off} + i \bmod b ] \gets x;\ n \gets n + 1 \end{array}} \]

Precondition
\( i \le n \)

◆ operator[]() [1/2]

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

the item at the index \( i \), at the place \( ( \mathit{off} + g + i ) \bmod b \) of the block \( \lfloor ( g + i ) / b \rfloor \), \( \mathit{off} \) the offset of the block

Precondition
\( i < n \)

◆ operator[]() [2/2]

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

the item at the index \( i \), constant

Precondition
\( i < n \)

◆ pop_back()

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

removes the last item, erase( n - 1 )

Precondition
the vector is not empty

◆ pop_front()

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

removes the first item, erase( 0 ), \( O( 1 ) \) amortized

Precondition
the vector is not empty

◆ push_back()

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

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

◆ push_front()

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

\( x \) at the front, insert( 0 , x ), \( O( 1 ) \) amortized

◆ size()

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

the number of items, \( n \)


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