mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-22 04:25:13 +00:00
Add a CircularQueue template class to AK.
This commit is contained in:
parent
b824f15619
commit
c8b7173aa8
Notes:
sideshowbarker
2024-07-19 18:45:03 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/c8b7173aa8a
3 changed files with 96 additions and 2 deletions
56
AK/CircularQueue.h
Normal file
56
AK/CircularQueue.h
Normal file
|
@ -0,0 +1,56 @@
|
|||
#pragma once
|
||||
|
||||
#include "Types.h"
|
||||
#include "kstdio.h"
|
||||
|
||||
namespace AK {
|
||||
|
||||
template<typename T, size_t capacity>
|
||||
class CircularQueue {
|
||||
public:
|
||||
CircularQueue()
|
||||
{
|
||||
for (size_t i = 0; i < capacity; ++i)
|
||||
m_elements[i] = T();
|
||||
}
|
||||
|
||||
bool isEmpty() const { return !m_size; }
|
||||
size_t size() const { return m_size; }
|
||||
|
||||
void dump() const
|
||||
{
|
||||
kprintf("CircularQueue<%zu>:\n", capacity);
|
||||
kprintf(" size: %zu\n", m_size);
|
||||
for (size_t i = 0; i < capacity; ++i) {
|
||||
kprintf(" [%zu] %d %c\n", i, m_elements[i], i == m_head ? '*' : ' ');
|
||||
}
|
||||
}
|
||||
|
||||
void enqueue(const T& t)
|
||||
{
|
||||
m_elements[(m_head + m_size) % capacity] = t;
|
||||
if (m_size == capacity)
|
||||
m_head = (m_head + 1) % capacity;
|
||||
else
|
||||
++m_size;
|
||||
}
|
||||
|
||||
T dequeue()
|
||||
{
|
||||
ASSERT(!isEmpty());
|
||||
T value = m_elements[m_head];
|
||||
m_head = (m_head + 1) % capacity;
|
||||
--m_size;
|
||||
return value;
|
||||
}
|
||||
|
||||
private:
|
||||
T m_elements[capacity];
|
||||
size_t m_size { 0 };
|
||||
size_t m_head { 0 };
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
using AK::CircularQueue;
|
||||
|
|
@ -8,12 +8,13 @@ static StringImpl* s_theEmptyStringImpl = nullptr;
|
|||
|
||||
void StringImpl::initializeGlobals()
|
||||
{
|
||||
s_theEmptyStringImpl = new StringImpl(ConstructTheEmptyStringImpl);;
|
||||
s_theEmptyStringImpl = nullptr;
|
||||
}
|
||||
|
||||
StringImpl& StringImpl::theEmptyStringImpl()
|
||||
{
|
||||
ASSERT(s_theEmptyStringImpl);
|
||||
if (!s_theEmptyStringImpl)
|
||||
s_theEmptyStringImpl = new StringImpl(ConstructTheEmptyStringImpl);;
|
||||
return *s_theEmptyStringImpl;
|
||||
}
|
||||
|
||||
|
|
37
AK/test.cpp
37
AK/test.cpp
|
@ -9,11 +9,48 @@
|
|||
#include "Buffer.h"
|
||||
#include "Weakable.h"
|
||||
#include "WeakPtr.h"
|
||||
#include "CircularQueue.h"
|
||||
|
||||
static void testWeakPtr();
|
||||
|
||||
int main(int, char**)
|
||||
{
|
||||
StringImpl::initializeGlobals();
|
||||
{
|
||||
CircularQueue<int, 4> queue;
|
||||
queue.dump();
|
||||
queue.enqueue(1);
|
||||
queue.dump();
|
||||
queue.enqueue(2);
|
||||
queue.dump();
|
||||
queue.enqueue(3);
|
||||
queue.dump();
|
||||
queue.enqueue(4);
|
||||
ASSERT(!queue.isEmpty());
|
||||
ASSERT(queue.size() == 4);
|
||||
ASSERT(queue.dequeue() == 1);
|
||||
queue.dump();
|
||||
ASSERT(queue.dequeue() == 2);
|
||||
queue.dump();
|
||||
ASSERT(queue.dequeue() == 3);
|
||||
queue.dump();
|
||||
ASSERT(queue.dequeue() == 4);
|
||||
queue.dump();
|
||||
ASSERT(queue.isEmpty());
|
||||
queue.enqueue(1);
|
||||
queue.enqueue(2);
|
||||
queue.enqueue(3);
|
||||
queue.enqueue(4);
|
||||
queue.enqueue(5);
|
||||
queue.enqueue(6);
|
||||
queue.enqueue(7);
|
||||
ASSERT(queue.dequeue() == 4);
|
||||
ASSERT(queue.dequeue() == 5);
|
||||
ASSERT(queue.dequeue() == 6);
|
||||
ASSERT(queue.dequeue() == 7);
|
||||
ASSERT(queue.isEmpty());
|
||||
}
|
||||
|
||||
{
|
||||
String path = "/////abc/def////g/h/i//";
|
||||
auto parts = path.split('/');
|
||||
|
|
Loading…
Add table
Reference in a new issue