47 : m_buffer{buffer}, m_index{index} {}
59 return m_buffer == other.m_buffer && m_index == other.m_index;
77 : m_buffer{buffer}, m_index{index} {}
89 return m_buffer == other.m_buffer && m_index == other.m_index;
114 size_t size()
const {
return m_length; }
124 const T&
front()
const {
return (*
this)[0]; }
132 T&
back() {
return m_data[(m_front + m_length - 1) % m_data.size()]; }
141 return m_data[(m_front + m_length - 1) % m_data.size()];
150 if (m_data.size() == 0) {
154 m_front = ModuloDec(m_front);
156 m_data[m_front] = value;
158 if (m_length < m_data.size()) {
169 if (m_data.size() == 0) {
173 m_data[(m_front + m_length) % m_data.size()] = value;
175 if (m_length < m_data.size()) {
179 m_front = ModuloInc(m_front);
189 template <
class... Args>
191 if (m_data.size() == 0) {
195 m_front = ModuloDec(m_front);
197 m_data[m_front] = T{args...};
199 if (m_length < m_data.size()) {
210 template <
class... Args>
212 if (m_data.size() == 0) {
216 m_data[(m_front + m_length) % m_data.size()] = T{args...};
218 if (m_length < m_data.size()) {
222 m_front = ModuloInc(m_front);
233 T& temp = m_data[m_front];
234 m_front = ModuloInc(m_front);
247 return m_data[(m_front + m_length) % m_data.size()];
255 if (
size > m_data.size()) {
257 size_t insertLocation = (m_front + m_length) % m_data.size();
260 if (insertLocation <= m_front) {
261 m_front +=
size - m_data.size();
265 m_data.insert(m_data.begin() + insertLocation,
size - m_data.size(), 0);
266 }
else if (
size < m_data.size()) {
272 size_t elemsToRemove = m_data.size() -
size;
273 auto frontIter = m_data.begin() + m_front;
274 if (m_front < elemsToRemove) {
278 m_data.erase(frontIter +
size, m_data.end());
281 m_data.erase(m_data.begin(), frontIter);
287 m_data.erase(frontIter - elemsToRemove, frontIter);
290 m_front -= elemsToRemove;
297 if (m_length >
size) {
315 return m_data[(m_front + index) % m_data.size()];
322 return m_data[(m_front + index) % m_data.size()];
326 std::vector<T> m_data;
340 size_t ModuloInc(
size_t index) {
return (index + 1) % m_data.size(); }
348 size_t ModuloDec(
size_t index) {
350 return m_data.size() - 1;
Definition circular_buffer.hpp:68
std::forward_iterator_tag iterator_category
Definition circular_buffer.hpp:70
T value_type
Definition circular_buffer.hpp:71
const_iterator(const circular_buffer *buffer, size_t index)
Definition circular_buffer.hpp:76
std::ptrdiff_t difference_type
Definition circular_buffer.hpp:72
const_iterator & operator++()
Definition circular_buffer.hpp:79
bool operator==(const iterator &other) const
Definition circular_buffer.hpp:88
const_reference operator*() const
Definition circular_buffer.hpp:91
T * pointer
Definition circular_buffer.hpp:73
const_iterator operator++(int)
Definition circular_buffer.hpp:83
const T & const_reference
Definition circular_buffer.hpp:74
Definition circular_buffer.hpp:38
iterator operator++(int)
Definition circular_buffer.hpp:53
std::ptrdiff_t difference_type
Definition circular_buffer.hpp:42
iterator & operator++()
Definition circular_buffer.hpp:49
T & reference
Definition circular_buffer.hpp:44
std::forward_iterator_tag iterator_category
Definition circular_buffer.hpp:40
bool operator==(const iterator &other) const
Definition circular_buffer.hpp:58
T value_type
Definition circular_buffer.hpp:41
T * pointer
Definition circular_buffer.hpp:43
iterator(circular_buffer *buffer, size_t index)
Definition circular_buffer.hpp:46
reference operator*()
Definition circular_buffer.hpp:61
This is a simple circular buffer so we don't need to "bucket brigade" copy old values.
Definition circular_buffer.hpp:24
const_iterator end() const
Definition circular_buffer.hpp:102
void push_front(T value)
Push a new value onto the front of the buffer.
Definition circular_buffer.hpp:149
circular_buffer(const circular_buffer &)=default
circular_buffer(size_t size)
Constructs a circular buffer.
Definition circular_buffer.hpp:31
const_iterator begin() const
Definition circular_buffer.hpp:101
void resize(size_t size)
Resizes internal buffer to given size.
Definition circular_buffer.hpp:253
circular_buffer & operator=(circular_buffer &&)=default
const_iterator cbegin() const
Definition circular_buffer.hpp:106
T pop_front()
Pop value at front of buffer.
Definition circular_buffer.hpp:232
const_iterator cend() const
Definition circular_buffer.hpp:107
const T & operator[](size_t index) const
Definition circular_buffer.hpp:321
size_t size() const
Returns number of elements in buffer.
Definition circular_buffer.hpp:114
void reset()
Empties internal buffer.
Definition circular_buffer.hpp:306
circular_buffer(circular_buffer &&)=default
circular_buffer & operator=(const circular_buffer &)=default
void emplace_front(Args &&... args)
Push a new value onto the front of the buffer that is constructed with the provided constructor argum...
Definition circular_buffer.hpp:190
T & front()
Returns value at front of buffer.
Definition circular_buffer.hpp:119
iterator end()
Definition circular_buffer.hpp:99
T pop_back()
Pop value at back of buffer.
Definition circular_buffer.hpp:245
T & operator[](size_t index)
Definition circular_buffer.hpp:314
iterator begin()
Definition circular_buffer.hpp:98
const T & front() const
Returns value at front of buffer.
Definition circular_buffer.hpp:124
const T & back() const
Returns value at back of buffer.
Definition circular_buffer.hpp:140
void emplace_back(Args &&... args)
Push a new value onto the back of the buffer that is constructed with the provided constructor argume...
Definition circular_buffer.hpp:211
T & back()
Returns value at back of buffer.
Definition circular_buffer.hpp:132
void push_back(T value)
Push a new value onto the back of the buffer.
Definition circular_buffer.hpp:168
Definition motor_constants.h:14