Stack

class guppylang.std.collections.Stack[source]

A last-in-first-out (LIFO) growable collection of values.

To ensure static allocation, the maximum stack size must be specified in advance and is tracked in the type. For example, the Stack[int, 10] is a stack that can hold at most 10 integers.

Use empty_stack to construct a new stack.

__iter__() Stack[T, MAX_SIZE][source]

Returns an iterator over the elements in the stack from top to bottom.

__len__() int[source]

Returns the number of elements currently stored in the stack.

__next__() Option[tuple[T, Stack[T, MAX_SIZE]]][source]
buf: array[Option[T], MAX_SIZE]

Underlying buffer holding the stack elements.

INVARIANT: All array elements up to and including index self.end - 1 are option.some variants and all further ones are option.nothing.

discard_empty() None[source]

Discards a stack of potentially non-droppable elements assuming that the stack is empty.

Panics if the stack is not empty.

end: int

Index of the next free index in self.buf.

peek() tuple[TCopyable, Stack[TCopyable, MAX_SIZE]][source]

Returns a copy of the top element of the stack without removing it.

Panics if the stack is empty.

Note that this operation is only allowed if the stack elements are copyable.

pop() tuple[T, Stack[T, MAX_SIZE]][source]

Removes the top element from the stack and returns it.

Panics if the stack is empty.

push(elem: T @ owned) Stack[T, MAX_SIZE][source]

Adds an element to the top of the stack.

Panics if the stack has already reached its maximum size.