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<
    unsigned int N,
    typename T,
    typename Size = uint32_t,
    typename Diff = int32_t>
class prevector;

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

const_iterator

Read‐only contiguous iterator over the container's elements.

iterator

Mutable contiguous iterator over the container's elements.

Type Aliases

Name

Description

const_pointer

Pointer to a const element.

const_reference

Reference to a const element.

difference_type

Signed type used to represent distances between iterators.

pointer

Pointer to an element.

reference

Reference to an element.

size_type

Unsigned type used to store element counts.

value_type

Type of the elements held by the container.

Member Functions

Name

Description

prevector [constructor]

Constructors

~prevector [destructor]

Destroys the container, freeing any heap allocation.

operator=

Assignment operators

allocated_memory

Returns the number of bytes held in the heap allocation, or zero when stored inline.

assign

assign overloads

back

back overloads

begin

begin overloads

capacity

Returns the number of elements the container can hold without reallocating.

clear

Removes all elements, leaving the container empty.

data

data overloads

emplace_back

Constructs a new element in place at the end of the container.

empty

Returns whether the container holds no elements.

end

end overloads

erase

erase overloads

front

front overloads

insert

insert overloads

operator[]

Subscript operators

pop_back

Removes the last element.

push_back

Appends a copy of value to the end of the container.

reserve

Ensures capacity for at least new_capacity elements without changing the size.

resize

Resizes the container to hold new_size elements, value‐initializing any new ones.

resize_uninitialized

Changes the size to new_size without initializing any newly added elements.

shrink_to_fit

Reduces the capacity to match the current size.

size

Returns the number of elements currently stored.

swap

Exchanges the contents of this container with other.

operator==

Returns whether this container holds the same elements as other.

operator<

Orders two containers lexicographically by their elements.

Static Data Members

Name

Description

STATIC_SIZE

Number of elements stored inline before switching to heap allocation.

Specializations

Name

Description

prevector<36, unsigned char>

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.

Non-Member Functions

Name

Description

memusage::DynamicUsage

Dynamic memory used by a prevector's overflow buffer.

Created with MrDocs