mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-23 18:02:05 -05:00
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:
parent
f11c85f4a7
commit
67041f3a8c
2 changed files with 25 additions and 1 deletions
24
AK/CircularDeque.h
Normal file
24
AK/CircularDeque.h
Normal 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;
|
|
@ -77,7 +77,7 @@ public:
|
||||||
|
|
||||||
int head_index() const { return m_head; }
|
int head_index() const { return m_head; }
|
||||||
|
|
||||||
private:
|
protected:
|
||||||
friend class ConstIterator;
|
friend class ConstIterator;
|
||||||
T m_elements[Capacity];
|
T m_elements[Capacity];
|
||||||
int m_size { 0 };
|
int m_size { 0 };
|
||||||
|
|
Loading…
Add table
Reference in a new issue