Skip to content

Engine → Core → small_vector.h

small_vector

Syntax

template <typename T, usize N> class small_vector

Constructors

AccessSignatureDescription
publicsmall_vector() noexcept;
publicexplicit small_vector( usize count);count value-initialised elements.
publicsmall_vector( usize count, const T &value);count copies of value.
publicsmall_vector( std::initializer_list<T> values);
publictemplate <std::input_iterator Iterator> small_vector( Iterator first, Iterator last);
publicexplicit small_vector( std::span<const T> values);
publicsmall_vector( const small_vector &other);
publicsmall_vector( small_vector &&other) noexcept;

Destructors

AccessSignatureDescription
public~small_vector();

Variables

AccessNameTypeDefaultDescription
publicINLINE_CAPACITYstatic constexpr usizeNElements stored inside the object before the heap is used.
publicIS_TRIVIALLY_RELOCATABLEstatic constexpr boolstd::is_trivially_copyable_v<T>True when elements are relocated with memcpy instead of move + destroy.
privatemCapacityusizeN
privatemDataT *nullptr
privatemInlineinline_storage{}
privatemSizeusize0

Functions

Public

assign

void assign(
    usize count,
    const T &value);

Replaces the contents with count copies of value. value may refer to an element of this vector.

template <std::input_iterator Iterator> void assign(
    Iterator first,
    Iterator last);

Replaces the contents with a copy of the range.

at

[[nodiscard]] T &at(
    usize index) noexcept;

Bounds-checked access in every configuration. An out-of-range index is a fatal programmer error (asserts, then terminates), never an exception.

[[nodiscard]] const T &at(
    usize index) const noexcept;

back

[[nodiscard]] T &back() noexcept;
[[nodiscard]] const T &back() const noexcept;

begin

[[nodiscard]] iterator begin() noexcept;
[[nodiscard]] const_iterator begin() const noexcept;

capacity

[[nodiscard]] usize capacity() const noexcept;

cbegin

[[nodiscard]] const_iterator cbegin() const noexcept;

cend

[[nodiscard]] const_iterator cend() const noexcept;

clear

void clear() noexcept;

Destroys every element; keeps the current storage.

data

[[nodiscard]] T *data() noexcept;
[[nodiscard]] const T *data() const noexcept;

emplace

template <typename... Args> iterator emplace(
    const_iterator position,
    Args &&...args);

Constructs an element before position (which may be end()). Arguments may refer to existing elements. Requires a move-assignable T unless T is trivially copyable.

emplace_back

template <typename... Args> T &emplace_back(
    Args &&...args);

Constructs an element at the end and returns it. Arguments may refer to existing elements; they are read before any reallocation.

empty

[[nodiscard]] bool empty() const noexcept;

end

[[nodiscard]] iterator end() noexcept;
[[nodiscard]] const_iterator end() const noexcept;

erase

iterator erase(
    const_iterator position);

Erases the element at position. Returns the iterator following it.

Erases [first, last). Returns the iterator following the last erased element.

front

[[nodiscard]] T &front() noexcept;
[[nodiscard]] const T &front() const noexcept;

insert

iterator insert(
    const_iterator position,
    const T &value);

Inserts a copy of value before position. Returns an iterator to the new element.

iterator insert(
    const_iterator position,
    T &&value);
template <std::input_iterator Iterator> iterator insert(
    const_iterator position,
    Iterator first,
    Iterator last);

Inserts a copy of the range before position (which may be end()). The range must not alias this vector. Returns an iterator to the first inserted element. Single-pass (input) iterators are collected into a temporary vector first.

iterator insert(
    const_iterator position,
    std::initializer_list<T> values);

is_inline

[[nodiscard]] bool is_inline() const noexcept;

True while the elements live inside the object (no heap allocation).

max_size

[[nodiscard]] static constexpr usize max_size() noexcept;

pop_back

void pop_back() noexcept;

Destroys the last element. Asserts when empty.

push_back

void push_back(
    const T &value);

Appends a copy of value. value may refer to an element of this vector.

void push_back(
    T &&value);

Appends by move.

rbegin

[[nodiscard]] reverse_iterator rbegin() noexcept;
[[nodiscard]] const_reverse_iterator rbegin() const noexcept;

rend

[[nodiscard]] reverse_iterator rend() noexcept;
[[nodiscard]] const_reverse_iterator rend() const noexcept;

reserve

void reserve(
    usize count);

Makes room for at least count elements without constructing any.

resize

void resize(
    usize count);

Grows with value-initialised elements or shrinks by destroying the tail.

void resize(
    usize count,
    const T &value);

Grows with copies of value or shrinks by destroying the tail. value may refer to an element of this vector.

shrink_to_fit

void shrink_to_fit();

Moves the elements back inline when they fit, or trims heap capacity to size().

size

[[nodiscard]] usize size() const noexcept;

swap

void swap(
    small_vector &other) noexcept;

Exchanges contents with another vector of the same type.

Private

allocate_storage

[[nodiscard]] static T *allocate_storage(
    usize count);

append_range

template <typename Iterator> void append_range(
    Iterator first,
    Iterator last);

assign_range

template <typename Iterator> void assign_range(
    Iterator first,
    Iterator last);

deallocate_storage

static void deallocate_storage(
    T *storage,
    usize count) noexcept;

destroy_range

static void destroy_range(
    T *first,
    usize count) noexcept;

emplace_with_growth

template <typename... Args> T *emplace_with_growth(
    usize index,
    Args &&...args);

Reallocates leaving an uninitialised hole at index and constructs the new element there first, so arguments aliasing old elements are still intact when read.

grown_capacity

[[nodiscard]] usize grown_capacity(
    usize required) const noexcept;

index_of

[[nodiscard]] usize index_of(
    const_iterator position) const noexcept;

inline_data

[[nodiscard]] T *inline_data() noexcept;
[[nodiscard]] const T *inline_data() const noexcept;

open_gap

void open_gap(
    usize index,
    usize count);

Shifts [index, size) right by count leaving uninitialised storage in the gap. mSize is not updated; the caller fills the gap and adds count.

reallocate

void reallocate(
    usize newCapacity);

release_heap

void release_heap() noexcept;

relocate_range

static void relocate_range(
    T *source,
    T *destination,
    usize count) noexcept;

Moves count elements from source to uninitialised destination and ends the lifetime of the sources. The ranges must not overlap.

take_from

void take_from(
    small_vector &other) noexcept;

Steals the heap buffer of other or relocates its inline elements. other ends up empty and inline. This vector must be empty and inline.

Operator Overloads

Public

operator<=>

[[nodiscard]] auto operator<=>(
    const small_vector &other) const requires detail::synth_three_way_comparable<T>;

Lexicographical ordering like std::vector's: uses T's operator<=> when it has one and falls back to operator< (weak ordering) otherwise. Absent for element types that are not even less-than comparable.

operator=

small_vector &operator=(
    const small_vector &other);
small_vector &operator=(
    small_vector &&other) noexcept;
small_vector &operator=(
    std::initializer_list<T> values);

operator==

[[nodiscard]] bool operator==(
    const small_vector &other) const;

operator[]

[[nodiscard]] T &operator[](
    usize index) noexcept;
[[nodiscard]] const T &operator[](
    usize index) const noexcept;