summaryrefslogtreecommitdiff
path: root/AK
diff options
context:
space:
mode:
authorDrew Stratford <drewstratford@outlook.com>2019-10-17 01:29:06 +1300
committerAndreas Kling <awesomekling@gmail.com>2019-10-20 10:51:12 +0200
commit67041f3a8c6930300c5b40ef022f9c4c7f287cce (patch)
tree7c77507b53a7a0a9af92f21137f74f5da0d07128 /AK
parentf11c85f4a7bde369953227ebf39d2d80db42707d (diff)
downloadserenity-67041f3a8c6930300c5b40ef022f9c4c7f287cce.zip
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.
Diffstat (limited to 'AK')
-rw-r--r--AK/CircularDeque.h24
-rw-r--r--AK/CircularQueue.h2
2 files changed, 25 insertions, 1 deletions
diff --git a/AK/CircularDeque.h b/AK/CircularDeque.h
new file mode 100644
index 0000000000..7f7f4d7f19
--- /dev/null
+++ b/AK/CircularDeque.h
@@ -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;
diff --git a/AK/CircularQueue.h b/AK/CircularQueue.h
index 7f1ce3ccf9..e48249bf5e 100644
--- a/AK/CircularQueue.h
+++ b/AK/CircularQueue.h
@@ -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 };