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

The space-efficient list of section 3.3 of Morin's book, beyond the book: a doubly linked list of blocks, each a circular buffer of at most \( b + 1 \) items (an unrolled list), the blocks but the first and the last holding between \( b - 1 \) and \( b + 1 \) items, so that the \( n \) items take \( n + O( b + n / b ) \) places and pointers where a List [see List.h] takes \( 2 n \) pointers more, and a walk reads \( b \) items per block. More...

#include "ADS/Contract.h"
#include <cstddef>
#include <initializer_list>
#include <iterator>
#include <new>
#include <type_traits>
#include <utility>
#include "ADS/sequence/impl/SEList.tpp"

Go to the source code of this file.

Classes

class  ADS::SEList< T >
 a list of blocks of \( b - 1 \) to \( b + 1 \) items, with \( O( n / b ) \) pointers More...
class  ADS::SEList< T >::Iterator< Const >
 the bidirectional iterator, on items const if Const More...

Namespaces

namespace  ADS

Detailed description

The space-efficient list of section 3.3 of Morin's book, beyond the book: a doubly linked list of blocks, each a circular buffer of at most \( b + 1 \) items (an unrolled list), the blocks but the first and the last holding between \( b - 1 \) and \( b + 1 \) items, so that the \( n \) items take \( n + O( b + n / b ) \) places and pointers where a List [see List.h] takes \( 2 n \) pointers more, and a walk reads \( b \) items per block.

The item of rank \( i \) is reached from the nearer end, block by block, in \( O( 1 + \min\{ i , n - i \} / b ) \) time; an insertion or a removal at the rank \( i \) takes \( O( b + \min\{ i , n - i \} / b ) \) amortized time.

An insertion into a full block looks at the next \( b \) blocks at most for one with room: if one is found, the last item of each block in between passes to the front of the next one, \( O( 1 ) \) per block; if the walk reaches the end, a block is added there; if the \( b \) blocks are all full, spread() makes \( b + 1 \) blocks of \( b \) items out of them, in \( O( b^2 ) \). A removal that leaves a middle block with \( b - 2 \) items is the symmetric, gather() making \( b - 1 \) blocks out of \( b \) blocks that hold \( b^2 - b - 1 \) items. A spread or a gather leaves its blocks with \( b \) items, or \( b - 1 \), so that \( \Omega( b ) \) operations on them come before the next one, which pays for its \( O( b^2 ) \) (section 3.3.5 of Morin's book).

Unlike Morin's, the first and the last block may hold any number of items from 1 to \( b + 1 \): push_back() adds a block when the last one is full and pop_back() drops it when it is empty, and the same at the front, so that the four operations of a double ended queue never spread nor gather and take \( O( 1 ) \) time, independent of \( b \) (which exercise 3.18 asks for, and exercise 3.17 for the stack); the bound on the space loses the \( 2 b \) places of the two ends at most. A block is one allocation, its header followed by its places, which are raw memory, and one block emptied is kept for the next one needed, so that the operations at a boundary of the blocks do not call the allocator each time.

Against Morin's code, the claims for the benchmarks: a block is one allocation of exactly \( b + 1 \) places, its index reduced by a comparison and a subtraction where Morin's BDeque, an ArrayDeque, divides by \( b + 1 \), and holds raw places where Morin's builds \( b + 1 \) items by default; the operations at the two ends never spread nor gather; one block emptied is kept, so that a queue that crosses the boundary of a block back and forth allocates nothing.

The contracts [see Contract.h]: \( b \ge 3 \), a rank is below size() (at most size() for an insertion), and the removals at the ends need a list that is not empty; the invariant is the bound on the items of each block, none empty, and that the blocks count size() items, an \( O( n / b ) \) check. The operations are defined in SEList.tpp.

See also
P. Morin, Open Data Structures (in C++), Edition 0.1G, 2013, https://opendatastructures.org/ods-cpp.pdf, section 3.3 and exercises 3.17 and 3.18
Z. Shao, J. H. Reppy, A. W. Appel, Unrolling lists, Proceedings of the 1994 ACM Conference on LISP and Functional Programming, 185-195, 1994, doi:10.1145/182409.182453
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa