AK: Add CircularDeque.

This class inherits from CircularQueue and adds the ability dequeue
from the end of the queue using dequeue_end().

Note that I had to make some of CircularQueue's fields protected to
properly implement dequeue_end.
This commit is contained in:
Drew Stratford 2019-10-17 01:29:06 +13:00 committed by Andreas Kling
parent f11c85f4a7
commit 67041f3a8c
Notes: sideshowbarker 2024-07-19 11:37:15 +09:00
2 changed files with 25 additions and 1 deletions

24
AK/CircularDeque.h Normal file
View File

@ -0,0 +1,24 @@
#pragma once
#include <AK/Assertions.h>
#include <AK/CircularQueue.h>
#include <AK/Types.h>
namespace AK {
template<typename T, int Capacity>
class CircularDeque : public CircularQueue<T, Capacity> {
public:
T dequeue_end()
{
ASSERT(!this->is_empty());
T value = this->m_elements[(this->m_head + this->m_size - 1) % Capacity];
this->m_size--;
return value;
}
};
}
using AK::CircularDeque;

View File

@ -77,7 +77,7 @@ public:
int head_index() const { return m_head; }
private:
protected:
friend class ConstIterator;
T m_elements[Capacity];
int m_size { 0 };