Fix Queue to be a FIFO instead of FILO data structure

v1
Brett 2023-01-05 11:45:27 -05:00
parent beff47b8f0
commit deb947653b
1 changed files with 6 additions and 9 deletions

View File

@ -27,7 +27,6 @@ namespace BLT {
private: private:
int size = 16; int size = 16;
int insertIndex = 0; int insertIndex = 0;
int headIndex = 0;
T* data = new T[size]; T* data = new T[size];
/** /**
@ -37,11 +36,9 @@ namespace BLT {
*/ */
void expand(int newSize){ void expand(int newSize){
auto tempData = new T[newSize]; auto tempData = new T[newSize];
for (int i = 0; i < size - headIndex; i++) for (int i = 0; i < insertIndex; i++)
tempData[i] = data[i + headIndex]; tempData[i] = data[i];
delete[] data; delete[] data;
insertIndex = size - headIndex;
headIndex = 0;
data = tempData; data = tempData;
size = newSize; size = newSize;
} }
@ -60,19 +57,19 @@ namespace BLT {
* @return the element at the "front" of the queue. * @return the element at the "front" of the queue.
*/ */
[[nodiscard]] const T& front() const { [[nodiscard]] const T& front() const {
return data[headIndex]; return data[insertIndex-1];
} }
void pop() { void pop() {
// TODO: throw exception when popping would result in a overflow? // 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. // I didn't make it an exception here due to not wanting to import the class.
if (headIndex >= size) if (isEmpty())
return; return;
headIndex++; insertIndex--;
} }
bool isEmpty(){ bool isEmpty(){
return headIndex >= size; return insertIndex <= 0;
} }
~flat_queue() { ~flat_queue() {