Skip to content

Stack

Spice
import "std/data/stack";

Stack<T> struct

A stack in Spice is a commonly used data structure, which uses the FiLo (first in, last out) principle.

Time complexity:
Insert: O(1)
Delete: O(1)
Search: O(n)

Stacks pre-allocate space using an initial size and a resize factor to not have to re-allocate with every item pushed.

Constructors

ctor

Spice
public p Stack.ctor(unsigned long initAllocItems, const T &defaultValue)

Construct a stack pre-filled with a number of copies of a default value

Parameters

Name Type Description
initAllocItems unsigned long Number of items to insert
defaultValue const T & Value to fill the stack with

ctor

Spice
public p Stack.ctor(unsigned int initAllocItems)

Construct a stack, pre-allocating space for the given number of items

Parameters

Name Type Description
initAllocItems unsigned int Number of items to pre-allocate space for

ctor

Spice
public p Stack.ctor(unsigned long initAllocItems = INITIAL_CAPACITY)

Construct a stack, pre-allocating space for the given number of items

Parameters

Name Type Description
initAllocItems unsigned long Number of items to pre-allocate space for (default: INITIAL_CAPACITY)

ctor

Spice
public p Stack.ctor(const Stack<T>& original)

Construct a stack as a deep copy of another stack

Parameters

Name Type Description
original const Stack<T>& Stack to copy

Methods

push

Spice
public p Stack.push(const T& item)

Add an item to the stack

Parameters

Name Type Description
item const T&

pop

Spice
public f<T&> Stack.pop()

Retrieve item and remove it from the stack

Returns: T&

top

Spice
public f<T&> Stack.top()

Retrieve topmost without removing it from the stack

Returns: T&

getSize

Spice
public f<long> Stack.getSize()

Retrieve the current size of the stack

Returns: long — Current size of the stack

getCapacity

Spice
public f<long> Stack.getCapacity()

Retrieve the current capacity of the stack

Returns: long — Current capacity of the stack

isEmpty

Spice
public f<bool> Stack.isEmpty()

Checks if the queue contains any items at the moment

Returns: bool — Empty or not empty

isFull

Spice
public f<bool> Stack.isFull()

Checks if the queue exhausts its capacity and needs to resize at the next call of push

Returns: bool — Full or not full

pack

Spice
public p Stack.pack()

Frees allocated memory that is not used by the queue

Operators

operator=

Spice
public p operator=<T>(Stack<T>& this, const Stack<T>& newValue)

Copy-assign the contents of another stack into this one

Parameters

Name Type Description
newValue const Stack<T>& Stack to copy from

operator==

Spice
public f<bool> operator==<T>(const Stack<T>& lhs, const Stack<T>& rhs)

Check if two stacks are equal, i.e. they have the same size and equal contents

Parameters

Name Type Description
lhs const Stack<T>&
rhs const Stack<T>&

Returns: bool — Equal or not equal

operator!=

Spice
public f<bool> operator!=<T>(const Stack<T>& lhs, const Stack<T>& rhs)

Check if two stacks are not equal

Parameters

Name Type Description
lhs const Stack<T>&
rhs const Stack<T>&

Returns: bool — Not equal or equal