Zth (libzth)
zth::small_vector< T, Prealloc, Allocator > Class Template Reference

A simple std::vector, which can contain Prealloc without heap allocation. More...

#include <util.h>

Public Types

enum  
 
typedef T value_type
 
typedef Allocator allocator_type
 
typedef std::vector< value_type, allocator_typevector_type
 

Public Member Functions

constexpr small_vector () noexcept
 Default ctor. More...
 
 ~small_vector ()
 Dtor. More...
 
value_typeoperator[] (size_t index) noexcept
 Access an element. More...
 
value_type const & operator[] (size_t index) const noexcept
 Access an element. More...
 
value_typefront () noexcept
 Access the first element. More...
 
value_type const & front () const noexcept
 Access the first element. More...
 
value_typeback () noexcept
 Access the last element. More...
 
value_type const & back () const noexcept
 Access the last element. More...
 
value_typedata () noexcept
 Access the data array. More...
 
value_type const * data () const noexcept
 Access the data array. More...
 
bool empty () const noexcept
 Check if the vector is empty. More...
 
size_t size () const noexcept
 Return the number of elements stored in the vector. More...
 
void reserve (size_t new_cap)
 Reserve memory to accommodate at least the given number of elements. More...
 
size_t capacity () const noexcept
 Return the number of elements for which memory is currently reesrved. More...
 
void clear () noexcept
 Erase all elements from the vector. More...
 
void clear_and_release () noexcept
 Erase all elements from the vector and release all heap memory. More...
 
void push_back (value_type const &v)
 Append an element to the vector using the copy constructor. More...
 
template<class... Args>
void emplace_back (Args &&... args)
 Append an element to the vector by construct in-place. More...
 
void pop_back () noexcept
 Remove the last element. More...
 
void resize (size_t count, value_type const &x=value_type())
 Resize the number of elements in the vector. More...
 

Protected Member Functions

bool is_small () const noexcept
 Check if the current vector still fits in the internal buffer. More...
 
value_typearray () noexcept
 Interpret buffer as the small vector. More...
 
value_type const * array () const noexcept
 Interpret buffer as the small vector. More...
 
vector_typevector () noexcept
 Interpret buffer as an std::vector. More...
 
vector_type const & vector () const noexcept
 Interpret buffer as an std::vector. More...
 
void make_vector (size_t new_cap)
 Make sure the std::vector is used as storage. More...
 

Detailed Description

template<typename T, int8_t Prealloc = 1, typename Allocator = typename Config::Allocator<T>::type>
class zth::small_vector< T, Prealloc, Allocator >

A simple std::vector, which can contain Prealloc without heap allocation.

When the internal buffer is exhausted, the vector grows automatically into an std::vector, using heap memory after all.

In all cases, the elements are stored in contiguous memory, like guaranteed by std::vector. Upon a push_back(), underlying memory may be reallocated to accommodate the new element, which renders previous pointers invalid.

Template Parameters
Tthe type of elements to contain
Preallocthe minimum number of elements to contain without using heap
Allocatorthe allocator to use for std::vector

Definition at line 1021 of file util.h.

Member Typedef Documentation

◆ allocator_type

template<typename T , int8_t Prealloc = 1, typename Allocator = typename Config::Allocator<T>::type>
typedef Allocator zth::small_vector< T, Prealloc, Allocator >::allocator_type

Definition at line 1024 of file util.h.

◆ value_type

template<typename T , int8_t Prealloc = 1, typename Allocator = typename Config::Allocator<T>::type>
typedef T zth::small_vector< T, Prealloc, Allocator >::value_type

Definition at line 1023 of file util.h.

◆ vector_type

template<typename T , int8_t Prealloc = 1, typename Allocator = typename Config::Allocator<T>::type>
typedef std::vector<value_type, allocator_type> zth::small_vector< T, Prealloc, Allocator >::vector_type

Definition at line 1025 of file util.h.

Member Enumeration Documentation

◆ anonymous enum

template<typename T , int8_t Prealloc = 1, typename Allocator = typename Config::Allocator<T>::type>
anonymous enum

Definition at line 1027 of file util.h.

Constructor & Destructor Documentation

◆ small_vector()

template<typename T , int8_t Prealloc = 1, typename Allocator = typename Config::Allocator<T>::type>
constexpr zth::small_vector< T, Prealloc, Allocator >::small_vector ( )
inlineconstexprnoexcept

Default ctor.

Definition at line 1039 of file util.h.

◆ ~small_vector()

template<typename T , int8_t Prealloc = 1, typename Allocator = typename Config::Allocator<T>::type>
zth::small_vector< T, Prealloc, Allocator >::~small_vector ( )
inline

Dtor.

Definition at line 1048 of file util.h.

Member Function Documentation

◆ array() [1/2]

template<typename T , int8_t Prealloc = 1, typename Allocator = typename Config::Allocator<T>::type>
value_type const* zth::small_vector< T, Prealloc, Allocator >::array ( ) const
inlineprotectednoexcept

Interpret buffer as the small vector.

Definition at line 1291 of file util.h.

◆ array() [2/2]

template<typename T , int8_t Prealloc = 1, typename Allocator = typename Config::Allocator<T>::type>
value_type* zth::small_vector< T, Prealloc, Allocator >::array ( )
inlineprotectednoexcept

Interpret buffer as the small vector.

Definition at line 1282 of file util.h.

◆ back() [1/2]

template<typename T , int8_t Prealloc = 1, typename Allocator = typename Config::Allocator<T>::type>
value_type const& zth::small_vector< T, Prealloc, Allocator >::back ( ) const
inlinenoexcept

Access the last element.

Do not call when the vector is empty().

Definition at line 1110 of file util.h.

◆ back() [2/2]

template<typename T , int8_t Prealloc = 1, typename Allocator = typename Config::Allocator<T>::type>
value_type& zth::small_vector< T, Prealloc, Allocator >::back ( )
inlinenoexcept

Access the last element.

Do not call when the vector is empty().

Definition at line 1099 of file util.h.

◆ capacity()

template<typename T , int8_t Prealloc = 1, typename Allocator = typename Config::Allocator<T>::type>
size_t zth::small_vector< T, Prealloc, Allocator >::capacity ( ) const
inlinenoexcept

Return the number of elements for which memory is currently reesrved.

See also
reserve()

Definition at line 1168 of file util.h.

◆ clear()

template<typename T , int8_t Prealloc = 1, typename Allocator = typename Config::Allocator<T>::type>
void zth::small_vector< T, Prealloc, Allocator >::clear ( )
inlinenoexcept

Erase all elements from the vector.

This does not release the memory.

Definition at line 1178 of file util.h.

◆ clear_and_release()

template<typename T , int8_t Prealloc = 1, typename Allocator = typename Config::Allocator<T>::type>
void zth::small_vector< T, Prealloc, Allocator >::clear_and_release ( )
inlinenoexcept

Erase all elements from the vector and release all heap memory.

Definition at line 1189 of file util.h.

◆ data() [1/2]

template<typename T , int8_t Prealloc = 1, typename Allocator = typename Config::Allocator<T>::type>
value_type const* zth::small_vector< T, Prealloc, Allocator >::data ( ) const
inlinenoexcept

Access the data array.

Definition at line 1127 of file util.h.

◆ data() [2/2]

template<typename T , int8_t Prealloc = 1, typename Allocator = typename Config::Allocator<T>::type>
value_type* zth::small_vector< T, Prealloc, Allocator >::data ( )
inlinenoexcept

Access the data array.

Definition at line 1119 of file util.h.

◆ emplace_back()

template<typename T , int8_t Prealloc = 1, typename Allocator = typename Config::Allocator<T>::type>
template<class... Args>
void zth::small_vector< T, Prealloc, Allocator >::emplace_back ( Args &&...  args)
inline

Append an element to the vector by construct in-place.

Exceptions
std::bad_allocwhen allocation fails

Definition at line 1220 of file util.h.

◆ empty()

template<typename T , int8_t Prealloc = 1, typename Allocator = typename Config::Allocator<T>::type>
bool zth::small_vector< T, Prealloc, Allocator >::empty ( ) const
inlinenoexcept

Check if the vector is empty.

Definition at line 1135 of file util.h.

◆ front() [1/2]

template<typename T , int8_t Prealloc = 1, typename Allocator = typename Config::Allocator<T>::type>
value_type const& zth::small_vector< T, Prealloc, Allocator >::front ( ) const
inlinenoexcept

Access the first element.

Do not call when the vector is empty().

Definition at line 1089 of file util.h.

◆ front() [2/2]

template<typename T , int8_t Prealloc = 1, typename Allocator = typename Config::Allocator<T>::type>
value_type& zth::small_vector< T, Prealloc, Allocator >::front ( )
inlinenoexcept

Access the first element.

Do not call when the vector is empty().

Definition at line 1079 of file util.h.

◆ is_small()

template<typename T , int8_t Prealloc = 1, typename Allocator = typename Config::Allocator<T>::type>
bool zth::small_vector< T, Prealloc, Allocator >::is_small ( ) const
inlineprotectednoexcept

Check if the current vector still fits in the internal buffer.

Definition at line 1274 of file util.h.

◆ make_vector()

template<typename T , int8_t Prealloc = 1, typename Allocator = typename Config::Allocator<T>::type>
void zth::small_vector< T, Prealloc, Allocator >::make_vector ( size_t  new_cap)
inlineprotected

Make sure the std::vector is used as storage.

Definition at line 1320 of file util.h.

◆ operator[]() [1/2]

template<typename T , int8_t Prealloc = 1, typename Allocator = typename Config::Allocator<T>::type>
value_type const& zth::small_vector< T, Prealloc, Allocator >::operator[] ( size_t  index) const
inlinenoexcept

Access an element.

Definition at line 1068 of file util.h.

◆ operator[]() [2/2]

template<typename T , int8_t Prealloc = 1, typename Allocator = typename Config::Allocator<T>::type>
value_type& zth::small_vector< T, Prealloc, Allocator >::operator[] ( size_t  index)
inlinenoexcept

Access an element.

Definition at line 1059 of file util.h.

◆ pop_back()

template<typename T , int8_t Prealloc = 1, typename Allocator = typename Config::Allocator<T>::type>
void zth::small_vector< T, Prealloc, Allocator >::pop_back ( )
inlinenoexcept

Remove the last element.

Make sure the vector is not empty() before calling.

Definition at line 1237 of file util.h.

◆ push_back()

template<typename T , int8_t Prealloc = 1, typename Allocator = typename Config::Allocator<T>::type>
void zth::small_vector< T, Prealloc, Allocator >::push_back ( value_type const &  v)
inline

Append an element to the vector using the copy constructor.

Exceptions
std::bad_allocwhen allocation fails

Definition at line 1203 of file util.h.

◆ reserve()

template<typename T , int8_t Prealloc = 1, typename Allocator = typename Config::Allocator<T>::type>
void zth::small_vector< T, Prealloc, Allocator >::reserve ( size_t  new_cap)
inline

Reserve memory to accommodate at least the given number of elements.

Exceptions
std::bad_allocwhen allocation fails
See also
capacity()

Definition at line 1153 of file util.h.

◆ resize()

template<typename T , int8_t Prealloc = 1, typename Allocator = typename Config::Allocator<T>::type>
void zth::small_vector< T, Prealloc, Allocator >::resize ( size_t  count,
value_type const &  x = value_type() 
)
inline

Resize the number of elements in the vector.

Parameters
countthe requested number of elements
xthe value to save for newly constructed elements

Definition at line 1251 of file util.h.

◆ size()

template<typename T , int8_t Prealloc = 1, typename Allocator = typename Config::Allocator<T>::type>
size_t zth::small_vector< T, Prealloc, Allocator >::size ( ) const
inlinenoexcept

Return the number of elements stored in the vector.

Definition at line 1143 of file util.h.

◆ vector() [1/2]

template<typename T , int8_t Prealloc = 1, typename Allocator = typename Config::Allocator<T>::type>
vector_type const& zth::small_vector< T, Prealloc, Allocator >::vector ( ) const
inlineprotectednoexcept

Interpret buffer as an std::vector.

Definition at line 1310 of file util.h.

◆ vector() [2/2]

template<typename T , int8_t Prealloc = 1, typename Allocator = typename Config::Allocator<T>::type>
vector_type& zth::small_vector< T, Prealloc, Allocator >::vector ( )
inlineprotectednoexcept

Interpret buffer as an std::vector.

Definition at line 1300 of file util.h.


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