ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
CubishArrayStack.h File Reference

The cubish array stack of exercise 2.12 of Morin's book, beyond the book: a LIST by rank in three levels, whose operator[] takes \( O( 1 ) \) time and whose insertions and removals take \( O( n^{1/3} ) \) amortized time, with \( O( n^{2/3} ) \) places wasted. More...

#include "ADS/Contract.h"
#include "ADS/Iterators.h"
#include <bit>
#include <cstddef>
#include <initializer_list>
#include <iterator>
#include <memory>
#include <utility>
#include <vector>
#include "ADS/sequence/impl/CubishArrayStack.tpp"

Go to the source code of this file.

Classes

class  ADS::CubishArrayStack< T >
 a LIST by rank in three levels, \( O( 1 ) \) access and \( O( n^{1/3} ) \) amortized insertions and removals More...

Namespaces

namespace  ADS

Detailed description

The cubish array stack of exercise 2.12 of Morin's book, beyond the book: a LIST by rank in three levels, whose operator[] takes \( O( 1 ) \) time and whose insertions and removals take \( O( n^{1/3} ) \) amortized time, with \( O( n^{2/3} ) \) places wasted.

It is the tiered vector of Goodrich and Kloss with three levels [see TieredVector.h for the one with two]: \( b \) a power of 2 with \( b^3 / 8 \le n \le 8 b^3 \), the items are in leaves of \( b \) places, each a circular buffer with the offset of its first item; the leaves are in middle blocks of \( b^2 \) items each, all full but the last, and a middle block is a circular buffer of \( 2 b \) leaves whose leaves are all full but the first, with \( g < b \) places free at its front, and the last. The item of rank \( i \) is the item \( r = i \bmod b^2 \) of the middle block \( \lfloor i / b^2 \rfloor \), and the item of rank \( r \) of a middle block is the item \( ( g + r ) \bmod b \) of its leaf \( \lfloor ( g + r ) / b \rfloor \): shifts, masks and five reads.

A middle block takes an item at its front or gives one from its back in \( O( 1 ) \) time, by its gap and the offsets of its leaves, so that an insertion at the rank \( i \) moves the last item of each full middle block after the one of \( i \) to the front of the next one, at most \( n / b^2 + 1 = O( n^{1/3} ) \) of them, and then inserts in its middle block as a tiered vector does, \( O( b ) \) items of the leaf and one item for each of the \( O( b ) \) leaves after it; a removal is the symmetric. The size of the leaves follows \( n \), the items being copied to leaves of the new \( b \) in \( O( n ) \) time once every \( \Omega( n ) \) operations. The places wasted are those of the first and the last leaf of each middle block and the directories of the leaves, \( O( b ) \) for each of the \( O( n / b^2 ) \) middle blocks, i.e., \( O( n / b ) = O( n^{2/3} ) \); a few leaves freed are kept for the next ones, since an insertion frees a leaf at the back of a middle block and takes one at the front of the next.

The contracts [see Contract.h]: a rank is below size() (at most size() for an insertion), and pop_back() needs a list that is not empty; the invariant is that the middle blocks but the last hold \( b^2 \) items and count size() items, each with its gap below \( b \) and the leaves its items need. The operations are defined in CubishArrayStack.tpp.

See also
M. T. Goodrich, J. G. Kloss II, Tiered vectors: efficient dynamic arrays for rank-based sequences, Algorithms and Data Structures (WADS 1999), Lecture Notes in Computer Science 1663, 205-216, 1999, doi:10.1007/3-540-48447-7_21
P. Morin, Open Data Structures (in C++), Edition 0.1G, 2013, https://opendatastructures.org/ods-cpp.pdf, exercise 2.12
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa