From deb947653ba1d75205519fe0fe20469d0221bcc2 Mon Sep 17 00:00:00 2001 From: Brett Date: Thu, 5 Jan 2023 11:45:27 -0500 Subject: [PATCH] Fix Queue to be a FIFO instead of FILO data structure --- include/blt/std/queues.h | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/include/blt/std/queues.h b/include/blt/std/queues.h index 8daa47e..51543ee 100644 --- a/include/blt/std/queues.h +++ b/include/blt/std/queues.h @@ -27,7 +27,6 @@ namespace BLT { private: int size = 16; int insertIndex = 0; - int headIndex = 0; T* data = new T[size]; /** @@ -37,11 +36,9 @@ namespace BLT { */ void expand(int newSize){ auto tempData = new T[newSize]; - for (int i = 0; i < size - headIndex; i++) - tempData[i] = data[i + headIndex]; + for (int i = 0; i < insertIndex; i++) + tempData[i] = data[i]; delete[] data; - insertIndex = size - headIndex; - headIndex = 0; data = tempData; size = newSize; } @@ -60,19 +57,19 @@ namespace BLT { * @return the element at the "front" of the queue. */ [[nodiscard]] const T& front() const { - return data[headIndex]; + return data[insertIndex-1]; } void pop() { // TODO: throw exception when popping would result in a overflow? // I didn't make it an exception here due to not wanting to import the class. - if (headIndex >= size) + if (isEmpty()) return; - headIndex++; + insertIndex--; } bool isEmpty(){ - return headIndex >= size; + return insertIndex <= 0; } ~flat_queue() {