|
ADS
Algorithms and Data Structures
|
The tiered vector of Goodrich and Kloss, beyond the book: a sequence with access by index in \( O( 1 ) \) time, as a vector, and insertion and removal at any index in \( O( \sqrt{n} ) \) time, where a vector [see ArraySequence.h] moves \( O( n ) \) items. More...
#include "ADS/Contract.h"#include "ADS/memory/AllocatorAware.h"#include <bit>#include <cstddef>#include <iterator>#include <memory>#include <utility>#include <vector>#include "ADS/sequence/impl/TieredVector.tpp"Go to the source code of this file.
Classes | |
| 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 More... | |
| class | ADS::TieredVector< T, A >::Iterator< Const > |
| the iterator by index, random access, on items const if Const More... | |
Namespaces | |
| namespace | ADS |
The tiered vector of Goodrich and Kloss, beyond the book: a sequence with access by index in \( O( 1 ) \) time, as a vector, and insertion and removal at any index in \( O( \sqrt{n} ) \) time, where a vector [see ArraySequence.h] moves \( O( n ) \) items.
The items are in blocks of \( b \) items, \( b \) a power of 2, each a circular buffer with the offset of its first item, all full but the first and the last one, the first with \( g < b \) places free at its front; the item of index \( i \) is the item \( ( g + i ) \bmod b \) of the block \( \lfloor ( g + i ) / b \rfloor \), an addition, a shift, a mask and two reads. An insertion at \( i \) moves the items of its block after \( i \) by one place, at most \( b - 1 \) of them, and then the last item of each following block to the front of the next one, \( O( 1 ) \) per block since a circular buffer grows at the front by moving its offset: \( O( b + n / b ) \) moves. A removal is the symmetric. \( b \) follows the size, the least power of 2 not below \( \sqrt{n} \) and 64 at least: when \( n \) leaves \( [ b^2 / 4 , 4 b^2 ] \) the items are copied into the blocks of the new \( b \), in \( O( n ) \) time once every \( \Omega( n ) \) operations, so that an insertion or a removal costs \( O( \sqrt{n} ) \) amortized and the space is \( n + O( \sqrt{n} ) \) items.
An insertion or a removal at \( i < n / 2 \) moves the items before \( i \) instead, toward the front: the items of its block before \( i \), and the first item of each block before it to the back of the block that precedes it, which costs \( O( 1 ) \) per block as above; the first block gives or takes a place of its gap \( g \), and a block is added at the front when \( g = 0 \) (removed when \( g = b \)), once every \( b \) insertions at most. Hence an operation at \( i \) moves \( O( 1 + \min\{ i , b , n - i \} + \min\{ i , n - i \} / b ) \) items, i.e., \( O( 1 + \min\{ i , \sqrt{n} , n - i \} ) \) amortized, which is the bound of exercise 2.11 of Morin's book (that of exercise 2.10, \( O( 1 + \min\{ \sqrt{n} , n - i \} ) \), holds already for the shifts toward the back), with \( O( \sqrt{n} ) \) places wasted: the gap, the free places of the last block and the directory of the blocks.
The blocks and the vector of the blocks come from the allocator A, std::allocator by default (the blocks from std::make_unique), with the rules of the standard [see AllocatorAware.h].
The contracts [see Contract.h]: an index is below size() (at most size() for an insertion), and pop_back() needs a vector that is not empty, as pop_front(); the invariant is that the blocks but the first and the last are full, that the gap is below \( b \) and that they count size() items. The operations are defined in TieredVector.tpp.