Implements a drop‐in replacement for std::vector<T> which stores up to N elements directly (without heap allocation). The types Size and Diff are used to store element counts, and can be any unsigned + signed type.
Synopsis
Declared in <prevector.h>
template<>
class prevector<36, unsigned char>;
Description
Storage layout is either:
-
Direct allocation:
-
Size _size: the number of used elements (between 0 and N)
-
T direct[N]: an array of N elements of type T (only the first _size are initialized).
-
Indirect allocation:
-
Size _size: the number of used elements plus N + 1
-
Size capacity: the number of allocated elements
-
T* indirect: a pointer to an array of capacity elements of type T (only the first _size are initialized).
The data type T must be movable by memmove/realloc(). Once we switch to C++, move constructors can be used instead.
Types
Name |
Description |
Read‐only contiguous iterator over the container's elements. |
|
Mutable contiguous iterator over the container's elements. |
Type Aliases
Name |
Description |
Pointer to a const element. |
|
Reference to a const element. |
|
Signed type used to represent distances between iterators. |
|
Pointer to an element. |
|
Reference to an element. |
|
Unsigned type used to store element counts. |
|
Type of the elements held by the container. |
Member Functions
Name |
Description |
|
Constructors |
|
Destroys the container, freeing any heap allocation. |
Assignment operators |
|
Returns the number of bytes held in the heap allocation, or zero when stored inline. |
|
|
|
|
|
|
|
Returns the number of elements the container can hold without reallocating. |
|
Removes all elements, leaving the container empty. |
|
|
|
Constructs a new element in place at the end of the container. |
|
Returns whether the container holds no elements. |
|
|
|
|
|
|
|
|
|
Subscript operators |
|
Removes the last element. |
|
Appends a copy of |
|
Ensures capacity for at least |
|
Resizes the container to hold |
|
Changes the size to |
|
Reduces the capacity to match the current size. |
|
Returns the number of elements currently stored. |
|
Exchanges the contents of this container with |
|
Returns whether this container holds the same elements as |
|
Orders two containers lexicographically by their elements. |
Static Data Members
Name |
Description |
Number of elements stored inline before switching to heap allocation. |
Created with MrDocs