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