dreamworkAPI
Ctrl K
GitHub
classDocs / Stack

Stack

classsharedmenusource
Object → Stack

A stack is a last-in-first-out (LIFO) data structure object.

Values are added and removed from the same end (the "top") via push and pop, so the most recently pushed value is always the first one popped.

The stack can also be iterated destructively with iterator, which pops values one by one until it is empty.

Constructor

Stack(): Stack

Fields

NameTypeDescription
sizeintegerThe size of the stack. Read-only

Methods

clearEmpties the stack, removing every value and resetting its size to 0.
filterRemoves every item for which predicate returns true, keeping the order of the remaining items.
isEmptyChecks if the stack is empty.
iteratorReturns an iterator for the stack, suitable for use in a for loop (e.g. for value in stack:iterator() do ... end).
peekReturns the value at the top of the stack.
popPops the value from the top of the stack.
pushPushes a value onto the stack.

#Stack:clear

sharedmenusource
function Stack:clear()

Empties the stack, removing every value and resetting its size to 0.

#Stack:filter

sharedmenusource
function Stack:filter(predicate: fun( item: T ): boolean)

Removes every item for which predicate returns true, keeping the order of the remaining items.

Parameters
NameTypeDescription
predicatefun( item: T ): booleanThe function that decides whether a frame is removed.

#Stack:isEmpty

sharedmenunodiscardsource
function Stack:isEmpty(): boolean

Checks if the stack is empty.

Returns
TypeNameDescription
booleanis_emptyReturns true if the stack has no values, otherwise false.

#Stack:iterator

sharedmenunodiscardsource
function Stack:iterator(): (fun( self: Stack<T> ): T | nil), Stack<T>

Returns an iterator for the stack, suitable for use in a for loop (e.g. for value in stack:iterator() do ... end). Iterating pops values off the stack one at a time until it is empty, so this consumes the stack.

Returns
TypeNameDescription
fun( self: Stack<T> ): T | niliteratorThe iterator function; calling it pops and returns the next value, or nil once the stack is empty.
Stack<T>stackThe stack being iterated over, passed as state to the iterator function.

#Stack:peek

sharedmenunodiscardsource
function Stack:peek(): T | nil

Returns the value at the top of the stack.

Returns
TypeNameDescription
T | nilvalueThe value at the top of the stack, or nil if the stack is empty.

#Stack:pop

sharedmenusource
function Stack:pop(): T | nil

Pops the value from the top of the stack.

Returns
TypeNameDescription
T | nilvalueThe value that was removed from the stack, or nil if the stack is empty.

#Stack:push

sharedmenusource
function Stack:push(value: T): integer

Pushes a value onto the stack.

Parameters
NameTypeDescription
valueTThe value to push onto the stack.
Returns
TypeNameDescription
integerpositionThe position of the value in the stack.
Generated from the dreamwork meta branch.