3#include "../util/util.hpp"
15 template<std::
unsigned_
integral I, std::
unsigned_
integral S>
17 if (i < containerSize - 1) { i++; }
20 template<std::
unsigned_
integral I, std::
unsigned_
integral S>
23 else { i = containerSize - 1; }
25 template<std::
unsigned_
integral I, std::
unsigned_
integral S>
27 if (i < containerSize - 1) {
return i + 1; }
30 template<std::
unsigned_
integral I, std::
unsigned_
integral S>
32 if (i > 0) {
return i - 1; }
33 else {
return containerSize - 1; }
36 template<std::
integral I, std::
unsigned_
integral S>
39 return containerSize - (-i) % containerSize - 1;
41 else if (i >=
static_cast<int>(containerSize)) {
42 return i % containerSize;
59 template<std::swappable T>
67 Queue(
size_t size=10,
size_t maxSize=-1);
70 void push_back(T&& t) { push_back(t); };
71 void emplace_back(T&& t);
96 std::vector<T>& getInternalBuffer() {
return buffer; }
107 std::vector<T> buffer;
108 size_t vectorCapacity;
113 template<std::swappable T>
115 : vectorCapacity(capacity), maxSize(maxSize) {
116 buffer.reserve(capacity);
124 template<std::swappable T>
127 if (buffer.size() == maxSize) {
128 incrementIndex(readIndex, buffer.size());
132 if (writeIndex != vectorCapacity - 1) {
134 std::rotate(buffer.begin(), buffer.begin() + readIndex, buffer.end());
136 writeIndex = vectorCapacity - 1;
139 buffer.reserve(std::min(std::max(
static_cast<size_t>(1.1 * vectorCapacity), vectorCapacity + 3), maxSize));
140 vectorCapacity = buffer.capacity();
144 template<std::swappable T>
148 if (readIndex == getIncrementedIndex(writeIndex, vectorCapacity)) { resize(); }
150 util::incrementIndex(writeIndex, vectorCapacity);
151 if (buffer.size() < vectorCapacity) {
155 buffer[writeIndex] = t;
162 template<std::swappable T>
163 void Queue<T>::emplace_back(T&& t) {
167 util::incrementIndex(writeIndex, vectorCapacity);
168 if (buffer.size() < vectorCapacity) {
169 buffer.emplace_back(std::move(t));
172 buffer[writeIndex] = std::move(t);
178 template<std::swappable T>
181 bool hasElement = writeIndex != readIndex;
187 template<std::swappable T>
190 incrementIndex(readIndex, vectorCapacity);
191 size_t i = readIndex;
192 T& element = buffer[i];
198 template<std::swappable T>
201 incrementIndex(readIndex, vectorCapacity);
202 size_t i = readIndex;
204 return std::move(buffer[i]);
208 template<std::swappable T>
A thread-safe queue with a dynamic size up until a maximum size.
Definition: queue.hpp:60
void clear()
Remove all elements.
Definition: queue.hpp:209
T & getRef()
Get a reference to the oldest element.
Definition: queue.hpp:188
size_t writeIndex
Points to the element that was last written.
Definition: queue.hpp:105
size_t readIndex
Points to the element that was last read.
Definition: queue.hpp:106
Queue(size_t size=10, size_t maxSize=-1)
Create a new queue.
Definition: queue.hpp:114
void resize()
Resize the queue (if possible)
Definition: queue.hpp:125
bool hasElement()
Check if the contains has an element that can be retrieved by get()
Definition: queue.hpp:179
T getCopy()
Get a copy of the oldest element.
Definition: queue.hpp:199
void incrementIndex(I &i, const S containerSize)
Increment an index. Up to containerSize, then restart at 0.
Definition: util.hpp:14
void decrementIndex(I &i, const S containerSize)
Decrement an index. Down to 0, then restart at containerSize - 1.
Definition: util.hpp:22
I getDecrementedIndex(const I i, const S containerSize)
Like decrementIndex, but returns a new number.
Definition: util.hpp:38
std::size_t getValidIndex(const I i, const S containerSize)
Wrap an index around, to make it valid.
Definition: util.hpp:51
I getIncrementedIndex(const I i, const S containerSize)
Like incrementIndex, but returns a new number.
Definition: util.hpp:30