15#if __has_include("defs.h")
32 using difference_type = std::ptrdiff_t;
34 using reference =
typename std::conditional_t<C_, T const&, T&>;
35 using pointer =
typename std::conditional_t<C_, T const*, T*>;
36 using iterator_category = std::bidirectional_iterator_tag;
49 if (pos == (
size_t)(-1))
50 throw std::range_error (
"Ring buffer iterator at end!");
51 return ring->buf[pos];
57 if (pos == (
size_t)(-1))
58 throw std::range_error (
"Ring buffer iterator at end!");
59 return ring->buf[pos];
65 if (pos == (
size_t)(-1))
66 throw std::range_error (
"Ring buffer iterator at end!");
67 return &ring->buf[pos];
73 if (pos == (
size_t)(-1))
74 throw std::range_error (
"Ring buffer iterator at end!");
75 return &ring->buf[pos];
82 pos = ring->increment (pos);
89 pos = ring->increment (pos);
97 pos = ring->decrement (pos);
104 pos = ring->decrement (pos);
111 return (ring ==
it.ring) && (pos ==
it.pos);
135 tmp.pos = ring->add (pos,
inc);
142 pos = ring->add (pos,
inc);
150 tmp.pos = ring->subtract (pos,
dec);
157 pos = ring->subtract (pos,
dec);
165 size_t p1 = (pos != -1) ? pos
166 : (ring->back_idx == ring->front_idx) ? ring->sz
169 : (ring->back_idx == ring->front_idx) ? ring->sz
174 return ring->cap -
p2 +
p1;
188 typedef iterator_type<false> iterator;
189 typedef iterator_type<true> const_iterator;
210 : front_idx (
other.front_idx)
211 , back_idx (
other.back_idx)
217 buf = std::unique_ptr<T[]> (
new T[
other.cap]);
218 for (
size_t i = 0;
i < sz;
i++)
220 size_t idx = (front_idx +
i) % cap;
235 for (
const auto v :
il)
236 buf[
i++] = std::move (
v);
244 front_idx =
rhs.front_idx;
245 back_idx =
rhs.back_idx;
250 buf.reset (
new T[
rhs.cap]);
251 for (
size_t i = 0;
i < sz;
i++)
253 size_t idx = (front_idx +
i) % cap;
268 for (
size_t i = 0;
i < sz;
i++)
270 size_t idx1 = (front_idx +
i) % cap;
287 operator std::vector<T> ()
const
289 std::vector<T>
v (sz);
290 for (
size_t i = 0;
i < sz;
i++)
291 v[
i] = buf[(front_idx +
i) % cap];
302 if (sz && back_idx == front_idx)
303 front_idx = (front_idx + 1) % cap;
306 buf[back_idx] =
item;
307 back_idx = (back_idx + 1) % cap;
331 return iterator (
this, (
size_t)(-1));
351 front_idx = (front_idx + 1) % cap;
359 return buf[front_idx];
365 return buf[front_idx];
371 return buf[(back_idx + cap - 1) % cap];
377 return buf[(back_idx + cap - 1) % cap];
383 front_idx = back_idx;
413 for (
size_t i = 0;
i < sz;
i++)
414 newbuf[
i] = buf[(front_idx +
i) % cap];
421 back_idx = cap ? (front_idx + sz) % cap : 0;
432 size_t increment (
size_t pos)
const
434 if (cap && pos != (
size_t)-1)
435 pos = (pos + 1) % cap;
442 size_t decrement (
size_t pos)
const
446 if (pos == (
size_t)-1)
447 pos = (back_idx + cap - 1) % cap;
448 else if (pos != front_idx)
449 pos = (pos + cap - 1) % cap;
455 size_t add (
size_t oldpos,
size_t delta)
const
457 if (cap && oldpos != -1)
459 size_t np = oldpos + (delta % cap);
460 if (np >= cap && np >= back_idx + cap)
470 size_t subtract (
size_t oldpos,
size_t delta)
const
474 size_t np = (oldpos == (size_t)-1 ? back_idx : oldpos) - (delta % cap) + cap;
484 std::unique_ptr<T[]> buf;
485 size_t front_idx, back_idx, cap, sz;
Iterator through the circular buffer.
Definition ringbuf.h:29
iterator_type< C_ > & operator+=(size_t inc)
Addition assignment operator.
Definition ringbuf.h:140
pointer operator->()
Object pointer.
Definition ringbuf.h:63
iterator_type< C_ > & operator--()
Decrement operator (prefix)
Definition ringbuf.h:102
iterator_type< C_ > operator+(size_t inc) const
Addition operator.
Definition ringbuf.h:132
iterator_type< C_ > & operator++()
Increment operator (prefix)
Definition ringbuf.h:87
iterator_type()
Default constructor.
Definition ringbuf.h:39
iterator_type< C_ > & operator=(const iterator_type< C_ > &rhs)
Assignment operator.
Definition ringbuf.h:121
reference operator*()
Dereference operator.
Definition ringbuf.h:47
iterator_type< C_ > operator-(size_t dec) const
Subtraction operator.
Definition ringbuf.h:147
bool operator!=(const iterator_type< C_ > &it) const
Inequality comparison.
Definition ringbuf.h:115
const pointer operator->() const
Object pointer (const version)
Definition ringbuf.h:71
iterator_type< C_ > & operator-=(size_t dec)
Subtraction assignment operator.
Definition ringbuf.h:155
bool operator==(const iterator_type< C_ > &it) const
Equality comparison.
Definition ringbuf.h:109
Circular buffer.
Definition ringbuf.h:24
void pop_front()
Remove oldest element from buffer.
Definition ringbuf.h:347
void clear(void)
Remove all elements from buffer.
Definition ringbuf.h:381
size_t size() const
Return number of elements in buffer.
Definition ringbuf.h:425
ring_buffer(const ring_buffer &other)
Copy constructor.
Definition ringbuf.h:209
T & front()
Return a reference to first (oldest) element in buffer.
Definition ringbuf.h:357
void push_back(const T &item)
Inserts new element in buffer.
Definition ringbuf.h:297
ring_buffer()
Default constructor.
Definition ringbuf.h:201
bool empty(void)
Return true if buffer is empty.
Definition ringbuf.h:388
const T & front() const
Return a reference to first (oldest) element in buffer.
Definition ringbuf.h:363
ring_buffer(std::initializer_list< T > il)
Initializer list constructor.
Definition ringbuf.h:227
bool full(void) const
Return true if buffer is full.
Definition ringbuf.h:394
bool operator==(const ring_buffer< T > &other) const
Equality operator.
Definition ringbuf.h:264
bool operator!=(const ring_buffer< T > &other) const
Inequality operator.
Definition ringbuf.h:281
const_iterator begin() const
Return a const iterator pointing to first (oldest) element in buffer.
Definition ringbuf.h:317
void resize(size_t new_cap)
(Re)allocate buffer with a different capacity
Definition ringbuf.h:406
size_t capacity(void) const
Return maximum buffer size.
Definition ringbuf.h:400
const T & back() const
Return reference to last (newest) element in buffer.
Definition ringbuf.h:375
const_iterator cbegin()
Return a const iterator pointing to first (oldest) element in buffer.
Definition ringbuf.h:323
ring_buffer & operator=(const ring_buffer &rhs)
Assignment operator.
Definition ringbuf.h:240
iterator end()
Return an iterator pointing past the last (newest) element in buffer.
Definition ringbuf.h:329
T & back()
Return reference to last (newest) element in buffer.
Definition ringbuf.h:369
const_iterator end() const
Return a const iterator pointing past the last (newest) element in buffer.
Definition ringbuf.h:335
ring_buffer(size_t size)
Constructor.
Definition ringbuf.h:192
const_iterator cend()
Return an iterator pointing past the last (newest) element in buffer.
Definition ringbuf.h:341
iterator begin()
Return an iterator pointing to first (oldest) element in buffer.
Definition ringbuf.h:311