Donner
C++20 SVG rendering library
Loading...
Searching...
No Matches
donner::SmallVector< T, DefaultSize > Class Template Reference

A vector with small-size optimization. More...

#include "donner/base/SmallVector.h"

Public Types

using value_type = T
 Type of elements stored in the vector.
 
using size_type = std::size_t
 Type used to represent the size of the vector.
 
using reference = T&
 Reference to an element in the vector.
 
using const_reference = const T&
 Const reference to an element in the vector.
 
using iterator = T*
 Iterator to an element in the vector.
 
using const_iterator = const T*
 Const iterator to an element in the vector.
 

Public Member Functions

 SmallVector () noexcept
 Constructs an empty SmallVector.
 
 SmallVector (std::initializer_list< T > init)
 Constructs a SmallVector with the elements from the initializer list.
 
 ~SmallVector ()
 Destructor for SmallVector.
 
 SmallVector (const SmallVector &other)
 Copy constructor.
 
SmallVectoroperator= (const SmallVector &other)
 Copy assignment operator.
 
 SmallVector (SmallVector &&other) noexcept
 Move constructor.
 
SmallVectoroperator= (SmallVector &&other) noexcept
 Move assignment operator.
 
void push_back (const T &value)
 Adds an element to the end of the vector.
 
void pop_back () noexcept
 Removes the last element from the vector.
 
void clear () noexcept
 Clears the contents of the vector.
 
T * data () noexcept
 Gets the data stored in the vector.
 
const T * data () const noexcept
 Gets the data stored in the vector (const version).
 
std::size_t size () const noexcept
 Returns the number of elements in the vector.
 
std::size_t capacity () const noexcept
 Returns the capacity of the vector.
 
bool empty () const noexcept
 Checks if the vector is empty.
 
T & operator[] (std::size_t index) noexcept(false)
 Accesses the element at the specified index.
 
const T & operator[] (std::size_t index) const noexcept(false)
 Accesses the element at the specified index (const version).
 
iterator begin () noexcept
 Returns an iterator to the beginning of the vector.
 
iterator end () noexcept
 Returns an iterator to the end of the vector.
 
const_iterator begin () const noexcept
 Returns a const iterator to the beginning of the vector.
 
const_iterator end () const noexcept
 Returns a const iterator to the end of the vector.
 

Detailed Description

template<typename T, std::size_t DefaultSize>
class donner::SmallVector< T, DefaultSize >

A vector with small-size optimization.

This vector can store a small number of elements on the stack. If the number of elements exceeds the stack capacity, it will allocate memory on the heap. This can reduce memory allocations for small vectors.

Template Parameters
TType of elements stored in the vector.
DefaultSizeNumber of elements that can be stored on the stack.

Constructor & Destructor Documentation

◆ SmallVector() [1/3]

template<typename T , std::size_t DefaultSize>
donner::SmallVector< T, DefaultSize >::SmallVector ( std::initializer_list< T > init)
inline

Constructs a SmallVector with the elements from the initializer list.

Parameters
initInitializer list to initialize the elements of the vector.

◆ SmallVector() [2/3]

template<typename T , std::size_t DefaultSize>
donner::SmallVector< T, DefaultSize >::SmallVector ( const SmallVector< T, DefaultSize > & other)
inline

Copy constructor.

Parameters
otherThe SmallVector to copy from.

◆ SmallVector() [3/3]

template<typename T , std::size_t DefaultSize>
donner::SmallVector< T, DefaultSize >::SmallVector ( SmallVector< T, DefaultSize > && other)
inlinenoexcept

Move constructor.

Parameters
otherThe SmallVector to move from.

Member Function Documentation

◆ empty()

template<typename T , std::size_t DefaultSize>
bool donner::SmallVector< T, DefaultSize >::empty ( ) const
inlinenoexcept

Checks if the vector is empty.

Returns
True if the vector is empty, false otherwise.

◆ operator=() [1/2]

template<typename T , std::size_t DefaultSize>
SmallVector & donner::SmallVector< T, DefaultSize >::operator= ( const SmallVector< T, DefaultSize > & other)
inline

Copy assignment operator.

Parameters
otherThe SmallVector to copy from.
Returns
Reference to this SmallVector.

◆ operator=() [2/2]

template<typename T , std::size_t DefaultSize>
SmallVector & donner::SmallVector< T, DefaultSize >::operator= ( SmallVector< T, DefaultSize > && other)
inlinenoexcept

Move assignment operator.

Parameters
otherThe SmallVector to move from.
Returns
Reference to this SmallVector.

◆ operator[]() [1/2]

template<typename T , std::size_t DefaultSize>
const T & donner::SmallVector< T, DefaultSize >::operator[] ( std::size_t index) const
inline

Accesses the element at the specified index (const version).

Parameters
indexIndex of the element to access.
Returns
Const reference to the element at the specified index.

◆ operator[]() [2/2]

template<typename T , std::size_t DefaultSize>
T & donner::SmallVector< T, DefaultSize >::operator[] ( std::size_t index)
inline

Accesses the element at the specified index.

Parameters
indexIndex of the element to access.
Returns
Reference to the element at the specified index.

◆ push_back()

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

Adds an element to the end of the vector.

Parameters
valueThe value to add.

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