mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-11 05:25:32 +03:00
AK: Add a simple Queue<T> class.
The underlying data structure is a singly-linked list of Vector<T>. We never shift any of the vector contents around, but we batch the memory allocations into 1000-element segments.
This commit is contained in:
parent
9443957c14
commit
c699d9d79d
Notes:
sideshowbarker
2024-07-19 13:36:11 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/c699d9d79df
49
AK/Queue.h
Normal file
49
AK/Queue.h
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <AK/OwnPtr.h>
|
||||||
|
#include <AK/SinglyLinkedList.h>
|
||||||
|
#include <AK/Vector.h>
|
||||||
|
|
||||||
|
namespace AK {
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
class Queue {
|
||||||
|
public:
|
||||||
|
Queue() { }
|
||||||
|
~Queue() { }
|
||||||
|
|
||||||
|
int size() const { return m_size; }
|
||||||
|
bool is_empty() const { return m_size == 0; }
|
||||||
|
|
||||||
|
void enqueue(T&& value)
|
||||||
|
{
|
||||||
|
if (m_segments.is_empty() || m_segments.last()->size() >= segment_size)
|
||||||
|
m_segments.append(make<Vector<T, segment_size>>());
|
||||||
|
m_segments.last()->append(move(value));
|
||||||
|
++m_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
T dequeue()
|
||||||
|
{
|
||||||
|
ASSERT(!is_empty());
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
static const int segment_size = 1000;
|
||||||
|
|
||||||
|
SinglyLinkedList<OwnPtr<Vector<T, segment_size>>> m_segments;
|
||||||
|
int m_index_into_first { 0 };
|
||||||
|
int m_size { 0 };
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
using AK::Queue;
|
||||||
|
|
@ -9,7 +9,7 @@ class SinglyLinkedList {
|
|||||||
private:
|
private:
|
||||||
struct Node {
|
struct Node {
|
||||||
explicit Node(T&& v)
|
explicit Node(T&& v)
|
||||||
: value(v)
|
: value(move(v))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
T value;
|
T value;
|
||||||
@ -66,7 +66,7 @@ public:
|
|||||||
{
|
{
|
||||||
ASSERT(m_head);
|
ASSERT(m_head);
|
||||||
auto* prev_head = m_head;
|
auto* prev_head = m_head;
|
||||||
T value = first();
|
T value = move(first());
|
||||||
if (m_tail == m_head)
|
if (m_tail == m_head)
|
||||||
m_tail = nullptr;
|
m_tail = nullptr;
|
||||||
m_head = m_head->next;
|
m_head = m_head->next;
|
||||||
|
@ -1,9 +1,12 @@
|
|||||||
all: TestString
|
all: TestString TestQueue
|
||||||
|
|
||||||
CXXFLAGS = -std=c++17 -Wall -Wextra
|
CXXFLAGS = -std=c++17 -Wall -Wextra
|
||||||
|
|
||||||
TestString: TestString.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp TestHelpers.h
|
TestString: TestString.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp TestHelpers.h
|
||||||
$(CXX) $(CXXFLAGS) -I../ -I../../ -o $@ TestString.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp
|
$(CXX) $(CXXFLAGS) -I../ -I../../ -o $@ TestString.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp
|
||||||
|
|
||||||
|
TestQueue: TestQueue.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp TestHelpers.h
|
||||||
|
$(CXX) $(CXXFLAGS) -I../ -I../../ -o $@ TestQueue.cpp ../String.cpp ../StringImpl.cpp ../StringBuilder.cpp ../StringView.cpp
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f TestString
|
rm -f TestString TestQueue
|
||||||
|
31
AK/Tests/TestQueue.cpp
Normal file
31
AK/Tests/TestQueue.cpp
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
#include "TestHelpers.h"
|
||||||
|
#include <AK/AKString.h>
|
||||||
|
#include <AK/Queue.h>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
EXPECT(Queue<int>().is_empty());
|
||||||
|
EXPECT(Queue<int>().size() == 0);
|
||||||
|
|
||||||
|
Queue<int> ints;
|
||||||
|
ints.enqueue(1);
|
||||||
|
ints.enqueue(2);
|
||||||
|
ints.enqueue(3);
|
||||||
|
EXPECT(ints.size() == 3);
|
||||||
|
EXPECT(ints.dequeue() == 1);
|
||||||
|
EXPECT(ints.size() == 2);
|
||||||
|
EXPECT(ints.dequeue() == 2);
|
||||||
|
EXPECT(ints.size() == 1);
|
||||||
|
EXPECT(ints.dequeue() == 3);
|
||||||
|
EXPECT(ints.size() == 0);
|
||||||
|
|
||||||
|
Queue<String> strings;
|
||||||
|
strings.enqueue("ABC");
|
||||||
|
strings.enqueue("DEF");
|
||||||
|
EXPECT(strings.size() == 2);
|
||||||
|
EXPECT(strings.dequeue() == "ABC");
|
||||||
|
EXPECT(strings.dequeue() == "DEF");
|
||||||
|
EXPECT(strings.is_empty());
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user