dispenso 1.5.1
A library for task parallelism
Loading...
Searching...
No Matches
dispenso::SmallVector< T, N > Class Template Reference

#include <small_vector.h>

Public Member Functions

 SmallVector () noexcept
 
 SmallVector (size_type count)
 
 SmallVector (size_type count, const T &value)
 
 SmallVector (std::initializer_list< T > init)
 
 SmallVector (const SmallVector &other)
 
 SmallVector (SmallVector &&other) noexcept
 
reference front ()
 
const_reference front () const
 
reference back ()
 
const_reference back () const
 
pointer data () noexcept
 
const_pointer data () const noexcept
 
iterator begin () noexcept
 
const_iterator begin () const noexcept
 
const_iterator cbegin () const noexcept
 
iterator end () noexcept
 
const_iterator end () const noexcept
 
const_iterator cend () const noexcept
 
bool empty () const noexcept
 
size_type size () const noexcept
 
size_type capacity () const noexcept
 
void reserve (size_type newCap)
 
void clear () noexcept
 
void push_back (const T &value)
 
void push_back (T &&value)
 
template<typename... Args>
reference emplace_back (Args &&... args)
 
void pop_back ()
 
void resize (size_type count)
 
void resize (size_type count, const T &value)
 
iterator erase (const_iterator pos)
 

Detailed Description

template<typename T, size_t N = 4>
class dispenso::SmallVector< T, N >

A vector-like container that stores up to N elements inline (on the stack), avoiding heap allocation for small sizes. Falls back to heap allocation when the size exceeds N.

Template Parameters
TThe element type.
NThe number of elements to store inline (default 4). Must be in range [1, 65535].

Memory layout: Uses a union to share space between inline storage and heap pointer/capacity. The high bit of the size field tracks whether heap storage is active, so no additional bool or flag field is needed.

Once on heap, the vector stays on heap until clear() is called. This ensures reserve() capacity is preserved across push_back/pop_back sequences.

Definition at line 48 of file small_vector.h.

Constructor & Destructor Documentation

◆ SmallVector() [1/6]

template<typename T , size_t N = 4>
dispenso::SmallVector< T, N >::SmallVector ( )
inlinenoexcept

Construct an empty SmallVector.

Definition at line 66 of file small_vector.h.

◆ SmallVector() [2/6]

template<typename T , size_t N = 4>
dispenso::SmallVector< T, N >::SmallVector ( size_type count)
inlineexplicit

Construct a SmallVector with count default-constructed elements.

Parameters
countThe number of elements.

Definition at line 73 of file small_vector.h.

◆ SmallVector() [3/6]

template<typename T , size_t N = 4>
dispenso::SmallVector< T, N >::SmallVector ( size_type count,
const T & value )
inline

Construct a SmallVector with count copies of value.

Parameters
countThe number of elements.
valueThe value to copy into each element.

Definition at line 83 of file small_vector.h.

◆ SmallVector() [4/6]

template<typename T , size_t N = 4>
dispenso::SmallVector< T, N >::SmallVector ( std::initializer_list< T > init)
inline

Construct a SmallVector from an initializer list.

Parameters
initThe initializer list of elements.

Definition at line 92 of file small_vector.h.

◆ SmallVector() [5/6]

template<typename T , size_t N = 4>
dispenso::SmallVector< T, N >::SmallVector ( const SmallVector< T, N > & other)
inline

Copy constructor.

Definition at line 100 of file small_vector.h.

◆ SmallVector() [6/6]

template<typename T , size_t N = 4>
dispenso::SmallVector< T, N >::SmallVector ( SmallVector< T, N > && other)
inlinenoexcept

Move constructor. Moves elements and leaves other empty.

Definition at line 108 of file small_vector.h.

◆ ~SmallVector()

template<typename T , size_t N = 4>
dispenso::SmallVector< T, N >::~SmallVector ( )
inline

Definition at line 123 of file small_vector.h.

Member Function Documentation

◆ back() [1/2]

template<typename T , size_t N = 4>
reference dispenso::SmallVector< T, N >::back ( )
inline

Access the last element.

Definition at line 181 of file small_vector.h.

◆ back() [2/2]

template<typename T , size_t N = 4>
const_reference dispenso::SmallVector< T, N >::back ( ) const
inline

Access the last element.

Definition at line 185 of file small_vector.h.

◆ begin() [1/2]

template<typename T , size_t N = 4>
const_iterator dispenso::SmallVector< T, N >::begin ( ) const
inlinenoexcept

Return an iterator to the first element.

Definition at line 205 of file small_vector.h.

◆ begin() [2/2]

template<typename T , size_t N = 4>
iterator dispenso::SmallVector< T, N >::begin ( )
inlinenoexcept

Return an iterator to the first element.

Definition at line 201 of file small_vector.h.

◆ capacity()

template<typename T , size_t N = 4>
size_type dispenso::SmallVector< T, N >::capacity ( ) const
inlinenoexcept

Return the current capacity. Returns N while using inline storage, or the heap capacity otherwise.

Definition at line 240 of file small_vector.h.

◆ cbegin()

template<typename T , size_t N = 4>
const_iterator dispenso::SmallVector< T, N >::cbegin ( ) const
inlinenoexcept

Return an iterator to the first element.

Definition at line 209 of file small_vector.h.

◆ cend()

template<typename T , size_t N = 4>
const_iterator dispenso::SmallVector< T, N >::cend ( ) const
inlinenoexcept

Return an iterator past the last element.

Definition at line 221 of file small_vector.h.

◆ clear()

template<typename T , size_t N = 4>
void dispenso::SmallVector< T, N >::clear ( )
inlinenoexcept

Remove all elements and release heap storage (if any). After clear(), the vector returns to inline storage mode.

Definition at line 259 of file small_vector.h.

◆ data() [1/2]

template<typename T , size_t N = 4>
const_pointer dispenso::SmallVector< T, N >::data ( ) const
inlinenoexcept

Return a pointer to the underlying element storage.

Definition at line 194 of file small_vector.h.

◆ data() [2/2]

template<typename T , size_t N = 4>
pointer dispenso::SmallVector< T, N >::data ( )
inlinenoexcept

Return a pointer to the underlying element storage.

Definition at line 190 of file small_vector.h.

◆ emplace_back()

template<typename T , size_t N = 4>
template<typename... Args>
reference dispenso::SmallVector< T, N >::emplace_back ( Args &&... args)
inline

Construct an element in-place at the end.

Parameters
argsArguments forwarded to the element constructor.
Returns
A reference to the newly constructed element.

Definition at line 280 of file small_vector.h.

◆ empty()

template<typename T , size_t N = 4>
bool dispenso::SmallVector< T, N >::empty ( ) const
inlinenoexcept

Check whether the vector is empty.

Definition at line 228 of file small_vector.h.

◆ end() [1/2]

template<typename T , size_t N = 4>
const_iterator dispenso::SmallVector< T, N >::end ( ) const
inlinenoexcept

Return an iterator past the last element.

Definition at line 217 of file small_vector.h.

◆ end() [2/2]

template<typename T , size_t N = 4>
iterator dispenso::SmallVector< T, N >::end ( )
inlinenoexcept

Return an iterator past the last element.

Definition at line 213 of file small_vector.h.

◆ erase()

template<typename T , size_t N = 4>
iterator dispenso::SmallVector< T, N >::erase ( const_iterator pos)
inline

Erase the element at pos. Elements after pos are shifted left. Returns an iterator to the element that now occupies the erased position (or end() if the last element was erased).

Parameters
posIterator to the element to erase.
Returns
Iterator to the element following the erased one.

Definition at line 370 of file small_vector.h.

◆ front() [1/2]

template<typename T , size_t N = 4>
reference dispenso::SmallVector< T, N >::front ( )
inline

Access the first element.

Definition at line 173 of file small_vector.h.

◆ front() [2/2]

template<typename T , size_t N = 4>
const_reference dispenso::SmallVector< T, N >::front ( ) const
inline

Access the first element.

Definition at line 177 of file small_vector.h.

◆ pop_back()

template<typename T , size_t N = 4>
void dispenso::SmallVector< T, N >::pop_back ( )
inline

Remove the last element.

Definition at line 306 of file small_vector.h.

◆ push_back() [1/2]

template<typename T , size_t N = 4>
void dispenso::SmallVector< T, N >::push_back ( const T & value)
inline

Append a copy of value.

Definition at line 265 of file small_vector.h.

◆ push_back() [2/2]

template<typename T , size_t N = 4>
void dispenso::SmallVector< T, N >::push_back ( T && value)
inline

Append value by moving.

Definition at line 269 of file small_vector.h.

◆ reserve()

template<typename T , size_t N = 4>
void dispenso::SmallVector< T, N >::reserve ( size_type newCap)
inline

Reserve capacity for at least newCap elements. If newCap > N, transitions to heap storage. Once on heap, capacity is preserved until clear().

Definition at line 249 of file small_vector.h.

◆ resize() [1/2]

template<typename T , size_t N = 4>
void dispenso::SmallVector< T, N >::resize ( size_type count)
inline

Resize the vector to count elements, default-constructing new elements if growing.

Parameters
countThe desired number of elements.

Definition at line 319 of file small_vector.h.

◆ resize() [2/2]

template<typename T , size_t N = 4>
void dispenso::SmallVector< T, N >::resize ( size_type count,
const T & value )
inline

Resize the vector to count elements, copy-constructing new elements from value if growing.

Parameters
countThe desired number of elements.
valueThe value to copy into new elements.

Definition at line 344 of file small_vector.h.

◆ size()

template<typename T , size_t N = 4>
size_type dispenso::SmallVector< T, N >::size ( ) const
inlinenoexcept

Return the number of elements.

Definition at line 232 of file small_vector.h.


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