Module: collections.stack

Source: ./collections/stack.reef


Overview

collections/stack - Generic Stack[T] collection (LIFO)

Provides a last-in, first-out stack with generic type support. Capacity grows automatically on push when full (double capacity, min 1). push returns true on success; false only on hard failure — not "full". stack_is_full still reports whether the current backing array is full (before the next auto-grow).

Usage: let s: Stack[int] = create_int_stack() push[:int](s, 42) let value = pop:int // Some(42) -- no annotation needed, infers Option[int]


Types

Stack

Stack data structure - generic struct with typed array

Fields:

Name Type
items [T]
top int
cap int

Functions

fn push(s: Stack[T], item: T): bool

Pushes an item onto the stack. Auto-grows when full. Returns true on success (false only on hard failure).

fn pop(s: Stack[T]): option.Option[T]

Pops and returns the top item from the stack Returns Some(elem) if non-empty, None if empty.

fn peek(s: Stack[T]): option.Option[T]

Returns the top item without removing it Returns Some(elem) if non-empty, None if empty.

fn stack_size(s: Stack[T]): int

Returns the number of items in the stack

fn stack_capacity(s: Stack[T]): int

Returns the capacity of the stack

fn stack_is_empty(s: Stack[T]): bool

Returns true if the stack is empty

fn stack_is_full(s: Stack[T]): bool

Returns true if the stack is full

fn stack_to_array(s: Stack[T], arr: [T], max: int): int

Copies stack elements to an array (bottom to top order) Returns the number of elements copied

fn create_int_stack(): Stack[int]

Creates an empty int stack with capacity 16

fn create_string_stack(): Stack[string]

Creates an empty string stack with capacity 16

fn create_float_stack(): Stack[float]

Creates an empty float stack with capacity 16

fn create_bool_stack(): Stack[bool]

Creates an empty bool stack with capacity 16

fn create_int_stack_cap(cap: int): Stack[int]

Creates an int stack with specified capacity (8, 16, 32, or 64)


Procedures

proc grow_stack(s: Stack[T])

Grow backing storage to at least 2x current capacity (min 1).

proc stack_clear(s: Stack[T])

Clears all items from the stack


Generated by reefc doc