2#include "../Memory/MemoryBuffer.h"
3#include "../BitTwiddling/PowerOfTwo.h"
16 template<
typename Element,
typename IndexBaseType>
23 typedef typename std::make_unsigned<IndexBaseType>::type
IndexType;
34 IndexType frontOffsetWrap = frontOffset & sizeMask;
35 IndexType backOffsetWrap = backOffset & sizeMask;
36 return frontOffsetWrap == backOffsetWrap;
42 IndexType frontOffsetWrap = frontOffset & sizeMask;
43 IndexType backOffsetWrap = backOffset & sizeMask;
44 return backOffsetWrap - frontOffsetWrap + (backOffsetWrap < frontOffsetWrap ? capacity() : static_cast<IndexType>(0));
50 offsetToFixedIndexShift += backOffset;
60 offsetToFixedIndexShift = 0;
67 if (sizeL2New == sizeL2)
return;
77 storage[backOffset & sizeMask] = e;
87 storage[frontOffset & sizeMask] = e;
94 assert(
false &&
"queue is empty");
103 assert(
empty() ==
false);
106 IndexType offset = fixedIndex - offsetToFixedIndexShift;
107 return storage[offset & sizeMask];
113 assert(
empty() ==
false);
116 IndexType offset = fixedIndex - offsetToFixedIndexShift;
117 return storage[offset & sizeMask];
121 Element&
front() { assert(
empty() ==
false);
return storage[frontOffset & sizeMask]; }
124 const Element&
front()
const { assert(
empty() ==
false);
return storage[frontOffset & sizeMask]; }
127 Element&
back() { assert(
empty() ==
false);
return storage[(backOffset -
IndexType(1)) & sizeMask]; }
130 const Element&
back()
const { assert(
empty() ==
false);
return storage[(backOffset -
IndexType(1)) & sizeMask]; }
141 if (
empty())
return false;
157 bool noCapacity()
const {
return ((frontOffset & sizeMask) == ((backOffset + 1) & sizeMask)); }
177 for (uint32_t i = 0; i < N; i++) {
178 storageNew[i] = storage[(frontOffset + i) & sizeMask];
180 offsetToFixedIndexShift += frontOffset;
185 sizeMask = sizeNew - 1u;
186 storage.swap(storageNew);
Indexable queue where an element index is fixed through arbitrary enqueues and dequeues.
void pushFront(Element e)
Adds a new element to the front of the queue.
IndexType count() const
Returns number of elements currently in the queue.
bool validIndex(uint32_t fixedIndex) const
Check if a fixed index is valid, that is, refers to an element in the queue.
Element & operator[](IndexType fixedIndex)
Look up an element in the queue by a fixed index.
void clear()
Removes all entries from the queue, but will continue with new unused indices.
Element & front()
Returns front element.
const Element & front() const
Returns front element.
void shrink()
Reduce capacity to the minimum needed to preserve the contents.
bool noCapacity() const
True if there is no free capacity left.
void pushBack(Element e)
Adds a new element to the back of the queue.
IndexType frontIndex() const
Returns fixed index of front.
void reset()
Removes all entries from the queue, recycles indices and starts from zero.
IndexType empty() const
Returns true if there are currently no elements in the queue.
const IndexType capacity() const
Returns the current capacity of the queue.
const Element & operator[](IndexType fixedIndex) const
Look up an element in the queue by a fixed index.
Element & back()
Returns back element.
std::make_signed< IndexBaseType >::type SignedIndexType
Signed helper type.
void grow()
Double the capacity of the queue.
const Element & back() const
Returns back element.
void popFront()
Removes the front from the queue.
IndexType backIndex() const
Returns fixed index of current back element.
std::make_unsigned< IndexBaseType >::type IndexType
Unsigned type used for indices, typically uint32_t or uin64_t.
FixedIndexQueue()
Constructor.
Contains all Cogs related functionality.
uint8_t roundUpToPowerOfTwoShift(uint8_t x)