Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
roc::core::Array< T, EmbeddedCapacity > Class Template Reference

Dynamic array. More...

#include <array.h>

Inheritance diagram for roc::core::Array< T, EmbeddedCapacity >:
Collaboration diagram for roc::core::Array< T, EmbeddedCapacity >:

Public Member Functions

 Array (IArena &arena)
 Initialize empty array with arena. More...
 
size_t capacity () const
 Get maximum number of elements that can be added without reallocation. More...
 
size_t size () const
 Get number of elements. More...
 
bool is_empty () const
 Check if size is zero. More...
 
T * data ()
 Get pointer to first element. More...
 
const T * data () const
 Get pointer to first element. More...
 
T & operator[] (size_t index)
 Get element at given position. More...
 
const T & operator[] (size_t index) const
 Get element at given position. More...
 
T & front ()
 Get reference to first element. More...
 
const T & front () const
 Get const reference to first element. More...
 
T & back ()
 Get reference to last element. More...
 
const T & back () const
 Get const reference to last element. More...
 
ROC_ATTR_NODISCARD bool push_back (const T &value)
 Append element to array. More...
 
void pop_back ()
 Remove last element from the array. More...
 
ROC_ATTR_NODISCARD bool resize (size_t new_size)
 Set array size. More...
 
void clear ()
 Set array size to zero. More...
 
ROC_ATTR_NODISCARD bool grow (size_t min_capacity)
 Increase array capacity. More...
 
ROC_ATTR_NODISCARD bool grow_exp (size_t min_capacity)
 Increase array capacity exponentially. More...
 

Detailed Description

template<class T, size_t EmbeddedCapacity = 0>
class roc::core::Array< T, EmbeddedCapacity >

Dynamic array.

Elements are stored continuously in a memory chunk allocated using IArena, or directly in Array object when number of elements is small.

Array supports resizing and inserting and removing elements in the end with amortized O(1) complexity.

Template Parameters
Tdefines array element type. It should have default constructor, copy constructor, and assignment operator.
EmbeddedCapacitydefines number of elements in the fixed-size chunk embedded directly into Array object; it is used instead of dynamic memory if the array size is small enough.

Definition at line 40 of file array.h.

Constructor & Destructor Documentation

◆ Array()

template<class T , size_t EmbeddedCapacity = 0>
roc::core::Array< T, EmbeddedCapacity >::Array ( IArena arena)
inlineexplicit

Initialize empty array with arena.

Remarks
Array capacity may grow using arena.

Definition at line 45 of file array.h.

Member Function Documentation

◆ back() [1/2]

template<class T , size_t EmbeddedCapacity = 0>
T& roc::core::Array< T, EmbeddedCapacity >::back ( )
inline

Get reference to last element.

Definition at line 134 of file array.h.

◆ back() [2/2]

template<class T , size_t EmbeddedCapacity = 0>
const T& roc::core::Array< T, EmbeddedCapacity >::back ( ) const
inline

Get const reference to last element.

Definition at line 142 of file array.h.

◆ capacity()

template<class T , size_t EmbeddedCapacity = 0>
size_t roc::core::Array< T, EmbeddedCapacity >::capacity ( ) const
inline

Get maximum number of elements that can be added without reallocation.

Definition at line 61 of file array.h.

◆ clear()

template<class T , size_t EmbeddedCapacity = 0>
void roc::core::Array< T, EmbeddedCapacity >::clear ( )
inline

Set array size to zero.

Remarks
Never fails.

Definition at line 207 of file array.h.

◆ data() [1/2]

template<class T , size_t EmbeddedCapacity = 0>
T* roc::core::Array< T, EmbeddedCapacity >::data ( )
inline

Get pointer to first element.

Remarks
Returns null if the array is empty.

Definition at line 78 of file array.h.

◆ data() [2/2]

template<class T , size_t EmbeddedCapacity = 0>
const T* roc::core::Array< T, EmbeddedCapacity >::data ( ) const
inline

Get pointer to first element.

Remarks
Returns null if the array is empty.

Definition at line 88 of file array.h.

◆ front() [1/2]

template<class T , size_t EmbeddedCapacity = 0>
T& roc::core::Array< T, EmbeddedCapacity >::front ( )
inline

Get reference to first element.

Definition at line 118 of file array.h.

◆ front() [2/2]

template<class T , size_t EmbeddedCapacity = 0>
const T& roc::core::Array< T, EmbeddedCapacity >::front ( ) const
inline

Get const reference to first element.

Definition at line 126 of file array.h.

◆ grow()

template<class T , size_t EmbeddedCapacity = 0>
ROC_ATTR_NODISCARD bool roc::core::Array< T, EmbeddedCapacity >::grow ( size_t  min_capacity)
inline

Increase array capacity.

Remarks
If min_capacity is greater than the current capacity, a larger memory region is allocated and the array elements are copied there.
Returns
false if the allocation failed.

Definition at line 217 of file array.h.

◆ grow_exp()

template<class T , size_t EmbeddedCapacity = 0>
ROC_ATTR_NODISCARD bool roc::core::Array< T, EmbeddedCapacity >::grow_exp ( size_t  min_capacity)
inline

Increase array capacity exponentially.

Remarks
If min_capacity is greater than the current capacity, a larger memory region is allocated and the array elements are copied there. The size growth will follow the sequence: 0, 2, 4, 8, 16, ... until it reaches some threshold, and then starts growing linearly.
Returns
false if the allocation failed.

Definition at line 258 of file array.h.

◆ is_empty()

template<class T , size_t EmbeddedCapacity = 0>
bool roc::core::Array< T, EmbeddedCapacity >::is_empty ( ) const
inline

Check if size is zero.

Definition at line 71 of file array.h.

◆ operator[]() [1/2]

template<class T , size_t EmbeddedCapacity = 0>
T& roc::core::Array< T, EmbeddedCapacity >::operator[] ( size_t  index)
inline

Get element at given position.

Precondition
Panics if index is out of bounds.

Definition at line 98 of file array.h.

◆ operator[]() [2/2]

template<class T , size_t EmbeddedCapacity = 0>
const T& roc::core::Array< T, EmbeddedCapacity >::operator[] ( size_t  index) const
inline

Get element at given position.

Precondition
Panics if index is out of bounds.

Definition at line 109 of file array.h.

◆ pop_back()

template<class T , size_t EmbeddedCapacity = 0>
void roc::core::Array< T, EmbeddedCapacity >::pop_back ( )
inline

Remove last element from the array.

Precondition
Panics if array is empty.

Definition at line 168 of file array.h.

◆ push_back()

template<class T , size_t EmbeddedCapacity = 0>
ROC_ATTR_NODISCARD bool roc::core::Array< T, EmbeddedCapacity >::push_back ( const T &  value)
inline

Append element to array.

Returns
false if the allocation failed.
Note
has amortized O(1) complexity, O(n) in worst case.

Definition at line 154 of file array.h.

◆ resize()

template<class T , size_t EmbeddedCapacity = 0>
ROC_ATTR_NODISCARD bool roc::core::Array< T, EmbeddedCapacity >::resize ( size_t  new_size)
inline

Set array size.

Remarks
Calls grow() to ensure that there is enough space in array.
Returns
false if the allocation failed

Definition at line 183 of file array.h.

◆ size()

template<class T , size_t EmbeddedCapacity = 0>
size_t roc::core::Array< T, EmbeddedCapacity >::size ( ) const
inline

Get number of elements.

Definition at line 66 of file array.h.


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