Skip to content

ESE_DynamicStack Class

NarcoMarshDev edited this page Oct 22, 2022 · 3 revisions

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

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

Overview

FILO data structure with no maximum size, pushing new entries simply expands the stack size, and popping reduces it. Unlike ESE_Queue & ESE_DynamicQueue, the raw data from ESE_DynamicStack.Raw is stored in conventional order and can be iterated over like any other array.

Code example of creating a new dynamic stack, pushing values and popping them again:

auto m_Stack = new ESE_Stack<int>(); // no starting size parameter
for (int i = 0; i < 5; i++)
{
    m_Stack.Push(i);
}
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

// push new value
m_Stack.Push(5);
m_Stack.PrintRaw(); // >>> {0,1,2,3,5}

Constructor

Name⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ Description
ESE_Stack<Class T> () Template constructor, no other parameters

Methods

Name⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ Return⠀⠀⠀⠀⠀ Description
Push (T value) void Pushes given value to the top of the stack, increasing the size when needed.
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.
TrimTo (int size) void Trims stack to given size. If new size is smaller than existing 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.
CopyToStaticStack (notnull inout ESE_DynamicStack<T> newStack) void (inout) Copies stack contents to static stack and resizes new stack setting its max size to the count of current dynamic stack.
PrintRaw () void Prints raw array data, Print(ESE_DynamicStack.Raw) also works for this use.

Properties

Name⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ Type⠀⠀⠀⠀⠀ Description
Raw ref array<T> Raw data array of the stack.
nullValue T Null value used in returns. This is needed because null is sometimes invalid for certain types.
Clone this wiki locally