aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorFinlay Davidson <finlay.davidson@coderclass.nl>2023-03-17 22:14:19 +0100
committerJF <JF002@users.noreply.github.com>2023-08-17 21:21:22 +0200
commit6d5847e0467863addbbb1d8e85905dd5e682d145 (patch)
tree304f812a41fd26def1992cec644687a73fdbb4f5 /src
parentcf782bb6158b6ea4a63d2239c3b1a49df138a911 (diff)
circularbuffer: Add circular buffer utility struct
Diffstat (limited to 'src')
-rw-r--r--src/utility/CircularBuffer.h51
1 files changed, 51 insertions, 0 deletions
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 <array>
+#include <cstddef>
+
+namespace Pinetime {
+ namespace Utility {
+ template <class T, size_t S>
+ 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<T, S> data;
+ size_t idx = 0;
+ };
+ }
+}