Line data Source code
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 : 36 : template<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 : 43 : public: 44 2074 : RingBuf() : data() { clear(); } 45 133 : void clear() { size_ = 0; head = 0; tail = n-1; } 46 : 47 1003 : size_t size() const { return size_; } 48 149 : 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 : 63 149 : T& front() 64 : { 65 149 : ENSURE(!empty()); 66 149 : return data[head]; 67 : } 68 : const T& front() const 69 : { 70 : ENSURE(!empty()); 71 : return data[head]; 72 : } 73 : T& back() 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 10122 : void push_back(const T& item) 85 : { 86 10122 : if(size_ < n) 87 759 : size_++; 88 : // do not complain - overwriting old values is legit 89 : // (e.g. sliding window). 90 : else 91 9363 : head = (head + 1) % n; 92 : 93 10122 : tail = (tail + 1) % n; 94 10122 : data[tail] = item; 95 : } 96 : 97 450 : void pop_front() 98 : { 99 450 : if(size_ != 0) 100 : { 101 450 : size_--; 102 450 : head = (head + 1) % n; 103 : } 104 : else 105 0 : DEBUG_WARN_ERR(ERR::LOGIC); // underflow 106 450 : } 107 : 108 : class iterator 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 1000 : iterator(T* data_, size_t pos_) : data(data_), pos(pos_) 120 : {} 121 : T& operator[](int idx) const 122 : { return data[(pos+idx) % n]; } 123 44284 : T& operator*() const 124 44284 : { return data[pos % n]; } 125 : T* operator->() const 126 : { return &**this; } 127 44284 : iterator& operator++() // pre 128 44284 : { ++pos; return (*this); } 129 : iterator operator++(int) // post 130 : { iterator tmp = *this; ++*this; return tmp; } 131 45623 : bool operator==(const iterator& rhs) const 132 45623 : { return data == rhs.data && pos == rhs.pos; } 133 45623 : bool operator!=(const iterator& rhs) const 134 45623 : { return !(*this == rhs); } 135 : bool operator<(const iterator& rhs) const 136 : { return (pos < rhs.pos); } 137 2678 : iterator& operator+=(difference_type ofs) 138 1339 : { pos += ofs; return *this; } 139 : iterator& operator-=(difference_type ofs) 140 : { return (*this += -ofs); } 141 1339 : iterator operator+(difference_type ofs) const 142 1339 : { iterator tmp = *this; return (tmp += ofs); } 143 : iterator operator-(difference_type ofs) const 144 : { iterator tmp = *this; return (tmp -= ofs); } 145 1000 : difference_type operator-(const iterator right) const 146 1000 : { return (difference_type)(pos - right.pos); } 147 : 148 : protected: 149 : T* data; 150 : size_t pos; 151 : // not mod-N so that begin != end when buffer is full. 152 : }; 153 : 154 : class const_iterator 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 : 163 : const_iterator() : data(0), pos(0) 164 : {} 165 0 : 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 0 : const T& operator*() const 170 0 : { return data[pos % n]; } 171 : const T* operator->() const 172 : { return &**this; } 173 0 : const_iterator& operator++() // pre 174 0 : { ++pos; return (*this); } 175 : const_iterator operator++(int) // post 176 : { const_iterator tmp = *this; ++*this; return tmp; } 177 0 : bool operator==(const const_iterator& rhs) const 178 0 : { return data == rhs.data && pos == rhs.pos; } 179 0 : bool operator!=(const const_iterator& rhs) const 180 0 : { return !(*this == rhs); } 181 : bool operator<(const const_iterator& rhs) const 182 : { return (pos < rhs.pos); } 183 : iterator& operator+=(difference_type ofs) 184 : { pos += ofs; return *this; } 185 : iterator& operator-=(difference_type ofs) 186 : { return (*this += -ofs); } 187 : iterator operator+(difference_type ofs) const 188 : { iterator tmp = *this; return (tmp += ofs); } 189 : iterator operator-(difference_type ofs) const 190 : { iterator tmp = *this; return (tmp -= ofs); } 191 : difference_type operator-(const iterator right) const 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 : 200 1000 : iterator begin() 201 : { 202 1000 : return iterator(data, head); 203 : } 204 0 : const_iterator begin() const 205 : { 206 0 : return const_iterator(data, head); 207 : } 208 1000 : iterator end() 209 : { 210 1000 : return iterator(data, head+size_); 211 : } 212 0 : const_iterator end() const 213 : { 214 0 : return const_iterator(data, head+size_); 215 : } 216 : }; 217 : 218 : #endif // #ifndef INCLUDED_ADTS_RING_BUF