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

A vector with room for \( N \) items inside itself, beyond the book: the vector that doubles of section 4.2 [see ArraySequence.h] whose first \( N \) items need no memory of the allocator, so that the many short vectors of a structure (the children of a node, the arcs of a vertex, the items of a bucket) cost no allocation each and lie next to the object that holds them, at the price of \( N \) items of room in every vector. More...

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

Go to the source code of this file.

Classes

class  ADS::SmallVector< T, N, A >
 a vector with room for \( N \) items inside itself More...

Namespaces

namespace  ADS

Macros

#define ADS_SMALLVECTOR_BOOST   0

Detailed description

A vector with room for \( N \) items inside itself, beyond the book: the vector that doubles of section 4.2 [see ArraySequence.h] whose first \( N \) items need no memory of the allocator, so that the many short vectors of a structure (the children of a node, the arcs of a vertex, the items of a bucket) cost no allocation each and lie next to the object that holds them, at the price of \( N \) items of room in every vector.

Past \( N \) items the vector moves to the allocator and doubles there, as std::vector does: \( n \) pushes cost fewer than \( 2 n \) moves of items, and the space is at most \( 2 n + N \) items.

A move takes the memory of the other vector in \( O( 1 ) \) time when its items are on the allocator, and moves the items one by one, at most \( N \), when they are inside it; the vector moved from is left empty.

With ADS_WITH_BOOST defined (by the option of CMake of the same name, which defines it whenever the headers of Boost 1.83 or later are found, by the makefile, which does the same, or by hand), the items are those of boost::container::small_vector with the growth factor 2 of its options, the same vector with the same doubling, and ADS_SMALLVECTOR_BOOST defined to 0 keeps the code of ADS; the interface, the iterators (the pointers to the items), the costs and the contracts stay those above, small() telling whether the room is still the inner one of Boost.

The items beyond the inner room come from the allocator A, std::allocator by default, with the rules of the standard [see AllocatorAware.h]; with Boost the allocator is that of boost::container::small_vector. A swap moves the items in three moves, so that it needs no precondition on the allocators.

The contracts [see Contract.h]: an index is below size(), and front(), back() and pop_back() need a vector that is not empty; the invariant is \( n \leq c \) and \( c \geq N \), \( c \) the capacity. The operations are defined in SmallVector.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, section 4.2, 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)
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa

Macro definition documentation

◆ ADS_SMALLVECTOR_BOOST

#define ADS_SMALLVECTOR_BOOST   0