|
ADS
Algorithms and Data Structures
|
A double ended queue on a circular vector that grows, beyond the book: the circular vector of ArrayQueue [see ArrayQueue.h] whose capacity is a power of 2, so that a place is reduced by a mask instead of a division, and that doubles when full, the items moved in order to the front of the new vector (\( O( 1 ) \) amortized time per insertion). More...
#include "ADS/Contract.h"#include "ADS/Portable.h"#include <algorithm>#include <bit>#include <compare>#include <cstddef>#include <cstring>#include <initializer_list>#include <iterator>#include <memory>#include <new>#include <type_traits>#include <utility>#include "ADS/queue/impl/RingDeque.tpp"Go to the source code of this file.
Classes | |
| class | ADS::RingDeque< T > |
| a double ended queue on a circular vector of \( 2^k \) places that doubles More... | |
Namespaces | |
| namespace | ADS |
Macros | |
| #define | ADS_RING_INLINE ADS_ALWAYS_INLINE |
| the operations at the ends, inlined even where the compiler would not | |
| #define | ADS_RING_NOINLINE ADS_NOINLINE |
| the growth, never inlined | |
A double ended queue on a circular vector that grows, beyond the book: the circular vector of ArrayQueue [see ArrayQueue.h] whose capacity is a power of 2, so that a place is reduced by a mask instead of a division, and that doubles when full, the items moved in order to the front of the new vector (\( O( 1 ) \) amortized time per insertion).
The four operations at the ends take \( O( 1 ) \) time, an item is read by its rank in \( O( 1 ) \), and the items lie in at most two contiguous pieces, which the moves and the visits run through at the speed of the memory. The doubling of a deque of \( 2^j \) places moves its \( 2^j \) items, so that \( n \) insertions move fewer than \( 2 n \) items in all, and the places are fewer than \( 2 n + 8 \) for \( n \) items at the largest.
Compared with std::deque, which keeps the items in blocks of 512 bytes reached by a map of pointers, there is no indirection and no allocation but at the doublings; compared with boost::circular_buffer, the vector grows by itself; compared with boost::container::devector, the items freed at one end serve the insertions at the other, so that a queue used as a FIFO never moves its items. The places are raw memory, the items being built in them when inserted and destroyed when removed.
It is the ArrayQueue of section 2.3 of Morin's book and its ArrayDeque of section 2.4, with the mask of a power of 2 in place of the modulo (exercise 2.7) and the items moved by blocks (exercise 2.6): insert() and erase() at a rank move the items of the shorter side by std::move() on the runs contiguous in the vector, three at most, which is one memmove() each for the items trivially copyable, and a doubling moves them by memcpy(); rotate() rotates the ranks (exercise 2.5).
Against Morin's code, the claims for the benchmarks: the mask of a power of 2 in place of the division of %; the places are raw memory and a doubling moves them by memcpy(), where Morin's ArrayDeque builds its new array by default and assigns the items one by one; the deque does not shrink, so that a queue whose size oscillates never reallocates, where Morin's resizes whenever \( 3 n < m \).
The contracts [see Contract.h]: front(), back() and the removals need a deque that is not empty, and operator[] a rank below size(); the invariant is that the capacity is 0 or a power of 2 at least size(). The operations are defined in RingDeque.tpp.