geomc 1.0
A c++ linear algebra template library
Public Member Functions | Protected Member Functions | List of all members
CircularBuffer< T, N > Class Template Reference

A lightweight circular buffer. More...

#include <geomc/CircularBuffer.h>

Public Member Functions

 CircularBuffer ()
 Construct a new empty circular buffer, with capacity for N items.
 
 CircularBuffer (index_t capacity)
 Construct a new empty circular buffer, with space for at least capacity items.
 
template<typename InputIterator >
 CircularBuffer (InputIterator begin, index_t count)
 Construct a new circular buffer by copying count items in the sequence starting at begin.
 
 CircularBuffer (const CircularBuffer< T, N > &other)
 Construct a new circular buffer containing copies of all the items in other.
 
 CircularBuffer (CircularBuffer< T, N > &&other)
 Move the contents of other to a new CircularBuffer.
 
CircularBufferoperator= (const CircularBuffer &other)
 Assignment operator.
 
CircularBufferoperator= (CircularBuffer &&other)
 Move the contents of other to this buffer.
 
T & operator[] (index_t i)
 Get the ith element in the buffer. More...
 
const T & operator[] (index_t i) const
 Get the ith (const) element in the buffer. More...
 
bool operator== (const CircularBuffer< T, N > &other)
 Equality operator. More...
 
bool operator!= (const CircularBuffer< T, N > &other)
 Inequality operator.
 
index_t size () const
 Return the number of items in the buffer.
 
index_t capacity () const
 Return the total number of items that can be accommodated without an additional memory allocation.
 
template<typename U >
void push_back (U &&t)
 Add an element to the end of the buffer.
 
template<typename U >
void push_front (U &&t)
 Add an element to the beginning of the buffer. More...
 
pop_front ()
 Remove the first element in the buffer. More...
 
pop_back ()
 Remove the element at the end of the buffer.
 
T & front ()
 Return a reference to the item at the beginning of the buffer.
 
const T & front () const
 Return a const reference to the item at the beginning of the buffer.
 
T & back ()
 Return a reference to the item at the end of the buffer.
 
const T & back () const
 Return a const reference to the item at the end of the buffer.
 
void clear ()
 Empty the buffer of all items.
 
void reserve (index_t new_cap)
 Increase the capacity of the circular buffer to a value that's greater or equal to new_cap. If new_cap is greater than the current capacity, then new storage is allocated; otherwise the method does nothing.
 

Protected Member Functions

T * get ()
 
const T * get () const
 
T * item (index_t i)
 
const T * item (index_t i) const
 

Detailed Description

template<typename T, index_t N>
class geom::CircularBuffer< T, N >

A lightweight circular buffer.

A circular buffer can accommodate adding elements to either the front or the back of the list in constant time.

Furthermore, indexing off the end of the circular buffer wraps around to the beginning again. This works in both directions.

Template Parameters
TElement type.
NStatic capacity of the buffer. Adding more than this number of elements to the buffer will incur a heap memory allocation. To always use heap allocation, pass zero to this parameter, in which case memory will be allocated when the first element is added.

Member Function Documentation

◆ operator==()

bool operator== ( const CircularBuffer< T, N > &  other)
inline

Equality operator.

Two CircularBuffers are equal iff they contain equal elements in equal order.

◆ operator[]() [1/2]

T & operator[] ( index_t  i)
inline

Get the ith element in the buffer.

Indicies beyond the end of the buffer will wrap around again to the beginning. Negative indices are permitted and count from the end of the buffer, with -1 denoting the last element in the buffer.

◆ operator[]() [2/2]

const T & operator[] ( index_t  i) const
inline

Get the ith (const) element in the buffer.

Indicies beyond the end of the buffer will wrap around again to the beginning. Negative indices are permitted and count from the end of the buffer, with -1 denoting the last element in the buffer.

◆ pop_front()

T pop_front ( )
inline

Remove the first element in the buffer.

This decreases the indices of all the remaining elements by one.

◆ push_front()

void push_front ( U &&  t)
inline

Add an element to the beginning of the buffer.

This increases the indices of all the existing elements by one. The new element will have index zero.


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