From e50d6ab469be88215b519575ea29fb71bfef6792 Mon Sep 17 00:00:00 2001 From: Brett Date: Mon, 26 Dec 2022 01:10:37 -0500 Subject: [PATCH] flat queue --- include/blt/std/queues.h | 45 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/include/blt/std/queues.h b/include/blt/std/queues.h index 4d8150e..33ba7c2 100644 --- a/include/blt/std/queues.h +++ b/include/blt/std/queues.h @@ -21,7 +21,48 @@ namespace BLT { template class flat_queue { - + private: + int size = 16; + int insertIndex = 0; + int headIndex = 0; + T* data = new T[size]; + + void expand(int newSize){ + auto tempData = new T[newSize]; + for (int i = 0; i < size - headIndex; i++) + tempData[i] = data[i + headIndex]; + delete[] data; + insertIndex = size - headIndex; + headIndex = 0; + data = tempData; + size = newSize; + } + + public: + + void push(const T& t) { + if (insertIndex >= size){ + expand(size * 2); + } + data[insertIndex++] = t; + } + + [[nodiscard]] const T& front() const { + return data[headIndex]; + } + + void pop() { + headIndex++; + // queue is empty. Clear old data. + if (headIndex >= size){ + delete[] data; + data = new T[size]; + } + } + + ~flat_queue() { + delete[](data); + } }; template @@ -30,7 +71,7 @@ namespace BLT { node* head; public: - void insert(const T& t) { + void push(const T& t) { if (head == nullptr) head = new node(t, nullptr); else