2018-10-22 23:46:02 +03:00
|
|
|
#pragma once
|
|
|
|
|
2018-10-23 11:12:50 +03:00
|
|
|
#include "Assertions.h"
|
2018-10-22 23:46:02 +03:00
|
|
|
#include "Types.h"
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2018-11-12 03:28:46 +03:00
|
|
|
template<typename T, size_t Capacity>
|
2018-10-22 23:46:02 +03:00
|
|
|
class CircularQueue {
|
|
|
|
public:
|
|
|
|
CircularQueue()
|
|
|
|
{
|
2018-11-12 03:28:46 +03:00
|
|
|
for (size_t 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; }
|
2018-10-22 23:46:02 +03:00
|
|
|
size_t size() const { return m_size; }
|
|
|
|
|
2018-11-12 03:28:46 +03:00
|
|
|
size_t capacity() const { return Capacity; }
|
|
|
|
|
2018-10-22 23:46:02 +03:00
|
|
|
void dump() const
|
|
|
|
{
|
2018-11-12 03:28:46 +03:00
|
|
|
kprintf("CircularQueue<%zu>:\n", Capacity);
|
2018-10-22 23:46:02 +03:00
|
|
|
kprintf(" size: %zu\n", m_size);
|
2018-11-12 03:28:46 +03:00
|
|
|
for (size_t i = 0; i < Capacity; ++i) {
|
2018-10-22 23:46:02 +03:00
|
|
|
kprintf(" [%zu] %d %c\n", i, m_elements[i], i == m_head ? '*' : ' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2018-11-12 03:28:46 +03:00
|
|
|
T m_elements[Capacity];
|
2018-10-22 23:46:02 +03:00
|
|
|
size_t m_size { 0 };
|
|
|
|
size_t m_head { 0 };
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
using AK::CircularQueue;
|
|
|
|
|