Graphitti
A toolkit/software architecture to ease creating high-performance neural network simulators
Loading...
Searching...
No Matches
CircularBuffer< T > Class Template Reference

Public Member Functions

 CircularBuffer (int capacity=0)
 Create a circular buffer with the given capacity.
 
void resize (int capacity)
 Resize the circular buffer.
 
void put (T element)
 Inserts the given element at the front of the queue.
 
std::optional< T > get ()
 Retrieves the element at the end of the queue.
 
std::optional< T > peek () const
 Retrieves the element at the end of the buffer withouth dequeueing it.
 
void clear ()
 Clears the circular buffer.
 
bool isEmpty () const
 Returns true if the buffer is empty, false otherwise.
 
bool isFull () const
 Returns true if the buffer is full, false otherwise.
 
size_t capacity () const
 Retrieves the number of elements that this buffer can hold.
 
size_t size () const
 Retrieves the number of elements currently stored in the buffer.
 

Detailed Description

template<typename T>
class CircularBuffer< T >

Definition at line 28 of file CircularBuffer.h.

Constructor & Destructor Documentation

◆ CircularBuffer()

template<typename T >
CircularBuffer< T >::CircularBuffer ( int capacity = 0)
inline

Create a circular buffer with the given capacity.

Create and size the buffer so it can hold a set number of elements. If the size is set on instantiation, the buffer should not be resize. The size of the underlying data structure is set to capacity+1, to distinguish between a full and empty buffer.

Parameters
capacityThe number of elements that the buffer can hold, defaults to zero

Definition at line 38 of file CircularBuffer.h.

Member Function Documentation

◆ capacity()

template<typename T >
size_t CircularBuffer< T >::capacity ( ) const
inline

Retrieves the number of elements that this buffer can hold.

Note that the underlying vector is sized one over the buffer capacity to distinguish between a full and empty buffer.

Returns
The number of element that this buffer can hold

Definition at line 134 of file CircularBuffer.h.

◆ clear()

template<typename T >
void CircularBuffer< T >::clear ( )
inline

Clears the circular buffer.

Definition at line 103 of file CircularBuffer.h.

◆ get()

template<typename T >
std::optional< T > CircularBuffer< T >::get ( )
inline

Retrieves the element at the end of the queue.

The method returns the element wrapped in an std::optional data type. If the buffer is empty we return an empty std::optional constructed from std::nullopt.

Postcondition
The buffer will have one less element if it was not empty.
Returns
The element at the end of the buffer, or std::nullopt if it is empty.

Definition at line 75 of file CircularBuffer.h.

◆ isEmpty()

template<typename T >
bool CircularBuffer< T >::isEmpty ( ) const
inline

Returns true if the buffer is empty, false otherwise.

Returns
true if the buffer is empty, false otherwise

Definition at line 111 of file CircularBuffer.h.

◆ isFull()

template<typename T >
bool CircularBuffer< T >::isFull ( ) const
inline

Returns true if the buffer is full, false otherwise.

We distinguish between an empty and full buffer by always having an unused space between the front and the end of the queue. A full buffer has the front one space behind the end of the queue.

Returns
true if the buffer is full, false otherwise

Definition at line 123 of file CircularBuffer.h.

◆ peek()

template<typename T >
std::optional< T > CircularBuffer< T >::peek ( ) const
inline

Retrieves the element at the end of the buffer withouth dequeueing it.

Postcondition
The elements in the buffer remain the same
Returns
The element at the end of the buffer, or std::nulloopt if it is empty.

Definition at line 93 of file CircularBuffer.h.

◆ put()

template<typename T >
void CircularBuffer< T >::put ( T element)
inline

Inserts the given element at the front of the queue.

Precondition
The buffer must not be full
Parameters
elementThe element to be inserted into the queue

Definition at line 58 of file CircularBuffer.h.

◆ resize()

template<typename T >
void CircularBuffer< T >::resize ( int capacity)
inline

Resize the circular buffer.

Precondition
Current buffer must be empty (Without valid elements)
Parameters
capacityThe number of elements that the buffer can hold

Definition at line 46 of file CircularBuffer.h.

◆ size()

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

Retrieves the number of elements currently stored in the buffer.

Returns
The number of elements currently stored in the buffer

Definition at line 141 of file CircularBuffer.h.


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