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

The dual array deque of section 2.5 of Morin's book, beyond the book: a LIST by rank made of two stacks back to back, \( F \) with the first items from the top and \( B \) with the others from the bottom, so that the item of rank \( i \) is \( F[ |F| - 1 - i ] \) for \( i < |F| \) and \( B[ i - |F| ] \) otherwise. More...

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

Go to the source code of this file.

Classes

class  ADS::DualArrayDeque< T, S >
 a LIST by rank on two stacks S back to back, balanced More...

Namespaces

namespace  ADS

Detailed description

The dual array deque of section 2.5 of Morin's book, beyond the book: a LIST by rank made of two stacks back to back, \( F \) with the first items from the top and \( B \) with the others from the bottom, so that the item of rank \( i \) is \( F[ |F| - 1 - i ] \) for \( i < |F| \) and \( B[ i - |F| ] \) otherwise.

An insertion or a removal at the rank \( i \) moves the items between \( i \) and the top of its stack, which are \( O( 1 + \min\{ i , n - i \} ) \) while the two stacks are balanced, \( 3 |F| \ge |B| \) and \( 3 |B| \ge |F| \); when an operation breaks the balance, the items are dealt again into two stacks of \( \lfloor n / 2 \rfloor \) and \( \lceil n / 2 \rceil \) items, in \( O( n ) \) time, which the \( \Omega( n ) \) operations since the last balance pay for (section 2.5.1 of Morin's book).

The stacks are of the type S, a vector by default; with S = RootishArrayStack< T > [see RootishArrayStack.h] the deque wastes \( O( \sqrt{n} ) \) places outside a balance and inserts and removes at the rank \( i \) in \( O( 1 + \min\{ i , n - i \} ) \) amortized time (exercise 2.9). A balance is done in place: the \( t \) items at the bottom of the larger stack go to the bottom of the other, which grows by \( t \le \lceil n / 2 \rceil \) places (fewer than \( n / 2 \) unless it was empty), before the larger one shrinks, i.e., the places of \( n + t \) items at the peak instead of those of \( 2 n \), and \( n \) moves (see Lists by rank (Morin's book) for why it is not spread over the operations). A stack S has size(), operator[], push_back(), pop_back() and either the insert() and erase() of a rank or those of an iterator, as std::vector.

rotate( r ) moves the item of rank \( i \) to the rank \( ( i + r ) \bmod n \) by \( \min\{ r , n - r \} \) moves from one end to the other, in \( O( 1 + \min\{ r , n - r \} ) \) amortized time (exercise 2.5).

Against Morin's code, the claims for the benchmarks: the stacks are std::vector, whose shifts are one memmove() for the items trivially copyable and whose growth moves the items, where Morin's ArrayStack copies them one by one; a balance moves each item once, in place.

The contracts [see Contract.h]: a rank is below size() (at most size() for an insertion), and the removals at the ends need a deque that is not empty; the invariant is the balance of the two stacks, for \( n \ge 2 \). The operations are defined in DualArrayDeque.tpp.

See also
P. Morin, Open Data Structures (in C++), Edition 0.1G, 2013, https://opendatastructures.org/ods-cpp.pdf, section 2.5 and exercises 2.5 and 2.9
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa