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

The rootish array stack of Brodnik, Carlsson, Demaine, Munro and Sedgewick, beyond the book: a LIST by rank, operator[], insert( i , x ) and erase( i ) (section 2.6 of Morin's book), whose items lie in \( r \) blocks of \( 1 , 2 , \ldots , r \) places, so that a vector that grows never copies its items and wastes only \( O( \sqrt{n} ) \) places, where a vector that doubles [see ArraySequence.h] wastes up to \( 2 n \) of them and copies each item once on average. More...

#include "ADS/Contract.h"
#include "ADS/memory/AllocatorAware.h"
#include "ADS/Iterators.h"
#include <cmath>
#include <cstddef>
#include <initializer_list>
#include <iterator>
#include <memory>
#include <new>
#include <utility>
#include <vector>
#include "ADS/sequence/impl/RootishArrayStack.tpp"

Go to the source code of this file.

Classes

class  ADS::RootishArrayStack< T, A >
 a LIST by rank on blocks of \( 1 , 2 , \ldots , r \) places, with \( O( \sqrt{n} ) \) places wasted More...

Namespaces

namespace  ADS

Detailed description

The rootish array stack of Brodnik, Carlsson, Demaine, Munro and Sedgewick, beyond the book: a LIST by rank, operator[], insert( i , x ) and erase( i ) (section 2.6 of Morin's book), whose items lie in \( r \) blocks of \( 1 , 2 , \ldots , r \) places, so that a vector that grows never copies its items and wastes only \( O( \sqrt{n} ) \) places, where a vector that doubles [see ArraySequence.h] wastes up to \( 2 n \) of them and copies each item once on average.

The item of rank \( i \) is in the block \( b \), the least with \( ( b + 1 ) ( b + 2 ) / 2 > i \), i.e.,

\[ b( i ) = \Bigl\lceil \frac{ -3 + \sqrt{ 9 + 8 i } }{ 2 } \Bigr\rceil \; , \tag{1} \]

at the place \( i - b ( b + 1 ) / 2 \) of it; (1) is computed with the square root of a double, exact on the integers below \( 2^{52} \), and a step of correction, cheaper than the tables of section 2.6.4 of Morin's book on a processor with a unit for the floating point. A block of \( r + 1 \) places is added when the \( r ( r + 1 ) / 2 \) places are full, and the last block is freed while \( ( r - 2 ) ( r - 1 ) / 2 \ge n \), so that one empty block at most is kept for the next insertions: an insertion or a removal at the rank \( i \) moves \( n - i \) items, plus \( O( 1 ) \) amortized for the blocks, and the places not used are at most \( 2 r - 1 \le \sqrt{ 8 n } + 3 \) (section 2.6.2 of Morin's book), the pointers to the blocks \( r \) more. The places are raw memory, the items being built in them when inserted and destroyed when removed.

Against Morin's code, the claims for the benchmarks: the blocks are raw memory, so that push_back() builds one item where Morin's blocks of new T[ b + 1 ] build \( b + 1 \) by default and assign them later; the shifts move the items instead of copying them; operator[] costs the same square root plus two comparisons, predicted, of the correction.

The blocks and the vector of their addresses come from the allocator A, std::allocator by default, with the rules of the standard [see AllocatorAware.h].

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 \( r \) blocks hold the \( n \) items with one block to spare at most, \( ( r - 2 ) ( r - 1 ) / 2 < n \le r ( r + 1 ) / 2 \), and that the empty list has no block. The operations are defined in RootishArrayStack.tpp.

See also
A. Brodnik, S. Carlsson, E. D. Demaine, J. I. Munro, R. Sedgewick, Resizable arrays in optimal time and space, Algorithms and Data Structures (WADS 1999), Lecture Notes in Computer Science 1663, 37-48, 1999, doi:10.1007/3-540-48447-7_4
P. Morin, Open Data Structures (in C++), Edition 0.1G, 2013, https://opendatastructures.org/ods-cpp.pdf, section 2.6
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa