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-02-26 04:17:25 +03:00
|
|
|
for (int i = 0; i < Capacity; ++i)
|
2018-10-22 23:46:02 +03:00
|
|
|
m_elements[i] = T();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2018-10-22 23:46:02 +03:00
|
|
|
void enqueue(const T& t)
|
|
|
|
{
|
2018-11-12 03:28:46 +03:00
|
|
|
m_elements[(m_head + m_size) % Capacity] = t;
|
|
|
|
if (m_size == Capacity)
|
|
|
|
m_head = (m_head + 1) % Capacity;
|
2018-10-22 23:46:02 +03:00
|
|
|
else
|
|
|
|
++m_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
T dequeue()
|
|
|
|
{
|
2018-12-03 02:39:25 +03:00
|
|
|
ASSERT(!is_empty());
|
2018-10-22 23:46:02 +03:00
|
|
|
T value = m_elements[m_head];
|
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-03-15 14:13:51 +03:00
|
|
|
const T& at(int index) const { return m_elements[(m_head + index) % Capacity]; }
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
const T& operator*() const { return m_queue.m_elements[m_index]; }
|
|
|
|
private:
|
|
|
|
friend class CircularQueue;
|
2019-02-26 04:17:25 +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; }
|
|
|
|
|
2018-10-22 23:46:02 +03:00
|
|
|
private:
|
2019-01-29 00:40:55 +03:00
|
|
|
friend class ConstIterator;
|
2018-11-12 03:28:46 +03:00
|
|
|
T m_elements[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;
|
|
|
|
|