ESE_Queue Class - NarcoMarshDev/Enforce-Script-Extensions GitHub Wiki

Category: Data Structure
Path: scripts/Game/Collections/ESE_Queue.c

Template class for statically sized first-in, first-out collection of objects.

Overview

FIFO data structure with maximum assigned size that automatically dequeues oldest entry when attempting to enqueue new entry above max size. The raw data behind the scenes is stored in reverse order for optimisation so if wanting to access it directly with ESE_Queue.Raw make sure to account for this.

Code example of creating, enqueueing entries up the max size and automatically dequeuing to make space for new entries:

auto m_Queue = new ESE_Queue<int>(5); // create queue with max size of 5
for (int i = 0; i < 5; i++)
{
    m_Queue.Enqueue(i);
}
m_Queue.PrintRaw(); // >>> {4,3,2,1,0}

// add new entry to show auto dequeue of oldest entry
m_Queue.Enqueue(5);
m_Queue.PrintRaw(); // >>> {5,4,3,2,1}

// manual dequeue
int val = m_Queue.Dequeue();
m_Queue.PrintRaw(); // >>> {10,4,3,2}
Print(val); // >>> 1

Constructor

Name⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ Description
ESE_Queue<Class T> (int size) Template constructor, size parameter sets max size of the queue.

Methods

Name⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ Return⠀⠀⠀⠀⠀ Description
Enqueue (T value) void Enqueues entry to the end of the queue, automatically removes oldest entry if no space is left for it.
Dequeue () T Returns oldest entry from the queue and removes it. Returned value will be null if queue is empty.
Peek () T Returns oldest entry from the queue without removing it. Returned value will be null if queue is empty.
TryEnqueue (T value) bool Tries to enqueue new value and will return true if successful, returns false if the queue is full.
TryDequeue (out T output) bool (out T) Attempts to dequeue from the queue, returning true if successful or false if the queue is empty. The value dequeued is outputted as an out parameter and will be null if empty.
TryPeek (out T output) bool (out T) Same as TryDequeue() but does not remove entry from queue if found.
Contains (T value) bool Returns true if given value is found in the queue, false otherwise.
Clear () void Clears the contents of the queue.
Count () int Returns number of elements currently in the queue.
GetMaxSize () void Returns current max size of the queue.
Resize (int size) void Changes queue max size, if new size is less than current size, oldest entries beyond the new size are removed.
Compact () void Compacts underlying array to reduce memory usage, this has a decent performance impact so don't use in tight loops.
GetDataType () typename Returns current data type of queue contents.
CopyToArray (notnull inout array<T> newArray) void (inout) Copies raw array data of the queue to another array, NOTE the queue data is in reverse order.
CopyFromArray (notnull array<T> inArray) void Copies contents from given array to current queue, and resizes queue to accommodate the new array data.
CopyToDynamicQueue (notnull inout ESE_DynamicQueue<T> newQueue) void (inout) Copies queue contents to dynamic queue and compacts the new queue.
PrintRaw () void Prints raw array data, Print(ESE_Queue.Raw) also works for this use.

Properties

Name⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ Type⠀⠀⠀⠀⠀ Description
Raw ref array<T> Raw data array of the queue. Data is stored in reverse order.
MaxSize int Maximum size of the queue, any entries enqueued beyond this size will remove the oldest entry.
nullValue T Null value used in returns. This is needed because null is sometimes invalid for certain types.
⚠️ **GitHub.com Fallback** ⚠️