Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
ring_buf.h
Go to the documentation of this file.
1/* Copyright (C) 2025 Wildfire Games.
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining
4 * a copy of this software and associated documentation files (the
5 * "Software"), to deal in the Software without restriction, including
6 * without limitation the rights to use, copy, modify, merge, publish,
7 * distribute, sublicense, and/or sell copies of the Software, and to
8 * permit persons to whom the Software is furnished to do so, subject to
9 * the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 */
22
23/*
24 * static array, accessible modulo n
25 */
26
27#ifndef INCLUDED_ADTS_RING_BUF
28#define INCLUDED_ADTS_RING_BUF
29
30#include "lib/debug.h"
31#include "lib/status.h"
32
33#include <cstddef>
34#include <iterator>
35
36template<class T, size_t n> class RingBuf
37{
38 size_t size_; // # of entries in buffer
39 size_t head; // index of oldest item
40 size_t tail; // index of newest item
41 T data[n];
42
43public:
44 RingBuf() : data() { clear(); }
45 void clear() { size_ = 0; head = 0; tail = n-1; }
46
47 size_t size() const { return size_; }
48 bool empty() const { return size_ == 0; }
49
50 const T& operator[](int ofs) const
51 {
52 ENSURE(!empty());
53 size_t idx = (size_t)(head + ofs);
54 return data[idx % n];
55 }
56 T& operator[](int ofs)
57 {
58 ENSURE(!empty());
59 size_t idx = (size_t)(head + ofs);
60 return data[idx % n];
61 }
62
64 {
65 ENSURE(!empty());
66 return data[head];
67 }
68 const T& front() const
69 {
70 ENSURE(!empty());
71 return data[head];
72 }
74 {
75 ENSURE(!empty());
76 return data[tail];
77 }
78 const T& back() const
79 {
80 ENSURE(!empty());
81 return data[tail];
82 }
83
84 void push_back(const T& item)
85 {
86 if(size_ < n)
87 size_++;
88 // do not complain - overwriting old values is legit
89 // (e.g. sliding window).
90 else
91 head = (head + 1) % n;
92
93 tail = (tail + 1) % n;
94 data[tail] = item;
95 }
96
97 void pop_front()
98 {
99 if(size_ != 0)
100 {
101 size_--;
102 head = (head + 1) % n;
103 }
104 else
105 DEBUG_WARN_ERR(ERR::LOGIC); // underflow
106 }
107
109 {
110 public:
111 typedef std::random_access_iterator_tag iterator_category;
112 typedef T value_type;
113 typedef ptrdiff_t difference_type;
114 typedef T* pointer;
115 typedef T& reference;
116
117 iterator() : data(0), pos(0)
118 {}
119 iterator(T* data_, size_t pos_) : data(data_), pos(pos_)
120 {}
121 T& operator[](int idx) const
122 { return data[(pos+idx) % n]; }
123 T& operator*() const
124 { return data[pos % n]; }
125 T* operator->() const
126 { return &**this; }
128 { ++pos; return (*this); }
129 iterator operator++(int) // post
130 { iterator tmp = *this; ++*this; return tmp; }
131 bool operator==(const iterator& rhs) const
132 { return data == rhs.data && pos == rhs.pos; }
133 bool operator!=(const iterator& rhs) const
134 { return !(*this == rhs); }
135 bool operator<(const iterator& rhs) const
136 { return (pos < rhs.pos); }
138 { pos += ofs; return *this; }
140 { return (*this += -ofs); }
142 { iterator tmp = *this; return (tmp += ofs); }
144 { iterator tmp = *this; return (tmp -= ofs); }
146 { return (difference_type)(pos - right.pos); }
147
148 protected:
150 size_t pos;
151 // not mod-N so that begin != end when buffer is full.
152 };
153
155 {
156 public:
157 typedef std::random_access_iterator_tag iterator_category;
158 typedef T value_type;
159 typedef ptrdiff_t difference_type;
160 typedef const T* pointer;
161 typedef const T& reference;
162
164 {}
165 const_iterator(const T* data_, size_t pos_) : data(data_), pos(pos_)
166 {}
167 const T& operator[](int idx) const
168 { return data[(pos+idx) % n]; }
169 const T& operator*() const
170 { return data[pos % n]; }
171 const T* operator->() const
172 { return &**this; }
174 { ++pos; return (*this); }
176 { const_iterator tmp = *this; ++*this; return tmp; }
177 bool operator==(const const_iterator& rhs) const
178 { return data == rhs.data && pos == rhs.pos; }
179 bool operator!=(const const_iterator& rhs) const
180 { return !(*this == rhs); }
181 bool operator<(const const_iterator& rhs) const
182 { return (pos < rhs.pos); }
184 { pos += ofs; return *this; }
186 { return (*this += -ofs); }
188 { iterator tmp = *this; return (tmp += ofs); }
190 { iterator tmp = *this; return (tmp -= ofs); }
192 { return (difference_type)(pos - right.pos); }
193
194 protected:
195 const T* data;
196 size_t pos;
197 // not mod-N so that begin != end when buffer is full.
198 };
199
201 {
202 return iterator(data, head);
203 }
205 {
206 return const_iterator(data, head);
207 }
209 {
210 return iterator(data, head+size_);
211 }
213 {
214 return const_iterator(data, head+size_);
215 }
216};
217
218#endif // #ifndef INCLUDED_ADTS_RING_BUF
Definition: ring_buf.h:155
bool operator!=(const const_iterator &rhs) const
Definition: ring_buf.h:179
const_iterator()
Definition: ring_buf.h:163
ptrdiff_t difference_type
Definition: ring_buf.h:159
const_iterator & operator++()
Definition: ring_buf.h:173
difference_type operator-(const iterator right) const
Definition: ring_buf.h:191
const T & reference
Definition: ring_buf.h:161
const_iterator(const T *data_, size_t pos_)
Definition: ring_buf.h:165
const T & operator[](int idx) const
Definition: ring_buf.h:167
const T & operator*() const
Definition: ring_buf.h:169
bool operator<(const const_iterator &rhs) const
Definition: ring_buf.h:181
iterator operator-(difference_type ofs) const
Definition: ring_buf.h:189
const_iterator operator++(int)
Definition: ring_buf.h:175
const T * data
Definition: ring_buf.h:195
iterator & operator-=(difference_type ofs)
Definition: ring_buf.h:185
const T * operator->() const
Definition: ring_buf.h:171
iterator operator+(difference_type ofs) const
Definition: ring_buf.h:187
bool operator==(const const_iterator &rhs) const
Definition: ring_buf.h:177
T value_type
Definition: ring_buf.h:158
size_t pos
Definition: ring_buf.h:196
const T * pointer
Definition: ring_buf.h:160
iterator & operator+=(difference_type ofs)
Definition: ring_buf.h:183
std::random_access_iterator_tag iterator_category
Definition: ring_buf.h:157
Definition: ring_buf.h:109
iterator operator+(difference_type ofs) const
Definition: ring_buf.h:141
std::random_access_iterator_tag iterator_category
Definition: ring_buf.h:111
iterator operator++(int)
Definition: ring_buf.h:129
T value_type
Definition: ring_buf.h:112
T & operator*() const
Definition: ring_buf.h:123
T & reference
Definition: ring_buf.h:115
T * pointer
Definition: ring_buf.h:114
iterator operator-(difference_type ofs) const
Definition: ring_buf.h:143
iterator & operator+=(difference_type ofs)
Definition: ring_buf.h:137
iterator & operator-=(difference_type ofs)
Definition: ring_buf.h:139
bool operator==(const iterator &rhs) const
Definition: ring_buf.h:131
bool operator<(const iterator &rhs) const
Definition: ring_buf.h:135
iterator(T *data_, size_t pos_)
Definition: ring_buf.h:119
bool operator!=(const iterator &rhs) const
Definition: ring_buf.h:133
T & operator[](int idx) const
Definition: ring_buf.h:121
T * data
Definition: ring_buf.h:149
ptrdiff_t difference_type
Definition: ring_buf.h:113
iterator & operator++()
Definition: ring_buf.h:127
size_t pos
Definition: ring_buf.h:150
T * operator->() const
Definition: ring_buf.h:125
iterator()
Definition: ring_buf.h:117
difference_type operator-(const iterator right) const
Definition: ring_buf.h:145
Definition: ring_buf.h:37
T & front()
Definition: ring_buf.h:63
RingBuf()
Definition: ring_buf.h:44
const T & back() const
Definition: ring_buf.h:78
T & back()
Definition: ring_buf.h:73
size_t tail
Definition: ring_buf.h:40
iterator begin()
Definition: ring_buf.h:200
T data[n]
Definition: ring_buf.h:41
T & operator[](int ofs)
Definition: ring_buf.h:56
void push_back(const T &item)
Definition: ring_buf.h:84
const T & front() const
Definition: ring_buf.h:68
const T & operator[](int ofs) const
Definition: ring_buf.h:50
const_iterator end() const
Definition: ring_buf.h:212
bool empty() const
Definition: ring_buf.h:48
size_t head
Definition: ring_buf.h:39
size_t size() const
Definition: ring_buf.h:47
void pop_front()
Definition: ring_buf.h:97
iterator end()
Definition: ring_buf.h:208
void clear()
Definition: ring_buf.h:45
const_iterator begin() const
Definition: ring_buf.h:204
size_t size_
Definition: ring_buf.h:38
#define DEBUG_WARN_ERR(status)
display the error dialog with text corresponding to the given error code.
Definition: debug.h:271
#define ENSURE(expr)
ensure the expression <expr> evaluates to non-zero.
Definition: debug.h:222
const Status LOGIC
Definition: status.h:417
#define T(string_literal)
Definition: secure_crt.cpp:81