MetroCollect  2.3.4
CircularArray::CircularArray< T > Class Template Reference

A circular array container. More...

#include <CircularArray.h>

Public Types

using ValueType = T
 Type of stored elements in the container. More...
 

Public Member Functions

 CircularArray ()
 Construct a new Circular Array object. More...
 
 CircularArray (size_t n)
 Construct a new Circular Array object. More...
 
 CircularArray (size_t n, const T &value)
 Construct a new Circular Array object. More...
 
size_t capacity () const noexcept
 Returns the number of elements that can be held in currently allocated storage. More...
 
size_t size () const noexcept
 Returns the number of elements currently used in the container. More...
 
T & at (ptrdiff_t index)
 Access specified element. More...
 
const T & at (ptrdiff_t index) const
 Access specified element. More...
 
T & operator[] (ptrdiff_t index)
 Access specified element. More...
 
const T & operator[] (ptrdiff_t index) const
 Access specified element. More...
 
size_t absoluteIndex (ptrdiff_t index) const noexcept
 Returns the corresponding index in the underlying storage. More...
 
T & atAbsoluteIndex (size_t index)
 Access specified element in underlying storage. More...
 
const T & atAbsoluteIndex (size_t index) const
 Access specified element in underlying storage. More...
 
bool absoluteIndexIsInBounds (size_t index) const noexcept
 Checks wether absolute index is in the currently used circular range. More...
 
T & front ()
 Access the first element. More...
 
const T & front () const
 Access the first element. More...
 
T & back ()
 Access the last element. More...
 
const T & back () const
 Access the last element. More...
 
void moveBegin (ptrdiff_t indexes) noexcept
 Moves the beginning of the circular array. More...
 
void moveEnd (ptrdiff_t indexes) noexcept
 Moves the end of the circular array. More...
 
void reset () noexcept
 Resets beginning and end indexes. More...
 
void reset (size_t capacity) noexcept
 Resets beginning and end indexes and resizes the container. More...
 
void reset (size_t capacity, const T &value) noexcept
 Resets beginning and end indexes and resizes the container. More...
 
void reset (const T &value) noexcept
 Resets beginning and end indexes and the container's elements. More...
 

Protected Attributes

std::vector< T > data_
 Underlying data storage. More...
 
size_t capacity_
 Number of elements that can be held in currently allocated storage. More...
 
size_t begin_
 First index in internal storage. More...
 
size_t size_
 Number of elements currently stored. More...
 

Detailed Description

template<typename T>
class CircularArray::CircularArray< T >

A circular array container.

A circular array is a container which maintains a range of active elements. The active range is defined by a starting index in the underlying container and a size. It may grow or shrink from both ends and users can access its elements. However, the active range size is limited by the underlying container capacity, in case of overflow elements at the beginning are replaced by elemnts added at the end.

For a more thorough overview of circular buffers, refer to boost::circular_buffer's documentation.

In this implementation, elements may be accessed from the beginning of the active range in the usual way, but also from the beginning of the underlying container to maintain a consistent access.

Template Parameters
TType of stored elements in the container

Definition at line 42 of file CircularArray.h.

Member Typedef Documentation

◆ ValueType

template<typename T>
using CircularArray::CircularArray< T >::ValueType = T

Type of stored elements in the container.

Definition at line 50 of file CircularArray.h.

Constructor & Destructor Documentation

◆ CircularArray() [1/3]

template<typename T >
CircularArray::CircularArray< T >::CircularArray ( )

Construct a new Circular Array object.

Definition at line 31 of file CircularArray.cc.

◆ CircularArray() [2/3]

template<typename T >
CircularArray::CircularArray< T >::CircularArray ( size_t  n)

Construct a new Circular Array object.

Parameters
nsize of the container

Definition at line 34 of file CircularArray.cc.

◆ CircularArray() [3/3]

template<typename T>
CircularArray::CircularArray< T >::CircularArray ( size_t  n,
const T &  value 
)

Construct a new Circular Array object.

Parameters
nsize of the container
valuevalue to initialize each element with

Definition at line 37 of file CircularArray.cc.

Member Function Documentation

◆ absoluteIndex()

template<typename T >
size_t CircularArray::CircularArray< T >::absoluteIndex ( ptrdiff_t  index) const
noexcept

Returns the corresponding index in the underlying storage.

Parameters
indexposition of the element
Returns
Absolute index position of the element in the underlying storage

Definition at line 73 of file CircularArray.cc.

◆ absoluteIndexIsInBounds()

template<typename T >
bool CircularArray::CircularArray< T >::absoluteIndexIsInBounds ( size_t  index) const
noexcept

Checks wether absolute index is in the currently used circular range.

Parameters
indexabsolute index position to check
Returns
true if absolute index is in bounds
false otherwise

Definition at line 88 of file CircularArray.cc.

◆ at() [1/2]

template<typename T >
T & CircularArray::CircularArray< T >::at ( ptrdiff_t  index)

Access specified element.

Parameters
indexposition of the element to return
Returns
Reference to the requested element

Definition at line 52 of file CircularArray.cc.

Here is the caller graph for this function:

◆ at() [2/2]

template<typename T >
const T & CircularArray::CircularArray< T >::at ( ptrdiff_t  index) const

Access specified element.

Parameters
indexposition of the element to return
Returns
Const reference to the requested element

Definition at line 57 of file CircularArray.cc.

◆ atAbsoluteIndex() [1/2]

template<typename T >
T & CircularArray::CircularArray< T >::atAbsoluteIndex ( size_t  index)

Access specified element in underlying storage.

Parameters
indexabsolute index position of the element to return
Returns
Reference to the requested element

Definition at line 78 of file CircularArray.cc.

◆ atAbsoluteIndex() [2/2]

template<typename T >
const T & CircularArray::CircularArray< T >::atAbsoluteIndex ( size_t  index) const

Access specified element in underlying storage.

Parameters
indexabsolute index position of the element to return
Returns
Const reference to the requested element

Definition at line 83 of file CircularArray.cc.

◆ back() [1/2]

template<typename T >
T & CircularArray::CircularArray< T >::back ( )

Access the last element.

Returns
Reference to the last element

Definition at line 107 of file CircularArray.cc.

◆ back() [2/2]

template<typename T >
const T & CircularArray::CircularArray< T >::back ( ) const

Access the last element.

Returns
Const reference to the last element

Definition at line 112 of file CircularArray.cc.

◆ capacity()

template<typename T >
size_t CircularArray::CircularArray< T >::capacity ( ) const
noexcept

Returns the number of elements that can be held in currently allocated storage.

Returns
Capacity of the currently allocated storage

Definition at line 41 of file CircularArray.cc.

◆ front() [1/2]

template<typename T >
T & CircularArray::CircularArray< T >::front ( )

Access the first element.

Returns
Reference to the first element

Definition at line 97 of file CircularArray.cc.

◆ front() [2/2]

template<typename T >
const T & CircularArray::CircularArray< T >::front ( ) const

Access the first element.

Returns
Const reference to the first element

Definition at line 102 of file CircularArray.cc.

◆ moveBegin()

template<typename T >
void CircularArray::CircularArray< T >::moveBegin ( ptrdiff_t  indexes)
noexcept

Moves the beginning of the circular array.

Parameters
indexesthe number of indexes to add to the current beginning

Definition at line 118 of file CircularArray.cc.

◆ moveEnd()

template<typename T >
void CircularArray::CircularArray< T >::moveEnd ( ptrdiff_t  indexes)
noexcept

Moves the end of the circular array.

Parameters
indexesthe number of indexes to add to the current end

Definition at line 129 of file CircularArray.cc.

◆ operator[]() [1/2]

template<typename T >
T & CircularArray::CircularArray< T >::operator[] ( ptrdiff_t  index)

Access specified element.

Parameters
indexposition of the element to return
Returns
Reference to the requested element

Definition at line 62 of file CircularArray.cc.

◆ operator[]() [2/2]

template<typename T >
const T & CircularArray::CircularArray< T >::operator[] ( ptrdiff_t  index) const

Access specified element.

Parameters
indexposition of the element to return
Returns
Const reference to the requested element

Definition at line 67 of file CircularArray.cc.

◆ reset() [1/4]

template<typename T >
void CircularArray::CircularArray< T >::reset ( )
noexcept

Resets beginning and end indexes.

Definition at line 142 of file CircularArray.cc.

◆ reset() [2/4]

template<typename T >
void CircularArray::CircularArray< T >::reset ( size_t  capacity)
noexcept

Resets beginning and end indexes and resizes the container.

Parameters
capacitynew size of the container

Definition at line 148 of file CircularArray.cc.

◆ reset() [3/4]

template<typename T>
void CircularArray::CircularArray< T >::reset ( size_t  capacity,
const T &  value 
)
noexcept

Resets beginning and end indexes and resizes the container.

Parameters
capacitynew size of the container
valuevalue to re-initialize all elements with

Definition at line 155 of file CircularArray.cc.

◆ reset() [4/4]

template<typename T>
void CircularArray::CircularArray< T >::reset ( const T &  value)
noexcept

Resets beginning and end indexes and the container's elements.

Parameters
valuevalue to re-initialize all elements with

Definition at line 163 of file CircularArray.cc.

◆ size()

template<typename T >
size_t CircularArray::CircularArray< T >::size ( ) const
noexcept

Returns the number of elements currently used in the container.

Returns
The number of elements in the container

Definition at line 46 of file CircularArray.cc.

Member Data Documentation

◆ begin_

template<typename T>
size_t CircularArray::CircularArray< T >::begin_
protected

First index in internal storage.

Definition at line 46 of file CircularArray.h.

◆ capacity_

template<typename T>
size_t CircularArray::CircularArray< T >::capacity_
protected

Number of elements that can be held in currently allocated storage.

Definition at line 45 of file CircularArray.h.

◆ data_

template<typename T>
std::vector<T> CircularArray::CircularArray< T >::data_
protected

Underlying data storage.

Definition at line 44 of file CircularArray.h.

◆ size_

template<typename T>
size_t CircularArray::CircularArray< T >::size_
protected

Number of elements currently stored.

Definition at line 47 of file CircularArray.h.


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