43 : m_buffer{buffer}, m_index{index} {}
71 : m_buffer{buffer}, m_index{index} {}
106 size_t size()
const {
return m_length; }
116 const T&
front()
const {
return (*
this)[0]; }
124 T&
back() {
return m_data[(m_front + m_length - 1) % m_data.size()]; }
133 return m_data[(m_front + m_length - 1) % m_data.size()];
142 if (m_data.size() == 0) {
146 m_front = ModuloDec(m_front);
148 m_data[m_front] = value;
150 if (m_length < m_data.size()) {
161 if (m_data.size() == 0) {
165 m_data[(m_front + m_length) % m_data.size()] = value;
167 if (m_length < m_data.size()) {
171 m_front = ModuloInc(m_front);
181 template <
class... Args>
183 if (m_data.size() == 0) {
187 m_front = ModuloDec(m_front);
189 m_data[m_front] = T{args...};
191 if (m_length < m_data.size()) {
202 template <
class... Args>
204 if (m_data.size() == 0) {
208 m_data[(m_front + m_length) % m_data.size()] = T{args...};
210 if (m_length < m_data.size()) {
214 m_front = ModuloInc(m_front);
225 T& temp = m_data[m_front];
226 m_front = ModuloInc(m_front);
239 return m_data[(m_front + m_length) % m_data.size()];
259 return m_data[(m_front + index) % m_data.size()];
266 return m_data[(m_front + index) % m_data.size()];
270 std::vector<T> m_data;
284 size_t ModuloInc(
size_t index) {
return (index + 1) % m_data.size(); }
292 size_t ModuloDec(
size_t index) {
294 return m_data.size() - 1;
Definition: circular_buffer.h:62
std::ptrdiff_t difference_type
Definition: circular_buffer.h:66
const_iterator & operator++()
Definition: circular_buffer.h:73
const_reference operator*() const
Definition: circular_buffer.h:83
T value_type
Definition: circular_buffer.h:65
bool operator==(const const_iterator &) const =default
std::forward_iterator_tag iterator_category
Definition: circular_buffer.h:64
const T & const_reference
Definition: circular_buffer.h:68
const_iterator(const circular_buffer *buffer, size_t index)
Definition: circular_buffer.h:70
T * pointer
Definition: circular_buffer.h:67
const_iterator operator++(int)
Definition: circular_buffer.h:77
Definition: circular_buffer.h:34
iterator & operator++()
Definition: circular_buffer.h:45
bool operator==(const iterator &) const =default
reference operator*()
Definition: circular_buffer.h:55
std::ptrdiff_t difference_type
Definition: circular_buffer.h:38
iterator operator++(int)
Definition: circular_buffer.h:49
T & reference
Definition: circular_buffer.h:40
T * pointer
Definition: circular_buffer.h:39
iterator(circular_buffer *buffer, size_t index)
Definition: circular_buffer.h:42
std::forward_iterator_tag iterator_category
Definition: circular_buffer.h:36
T value_type
Definition: circular_buffer.h:37
This is a simple circular buffer so we don't need to "bucket brigade" copy old values.
Definition: circular_buffer.h:20
size_t size() const
Returns number of elements in buffer.
Definition: circular_buffer.h:106
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.h:203
iterator end()
Definition: circular_buffer.h:91
iterator begin()
Definition: circular_buffer.h:90
T pop_back()
Pop value at back of buffer.
Definition: circular_buffer.h:237
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.h:182
circular_buffer & operator=(circular_buffer &&)=default
T & operator[](size_t index)
Definition: circular_buffer.h:258
void push_front(T value)
Push a new value onto the front of the buffer.
Definition: circular_buffer.h:141
circular_buffer(size_t size)
Constructs a circular buffer.
Definition: circular_buffer.h:27
const_iterator cbegin() const
Definition: circular_buffer.h:98
void resize(size_t size)
Resizes internal buffer to given size.
Definition: circular_buffer.inc:15
circular_buffer(const circular_buffer &)=default
circular_buffer(circular_buffer &&)=default
void reset()
Empties internal buffer.
Definition: circular_buffer.h:250
void push_back(T value)
Push a new value onto the back of the buffer.
Definition: circular_buffer.h:160
T pop_front()
Pop value at front of buffer.
Definition: circular_buffer.h:224
const T & operator[](size_t index) const
Definition: circular_buffer.h:265
const T & back() const
Returns value at back of buffer.
Definition: circular_buffer.h:132
const_iterator end() const
Definition: circular_buffer.h:94
const_iterator begin() const
Definition: circular_buffer.h:93
T & front()
Returns value at front of buffer.
Definition: circular_buffer.h:111
const_iterator cend() const
Definition: circular_buffer.h:99
const T & front() const
Returns value at front of buffer.
Definition: circular_buffer.h:116
T & back()
Returns value at back of buffer.
Definition: circular_buffer.h:124
Definition: ntcore_cpp.h:26