My Project
Public Member Functions | List of all members
cpu_mlp::RingBuffer< T > Class Template Reference

A fixed-capacity ring buffer (circular buffer). More...

#include <ring_buffer.hpp>

Public Member Functions

 RingBuffer (size_t capacity=0)
 Constructs a RingBuffer with a given capacity. More...
 
void set_capacity (size_t capacity)
 Resizes and resets the buffer. More...
 
void push_back (const T &val)
 Pushes a new element into the buffer. More...
 
T & operator[] (size_t i)
 Access element by logical index (0 is oldest). More...
 
const T & operator[] (size_t i) const
 Construct element by logical index (0 is oldest). More...
 
T & back ()
 Access the newest element. More...
 
const T & back () const
 Access the newest element (const). More...
 
size_t size () const
 Returns the number of elements currently stored. More...
 
bool empty () const
 Checks if the buffer is empty. More...
 
bool full () const
 Checks if the buffer is full. More...
 
void clear ()
 Clears the buffer content. More...
 
void reserve (size_t n)
 Reserve capacity (resets buffer if new capacity > old capacity). More...
 

Detailed Description

template<typename T>
class cpu_mlp::RingBuffer< T >

A fixed-capacity ring buffer (circular buffer).

Efficiently stores a sliding window of elements. Pushing back when full overwrites the oldest element (logical index 0). Accessing index i corresponds to the (i)-th oldest element.

Constructor & Destructor Documentation

◆ RingBuffer()

template<typename T >
cpu_mlp::RingBuffer< T >::RingBuffer ( size_t  capacity = 0)
inlineexplicit

Constructs a RingBuffer with a given capacity.

Parameters
capacityMaximum number of elements to store.

Member Function Documentation

◆ back() [1/2]

template<typename T >
T& cpu_mlp::RingBuffer< T >::back ( )
inline

Access the newest element.

Returns
Reference to the newest element.
Here is the caller graph for this function:

◆ back() [2/2]

template<typename T >
const T& cpu_mlp::RingBuffer< T >::back ( ) const
inline

Access the newest element (const).

Returns
Const reference to the newest element.

◆ clear()

template<typename T >
void cpu_mlp::RingBuffer< T >::clear ( )
inline

Clears the buffer content.

Here is the caller graph for this function:

◆ empty()

template<typename T >
bool cpu_mlp::RingBuffer< T >::empty ( ) const
inline

Checks if the buffer is empty.

Here is the caller graph for this function:

◆ full()

template<typename T >
bool cpu_mlp::RingBuffer< T >::full ( ) const
inline

Checks if the buffer is full.

◆ operator[]() [1/2]

template<typename T >
T& cpu_mlp::RingBuffer< T >::operator[] ( size_t  i)
inline

Access element by logical index (0 is oldest).

Parameters
iIndex.
Returns
Reference to the element.

◆ operator[]() [2/2]

template<typename T >
const T& cpu_mlp::RingBuffer< T >::operator[] ( size_t  i) const
inline

Construct element by logical index (0 is oldest).

Parameters
iIndex.
Returns
Const reference to the element.

◆ push_back()

template<typename T >
void cpu_mlp::RingBuffer< T >::push_back ( const T &  val)
inline

Pushes a new element into the buffer.

If the buffer is full, the oldest element is overwritten.

Parameters
valThe value to push.
Here is the caller graph for this function:

◆ reserve()

template<typename T >
void cpu_mlp::RingBuffer< T >::reserve ( size_t  n)
inline

Reserve capacity (resets buffer if new capacity > old capacity).

Parameters
nNew capacity.
Here is the call graph for this function:

◆ set_capacity()

template<typename T >
void cpu_mlp::RingBuffer< T >::set_capacity ( size_t  capacity)
inline

Resizes and resets the buffer.

Parameters
capacityNew capacity.
Here is the caller graph for this function:

◆ size()

template<typename T >
size_t cpu_mlp::RingBuffer< T >::size ( ) const
inline

Returns the number of elements currently stored.

Here is the caller graph for this function:

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