2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2019-06-15 08:34:03 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-05-22 14:17:32 +00:00
|
|
|
#include <AK/IntrusiveList.h>
|
2019-06-15 08:34:03 +00:00
|
|
|
#include <AK/OwnPtr.h>
|
|
|
|
#include <AK/SinglyLinkedList.h>
|
|
|
|
#include <AK/Vector.h>
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2019-11-03 11:33:51 +00:00
|
|
|
template<typename T, int segment_size = 1000>
|
2019-06-15 08:34:03 +00:00
|
|
|
class Queue {
|
|
|
|
public:
|
2021-01-10 23:29:28 +00:00
|
|
|
Queue() = default;
|
2021-05-22 14:17:32 +00:00
|
|
|
|
|
|
|
~Queue()
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
}
|
2019-06-15 08:34:03 +00:00
|
|
|
|
2020-02-25 13:55:04 +00:00
|
|
|
size_t size() const { return m_size; }
|
2019-06-15 08:34:03 +00:00
|
|
|
bool is_empty() const { return m_size == 0; }
|
|
|
|
|
2021-01-15 22:51:50 +00:00
|
|
|
template<typename U = T>
|
|
|
|
void enqueue(U&& value)
|
2019-06-15 08:34:03 +00:00
|
|
|
{
|
2021-05-22 14:17:32 +00:00
|
|
|
if (m_segments.is_empty() || m_segments.last()->data.size() >= segment_size) {
|
|
|
|
auto segment = new QueueSegment;
|
|
|
|
m_segments.append(*segment);
|
|
|
|
}
|
|
|
|
m_segments.last()->data.append(forward<U>(value));
|
2019-06-15 08:34:03 +00:00
|
|
|
++m_size;
|
|
|
|
}
|
|
|
|
|
2024-04-17 22:38:43 +00:00
|
|
|
ErrorOr<T> try_dequeue()
|
|
|
|
{
|
|
|
|
if (is_empty())
|
|
|
|
return Error::from_errno(ENOENT);
|
|
|
|
|
|
|
|
return dequeue();
|
|
|
|
}
|
|
|
|
|
2019-06-15 08:34:03 +00:00
|
|
|
T dequeue()
|
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(!is_empty());
|
2021-05-22 14:17:32 +00:00
|
|
|
auto value = move(m_segments.first()->data[m_index_into_first++]);
|
2019-06-15 08:34:03 +00:00
|
|
|
if (m_index_into_first == segment_size) {
|
2021-05-22 14:17:32 +00:00
|
|
|
delete m_segments.take_first();
|
2019-06-15 08:34:03 +00:00
|
|
|
m_index_into_first = 0;
|
|
|
|
}
|
|
|
|
--m_size;
|
2021-05-22 17:54:52 +00:00
|
|
|
if (m_size == 0 && !m_segments.is_empty()) {
|
|
|
|
// This is not necessary for correctness but avoids faulting in
|
|
|
|
// all the pages for the underlying Vector in the case where
|
|
|
|
// the caller repeatedly enqueues and then dequeues a single item.
|
|
|
|
m_index_into_first = 0;
|
|
|
|
m_segments.last()->data.clear_with_capacity();
|
|
|
|
}
|
2019-06-15 08:34:03 +00:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2022-10-16 22:06:11 +00:00
|
|
|
T const& head() const
|
2019-11-03 11:09:19 +00:00
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(!is_empty());
|
2021-05-22 14:17:32 +00:00
|
|
|
return m_segments.first()->data[m_index_into_first];
|
2019-11-03 11:09:19 +00:00
|
|
|
}
|
|
|
|
|
2023-03-14 12:41:13 +00:00
|
|
|
T& tail()
|
|
|
|
{
|
|
|
|
VERIFY(!is_empty());
|
|
|
|
return m_segments.last()->data.last();
|
|
|
|
}
|
|
|
|
|
2019-07-28 19:21:09 +00:00
|
|
|
void clear()
|
|
|
|
{
|
2021-05-22 14:17:32 +00:00
|
|
|
while (auto* segment = m_segments.take_first())
|
|
|
|
delete segment;
|
2019-07-28 19:21:09 +00:00
|
|
|
m_index_into_first = 0;
|
|
|
|
m_size = 0;
|
|
|
|
}
|
|
|
|
|
2019-06-15 08:34:03 +00:00
|
|
|
private:
|
2021-05-22 14:17:32 +00:00
|
|
|
struct QueueSegment {
|
|
|
|
Vector<T, segment_size> data;
|
|
|
|
IntrusiveListNode<QueueSegment> node;
|
|
|
|
};
|
|
|
|
|
2021-09-09 12:00:59 +00:00
|
|
|
IntrusiveList<&QueueSegment::node> m_segments;
|
2020-02-25 13:55:04 +00:00
|
|
|
size_t m_index_into_first { 0 };
|
|
|
|
size_t m_size { 0 };
|
2019-06-15 08:34:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-11-26 11:18:30 +00:00
|
|
|
#if USING_AK_GLOBALLY
|
2019-06-15 08:34:03 +00:00
|
|
|
using AK::Queue;
|
2022-11-26 11:18:30 +00:00
|
|
|
#endif
|