docs
Loading...
Searching...
No Matches
Vector< T, InlineCapacity >

#include <Ultralight/Vector.h>

Overview

template<typename T, size_t InlineCapacity = 4>
class ultralight::Vector< T, InlineCapacity >

A std::vector-like container with small buffer optimization and ABI-safe allocation.

This container can be used safely in public API boundaries where std::vector cannot, because STL types are not guaranteed to be ABI-compatible across different compilers, standard library implementations, or even different build configurations.

All heap allocation is routed through UltralightCore's exported allocator functions (ul_malloc / ul_free), ensuring that memory allocated by the library is always freed by the library – regardless of which runtime the caller was built with.

Small Buffer Optimization (SBO)

When InlineCapacity > 0 (the default), the first N elements are stored inline within the Vector object itself, avoiding heap allocation for common small lists. Once the size exceeds InlineCapacity, the Vector transparently switches to heap storage.

Type support

Works with both trivially-copyable types (uses memcpy/realloc fast paths) and non-trivial types like RefPtr<T> (proper construction, destruction, and move semantics).

Example

Vector<int> v = {1, 2, 3}; // stored inline (default InlineCapacity = 4)
v.push_back(4); // still inline
v.push_back(5); // transitions to heap
Vector<int, 0> heap_only; // pure heap, no inline storage
Vector<int, 16> large_inline; // 16 elements inline before heap
A std::vector-like container with small buffer optimization and ABI-safe allocation.
Definition Vector.h:88
void push_back(const T &value)
Definition Vector.h:368
friend class Vector
Definition Vector.h:95
Template Parameters
TElement type.
InlineCapacityNumber of elements stored inline before heap allocation (default 4). Use 0 for pure heap storage with no inline overhead.
Inheritance diagram for Vector< T, InlineCapacity >:

Public Types

using value_type = T
using size_type = size_t
using difference_type = ptrdiff_t
using reference = T&
using const_reference = const T&
using pointer = T*
using const_pointer = const T*
using iterator = T*
using const_iterator = const T*

Public Member Functions

 Vector ()=default
 Construct an empty vector.
 Vector (size_t count)
 Construct a vector with count value-initialized elements.
 Vector (size_t count, const T &value)
 Construct a vector with count copies of value.
 Vector (std::initializer_list< T > init)
template<typename InputIt, typename = typename std::enable_if<!std::is_integral<InputIt>::value>::type>
 Vector (InputIt first, InputIt last)
 Vector (const Vector &other)
template<size_t OtherInline>
 Vector (const Vector< T, OtherInline > &other)
 Vector (Vector &&other) noexcept(InlineCapacity==0||std::is_nothrow_move_constructible< T >::value)
template<size_t OtherInline>
 Vector (Vector< T, OtherInline > &&other) noexcept(std::is_nothrow_move_constructible< T >::value)
 ~Vector ()
Vector & operator= (const Vector &other)
template<size_t OtherInline>
Vector & operator= (const Vector< T, OtherInline > &other)
Vector & operator= (Vector &&other) noexcept(InlineCapacity==0||std::is_nothrow_move_constructible< T >::value)
template<size_t OtherInline>
Vector & operator= (Vector< T, OtherInline > &&other) noexcept(std::is_nothrow_move_constructible< T >::value)
Vector & operator= (std::initializer_list< T > init)
void assign (size_t count, const T &value)
template<typename InputIt, typename = typename std::enable_if<!std::is_integral<InputIt>::value>::type>
void assign (InputIt first, InputIt last)
void assign (std::initializer_list< T > init)
T & operator[] (size_t index)
 Access element by index.
const T & operator[] (size_t index) const
T & at (size_t index)
 Access element by index with bounds checking (asserts in debug builds).
const T & at (size_t index) const
T & front ()
const T & front () const
T & back ()
const T & back () const
T * data ()
const T * data () const
iterator begin ()
const_iterator begin () const
const_iterator cbegin () const
iterator end ()
const_iterator end () const
const_iterator cend () const
bool empty () const
size_t size () const
size_t capacity () const
 Returns the number of elements that can be held without allocating.
void reserve (size_t min_capacity)
 Ensure capacity for at least min_capacity elements, allocating if necessary.
void shrink_to_fit ()
 Reduce capacity to fit the current size.
void clear ()
void push_back (const T &value)
void push_back (T &&value)
template<typename... Args>
T & emplace_back (Args &&... args)
 Construct an element in-place at the end of the vector.
void unchecked_append (const T &value)
 Append an element without checking capacity.
template<typename... Args>
T & unchecked_emplace_back (Args &&... args)
 Construct an element in-place at the end without checking capacity.
void pop_back ()
T take_last ()
 Remove the last element and return it by move.
iterator insert (const_iterator pos, const T &value)
iterator insert (const_iterator pos, T &&value)
template<typename... Args>
iterator emplace (const_iterator pos, Args &&... args)
iterator erase (const_iterator pos)
iterator erase (const_iterator first, const_iterator last)
void resize (size_t count)
void resize (size_t count, const T &value)
void swap (Vector &other)
template<size_t OtherInline>
void append_vector (const Vector< T, OtherInline > &other)
 Append all elements from another vector.
template<size_t OtherInline>
void append_vector (Vector< T, OtherInline > &&other)
 Move-append all elements from another vector, leaving it empty.

Friends

template<typename U, size_t M>
class Vector
bool operator== (const Vector &a, const Vector &b)
bool operator!= (const Vector &a, const Vector &b)

Member Typedef Documentation

◆ const_iterator

template<typename T, size_t InlineCapacity = 4>
using const_iterator = const T*

◆ const_pointer

template<typename T, size_t InlineCapacity = 4>
using const_pointer = const T*

◆ const_reference

template<typename T, size_t InlineCapacity = 4>
using const_reference = const T&

◆ difference_type

template<typename T, size_t InlineCapacity = 4>
using difference_type = ptrdiff_t

◆ iterator

template<typename T, size_t InlineCapacity = 4>
using iterator = T*

◆ pointer

template<typename T, size_t InlineCapacity = 4>
using pointer = T*

◆ reference

template<typename T, size_t InlineCapacity = 4>
using reference = T&

◆ size_type

template<typename T, size_t InlineCapacity = 4>
using size_type = size_t

◆ value_type

template<typename T, size_t InlineCapacity = 4>
using value_type = T

Constructor & Destructor Documentation

◆ Vector() [1/9]

template<typename T, size_t InlineCapacity = 4>
Vector ( )
default

Construct an empty vector.

No heap allocation is performed.

◆ Vector() [2/9]

template<typename T, size_t InlineCapacity = 4>
Vector ( size_t count)
inlineexplicit

Construct a vector with count value-initialized elements.

◆ Vector() [3/9]

template<typename T, size_t InlineCapacity = 4>
Vector ( size_t count,
const T & value )
inline

Construct a vector with count copies of value.

◆ Vector() [4/9]

template<typename T, size_t InlineCapacity = 4>
Vector ( std::initializer_list< T > init)
inline

◆ Vector() [5/9]

template<typename T, size_t InlineCapacity = 4>
template<typename InputIt, typename = typename std::enable_if<!std::is_integral<InputIt>::value>::type>
Vector ( InputIt first,
InputIt last )
inline

◆ Vector() [6/9]

template<typename T, size_t InlineCapacity = 4>
Vector ( const Vector< T, InlineCapacity > & other)
inline

◆ Vector() [7/9]

template<typename T, size_t InlineCapacity = 4>
template<size_t OtherInline>
Vector ( const Vector< T, OtherInline > & other)
inline

◆ Vector() [8/9]

template<typename T, size_t InlineCapacity = 4>
Vector ( Vector< T, InlineCapacity > && other)
inlinenoexcept

◆ Vector() [9/9]

template<typename T, size_t InlineCapacity = 4>
template<size_t OtherInline>
Vector ( Vector< T, OtherInline > && other)
inlinenoexcept

◆ ~Vector()

template<typename T, size_t InlineCapacity = 4>
~Vector ( )
inline

Member Function Documentation

◆ append_vector() [1/2]

template<typename T, size_t InlineCapacity = 4>
template<size_t OtherInline>
void append_vector ( const Vector< T, OtherInline > & other)
inline

Append all elements from another vector.

Works across different InlineCapacity values.

◆ append_vector() [2/2]

template<typename T, size_t InlineCapacity = 4>
template<size_t OtherInline>
void append_vector ( Vector< T, OtherInline > && other)
inline

Move-append all elements from another vector, leaving it empty.

◆ assign() [1/3]

template<typename T, size_t InlineCapacity = 4>
template<typename InputIt, typename = typename std::enable_if<!std::is_integral<InputIt>::value>::type>
void assign ( InputIt first,
InputIt last )
inline

◆ assign() [2/3]

template<typename T, size_t InlineCapacity = 4>
void assign ( size_t count,
const T & value )
inline

◆ assign() [3/3]

template<typename T, size_t InlineCapacity = 4>
void assign ( std::initializer_list< T > init)
inline

◆ at() [1/2]

template<typename T, size_t InlineCapacity = 4>
T & at ( size_t index)
inline

Access element by index with bounds checking (asserts in debug builds).

◆ at() [2/2]

template<typename T, size_t InlineCapacity = 4>
const T & at ( size_t index) const
inline

◆ back() [1/2]

template<typename T, size_t InlineCapacity = 4>
T & back ( )
inline

◆ back() [2/2]

template<typename T, size_t InlineCapacity = 4>
const T & back ( ) const
inline

◆ begin() [1/2]

template<typename T, size_t InlineCapacity = 4>
iterator begin ( )
inline

◆ begin() [2/2]

template<typename T, size_t InlineCapacity = 4>
const_iterator begin ( ) const
inline

◆ capacity()

template<typename T, size_t InlineCapacity = 4>
size_t capacity ( ) const
inline

Returns the number of elements that can be held without allocating.

For a default-constructed vector with InlineCapacity > 0, this equals InlineCapacity (inline storage is always available without heap allocation).

◆ cbegin()

template<typename T, size_t InlineCapacity = 4>
const_iterator cbegin ( ) const
inline

◆ cend()

template<typename T, size_t InlineCapacity = 4>
const_iterator cend ( ) const
inline

◆ clear()

template<typename T, size_t InlineCapacity = 4>
void clear ( )
inline

◆ data() [1/2]

template<typename T, size_t InlineCapacity = 4>
T * data ( )
inline

◆ data() [2/2]

template<typename T, size_t InlineCapacity = 4>
const T * data ( ) const
inline

◆ emplace()

template<typename T, size_t InlineCapacity = 4>
template<typename... Args>
iterator emplace ( const_iterator pos,
Args &&... args )
inline

◆ emplace_back()

template<typename T, size_t InlineCapacity = 4>
template<typename... Args>
T & emplace_back ( Args &&... args)
inline

Construct an element in-place at the end of the vector.

◆ empty()

template<typename T, size_t InlineCapacity = 4>
bool empty ( ) const
inline

◆ end() [1/2]

template<typename T, size_t InlineCapacity = 4>
iterator end ( )
inline

◆ end() [2/2]

template<typename T, size_t InlineCapacity = 4>
const_iterator end ( ) const
inline

◆ erase() [1/2]

template<typename T, size_t InlineCapacity = 4>
iterator erase ( const_iterator first,
const_iterator last )
inline

◆ erase() [2/2]

template<typename T, size_t InlineCapacity = 4>
iterator erase ( const_iterator pos)
inline

◆ front() [1/2]

template<typename T, size_t InlineCapacity = 4>
T & front ( )
inline

◆ front() [2/2]

template<typename T, size_t InlineCapacity = 4>
const T & front ( ) const
inline

◆ insert() [1/2]

template<typename T, size_t InlineCapacity = 4>
iterator insert ( const_iterator pos,
const T & value )
inline

◆ insert() [2/2]

template<typename T, size_t InlineCapacity = 4>
iterator insert ( const_iterator pos,
T && value )
inline

◆ operator=() [1/5]

template<typename T, size_t InlineCapacity = 4>
Vector & operator= ( const Vector< T, InlineCapacity > & other)
inline

◆ operator=() [2/5]

template<typename T, size_t InlineCapacity = 4>
template<size_t OtherInline>
Vector & operator= ( const Vector< T, OtherInline > & other)
inline

◆ operator=() [3/5]

template<typename T, size_t InlineCapacity = 4>
Vector & operator= ( std::initializer_list< T > init)
inline

◆ operator=() [4/5]

template<typename T, size_t InlineCapacity = 4>
Vector & operator= ( Vector< T, InlineCapacity > && other)
inlinenoexcept

◆ operator=() [5/5]

template<typename T, size_t InlineCapacity = 4>
template<size_t OtherInline>
Vector & operator= ( Vector< T, OtherInline > && other)
inlinenoexcept

◆ operator[]() [1/2]

template<typename T, size_t InlineCapacity = 4>
T & operator[] ( size_t index)
inline

Access element by index.

No bounds checking.

◆ operator[]() [2/2]

template<typename T, size_t InlineCapacity = 4>
const T & operator[] ( size_t index) const
inline

◆ pop_back()

template<typename T, size_t InlineCapacity = 4>
void pop_back ( )
inline

◆ push_back() [1/2]

template<typename T, size_t InlineCapacity = 4>
void push_back ( const T & value)
inline

◆ push_back() [2/2]

template<typename T, size_t InlineCapacity = 4>
void push_back ( T && value)
inline

◆ reserve()

template<typename T, size_t InlineCapacity = 4>
void reserve ( size_t min_capacity)
inline

Ensure capacity for at least min_capacity elements, allocating if necessary.

◆ resize() [1/2]

template<typename T, size_t InlineCapacity = 4>
void resize ( size_t count)
inline

◆ resize() [2/2]

template<typename T, size_t InlineCapacity = 4>
void resize ( size_t count,
const T & value )
inline

◆ shrink_to_fit()

template<typename T, size_t InlineCapacity = 4>
void shrink_to_fit ( )
inline

Reduce capacity to fit the current size.

If InlineCapacity > 0 and the current size fits within the inline buffer, storage is moved back to inline (freeing the heap allocation).

◆ size()

template<typename T, size_t InlineCapacity = 4>
size_t size ( ) const
inline

◆ swap()

template<typename T, size_t InlineCapacity = 4>
void swap ( Vector< T, InlineCapacity > & other)
inline

◆ take_last()

template<typename T, size_t InlineCapacity = 4>
T take_last ( )
inline

Remove the last element and return it by move.

◆ unchecked_append()

template<typename T, size_t InlineCapacity = 4>
void unchecked_append ( const T & value)
inline

Append an element without checking capacity.

Caller must ensure size() < capacity().

◆ unchecked_emplace_back()

template<typename T, size_t InlineCapacity = 4>
template<typename... Args>
T & unchecked_emplace_back ( Args &&... args)
inline

Construct an element in-place at the end without checking capacity.

Caller must ensure size() < capacity().

◆ operator!=

template<typename T, size_t InlineCapacity = 4>
bool operator!= ( const Vector< T, InlineCapacity > & a,
const Vector< T, InlineCapacity > & b )
friend

◆ operator==

template<typename T, size_t InlineCapacity = 4>
bool operator== ( const Vector< T, InlineCapacity > & a,
const Vector< T, InlineCapacity > & b )
friend

◆ Vector

template<typename T, size_t InlineCapacity = 4>
template<typename U, size_t M>
friend class Vector
friend

The documentation for this class was generated from the following file: