ZenitUI
Retained-mode C++20 UI framework
Loading...
Searching...
No Matches
ZenitUI::SmallVector< T, N > Class Template Reference

A vector whose first N elements live inside the object. More...

#include <SmallVector.hpp>

Public Member Functions

 SmallVector (std::initializer_list< T > init)
 Construct from an initializer list.
 SmallVector (const SmallVector &)=default
SmallVector & operator= (const SmallVector &)=default
 SmallVector (SmallVector &&o) noexcept
SmallVector & operator= (SmallVector &&o) noexcept
SmallVector & operator= (std::initializer_list< T > init)
 Replace contents with the given initializer list.
std::size_t size () const
 Number of elements currently stored.
bool empty () const
 true when there are no elements.
void clear ()
 Remove all elements.
void reserve (std::size_t n)
 Reserve storage for at least n elements.
void push_back (const T &value)
 Append a copy of value.
void push_back (T &&value)
 Append a moved value.
T & operator[] (std::size_t i)
const T & operator[] (std::size_t i) const
T * begin ()
T * end ()
const T * begin () const
const T * end () const
bool operator== (const SmallVector &o) const
bool operator!= (const SmallVector &o) const

Detailed Description

template<typename T, std::size_t N>
class ZenitUI::SmallVector< T, N >

A vector whose first N elements live inside the object.

Template Parameters
TElement type. Must be default-constructible for the inline array.
NNumber of elements stored inline before a heap allocation is made.

Behaves like std::vector<T> but avoids a heap allocation for small payloads (typically 2–8 elements). When size() exceeds N, the inline contents are moved into an internal std::vector and all further growth uses that heap buffer.

The inline_ array is always present, so the object size is at least N * sizeof(T). Choose N accordingly.

Note
Copy and move operations are defaulted and forward to the inline array and the internal heap vector as appropriate. The inline → heap promotion is not exception-safe against throwing move constructors of T; for trivially-copyable element types (the intended use case) this is irrelevant.
Warning
Iterators returned by begin()/end() are invalidated whenever the vector transitions from inline to heap storage, and on the usual heap reallocation.

Member Function Documentation

◆ clear()

template<typename T, std::size_t N>
void ZenitUI::SmallVector< T, N >::clear ( )
inline

Remove all elements.

Does not free the heap buffer if one exists.

◆ push_back()

template<typename T, std::size_t N>
void ZenitUI::SmallVector< T, N >::push_back ( const T & value)
inline

Append a copy of value.

Triggers inline → heap promotion on overflow.

◆ reserve()

template<typename T, std::size_t N>
void ZenitUI::SmallVector< T, N >::reserve ( std::size_t n)
inline

Reserve storage for at least n elements.

Values n <= N are no-ops: inline storage is already sufficient.


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