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

A SEQUENCE [see ADT.h] realized with a vector: the position of an item is its index, \( \mathit{pos}_i = i \), with \( \mathit{pos}_{-1} = -1 \) and \( \mathit{pos}_n = n \). More...

#include "ADS/Contract.h"
#include <algorithm>
#include <cstddef>
#include <initializer_list>
#include <iterator>
#include <memory>
#include <utility>
#include "ADS/sequence/impl/ArraySequence.tpp"

Go to the source code of this file.

Classes

class  ADS::ArraySequence< T >
 a SEQUENCE realized with a vector that doubles when it is full More...

Namespaces

namespace  ADS

Detailed description

A SEQUENCE [see ADT.h] realized with a vector: the position of an item is its index, \( \mathit{pos}_i = i \), with \( \mathit{pos}_{-1} = -1 \) and \( \mathit{pos}_n = n \).

Moving along the sequence, reading and writing take \( O( 1 ) \) time, while insert() and remove() take \( O( n ) \) time, since the items after the position move by one place; inserting at the end takes \( O( 1 ) \) amortized time, since a full vector is replaced by one of twice its size. If the \( i \)-th insertion costs \( c_i = i \) when it finds the vector full, i.e., when \( i = 2^k + 1 \), and 1 otherwise, then \( n \) insertions cost

\[ C( n ) = \sum_{i=1}^n c_i \leq n + \sum_{k=0}^{\lfloor \log_2 n \rfloor} 2^k \leq n + 2^{\lfloor \log_2 n \rfloor + 1} - 1 \leq 3 n \; , \]

whereas growing the vector by a constant \( d \) makes them cost \( \Theta( n^2 ) \). Symmetrically, a vector that remove() leaves with \( n \leq c / 4 \), \( c \) its capacity, is replaced by one of half its size, so that the space stays \( O( n ) \) and any sequence of insertions and removals costs \( O( 1 ) \) amortized time each: after a resize the vector is half full, and \( \Omega( c ) \) operations must come before the next one. The empty sequence has no vector, also when remove() empties it, the first insertion makes one of one place, and moving a sequence takes its vector in \( O( 1 ) \) time, leaving the one moved from empty.

It is also the ArrayStack of section 2.1 of Morin's book, the LIST of get( i ), set( i , x ), add( i , x ) and remove( i ) on a vector (read(), write() and the two insert() and remove() here), and its FastArrayStack of section 2.2: the items are shifted by std::move_backward() and std::move(), which become one memmove() for the items trivially copyable; the range insert() is the addAll() of exercise 2.1. Morin halves the vector when \( 3 n \le c \), this vector when \( 4 n \le c \): the argument is the same, with another constant. A removed item leaves behind it, in the place \( n \), the husk of the item moved out of it (exercise 2.1 of the Java edition), which keeps its resources only if its move does not take them, until an insertion or a resize overwrites it.

Against Morin's code, the claims for the benchmarks: equal shifts (one memmove(), as his FastArrayStack with std::copy()), a growth that moves the items where his copies them, and a shrink at a quarter where his is at a third, i.e., fewer reallocations when the size oscillates.

The contracts [see Contract.h]: a position is an index from -1 to \( n \); next() needs one from -1 to \( n - 1 \) and prev() one from 0 to \( n \), since there is nothing after \( \mathit{pos}_n \) nor before \( \mathit{pos}_{-1} \); insert() needs one from 0 to \( n \), and read(), write() and remove() one that holds an item, from 0 to \( n - 1 \). insert() and write() ensure that \( v \) is read back in its place, and that the other items are where they were (or one place on, after the insertion), the latter costing \( O( n ) \) and therefore an audit; remove() likewise. The invariant is that the \( n \) items fit in the vector, \( 0 \leq n \leq c \). The operations are defined in ArraySequence.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, sections 4.2, 4.2.1 and 4.5, https://algoritmicamente.it/materiali/libro/capitoli/cap04.pdf
T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein, Introduction to Algorithms, 3rd ed., MIT Press, 2009, section 17.4 (dynamic tables, the contraction at a quarter)
P. Morin, Open Data Structures (in C++), Edition 0.1G, 2013, https://opendatastructures.org/ods-cpp.pdf, sections 2.1 and 2.2 and exercise 2.1
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa