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 ()
 Initialize empty array without arena. More...
 
 Array (IArena &arena)
 Initialize empty array with arena. More...
 
size_t capacity () const
 Get maximum number of elements. If array has arena, capacity can be grown. 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...
 
ROC_ATTR_NODISCARD bool push_back (const T &value)
 Append element to array. More...
 
ROC_ATTR_NODISCARD bool resize (size_t sz)
 Set array size. More...
 
void clear ()
 Set array size to zero. More...
 
ROC_ATTR_NODISCARD bool grow (size_t max_sz)
 Increase array capacity. More...
 
ROC_ATTR_NODISCARD bool grow_exp (size_t min_size)
 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. Small chunks can be stored directly in Array object, without extra allocation. Array can be resized only by explicitly calling resize(), grow(), or grow_exp(). Elements are copied during resize and old copies are destroyed.

Template Parameters
Tdefines array element type. It should have copy constructor and destructor.
EmbeddedCapacitydefines the size of the fixed-size array embedded directly into Array object; it is used instead of dynamic memory if the array size is small enough.

Definition at line 39 of file array.h.

Constructor & Destructor Documentation

◆ Array() [1/2]

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

Initialize empty array without arena.

Remarks
Array capacity will be limited to the embedded capacity.

Definition at line 44 of file array.h.

◆ Array() [2/2]

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 54 of file array.h.

Member Function Documentation

◆ capacity()

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

Get maximum number of elements. If array has arena, capacity can be grown.

Definition at line 71 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 168 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 88 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 99 of file array.h.

◆ grow()

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

Increase array capacity.

Remarks
If max_sz 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 178 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_size)
inline

Increase array capacity exponentially.

Remarks
If min_size 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 221 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 81 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.

Definition at line 108 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.

Definition at line 117 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

Definition at line 128 of file array.h.

◆ resize()

template<class T , size_t EmbeddedCapacity = 0>
ROC_ATTR_NODISCARD bool roc::core::Array< T, EmbeddedCapacity >::resize ( size_t  sz)
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 144 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 76 of file array.h.


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