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

A double ended queue with no modular arithmetic, beyond the book: the variant of the ArrayDeque of Morin's book asked by its exercise 2.8, the \( n \) items in order in one contiguous piece \( A[ h .. h + n - 1 ] \) of a vector of \( c \) places, so that the item of rank \( i \) is \( A[ h + i ] \), the iterators are pointers and data() gives the items to any function that wants an array. More...

#include "ADS/Contract.h"
#include <algorithm>
#include <cstddef>
#include <cstring>
#include <initializer_list>
#include <iterator>
#include <memory>
#include <new>
#include <type_traits>
#include <utility>
#include "ADS/sequence/impl/ContiguousDeque.tpp"

Go to the source code of this file.

Classes

class  ADS::ContiguousDeque< T >
 a double ended queue whose items are contiguous, rebuilt in the middle of its vector when they reach an end More...

Namespaces

namespace  ADS

Detailed description

A double ended queue with no modular arithmetic, beyond the book: the variant of the ArrayDeque of Morin's book asked by its exercise 2.8, the \( n \) items in order in one contiguous piece \( A[ h .. h + n - 1 ] \) of a vector of \( c \) places, so that the item of rank \( i \) is \( A[ h + i ] \), the iterators are pointers and data() gives the items to any function that wants an array.

When the items reach an end of the vector that an operation needs, rebuild() puts them in the middle of a vector of \( 3 n + 8 \) places (the same vector, moved in place by memmove(), when it is large enough and the items are trivially copyable), so that at least \( n + 4 \) places are free on each side: \( \Omega( n ) \) operations come before the next rebuild, which costs \( O( n ) \), and each operation at an end takes \( O( 1 ) \) amortized time, as in the ArrayDeque. An insertion or a removal at the rank \( i \) moves the items of the shorter side, in \( O( 1 + \min\{ i , n - i \} ) \) amortized time. A vector with more than \( 9 n + 24 \) places after a removal is rebuilt with \( 3 n + 8 \), so that the space stays \( O( n ) \). The rebuild is not spread over the operations: keeping the one contiguous piece in use while another is filled would need copies of the items and two pieces at once (see Lists by rank (Morin's book)).

Compared with RingDeque [see RingDeque.h], it pays the rebuilds that the circular vector avoids when a queue used as a FIFO runs along the vector, and saves the mask of every access and the split of the items into two pieces.

The contracts [see Contract.h]: a rank is below size() (at most size() for an insertion), and the removals and front() and back() need a deque that is not empty; the invariant is \( h + n \le c \). The operations are defined in ContiguousDeque.tpp.

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