47 : m_buffer{buffer}, m_index{index} {}
59 return m_buffer == other.m_buffer && m_index == other.m_index;
62 return !(*
this == other);
80 : m_buffer{buffer}, m_index{index} {}
92 return m_buffer == other.m_buffer && m_index == other.m_index;
95 return !(*
this == other);
120 size_t size()
const {
return m_length; }
130 const T&
front()
const {
return (*
this)[0]; }
138 T&
back() {
return m_data[(m_front + m_length - 1) % m_data.size()]; }
147 return m_data[(m_front + m_length - 1) % m_data.size()];
156 if (m_data.size() == 0) {
160 m_front = ModuloDec(m_front);
162 m_data[m_front] = value;
164 if (m_length < m_data.size()) {
175 if (m_data.size() == 0) {
179 m_data[(m_front + m_length) % m_data.size()] = value;
181 if (m_length < m_data.size()) {
185 m_front = ModuloInc(m_front);
195 template <
class... Args>
197 if (m_data.size() == 0) {
201 m_front = ModuloDec(m_front);
203 m_data[m_front] = T{args...};
205 if (m_length < m_data.size()) {
216 template <
class... Args>
218 if (m_data.size() == 0) {
222 m_data[(m_front + m_length) % m_data.size()] = T{args...};
224 if (m_length < m_data.size()) {
228 m_front = ModuloInc(m_front);
239 T& temp = m_data[m_front];
240 m_front = ModuloInc(m_front);
253 return m_data[(m_front + m_length) % m_data.size()];
261 if (
size > m_data.size()) {
263 size_t insertLocation = (m_front + m_length) % m_data.size();
266 if (insertLocation <= m_front) {
267 m_front +=
size - m_data.size();
271 m_data.insert(m_data.begin() + insertLocation,
size - m_data.size(), 0);
272 }
else if (
size < m_data.size()) {
278 size_t elemsToRemove = m_data.size() -
size;
279 auto frontIter = m_data.begin() + m_front;
280 if (m_front < elemsToRemove) {
284 m_data.erase(frontIter +
size, m_data.end());
287 m_data.erase(m_data.begin(), frontIter);
293 m_data.erase(frontIter - elemsToRemove, frontIter);
296 m_front -= elemsToRemove;
303 if (m_length >
size) {
321 return m_data[(m_front + index) % m_data.size()];
328 return m_data[(m_front + index) % m_data.size()];
332 std::vector<T> m_data;
346 size_t ModuloInc(
size_t index) {
return (index + 1) % m_data.size(); }
354 size_t ModuloDec(
size_t index) {
356 return m_data.size() - 1;
Definition circular_buffer.hpp:71
std::forward_iterator_tag iterator_category
Definition circular_buffer.hpp:73
T value_type
Definition circular_buffer.hpp:74
const_iterator(const circular_buffer *buffer, size_t index)
Definition circular_buffer.hpp:79
std::ptrdiff_t difference_type
Definition circular_buffer.hpp:75
const_iterator & operator++()
Definition circular_buffer.hpp:82
bool operator!=(const iterator &other) const
Definition circular_buffer.hpp:94
bool operator==(const iterator &other) const
Definition circular_buffer.hpp:91
const_reference operator*() const
Definition circular_buffer.hpp:97
T * pointer
Definition circular_buffer.hpp:76
const_iterator operator++(int)
Definition circular_buffer.hpp:86
const T & const_reference
Definition circular_buffer.hpp:77
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:61
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:64
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:108
void push_front(T value)
Push a new value onto the front of the buffer.
Definition circular_buffer.hpp:155
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:107
void resize(size_t size)
Resizes internal buffer to given size.
Definition circular_buffer.hpp:259
circular_buffer & operator=(circular_buffer &&)=default
const_iterator cbegin() const
Definition circular_buffer.hpp:112
T pop_front()
Pop value at front of buffer.
Definition circular_buffer.hpp:238
const_iterator cend() const
Definition circular_buffer.hpp:113
const T & operator[](size_t index) const
Definition circular_buffer.hpp:327
size_t size() const
Returns number of elements in buffer.
Definition circular_buffer.hpp:120
void reset()
Empties internal buffer.
Definition circular_buffer.hpp:312
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:196
T & front()
Returns value at front of buffer.
Definition circular_buffer.hpp:125
iterator end()
Definition circular_buffer.hpp:105
T pop_back()
Pop value at back of buffer.
Definition circular_buffer.hpp:251
T & operator[](size_t index)
Definition circular_buffer.hpp:320
iterator begin()
Definition circular_buffer.hpp:104
const T & front() const
Returns value at front of buffer.
Definition circular_buffer.hpp:130
const T & back() const
Returns value at back of buffer.
Definition circular_buffer.hpp:146
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:217
T & back()
Returns value at back of buffer.
Definition circular_buffer.hpp:138
void push_back(T value)
Push a new value onto the back of the buffer.
Definition circular_buffer.hpp:174
Definition StatusCodes.h:18