3#include "arrayLike.hpp"
14template <
typename TYPE,
unsigned int LEN>
25 circularIterator(
const TYPE *buffer,
int index) noexcept : ptr(buffer), offset(index) {}
42 return offset != other.offset;
50 return ptr[offset % LEN];
59template <
typename TYPE,
unsigned int LEN>
65 static_assert(LEN > 0,
"Buffer must have more than zero elements.");
79 template <
typename... ARGS>
89 for (
int i = 0; i < LEN; ++i) {
90 data[i] = default_value;
94 size_t size() const noexcept
override {
95 return sizeof(TYPE) * LEN;
98 int length() const noexcept
override {
109 const TYPE &
at(
const int index)
const noexcept override {
110 return data[
index % LEN];
126 return data[
index % LEN];
134 data[counter] = value;
135 counter = (counter + 1) % LEN;
156 return data[counter];
172 counter = (counter + LEN - 1) % LEN;
177 counter = (counter + 1) % LEN;
193 template <
typename... ARGS>
194 void populate(
const TYPE &first,
const ARGS &...args)
noexcept {
216 static_assert(std::is_arithmetic<TYPE>::value,
"circularBuffer::minimum() is only defined for arithmetic types.");
218 TYPE value = data[0];
219 for (
int i = 1; i < LEN; ++i) {
220 if (data[i] < value) {
233 static_assert(std::is_arithmetic<TYPE>::value,
"circularBuffer::maximum() is only defined for arithmetic types.");
235 TYPE value = data[0];
236 for (
int i = 1; i < LEN; ++i) {
237 if (data[i] > value) {
250 static_assert(std::is_arithmetic<TYPE>::value,
"circularBuffer::average() is only defined for arithmetic types.");
253 for (
int i = 0; i < LEN; ++i) {
254 avg += (double)data[i] / (
double)LEN;
An interface for all objects that can be both iterated over and directly indexed.
Definition arrayLike.hpp:13
TYPE minimum() const noexcept
Get the minimum value contained in this buffer.
Definition circularBuffer.hpp:215
TYPE & current() noexcept
Get a reference to the topmost item in the buffer.
Definition circularBuffer.hpp:155
circularIterator< TYPE, LEN > begin() const noexcept override
Get an iterator for the first element in this object.
Definition circularBuffer.hpp:180
void populate(const TYPE &first, const ARGS &...args) noexcept
Append an arbitrary number of elements to the buffer.
Definition circularBuffer.hpp:194
const TYPE & at(const int index) const noexcept override
Get an element from the buffer.
Definition circularBuffer.hpp:109
size_t size() const noexcept override
Get the size of the object in memory.
Definition circularBuffer.hpp:94
int index() const noexcept
Get the index of the current top of the buffer.
Definition circularBuffer.hpp:147
int count() const noexcept
Get the total number of items in the buffer.
Definition circularBuffer.hpp:166
TYPE maximum() const noexcept
Get the maximum value contained in this buffer.
Definition circularBuffer.hpp:232
void next() noexcept
Move current "top" to the next spot in the buffer.
Definition circularBuffer.hpp:176
void populate(const TYPE &arg) noexcept
Append an element to the buffer.
Definition circularBuffer.hpp:206
void prev() noexcept
Move current "top" to the previous spot in the buffer.
Definition circularBuffer.hpp:171
void append(const TYPE value) noexcept
Add a value to the buffer, and increment the current index.
Definition circularBuffer.hpp:133
circularIterator< TYPE, LEN > end() const noexcept override
Get an iterator after the last element of this object.
Definition circularBuffer.hpp:184
circularBuffer(TYPE default_value) noexcept
Initialize all elements with a single value.
Definition circularBuffer.hpp:88
int length() const noexcept override
Get the length of the array.
Definition circularBuffer.hpp:98
circularBuffer() noexcept
Default constructor.
Definition circularBuffer.hpp:69
double average() const noexcept
Get the average (mean) value contained in this buffer.
Definition circularBuffer.hpp:249
circularBuffer(const ARGS &...args) noexcept
Initialize the buffer with the contents of an array.
Definition circularBuffer.hpp:80
TYPE & at(const int index) noexcept
Get a modifiable element from the buffer.
Definition circularBuffer.hpp:125
A custom iterator for circular buffers.
Definition circularBuffer.hpp:15
circularIterator operator++() noexcept
Move to the next spot in the buffer.
Definition circularBuffer.hpp:31
circularIterator(const TYPE *buffer, int index) noexcept
Constructor.
Definition circularBuffer.hpp:25
bool operator!=(const circularIterator &other) const noexcept
Inequality operator.
Definition circularBuffer.hpp:41
const TYPE & operator*() const noexcept
Dereference operator.
Definition circularBuffer.hpp:49
An interface for getting an object's size.
Definition sizable.hpp:13
Section containing core functions and classes.