Ocean
Ocean::StackHeapVector< T, tStackCapacity > Class Template Reference

Vector like data structure combining stack and heap memory. More...

Inheritance diagram for Ocean::StackHeapVector< T, tStackCapacity >:

Data Structures

class  ConstIterator
 Definition of an iterator allowing to iterate through the vector. More...
 
class  Iterator
 Definition of an iterator allowing to iterate through the vector. More...
 

Public Member Functions

 StackHeapVector ()
 Creates a new vector object. More...
 
 StackHeapVector (const size_t size, const T &element)
 Creates a new vector object. More...
 
void pushBack (T &&element)
 Pushes a new element to the end of this vector. More...
 
void pushBack (const T &element)
 Pushes a new element to the end of this vector. More...
 
template<typename... TArgs>
T & emplaceBack (TArgs &&... args)
 Emplaces a new element to the end of this vector. More...
 
void popBack ()
 Removed the last elements from the vector. More...
 
void resize (const size_t size)
 Resizes the vector. More...
 
void assign (const size_t size, const T &element)
 Replaces the content of the vector with 'size' copies of the provided element. More...
 
size_t size () const
 Returns the number of elements of this vector. More...
 
size_t capacity () const
 Returns the overall capacity of this vector (including the capacity on the stack and on the heap). More...
 
bool isEmpty () const
 Returns whether this vector is empty. More...
 
void clear ()
 Clears this vector. More...
 
void setCapacity (const size_t capacity)
 Sets the capacity of this vector to a specified number of elements. More...
 
const T & front () const
 Returns the first element of this vector. More...
 
T & front ()
 Returns the first element of this vector. More...
 
const T & back () const
 Returns the last element of this vector. More...
 
T & back ()
 Returns the last element of this vector. More...
 
Iterator begin ()
 Returns an iterator to the first element in this vector. More...
 
Iterator end ()
 Returns an iterator to the element following the last element in this vector. More...
 
ConstIterator begin () const
 Returns a const iterator to the first element in this vector. More...
 
ConstIterator end () const
 Returns an iterator to the element following the last element in this vector. More...
 
const T & operator[] (const size_t index) const
 Elements access operator. More...
 
T & operator[] (const size_t index)
 Elements access operator. More...
 

Protected Attributes

stackElements_ [tStackCapacity]
 The elements located on the stack. More...
 
std::vector< T > heapElements_
 The remaining elements located on the heap. More...
 
size_t size_ = 0
 The number of elements in this vector. More...
 

Detailed Description

template<typename T, size_t tStackCapacity>
class Ocean::StackHeapVector< T, tStackCapacity >

Vector like data structure combining stack and heap memory.

This class implements a vector-like data structure which stores the first tStackCapacity elements on the stack, and any additional elements on the heap.
This approach can optimize performance and memory usage when the number of elements is often within the tStackCapacity but can occasionally exceed it.

Template Parameters
TData type of the elements that will be stored
tStackCapacityNumber of elements that can be stored on the stack, with range [1, infinity)

Constructor & Destructor Documentation

◆ StackHeapVector() [1/2]

template<typename T , size_t tStackCapacity>
Ocean::StackHeapVector< T, tStackCapacity >::StackHeapVector

Creates a new vector object.

◆ StackHeapVector() [2/2]

template<typename T , size_t tStackCapacity>
Ocean::StackHeapVector< T, tStackCapacity >::StackHeapVector ( const size_t  size,
const T &  element 
)

Creates a new vector object.

Parameters
sizeThe number of elements to be created
elementThe value that will be created in the first 'number' elements of this vector

Member Function Documentation

◆ assign()

template<typename T , size_t tStackCapacity>
void Ocean::StackHeapVector< T, tStackCapacity >::assign ( const size_t  size,
const T &  element 
)

Replaces the content of the vector with 'size' copies of the provided element.

Parameters
sizeThe new size of the vector, with range [0, infinity)
elementThe element which will be copied into all places of the resized vector

◆ back() [1/2]

template<typename T , size_t tStackCapacity>
T & Ocean::StackHeapVector< T, tStackCapacity >::back

Returns the last element of this vector.

Ensure that the vector is not empty before calling this function.

Returns
The vector's last element

◆ back() [2/2]

template<typename T , size_t tStackCapacity>
const T & Ocean::StackHeapVector< T, tStackCapacity >::back

Returns the last element of this vector.

Ensure that the vector is not empty before calling this function.

Returns
The vector's last element

◆ begin() [1/2]

template<typename T , size_t tStackCapacity>
StackHeapVector< T, tStackCapacity >::Iterator Ocean::StackHeapVector< T, tStackCapacity >::begin

Returns an iterator to the first element in this vector.

Returns
The vector's iterator to the first element

◆ begin() [2/2]

template<typename T , size_t tStackCapacity>
StackHeapVector< T, tStackCapacity >::ConstIterator Ocean::StackHeapVector< T, tStackCapacity >::begin

Returns a const iterator to the first element in this vector.

Returns
The vector's iterator to the first element

◆ capacity()

template<typename T , size_t tStackCapacity>
size_t Ocean::StackHeapVector< T, tStackCapacity >::capacity
inline

Returns the overall capacity of this vector (including the capacity on the stack and on the heap).

Returns
The vector's capacity, with range [size(), infinity)

◆ clear()

template<typename T , size_t tStackCapacity>
void Ocean::StackHeapVector< T, tStackCapacity >::clear

Clears this vector.

All stack elements will be overwritten with default values.

◆ emplaceBack()

template<typename T , size_t tStackCapacity>
template<typename... TArgs>
T & Ocean::StackHeapVector< T, tStackCapacity >::emplaceBack ( TArgs &&...  args)

Emplaces a new element to the end of this vector.

Parameters
argsThe arguments of the new element
Template Parameters
TArgsThe data types of the arguments

◆ end() [1/2]

template<typename T , size_t tStackCapacity>
StackHeapVector< T, tStackCapacity >::Iterator Ocean::StackHeapVector< T, tStackCapacity >::end

Returns an iterator to the element following the last element in this vector.

Returns
The vector's iterator to the element following the last element

◆ end() [2/2]

template<typename T , size_t tStackCapacity>
StackHeapVector< T, tStackCapacity >::ConstIterator Ocean::StackHeapVector< T, tStackCapacity >::end

Returns an iterator to the element following the last element in this vector.

Returns
The vector's iterator to the element following the last element

◆ front() [1/2]

template<typename T , size_t tStackCapacity>
T & Ocean::StackHeapVector< T, tStackCapacity >::front

Returns the first element of this vector.

Ensure that the vector is not empty before calling this function.

Returns
The vector's first element

◆ front() [2/2]

template<typename T , size_t tStackCapacity>
const T & Ocean::StackHeapVector< T, tStackCapacity >::front

Returns the first element of this vector.

Ensure that the vector is not empty before calling this function.

Returns
The vector's first element

◆ isEmpty()

template<typename T , size_t tStackCapacity>
bool Ocean::StackHeapVector< T, tStackCapacity >::isEmpty
inline

Returns whether this vector is empty.

Returns
True, if so

◆ operator[]() [1/2]

template<typename T , size_t tStackCapacity>
T & Ocean::StackHeapVector< T, tStackCapacity >::operator[] ( const size_t  index)
inline

Elements access operator.

Parameters
indexThe index of the element to access, with range [0, size() - 1]
Returns
The element with specified index

◆ operator[]() [2/2]

template<typename T , size_t tStackCapacity>
const T & Ocean::StackHeapVector< T, tStackCapacity >::operator[] ( const size_t  index) const
inline

Elements access operator.

Parameters
indexThe index of the element to access, with range [0, size() - 1]
Returns
The element with specified index

◆ popBack()

template<typename T , size_t tStackCapacity>
void Ocean::StackHeapVector< T, tStackCapacity >::popBack

Removed the last elements from the vector.

The vector must not be empty.

◆ pushBack() [1/2]

template<typename T , size_t tStackCapacity>
void Ocean::StackHeapVector< T, tStackCapacity >::pushBack ( const T &  element)

Pushes a new element to the end of this vector.

Parameters
elementThe new element to be pushed

◆ pushBack() [2/2]

template<typename T , size_t tStackCapacity>
void Ocean::StackHeapVector< T, tStackCapacity >::pushBack ( T &&  element)

Pushes a new element to the end of this vector.

Parameters
elementThe new element to be pushed

◆ resize()

template<typename T , size_t tStackCapacity>
void Ocean::StackHeapVector< T, tStackCapacity >::resize ( const size_t  size)

Resizes the vector.

Parameters
sizeThe new size of the vector

◆ setCapacity()

template<typename T , size_t tStackCapacity>
void Ocean::StackHeapVector< T, tStackCapacity >::setCapacity ( const size_t  capacity)

Sets the capacity of this vector to a specified number of elements.

In case the specified capacity is smaller than the current capacity or is smaller than the number of elements in this vector, nothing happens.

Parameters
capacityThe capacity to set, with range [0, infinity)

◆ size()

template<typename T , size_t tStackCapacity>
size_t Ocean::StackHeapVector< T, tStackCapacity >::size
inline

Returns the number of elements of this vector.

Returns
The vector's number of elements, with range [0, infinity)

Field Documentation

◆ heapElements_

template<typename T , size_t tStackCapacity>
std::vector<T> Ocean::StackHeapVector< T, tStackCapacity >::heapElements_
protected

The remaining elements located on the heap.

◆ size_

template<typename T , size_t tStackCapacity>
size_t Ocean::StackHeapVector< T, tStackCapacity >::size_ = 0
protected

The number of elements in this vector.

◆ stackElements_

template<typename T , size_t tStackCapacity>
T Ocean::StackHeapVector< T, tStackCapacity >::stackElements_[tStackCapacity]
protected

The elements located on the stack.


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