Vulpes  0.1.0
Versatile and parallel compUtational Library for high-order discontinuous Polytopal Element Simulations
Loading...
Searching...
No Matches
vulpes::datastructure::SmallVector< T, N > Class Template Reference

#include <crs_map.hpp>

Public Types

using iterator = T*
 Iterator for mutable access.
using const_iterator = const T*
 Iterator for constant access.

Public Member Functions

iterator begin ()
 Returns an iterator pointing to the beginning of the storage.
iterator end ()
 Returns an iterator pointing to the end of the storage.
const_iterator begin () const
 Returns a const iterator pointing to the beginning of the storage.
const_iterator end () const
 Returns a const iterator pointing to the end of the storage.
 SmallVector ()
 Default constructor.
 SmallVector (std::initializer_list< T > init_list)
 Constructor to initialize the vector with an initializer list.
 SmallVector (const T *begin, const T *end)
void push_back (const T &value)
 Add an element to the end of the vector.
template<typename... Args>
void emplace_back (Args &&... args)
 Constructs an element in-place at the end of the vector.
template<class InputIt>
void assign (InputIt first, InputIt last)
 Assign the values in range [first, last) to the container.
T & operator[] (std::size_t index)
 Access an element by index (mutable).
const T & operator[] (std::size_t index) const
 Access an element by index (constant).
constexpr std::size_t size () const
 Get the number of elements in the vector.

Private Types

using StaticStorage = std::array<T, N>
 Static array for small sizes.
using DynamicStorage = std::vector<T>
 Dynamic storage for larger sizes.

Private Attributes

std::variant< StaticStorage, DynamicStorage > m_storage
 Variant storing either static or dynamic memory.
std::size_t m_size
 Number of elements in the vector.

Detailed Description

template<typename T, std::size_t N>
class vulpes::datastructure::SmallVector< T, N >
Template Parameters
TThe type of the elements to be stored.
NThe maximum size before switching to dynamic storage.

This class behaves like a vector but uses coalesced memory for small sizes (up to N elements). Beyond that, it switches to dynamic memory.

Note
: iteration on the elements via iterators might be more performant than access with operator[] since the latter has a size check at each call

Member Typedef Documentation

◆ const_iterator

template<typename T, std::size_t N>
using vulpes::datastructure::SmallVector< T, N >::const_iterator = const T*

Iterator for constant access.

◆ DynamicStorage

template<typename T, std::size_t N>
using vulpes::datastructure::SmallVector< T, N >::DynamicStorage = std::vector<T>
private

Dynamic storage for larger sizes.

◆ iterator

template<typename T, std::size_t N>
using vulpes::datastructure::SmallVector< T, N >::iterator = T*

Iterator for mutable access.

◆ StaticStorage

template<typename T, std::size_t N>
using vulpes::datastructure::SmallVector< T, N >::StaticStorage = std::array<T, N>
private

Static array for small sizes.

Constructor & Destructor Documentation

◆ SmallVector() [1/3]

template<typename T, std::size_t N>
vulpes::datastructure::SmallVector< T, N >::SmallVector ( )
inline

Default constructor.

◆ SmallVector() [2/3]

template<typename T, std::size_t N>
vulpes::datastructure::SmallVector< T, N >::SmallVector ( std::initializer_list< T > init_list)
inline

Constructor to initialize the vector with an initializer list.

Parameters
init_listList of elements to initialize the vector.

◆ SmallVector() [3/3]

template<typename T, std::size_t N>
vulpes::datastructure::SmallVector< T, N >::SmallVector ( const T * begin,
const T * end )
inline

Member Function Documentation

◆ assign()

template<typename T, std::size_t N>
template<class InputIt>
void vulpes::datastructure::SmallVector< T, N >::assign ( InputIt first,
InputIt last )
inline

Assign the values in range [first, last) to the container.

Template Parameters
InputItThe iterator type.
Parameters
firstThe begin of the assign range.
lastThe end of the assign range.

◆ begin() [1/2]

template<typename T, std::size_t N>
iterator vulpes::datastructure::SmallVector< T, N >::begin ( )
inline

Returns an iterator pointing to the beginning of the storage.

This function provides an iterator to the first element of the underlying storage, whether it's statically or dynamically allocated. The type of storage (either StaticStorage or DynamicStorage) is determined at runtime using std::variant.

Returns
iterator A pointer to the first element of the storage.

◆ begin() [2/2]

template<typename T, std::size_t N>
const_iterator vulpes::datastructure::SmallVector< T, N >::begin ( ) const
inline

Returns a const iterator pointing to the beginning of the storage.

This const-qualified version provides a constant iterator to the first element of the storage, ensuring that the data cannot be modified. It supports both StaticStorage and DynamicStorage.

Returns
const_iterator A constant pointer to the first element of the storage.

◆ emplace_back()

template<typename T, std::size_t N>
template<typename... Args>
void vulpes::datastructure::SmallVector< T, N >::emplace_back ( Args &&... args)
inline

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

This method constructs an element directly in the storage without the need for creating a temporary object, providing better performance in some cases.

Template Parameters
ArgsTypes of the arguments to forward to the constructor of T.
Parameters
argsArguments to forward to the constructor of T.

◆ end() [1/2]

template<typename T, std::size_t N>
iterator vulpes::datastructure::SmallVector< T, N >::end ( )
inline

Returns an iterator pointing to the end of the storage.

This function provides an iterator to one past the last element of the underlying storage. It ensures that the iterator points to the correct end, whether using StaticStorage or DynamicStorage.

Returns
iterator A pointer to one past the last element of the storage.

◆ end() [2/2]

template<typename T, std::size_t N>
const_iterator vulpes::datastructure::SmallVector< T, N >::end ( ) const
inline

Returns a const iterator pointing to the end of the storage.

This const-qualified version provides a constant iterator to the last element of the storage, ensuring that the data cannot be modified. It supports both StaticStorage and DynamicStorage.

Returns
const_iterator A constant pointer to the last element of the storage.

◆ operator[]() [1/2]

template<typename T, std::size_t N>
T & vulpes::datastructure::SmallVector< T, N >::operator[] ( std::size_t index)
inline

Access an element by index (mutable).

Parameters
indexThe index of the element to access.
Returns
Reference to the element.

◆ operator[]() [2/2]

template<typename T, std::size_t N>
const T & vulpes::datastructure::SmallVector< T, N >::operator[] ( std::size_t index) const
inline

Access an element by index (constant).

Parameters
indexThe index of the element to access.
Returns
Constant reference to the element.

◆ push_back()

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

Add an element to the end of the vector.

Parameters
valueThe value to be added.

◆ size()

template<typename T, std::size_t N>
std::size_t vulpes::datastructure::SmallVector< T, N >::size ( ) const
inlineconstexpr

Get the number of elements in the vector.

Returns
The size of the vector.

Member Data Documentation

◆ m_size

template<typename T, std::size_t N>
std::size_t vulpes::datastructure::SmallVector< T, N >::m_size
private

Number of elements in the vector.

◆ m_storage

template<typename T, std::size_t N>
std::variant<StaticStorage, DynamicStorage> vulpes::datastructure::SmallVector< T, N >::m_storage
private

Variant storing either static or dynamic memory.


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