2020-01-18 03:38:21 -05:00
|
|
|
/*
|
2024-10-04 07:19:50 -04:00
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
|
2020-01-18 03:38:21 -05:00
|
|
|
*
|
2021-04-22 04:24:48 -04:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 03:38:21 -05:00
|
|
|
*/
|
|
|
|
|
2018-10-22 16:46:02 -04:00
|
|
|
#pragma once
|
|
|
|
|
2019-04-13 22:33:43 -04:00
|
|
|
#include <AK/Assertions.h>
|
2020-08-25 08:57:02 -04:00
|
|
|
#include <AK/Forward.h>
|
2020-02-15 19:27:25 -05:00
|
|
|
#include <AK/StdLibExtras.h>
|
2018-10-22 16:46:02 -04:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2020-02-20 07:18:42 -05:00
|
|
|
template<typename T, size_t Capacity>
|
2018-10-22 16:46:02 -04:00
|
|
|
class CircularQueue {
|
|
|
|
public:
|
2021-09-16 02:20:31 -04:00
|
|
|
CircularQueue() = default;
|
2019-10-23 06:20:45 -04:00
|
|
|
|
|
|
|
~CircularQueue()
|
|
|
|
{
|
|
|
|
clear();
|
2018-10-22 16:46:02 -04:00
|
|
|
}
|
|
|
|
|
2019-04-19 21:47:35 -04:00
|
|
|
void clear()
|
|
|
|
{
|
2020-02-20 07:18:42 -05:00
|
|
|
for (size_t i = 0; i < m_size; ++i)
|
2019-10-23 06:20:45 -04:00
|
|
|
elements()[(m_head + i) % Capacity].~T();
|
|
|
|
|
2019-04-19 21:47:35 -04:00
|
|
|
m_head = 0;
|
|
|
|
m_size = 0;
|
|
|
|
}
|
|
|
|
|
2021-11-06 16:12:16 -04:00
|
|
|
bool is_empty() const { return m_size == 0; }
|
2020-02-20 07:18:42 -05:00
|
|
|
size_t size() const { return m_size; }
|
2018-10-22 16:46:02 -04:00
|
|
|
|
2020-02-20 07:18:42 -05:00
|
|
|
size_t capacity() const { return Capacity; }
|
2018-11-11 19:28:46 -05:00
|
|
|
|
2021-01-15 17:56:05 -05:00
|
|
|
template<typename U = T>
|
|
|
|
void enqueue(U&& value)
|
2018-10-22 16:46:02 -04:00
|
|
|
{
|
2019-10-23 06:20:45 -04:00
|
|
|
auto& slot = elements()[(m_head + m_size) % Capacity];
|
|
|
|
if (m_size == Capacity)
|
|
|
|
slot.~T();
|
|
|
|
|
2021-01-15 17:56:05 -05:00
|
|
|
new (&slot) T(forward<U>(value));
|
2018-11-11 19:28:46 -05:00
|
|
|
if (m_size == Capacity)
|
|
|
|
m_head = (m_head + 1) % Capacity;
|
2018-10-22 16:46:02 -04:00
|
|
|
else
|
|
|
|
++m_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
T dequeue()
|
|
|
|
{
|
2021-02-23 14:42:32 -05:00
|
|
|
VERIFY(!is_empty());
|
2019-10-23 06:20:45 -04:00
|
|
|
auto& slot = elements()[m_head];
|
|
|
|
T value = move(slot);
|
|
|
|
slot.~T();
|
2018-11-11 19:28:46 -05:00
|
|
|
m_head = (m_head + 1) % Capacity;
|
2018-10-22 16:46:02 -04:00
|
|
|
--m_size;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2022-10-16 18:06:11 -04:00
|
|
|
T const& at(size_t index) const { return elements()[(m_head + index) % Capacity]; }
|
2022-04-03 11:41:21 -04:00
|
|
|
T& at(size_t index) { return elements()[(m_head + index) % Capacity]; }
|
2019-03-15 07:13:51 -04:00
|
|
|
|
2022-10-16 18:06:11 -04:00
|
|
|
T const& first() const { return at(0); }
|
|
|
|
T const& last() const { return at(size() - 1); }
|
2019-04-19 21:44:01 -04:00
|
|
|
|
2019-01-28 16:40:55 -05:00
|
|
|
class ConstIterator {
|
|
|
|
public:
|
2022-04-01 13:58:27 -04:00
|
|
|
bool operator!=(ConstIterator const& other) { return m_index != other.m_index; }
|
2019-01-28 16:40:55 -05:00
|
|
|
ConstIterator& operator++()
|
|
|
|
{
|
2021-10-07 22:06:51 -04:00
|
|
|
++m_index;
|
2019-01-28 16:40:55 -05:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-10-16 18:06:11 -04:00
|
|
|
T const& operator*() const { return m_queue.at(m_index); }
|
2019-05-28 05:53:16 -04:00
|
|
|
|
2019-01-28 16:40:55 -05:00
|
|
|
private:
|
|
|
|
friend class CircularQueue;
|
2024-04-18 15:32:56 -04:00
|
|
|
ConstIterator(CircularQueue const& queue, size_t const index)
|
2019-05-28 05:53:16 -04:00
|
|
|
: m_queue(queue)
|
|
|
|
, m_index(index)
|
|
|
|
{
|
|
|
|
}
|
2022-04-01 13:58:27 -04:00
|
|
|
CircularQueue const& m_queue;
|
2020-02-20 07:18:42 -05:00
|
|
|
size_t m_index { 0 };
|
2019-01-28 16:40:55 -05:00
|
|
|
};
|
|
|
|
|
2022-04-03 11:41:21 -04:00
|
|
|
class Iterator {
|
|
|
|
public:
|
|
|
|
bool operator!=(Iterator const& other) { return m_index != other.m_index; }
|
|
|
|
Iterator& operator++()
|
|
|
|
{
|
|
|
|
++m_index;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
T& operator*() const { return m_queue.at(m_index); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend class CircularQueue;
|
|
|
|
Iterator(CircularQueue& queue, size_t const index)
|
|
|
|
: m_queue(queue)
|
|
|
|
, m_index(index)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
CircularQueue& m_queue;
|
|
|
|
size_t m_index { 0 };
|
|
|
|
};
|
|
|
|
|
2021-10-07 22:06:51 -04:00
|
|
|
ConstIterator begin() const { return ConstIterator(*this, 0); }
|
|
|
|
ConstIterator end() const { return ConstIterator(*this, size()); }
|
2019-01-28 16:40:55 -05:00
|
|
|
|
2022-04-03 11:41:21 -04:00
|
|
|
Iterator begin() { return Iterator(*this, 0); }
|
|
|
|
Iterator end() { return Iterator(*this, size()); }
|
|
|
|
|
2020-02-20 07:18:42 -05:00
|
|
|
size_t head_index() const { return m_head; }
|
2019-03-27 09:56:35 -04:00
|
|
|
|
2019-10-16 08:29:06 -04:00
|
|
|
protected:
|
2019-10-23 06:20:45 -04:00
|
|
|
T* elements() { return reinterpret_cast<T*>(m_storage); }
|
2022-10-16 18:06:11 -04:00
|
|
|
T const* elements() const { return reinterpret_cast<T const*>(m_storage); }
|
2019-10-23 06:20:45 -04:00
|
|
|
|
2019-01-28 16:40:55 -05:00
|
|
|
friend class ConstIterator;
|
2019-10-23 06:20:45 -04:00
|
|
|
alignas(T) u8 m_storage[sizeof(T) * Capacity];
|
2020-02-20 07:18:42 -05:00
|
|
|
size_t m_size { 0 };
|
|
|
|
size_t m_head { 0 };
|
2018-10-22 16:46:02 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-11-26 06:18:30 -05:00
|
|
|
#if USING_AK_GLOBALLY
|
2018-10-22 16:46:02 -04:00
|
|
|
using AK::CircularQueue;
|
2022-11-26 06:18:30 -05:00
|
|
|
#endif
|