WPILibC++ 2024.3.2
static_circular_buffer.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <array>
8#include <cstddef>
9#include <iterator>
10
11namespace wpi {
12
13/**
14 * This is a simple circular buffer so we don't need to "bucket brigade" copy
15 * old values.
16 *
17 * @tparam T Buffer element type.
18 * @tparam N Maximum number of buffer elements.
19 */
20template <class T, size_t N>
22 public:
23 static_assert(N > 0, "Circular buffer size cannot be zero.");
24
25 class iterator {
26 public:
27 using iterator_category = std::forward_iterator_tag;
28 using value_type = T;
29 using difference_type = std::ptrdiff_t;
30 using pointer = T*;
31 using reference = T&;
32
33 iterator(static_circular_buffer* buffer, size_t index)
34 : m_buffer{buffer}, m_index{index} {}
35
37 ++m_index;
38 return *this;
39 }
41 iterator retval = *this;
42 ++(*this);
43 return retval;
44 }
45 bool operator==(const iterator&) const = default;
46 reference operator*() { return (*m_buffer)[m_index]; }
47
48 private:
49 static_circular_buffer* m_buffer;
50 size_t m_index;
51 };
52
54 public:
55 using iterator_category = std::forward_iterator_tag;
56 using value_type = T;
57 using difference_type = std::ptrdiff_t;
58 using pointer = T*;
59 using const_reference = const T&;
60
61 const_iterator(const static_circular_buffer* buffer, size_t index)
62 : m_buffer{buffer}, m_index{index} {}
63
65 ++m_index;
66 return *this;
67 }
69 const_iterator retval = *this;
70 ++(*this);
71 return retval;
72 }
73 bool operator==(const const_iterator&) const = default;
74 const_reference operator*() const { return (*m_buffer)[m_index]; }
75
76 private:
77 const static_circular_buffer* m_buffer;
78 size_t m_index;
79 };
80
81 /**
82 * Returns begin iterator.
83 */
84 iterator begin() { return iterator(this, 0); }
85
86 /**
87 * Returns end iterator.
88 */
91 }
92
93 /**
94 * Returns begin iterator.
95 */
96 const_iterator begin() const { return const_iterator(this, 0); }
97
98 /**
99 * Returns end iterator.
100 */
103 }
104
105 /**
106 * Returns begin iterator.
107 */
108 const_iterator cbegin() const { return const_iterator(this, 0); }
109
110 /**
111 * Returns end iterator.
112 */
115 }
116
117 /**
118 * Returns number of elements in buffer
119 */
120 size_t size() const { return m_length; }
121
122 /**
123 * Returns value at front of buffer
124 */
125 T& front() { return (*this)[0]; }
126
127 /**
128 * Returns value at front of buffer
129 */
130 const T& front() const { return (*this)[0]; }
131
132 /**
133 * Returns value at back of buffer
134 *
135 * If there are no elements in the buffer, calling this function results in
136 * undefined behavior.
137 */
138 T& back() { return m_data[(m_front + m_length - 1) % N]; }
139
140 /**
141 * Returns value at back of buffer
142 *
143 * If there are no elements in the buffer, calling this function results in
144 * undefined behavior.
145 */
146 const T& back() const { return m_data[(m_front + m_length - 1) % N]; }
147
148 /**
149 * Push a new value onto the front of the buffer.
150 *
151 * The value at the back is overwritten if the buffer is full.
152 */
153 void push_front(T value) {
154 m_front = ModuloDec(m_front);
155
156 m_data[m_front] = value;
157
158 if (m_length < N) {
159 m_length++;
160 }
161 }
162
163 /**
164 * Push a new value onto the back of the buffer.
165 *
166 * The value at the front is overwritten if the buffer is full.
167 */
168 void push_back(T value) {
169 m_data[(m_front + m_length) % N] = value;
170
171 if (m_length < N) {
172 m_length++;
173 } else {
174 // Increment front if buffer is full to maintain size
175 m_front = ModuloInc(m_front);
176 }
177 }
178
179 /**
180 * Push a new value onto the front of the buffer that is constructed with the
181 * provided constructor arguments.
182 *
183 * The value at the back is overwritten if the buffer is full.
184 */
185 template <class... Args>
186 void emplace_front(Args&&... args) {
187 m_front = ModuloDec(m_front);
188
189 m_data[m_front] = T{args...};
190
191 if (m_length < N) {
192 m_length++;
193 }
194 }
195
196 /**
197 * Push a new value onto the back of the buffer that is constructed with the
198 * provided constructor arguments.
199 *
200 * The value at the front is overwritten if the buffer is full.
201 */
202 template <class... Args>
203 void emplace_back(Args&&... args) {
204 m_data[(m_front + m_length) % N] = T{args...};
205
206 if (m_length < N) {
207 m_length++;
208 } else {
209 // Increment front if buffer is full to maintain size
210 m_front = ModuloInc(m_front);
211 }
212 }
213
214 /**
215 * Pop value at front of buffer.
216 *
217 * If there are no elements in the buffer, calling this function results in
218 * undefined behavior.
219 */
221 T& temp = m_data[m_front];
222 m_front = ModuloInc(m_front);
223 m_length--;
224 return temp;
225 }
226
227 /**
228 * Pop value at back of buffer.
229 *
230 * If there are no elements in the buffer, calling this function results in
231 * undefined behavior.
232 */
234 m_length--;
235 return m_data[(m_front + m_length) % N];
236 }
237
238 /**
239 * Empties internal buffer.
240 */
241 void reset() {
242 m_front = 0;
243 m_length = 0;
244 }
245
246 /**
247 * @return Element at index starting from front of buffer.
248 */
249 T& operator[](size_t index) { return m_data[(m_front + index) % N]; }
250
251 /**
252 * @return Element at index starting from front of buffer.
253 */
254 const T& operator[](size_t index) const {
255 return m_data[(m_front + index) % N];
256 }
257
258 private:
259 std::array<T, N> m_data;
260
261 // Index of element at front of buffer
262 size_t m_front = 0;
263
264 // Number of elements used in buffer
265 size_t m_length = 0;
266
267 /**
268 * Increment an index modulo the length of the buffer.
269 *
270 * @return The result of the modulo operation.
271 */
272 size_t ModuloInc(size_t index) { return (index + 1) % N; }
273
274 /**
275 * Decrement an index modulo the length of the buffer.
276 *
277 * @return The result of the modulo operation.
278 */
279 size_t ModuloDec(size_t index) {
280 if (index == 0) {
281 return N - 1;
282 } else {
283 return index - 1;
284 }
285 }
286};
287
288} // namespace wpi
Definition: static_circular_buffer.h:53
std::forward_iterator_tag iterator_category
Definition: static_circular_buffer.h:55
const_iterator(const static_circular_buffer *buffer, size_t index)
Definition: static_circular_buffer.h:61
bool operator==(const const_iterator &) const =default
const_iterator operator++(int)
Definition: static_circular_buffer.h:68
const_iterator & operator++()
Definition: static_circular_buffer.h:64
const_reference operator*() const
Definition: static_circular_buffer.h:74
const T & const_reference
Definition: static_circular_buffer.h:59
T * pointer
Definition: static_circular_buffer.h:58
T value_type
Definition: static_circular_buffer.h:56
std::ptrdiff_t difference_type
Definition: static_circular_buffer.h:57
Definition: static_circular_buffer.h:25
iterator operator++(int)
Definition: static_circular_buffer.h:40
T value_type
Definition: static_circular_buffer.h:28
reference operator*()
Definition: static_circular_buffer.h:46
std::ptrdiff_t difference_type
Definition: static_circular_buffer.h:29
bool operator==(const iterator &) const =default
T & reference
Definition: static_circular_buffer.h:31
iterator & operator++()
Definition: static_circular_buffer.h:36
iterator(static_circular_buffer *buffer, size_t index)
Definition: static_circular_buffer.h:33
T * pointer
Definition: static_circular_buffer.h:30
std::forward_iterator_tag iterator_category
Definition: static_circular_buffer.h:27
This is a simple circular buffer so we don't need to "bucket brigade" copy old values.
Definition: static_circular_buffer.h:21
const T & back() const
Returns value at back of buffer.
Definition: static_circular_buffer.h:146
T & operator[](size_t index)
Definition: static_circular_buffer.h:249
iterator end()
Returns end iterator.
Definition: static_circular_buffer.h:89
void emplace_back(Args &&... args)
Push a new value onto the back of the buffer that is constructed with the provided constructor argume...
Definition: static_circular_buffer.h:203
void push_front(T value)
Push a new value onto the front of the buffer.
Definition: static_circular_buffer.h:153
void reset()
Empties internal buffer.
Definition: static_circular_buffer.h:241
T & back()
Returns value at back of buffer.
Definition: static_circular_buffer.h:138
T pop_back()
Pop value at back of buffer.
Definition: static_circular_buffer.h:233
T pop_front()
Pop value at front of buffer.
Definition: static_circular_buffer.h:220
void push_back(T value)
Push a new value onto the back of the buffer.
Definition: static_circular_buffer.h:168
const_iterator end() const
Returns end iterator.
Definition: static_circular_buffer.h:101
const T & operator[](size_t index) const
Definition: static_circular_buffer.h:254
const_iterator begin() const
Returns begin iterator.
Definition: static_circular_buffer.h:96
const_iterator cbegin() const
Returns begin iterator.
Definition: static_circular_buffer.h:108
const T & front() const
Returns value at front of buffer.
Definition: static_circular_buffer.h:130
void emplace_front(Args &&... args)
Push a new value onto the front of the buffer that is constructed with the provided constructor argum...
Definition: static_circular_buffer.h:186
iterator begin()
Returns begin iterator.
Definition: static_circular_buffer.h:84
const_iterator cend() const
Returns end iterator.
Definition: static_circular_buffer.h:113
size_t size() const
Returns number of elements in buffer.
Definition: static_circular_buffer.h:120
T & front()
Returns value at front of buffer.
Definition: static_circular_buffer.h:125
Definition: ntcore_cpp.h:26