3#include "elementa/license.inc"
4#include "elementa/checks.inc"
6#ifndef ELEMENTA_ADTS_CIRCULARBUFFER_H
7#define ELEMENTA_ADTS_CIRCULARBUFFER_H
47template <
typename ElemType>
61 void clear(
void) { initpos_ = 0; size_ = 0; }
64 size_t capacity(
void)
const noexcept {
return(capacity_); }
67 size_t size(
void)
const noexcept {
return(size_); }
70 bool empty(
void)
const noexcept {
return(size_ == 0); }
74 ElemType &
back(
void);
83 ElemType &
front(
void);
97 const size_t capacity_;
98 std::vector<ElemType> storage_;
99 size_t initpos_,size_;
101 constexpr size_t index(
size_t i)
noexcept
102 {
return((initpos_ + i) % capacity_); }
120template <
typename ElemType>
129template <
typename ElemType>
133 return(storage_[index(size_ - 1)]);
136template <
typename ElemType>
139 if (size_ == capacity_) pop_front();
140 storage_[index(size_)] = e;
144template <
typename ElemType>
148 return(storage_[initpos_]);
151template <
typename ElemType>
156 if (initpos_ >= capacity_) initpos_ = 0;
160template <
typename ElemType>
164 return(storage_[index(ind)]);
size_t size(void) const noexcept
Return the number of elements currently in the buffer.
void clear(void)
Clear all content.
bool empty(void) const noexcept
Return true if the buffer is empty.
size_t capacity(void) const noexcept
Return the fixed room for elements in the buffer.
A circular buffer with fixed size, contiguous storage and generic elements.
CircularBuffer(size_t s)
Constructor.
void pop_front(void)
Erases the oldest element in the buffer.
void push_back(const ElemType &e)
Copy E into the buffer, as the last element.
ElemType & front(void)
Get a ref to the oldest element in the buffer.
ElemType & operator[](size_t index)
Access the n-th element in the buffer or throw if not such element.
ElemType & back(void)
Get a ref to the newest element in the buffer.
#define ELE_CODE_INVSTATE(expl)
To throw an invalid-state exception with an explanation.
#define ELE_CODE_OUTOFRANGE(expl)
To throw an out-of-range exception with an explanation.
#define ELE_CODE_INVARG(expl)
To throw an invalid-argument exception with an explanation.