a sequence with access in \( O( 1 ) \) and insertion and removal anywhere in \( O( \sqrt{n} ) \), in circular blocks of \( b \) items
More...
|
| | 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
|
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
template<class T, class A = std::allocator< T >>
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 \)
template<class T, class A = std::allocator< T >>
\( 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 \)