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
|
|
|
*/
|
|
|
|
|
2019-06-15 04:34:03 -04:00
|
|
|
#pragma once
|
|
|
|
|
2021-05-22 10:17:32 -04:00
|
|
|
#include <AK/IntrusiveList.h>
|
2019-06-15 04:34:03 -04:00
|
|
|
#include <AK/OwnPtr.h>
|
|
|
|
#include <AK/SinglyLinkedList.h>
|
|
|
|
#include <AK/Vector.h>
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2019-11-03 06:33:51 -05:00
|
|
|
template<typename T, int segment_size = 1000>
|
2019-06-15 04:34:03 -04:00
|
|
|
class Queue {
|
|
|
|
public:
|
2021-01-10 18:29:28 -05:00
|
|
|
Queue() = default;
|
2021-05-22 10:17:32 -04:00
|
|
|
|
|
|
|
~Queue()
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
}
|
2019-06-15 04:34:03 -04:00
|
|
|
|
2020-02-25 08:55:04 -05:00
|
|
|
size_t size() const { return m_size; }
|
2019-06-15 04:34:03 -04:00
|
|
|
bool is_empty() const { return m_size == 0; }
|
|
|
|
|
2021-01-15 17:51:50 -05:00
|
|
|
template<typename U = T>
|
|
|
|
void enqueue(U&& value)
|
2019-06-15 04:34:03 -04:00
|
|
|
{
|
2021-05-22 10:17:32 -04: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 04:34:03 -04:00
|
|
|
++m_size;
|
|
|
|
}
|
|
|
|
|
2024-04-17 18:38:43 -04:00
|
|
|
ErrorOr<T> try_dequeue()
|
|
|
|
{
|
|
|
|
if (is_empty())
|
|
|
|
return Error::from_errno(ENOENT);
|
|
|
|
|
|
|
|
return dequeue();
|
|
|
|
}
|
|
|
|
|
2019-06-15 04:34:03 -04:00
|
|
|
T dequeue()
|
|
|
|
{
|
2021-02-23 14:42:32 -05:00
|
|
|
VERIFY(!is_empty());
|
2021-05-22 10:17:32 -04:00
|
|
|
auto value = move(m_segments.first()->data[m_index_into_first++]);
|
2019-06-15 04:34:03 -04:00
|
|
|
if (m_index_into_first == segment_size) {
|
2021-05-22 10:17:32 -04:00
|
|
|
delete m_segments.take_first();
|
2019-06-15 04:34:03 -04:00
|
|
|
m_index_into_first = 0;
|
|
|
|
}
|
|
|
|
--m_size;
|
2021-05-22 13:54:52 -04: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 04:34:03 -04:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2022-10-16 18:06:11 -04:00
|
|
|
T const& head() const
|
2019-11-03 06:09:19 -05:00
|
|
|
{
|
2021-02-23 14:42:32 -05:00
|
|
|
VERIFY(!is_empty());
|
2021-05-22 10:17:32 -04:00
|
|
|
return m_segments.first()->data[m_index_into_first];
|
2019-11-03 06:09:19 -05:00
|
|
|
}
|
|
|
|
|
2023-03-14 08:41:13 -04:00
|
|
|
T& tail()
|
|
|
|
{
|
|
|
|
VERIFY(!is_empty());
|
|
|
|
return m_segments.last()->data.last();
|
|
|
|
}
|
|
|
|
|
2019-07-28 15:21:09 -04:00
|
|
|
void clear()
|
|
|
|
{
|
2021-05-22 10:17:32 -04:00
|
|
|
while (auto* segment = m_segments.take_first())
|
|
|
|
delete segment;
|
2019-07-28 15:21:09 -04:00
|
|
|
m_index_into_first = 0;
|
|
|
|
m_size = 0;
|
|
|
|
}
|
|
|
|
|
2019-06-15 04:34:03 -04:00
|
|
|
private:
|
2021-05-22 10:17:32 -04:00
|
|
|
struct QueueSegment {
|
|
|
|
Vector<T, segment_size> data;
|
|
|
|
IntrusiveListNode<QueueSegment> node;
|
|
|
|
};
|
|
|
|
|
2021-09-09 08:00:59 -04:00
|
|
|
IntrusiveList<&QueueSegment::node> m_segments;
|
2020-02-25 08:55:04 -05:00
|
|
|
size_t m_index_into_first { 0 };
|
|
|
|
size_t m_size { 0 };
|
2019-06-15 04:34:03 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-11-26 06:18:30 -05:00
|
|
|
#if USING_AK_GLOBALLY
|
2019-06-15 04:34:03 -04:00
|
|
|
using AK::Queue;
|
2022-11-26 06:18:30 -05:00
|
|
|
#endif
|