27 #ifndef NRG_RING_BUFFER_H
28 #define NRG_RING_BUFFER_H
37 template<
class T,
size_t N>
44 class iterator :
public std::iterator<std::forward_iterator_tag, const T> {
49 bool operator<(
const iterator& other)
const {
50 size_t dist_a = calcDist(*
this), dist_b = calcDist(other);
51 return dist_a < dist_b;
54 bool operator==(
const iterator& other)
const {
55 return idx == other.idx;
58 bool operator!=(
const iterator& other)
const {
59 return idx != other.idx;
63 if(idx != (ssize_t)buff->end_idx) idx = (idx + 1) % buff->data.size();
68 if(idx != (ssize_t)buff->start_idx) --idx;
69 if(idx < 0) idx = buff->data.size()-1;
73 const T* operator->()
const {
74 if(idx == buff->end_idx){
77 return &buff->data[idx];
81 const T& operator*()
const {
82 return buff->data[idx];
86 if(i.idx > i.buff.start_idx){
87 return i.idx - i.buff->start_idx;
89 return i.buff->size() - (i.buff->start_idx - i.idx);
98 T& ret = data[end_idx];
99 end_idx = (end_idx + 1) % data.size();
100 if(end_idx == start_idx){
101 start_idx = (start_idx + 1) % data.size();
108 return iterator(
this, start_idx);
113 return iterator(
this, end_idx);
117 std::reverse_iterator<iterator>
rbegin()
const {
118 return std::reverse_iterator<iterator>(
end());
122 std::reverse_iterator<iterator>
rend()
const {
123 return std::reverse_iterator<iterator>(
begin());
127 std::array<T, N> data;
128 size_t start_idx, end_idx;
iterator begin() const
Returns an Iterator to the beginning of the RingBuffer.
Definition: nrg_ringbuffer.h:107
Iterator class for the RingBuffer.
Definition: nrg_ringbuffer.h:44
iterator end() const
Returns an Iterator to the end of the RingBuffer.
Definition: nrg_ringbuffer.h:112
iterator(const RingBuffer *const rb, size_t i)
Default Iterator Constructor.
Definition: nrg_ringbuffer.h:47
ring / circular buffer implementation
Definition: nrg_ringbuffer.h:38
std::reverse_iterator< iterator > rend() const
Returns a Reverse-Iterator to the reverse-end of the RingBuffer.
Definition: nrg_ringbuffer.h:122
Common defines and includes used by all the other nrg header files.
std::reverse_iterator< iterator > rbegin() const
Returns a Reverse-Iterator to the reverse-beginning of the RingBuffer.
Definition: nrg_ringbuffer.h:117
RingBuffer()
Default Constructor.
Definition: nrg_ringbuffer.h:41
T & next(void)
Moves the ringbuffer up one, and returns a reference to this element.
Definition: nrg_ringbuffer.h:97