diff --git a/src/utility/CircularBuffer.h b/src/utility/CircularBuffer.h new file mode 100644 index 00000000..c8abe92e --- /dev/null +++ b/src/utility/CircularBuffer.h @@ -0,0 +1,51 @@ +#pragma once + +#include +#include + +namespace Pinetime { + namespace Utility { + template + struct CircularBuffer { + constexpr size_t Size() const { + return S; + } + + size_t Idx() const { + return idx; + } + + T& operator[](size_t n) { + return data[(idx + n) % S]; + } + + const T& operator[](size_t n) const { + return data[(idx + n) % S]; + } + + void operator++() { + idx++; + idx %= S; + } + + void operator++(int) { + operator++(); + } + + void operator--() { + if (idx > 0) { + idx--; + } else { + idx = S - 1; + } + } + + void operator--(int) { + operator--(); + } + + std::array data; + size_t idx = 0; + }; + } +}