2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2019-06-15 08:34:03 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#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;
|
|
|
|
~Queue() = default;
|
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
|
|
|
{
|
|
|
|
if (m_segments.is_empty() || m_segments.last()->size() >= segment_size)
|
|
|
|
m_segments.append(make<Vector<T, segment_size>>());
|
2021-01-15 22:51:50 +00:00
|
|
|
m_segments.last()->append(forward<U>(value));
|
2019-06-15 08:34:03 +00:00
|
|
|
++m_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
T dequeue()
|
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(!is_empty());
|
2019-06-15 08:34:03 +00:00
|
|
|
auto value = move((*m_segments.first())[m_index_into_first++]);
|
|
|
|
if (m_index_into_first == segment_size) {
|
|
|
|
m_segments.take_first();
|
|
|
|
m_index_into_first = 0;
|
|
|
|
}
|
|
|
|
--m_size;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2019-11-03 11:09:19 +00:00
|
|
|
const T& head() const
|
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(!is_empty());
|
2019-11-03 11:09:19 +00:00
|
|
|
return (*m_segments.first())[m_index_into_first];
|
|
|
|
}
|
|
|
|
|
2019-07-28 19:21:09 +00:00
|
|
|
void clear()
|
|
|
|
{
|
|
|
|
m_segments.clear();
|
|
|
|
m_index_into_first = 0;
|
|
|
|
m_size = 0;
|
|
|
|
}
|
|
|
|
|
2019-06-15 08:34:03 +00:00
|
|
|
private:
|
|
|
|
SinglyLinkedList<OwnPtr<Vector<T, segment_size>>> 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
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
using AK::Queue;
|