10 #include <mm_malloc.h>
15 template <
typename T,
typename TCounter = Size>
21 StorageType* data =
nullptr;
40 this->alloc(
size, 0, 0);
41 for (TCounter i = 0; i < maxSize; ++i) {
42 new (data + i) StorageType();
50 Queue(std::initializer_list<StorageType> list) {
51 this->alloc(list.size(), 0, 0);
53 for (
auto& l : list) {
54 new (data + i) StorageType(l);
64 , maxSize(other.maxSize) {
66 other.first = other.last = other.maxSize = 0;
70 for (TCounter i = first; i < last; ++i) {
71 data[i].~StorageType();
92 return data[first + idx];
100 return data[first + idx];
124 return data[last - 1];
132 return data[last - 1];
146 return last == first;
155 this->reserveFront(1);
159 new (data + first) StorageType();
167 if (last == maxSize) {
169 this->reserveBack(1);
173 new (data + last - 1) StorageType();
174 data[last - 1] = value;
182 T value = this->
front();
183 data[first].~StorageType();
193 T value = this->
back();
194 data[last - 1].~StorageType();
203 for (TCounter i = first; i < last; ++i) {
204 data[i].~StorageType();
243 void alloc(
const TCounter
size,
const TCounter extraFront,
const TCounter extraBack) {
244 maxSize =
size + extraFront + extraBack;
245 first = last = extraFront;
247 data = (StorageType*)_mm_malloc(maxSize *
sizeof(StorageType),
alignof(StorageType));
252 void reserveFront(
const TCounter num) {
255 newQueue.alloc(this->
size(),
max(num, this->
size()),
min(maxSize - last, this->
size()));
256 this->moveElements(std::move(newQueue));
263 void reserveBack(
const TCounter num) {
264 if (num > maxSize - last) {
267 this->moveElements(std::move(newQueue));
273 void moveElements(
Queue&& newQueue) {
274 newQueue.last = newQueue.first + this->
size();
275 SPH_ASSERT(newQueue.last <= newQueue.maxSize);
278 for (TCounter i = 0; i < this->
size(); ++i) {
279 new (newQueue.data + newQueue.first + i) StorageType(std::move(data[i + first]));
282 *
this = std::move(newQueue);
Simple non-owning view of a container.
#define SPH_ASSERT(x,...)
uint32_t Size
Integral type used to index arrays (by default).
constexpr INLINE T max(const T &f1, const T &f2)
NAMESPACE_SPH_BEGIN constexpr INLINE T min(const T &f1, const T &f2)
Minimum & Maximum value.
Additional math routines (with more includes).
#define INLINE
Macros for conditional compilation based on selected compiler.
#define NAMESPACE_SPH_END
Object providing safe access to continuous memory of data.
Simple (forward) iterator over continuous array of objects of type T.
Container allowing to add and remove elements from both ends.
void pushFront(const T &value)
Adds a new element to the front of the queue.
INLINE Iterator< StorageType > end()
Returns an iterator pointing to the one-past-last element of the queue.
Queue()=default
Constructs an empty queue.
T popBack()
Removes an element from the back of the queue.
INLINE bool empty() const
Returns true if the queue contains no elements.
INLINE T & operator[](const TCounter idx)
Returns a reference to idx-th element in the queue.
Queue(const TCounter size)
Constructs a queue with given number of elements.
INLINE Iterator< const StorageType > end() const
Returns a const iterator pointing to the one-past-last element of the queue.
INLINE T & back()
Returns a reference to the last element in the queue.
INLINE const T & operator[](const TCounter idx) const
Returns a const reference to idx-th element in the queue.
INLINE Size size() const
Returns the number of elements in the queue.
INLINE const T & back() const
Returns a const reference to the last element in the queue.
INLINE Iterator< StorageType > begin()
Returns an iterator pointing to the first element of the queue.
INLINE const T & front() const
Returns a const reference to the first element in the queue.
INLINE Iterator< const StorageType > begin() const
Returns a const iterator pointing to the first element of the queue.
void clear()
Removes all elements from the queue.
Queue & operator=(Queue &&other)
Queue(std::initializer_list< StorageType > list)
Constructs a queue from initializer list.
INLINE T & front()
Returns a reference to the first element in the queue.
T popFront()
Removes an element from the front of the queue.
void pushBack(const T &value)
Adds a new element to the back of the queue.
void swap(Sph::Array< T, TCounter > &ar1, Sph::Array< T, TCounter > &ar2)
Object with deleted copy constructor and copy operator.