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