Elementa v8.0.0
Minimalistic library for any C++ application (C++11 and up)
Loading...
Searching...
No Matches
Circular buffer abstract data type

Description


  A circular buffer with generic content.
Author
Juan-Antonio Fernandez-Madrigal. http://jafma.net
Date
2021
Collaboration diagram for Circular buffer abstract data type:

Classes

class  elementa::adts::CircularBuffer< ElemType >
 A circular buffer with fixed size, contiguous storage and generic elements. More...
 

Functions

 elementa::adts::CircularBuffer< ElemType >::CircularBuffer (size_t s)
 Constructor. More...
 
ElemType & elementa::adts::CircularBuffer< ElemType >::back (void)
 Get a ref to the newest element in the buffer. More...
 
void elementa::adts::CircularBuffer< ElemType >::push_back (const ElemType &e)
 Copy E into the buffer, as the last element. More...
 
ElemType & elementa::adts::CircularBuffer< ElemType >::front (void)
 Get a ref to the oldest element in the buffer. More...
 
void elementa::adts::CircularBuffer< ElemType >::pop_front (void)
 Erases the oldest element in the buffer. More...
 
ElemType & elementa::adts::CircularBuffer< ElemType >::operator[] (size_t index)
 Access the n-th element in the buffer or throw if not such element. More...
 

Class Documentation

◆ elementa::adts::CircularBuffer

class elementa::adts::CircularBuffer
template<typename ElemType>
class elementa::adts::CircularBuffer< ElemType >

A circular buffer with fixed size, contiguous storage and generic elements.

This buffer works as a FIFO container (first-in, first-out). Elements must be assignable and default constructible.

Definition at line 48 of file circularbuffers.h.

Public Member Functions

 CircularBuffer (size_t s)
 Constructor. More...
 
void clear (void)
 Clear all content. More...
 
size_t capacity (void) const noexcept
 Return the fixed room for elements in the buffer. More...
 
size_t size (void) const noexcept
 Return the number of elements currently in the buffer. More...
 
bool empty (void) const noexcept
 Return true if the buffer is empty. More...
 
ElemType & back (void)
 Get a ref to the newest element in the buffer. More...
 
void push_back (const ElemType &e)
 Copy E into the buffer, as the last element. More...
 
ElemType & front (void)
 Get a ref to the oldest element in the buffer. More...
 
void pop_front (void)
 Erases the oldest element in the buffer. More...
 
ElemType & operator[] (size_t index)
 Access the n-th element in the buffer or throw if not such element. More...
 

Member Function Documentation

◆ clear()

template<typename ElemType >
void elementa::adts::CircularBuffer< ElemType >::clear ( void  )
inline

Clear all content.

No existing element is destroyed; they are just prepared to be overwriting (assigned to) in further pushes.

Definition at line 61 of file circularbuffers.h.

◆ capacity()

template<typename ElemType >
size_t elementa::adts::CircularBuffer< ElemType >::capacity ( void  ) const
inlinenoexcept

Return the fixed room for elements in the buffer.

Definition at line 64 of file circularbuffers.h.

◆ size()

template<typename ElemType >
size_t elementa::adts::CircularBuffer< ElemType >::size ( void  ) const
inlinenoexcept

Return the number of elements currently in the buffer.

Definition at line 67 of file circularbuffers.h.

◆ empty()

template<typename ElemType >
bool elementa::adts::CircularBuffer< ElemType >::empty ( void  ) const
inlinenoexcept

Return true if the buffer is empty.

Definition at line 70 of file circularbuffers.h.

Function Documentation

◆ CircularBuffer()

template<typename ElemType >
elementa::adts::CircularBuffer< ElemType >::CircularBuffer ( size_t  s)

#include <elementa/adts/circularbuffers.h>

Constructor.

Throw if s == 0. Initially, the buffer is empty.

Definition at line 121 of file circularbuffers.h.

References ELE_CODE_INVARG.

◆ back()

template<typename ElemType >
ElemType & elementa::adts::CircularBuffer< ElemType >::back ( void  )

#include <elementa/adts/circularbuffers.h>

Get a ref to the newest element in the buffer.

Throw if the buffer is empty.

Definition at line 130 of file circularbuffers.h.

References ELE_CODE_INVSTATE.

◆ push_back()

template<typename ElemType >
void elementa::adts::CircularBuffer< ElemType >::push_back ( const ElemType &  e)

#include <elementa/adts/circularbuffers.h>

Copy E into the buffer, as the last element.

If the buffer had a previously popped element there, it is destroyed at this moment.

Definition at line 137 of file circularbuffers.h.

◆ front()

template<typename ElemType >
ElemType & elementa::adts::CircularBuffer< ElemType >::front ( void  )

#include <elementa/adts/circularbuffers.h>

Get a ref to the oldest element in the buffer.

Throw if the buffer is empty.

Definition at line 145 of file circularbuffers.h.

References ELE_CODE_INVSTATE.

◆ pop_front()

template<typename ElemType >
void elementa::adts::CircularBuffer< ElemType >::pop_front ( void  )

#include <elementa/adts/circularbuffers.h>

Erases the oldest element in the buffer.

Throw if the buffer is empty. No existing element is destroyed; they are just prepared to be overwriting (assigned to) in further pushes.

Definition at line 152 of file circularbuffers.h.

References ELE_CODE_INVSTATE.

◆ operator[]()

template<typename ElemType >
ElemType & elementa::adts::CircularBuffer< ElemType >::operator[] ( size_t  index)

#include <elementa/adts/circularbuffers.h>

Access the n-th element in the buffer or throw if not such element.

Definition at line 161 of file circularbuffers.h.

References ELE_CODE_OUTOFRANGE.