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:
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() {