4#include "../util/util.hpp"
42 template<std::swappable T>
57 T& operator*()
const {
return *ptr; }
64 if (ptr == &*b.buffer.begin()) { ptr =
const_cast<T*
>(&*b.buffer.rbegin()); }
70 if (ptr == &*b.buffer.begin()) { ptr =
const_cast<T*
>(&*b.buffer.rbegin()); }
75 return lhs.getCurrentIndex() - rhs.getCurrentIndex();
80 bool operator==(
const Iterator& other)
const {
81 return this->ptr == other.ptr;
85 if (ptr == &*b.buffer.rbegin()) { ptr =
const_cast<T*
>(&*b.buffer.begin()); }
91 if (ptr == &*b.buffer.rbegin()) { ptr =
const_cast<T*
>(&*b.buffer.begin()); }
119 return "Element: " + std::to_string(*ptr) +
", Index: " + std::to_string(getCurrentIndex()) +
", Pointer: " + std::to_string(
reinterpret_cast<long>(ptr));
122 size_t getCurrentIndex()
const {
123 return reinterpret_cast<long>(ptr - &*b.buffer.begin());
128 void push_back(T& t);
129 void push_back(T&& t) { push_back(t); };
130 void emplace_back(T&& t);
150 const Iterator end() {
return Iterator(*
this, util::getIncrementedIndex(
writeIndex, buffer.size())); }
151 const Iterator rbegin() {
return Iterator(*
this, util::getIncrementedIndex(
writeIndex + 1, buffer.size())); }
152 const Iterator rend() {
return Iterator(*
this, util::getIncrementedIndex(
writeIndex, buffer.size())); }
160 void resize(
const size_t size);
162 size_t capacity()
const {
return vectorCapacity - 1; }
163 size_t size()
const {
return buffer.size() - 1; }
167 std::vector<T> buffer;
168 size_t vectorCapacity;
171 template<std::swappable T>
173 buffer.reserve(capacity + 1);
175 vectorCapacity = capacity + 1;
180 template<std::swappable T>
182 if (size + 1 > buffer.capacity()) {
184 util::incrementIndex(writeIndex, buffer.size());
186 std::rotate(buffer.begin(), buffer.begin() + writeIndex, buffer.end());
187 buffer.reserve(size + 1);
188 writeIndex = buffer.size() - 1;
190 else if (size + 1 < buffer.size()) {
192 util::incrementIndex(writeIndex, buffer.size());
194 std::rotate(buffer.begin(), buffer.begin() + util::getValidIndex(
static_cast<int>(writeIndex - size), buffer.size()), buffer.end());
195 buffer.resize(size + 1);
196 writeIndex = util::getValidIndex(
static_cast<int>(buffer.size() - 2), buffer.size());
198 vectorCapacity = size + 1;
202 template<std::swappable T>
204 util::incrementIndex(writeIndex, vectorCapacity);
205 if (buffer.size() < vectorCapacity) {
209 buffer[writeIndex] = t;
212 template<std::swappable T>
213 void RingBuffer<T>::emplace_back(T&& t) {
214 util::incrementIndex(writeIndex, vectorCapacity);
215 if (buffer.size() < vectorCapacity) {
216 buffer.emplace_back(std::move(t));
219 buffer[writeIndex] = std::move(t);
A fixed size buffer that can store data continuously without needing to move data or reallocate memor...
Definition: ringbuffer.hpp:43
size_t writeIndex
Points to the element that was last written.
Definition: ringbuffer.hpp:166
const Iterator cend() const
Return an iterator poiting to the element preceeding the oldest element.
Definition: ringbuffer.hpp:139
void resize(const size_t size)
Resize the buffer to contain max size elements.
Definition: ringbuffer.hpp:181
const Iterator crend() const
Return an iterator pointing to the element following the newest element.
Definition: ringbuffer.hpp:147
const Iterator crbegin() const
Return an iterator pointing to the oldest object.
Definition: ringbuffer.hpp:143
const Iterator cbegin() const
Return an iterator pointing to the newest object.
Definition: ringbuffer.hpp:135
A generic iterator that satisfies std::forward_iterator.
Definition: iterator.hpp:9
Bidirectonal iterator for the RingBuffer.
Definition: ringbuffer.hpp:51
Iterator()
Definition: ringbuffer.hpp:79
std::string to_string() const
Get the index of the vector that ptr points to.
Definition: ringbuffer.hpp:118