2018-10-22 23:46:02 +03:00
|
|
|
#pragma once
|
|
|
|
|
2019-04-14 05:33:43 +03:00
|
|
|
#include <AK/Assertions.h>
|
|
|
|
#include <AK/Types.h>
|
2018-10-22 23:46:02 +03:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2019-02-26 04:17:25 +03:00
|
|
|
template<typename T, int Capacity>
|
2018-10-22 23:46:02 +03:00
|
|
|
class CircularQueue {
|
|
|
|
public:
|
|
|
|
CircularQueue()
|
|
|
|
{
|
2019-10-23 13:20:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
~CircularQueue()
|
|
|
|
{
|
|
|
|
clear();
|
2018-10-22 23:46:02 +03:00
|
|
|
}
|
|
|
|
|
2019-04-20 04:47:35 +03:00
|
|
|
void clear()
|
|
|
|
{
|
2019-10-23 13:20:45 +03:00
|
|
|
for (int i = 0; i < m_size; ++i)
|
|
|
|
elements()[(m_head + i) % Capacity].~T();
|
|
|
|
|
2019-04-20 04:47:35 +03:00
|
|
|
m_head = 0;
|
|
|
|
m_size = 0;
|
|
|
|
}
|
|
|
|
|
2018-12-03 02:39:25 +03:00
|
|
|
bool is_empty() const { return !m_size; }
|
2019-02-26 04:17:25 +03:00
|
|
|
int size() const { return m_size; }
|
2018-10-22 23:46:02 +03:00
|
|
|
|
2019-02-26 04:17:25 +03:00
|
|
|
int capacity() const { return Capacity; }
|
2018-11-12 03:28:46 +03:00
|
|
|
|
2019-10-23 13:20:45 +03:00
|
|
|
void enqueue(T&& value)
|
2018-10-22 23:46:02 +03:00
|
|
|
{
|
2019-10-23 13:20:45 +03:00
|
|
|
auto& slot = elements()[(m_head + m_size) % Capacity];
|
|
|
|
if (m_size == Capacity)
|
|
|
|
slot.~T();
|
|
|
|
|
|
|
|
new (&slot) T(value);
|
2018-11-12 03:28:46 +03:00
|
|
|
if (m_size == Capacity)
|
|
|
|
m_head = (m_head + 1) % Capacity;
|
2018-10-22 23:46:02 +03:00
|
|
|
else
|
|
|
|
++m_size;
|
|
|
|
}
|
|
|
|
|
2019-10-23 13:20:45 +03:00
|
|
|
void enqueue(const T& value)
|
|
|
|
{
|
|
|
|
enqueue(T(value));
|
|
|
|
}
|
|
|
|
|
2018-10-22 23:46:02 +03:00
|
|
|
T dequeue()
|
|
|
|
{
|
2018-12-03 02:39:25 +03:00
|
|
|
ASSERT(!is_empty());
|
2019-10-23 13:20:45 +03:00
|
|
|
auto& slot = elements()[m_head];
|
|
|
|
T value = move(slot);
|
|
|
|
slot.~T();
|
2018-11-12 03:28:46 +03:00
|
|
|
m_head = (m_head + 1) % Capacity;
|
2018-10-22 23:46:02 +03:00
|
|
|
--m_size;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2019-10-23 13:20:45 +03:00
|
|
|
const T& at(int index) const { return elements()[(m_head + index) % Capacity]; }
|
2019-03-15 14:13:51 +03:00
|
|
|
|
2019-04-20 04:44:01 +03:00
|
|
|
const T& first() const { return at(0); }
|
|
|
|
const T& last() const { return at(size() - 1); }
|
|
|
|
|
2019-01-29 00:40:55 +03:00
|
|
|
class ConstIterator {
|
|
|
|
public:
|
|
|
|
bool operator!=(const ConstIterator& other) { return m_index != other.m_index; }
|
|
|
|
ConstIterator& operator++()
|
|
|
|
{
|
|
|
|
m_index = (m_index + 1) % Capacity;
|
|
|
|
if (m_index == m_queue.m_head)
|
|
|
|
m_index = m_queue.m_size;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-10-23 13:20:45 +03:00
|
|
|
const T& operator*() const { return m_queue.elements()[m_index]; }
|
2019-05-28 12:53:16 +03:00
|
|
|
|
2019-01-29 00:40:55 +03:00
|
|
|
private:
|
|
|
|
friend class CircularQueue;
|
2019-05-28 12:53:16 +03:00
|
|
|
ConstIterator(const CircularQueue& queue, const int index)
|
|
|
|
: m_queue(queue)
|
|
|
|
, m_index(index)
|
|
|
|
{
|
|
|
|
}
|
2019-01-29 00:40:55 +03:00
|
|
|
const CircularQueue& m_queue;
|
2019-02-26 04:17:25 +03:00
|
|
|
int m_index { 0 };
|
2019-01-29 00:40:55 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
ConstIterator begin() const { return ConstIterator(*this, m_head); }
|
|
|
|
ConstIterator end() const { return ConstIterator(*this, size()); }
|
|
|
|
|
2019-03-27 16:56:35 +03:00
|
|
|
int head_index() const { return m_head; }
|
|
|
|
|
2019-10-16 15:29:06 +03:00
|
|
|
protected:
|
2019-10-23 13:20:45 +03:00
|
|
|
T* elements() { return reinterpret_cast<T*>(m_storage); }
|
|
|
|
const T* elements() const { return reinterpret_cast<const T*>(m_storage); }
|
|
|
|
|
2019-01-29 00:40:55 +03:00
|
|
|
friend class ConstIterator;
|
2019-10-23 13:20:45 +03:00
|
|
|
alignas(T) u8 m_storage[sizeof(T) * Capacity];
|
2019-02-26 04:17:25 +03:00
|
|
|
int m_size { 0 };
|
|
|
|
int m_head { 0 };
|
2018-10-22 23:46:02 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
using AK::CircularQueue;
|