Skip to content

ESE_Stack Class

NarcoMarshDev edited this page Nov 5, 2022 · 3 revisions

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

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

Overview

FILO data structure with maximum assigned size that disallows new entries to be added once the stack has reached it's max size. Unlike ESE_Queue & ESE_DynamicQueue, the raw data from ESE_Stack.Raw is stored in conventional order and can be iterated over like any other array.

Code example of creating a new stack, pushing values up to it's max size, and showing it disallowing new entries to be added until one is popped off the stack:

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

// if the push returns false, print that it failed
if (!m_Stack.Push(5))
    Print("Push failed!"); // >>> "Push failed!"

// stack hasn't changed
m_Stack.PrintRaw(); // >>> {0,1,2,3,4}

// manually pop value
int val = m_Stack.Pop();
m_Stack.PrintRaw(); // >>> {0,1,2,3}
Print(val); // >>> 4

Constructor

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

Methods

Name⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ Return⠀⠀⠀⠀⠀ Description
Push (T value) bool Pushes given value to the stack is space is available and returns true, if stack is full does nothing and returns false.
Pop () T Returns value at the top of the stack and removes it. If stack is empty returns null.
Peek () T Returns value at the top of the stack but does not remove it. If stack is empty returns null.
TryPop (out T output) bool (out T) Attempts to pop value from top of stack, returns true and outputs value to parameter if value is found. If stack is empty returns false and outputs null.
TryPeek (out T output) Same as TryPop() but does not remove entry from stack if found.
Contains (T value) bool Returns true if given value is found in the stack, false otherwise.
Clear () void Clears the contents of the stack.
Count () int Returns number of elements currently in the stack.
GetMaxSize () void Returns current max size of the stack.
Resize (int size) void Changes stack max size, if new size is less than current size, top 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 the stack.
CopyToArray (notnull inout array<T> newArray) void (inout) Copies raw array data of the stack to another array.
CopyFromArray (notnull array<T> inArray) void Copies contents from given array to current stack, and resizes stack to accommodate the new array data.
CopyToDynamicStack (notnull inout ESE_DynamicStack<T> newStack) void (inout) Copies stack contents to dynamic stack and compacts the new queue.
PrintRaw () void Prints raw array data, Print(ESE_Stack.Raw) also works for this use.

Properties

Name⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ Type⠀⠀⠀⠀⠀ Description
Raw ref array<T> Raw data array of the stack.
MaxSize int Maximum size of the stack, any entries pushed beyond this size will not get added.
nullValue T Null value used in returns. This is needed because null is sometimes invalid for certain types.
Clone this wiki locally