nimphea/nimphea_stack

Source   Edit  

Stack (Last-In-First-Out)

Pure Nim implementation of a fixed-capacity stack with zero heap allocation.

Features:

  • Fixed capacity at compile time (no dynamic allocation)
  • Standard LIFO operations (push/pop/peek)
  • Audio-rate safe (predictable performance)
  • Generic type support via templates

Use Cases:

  • Undo/redo systems
  • Expression evaluation
  • Function call tracking
  • Temporary value storage

Memory:

  • Stack allocated only
  • Size known at compile time
  • Zero runtime overhead

Usage:

import nimphea_stack

# Create stack for 32 floats
var stack: Stack[32, float32]
stack.init()

# Push values
discard stack.push(1.5)
discard stack.push(2.7)

# Pop values (LIFO order)
var value: float32
if stack.pop(value):
  echo value  # 2.7 (last in, first out)

# Peek without removing
if stack.peek(value):
  echo value  # 1.5 (next value, still on stack)

Types

Stack[N; T] = object

Fixed-capacity stack

Generic Parameters:

  • N - Capacity (must be known at compile time)
  • T - Element type

Fields:

  • data - Fixed array storage
  • top - Current stack top index (-1 when empty)
Source   Edit  

Procs

proc `$`[N: static int; T](this: Stack[N, T]): string

Convert stack to string representation (for debugging)

Example:

echo stack  # "Stack[32, float32](count=10/32)"

Source   Edit  
proc capacity[N: static int; T](this: Stack[N, T]): int {.inline.}

Get maximum capacity

Returns: Maximum capacity (compile-time constant N)

Example:

echo stack.capacity()  # 32

Source   Edit  
proc clear[N: static int; T](this: var Stack[N, T])

Clear all elements from the stack

Example:

stack.clear()

Source   Edit  
proc init[N: static int; T](this: var Stack[N, T])

Initialize the stack

Example:

var stack: Stack[16, int]
stack.init()

Source   Edit  
proc isEmpty[N: static int; T](this: Stack[N, T]): bool {.inline.}

Check if stack is empty

Returns: true if empty, false otherwise

Example:

if stack.isEmpty():
  echo "Stack is empty"

Source   Edit  
proc isFull[N: static int; T](this: Stack[N, T]): bool {.inline.}

Check if stack is full

Returns: true if full, false otherwise

Example:

if stack.isFull():
  echo "Stack is full, cannot push"

Source   Edit  
proc len[N: static int; T](this: Stack[N, T]): int {.inline.}

Get current number of elements

Returns: Number of elements (0 to N)

Example:

echo stack.len()  # e.g., 10

Source   Edit  
proc peek[N: static int; T](this: Stack[N, T]; value: var T): bool {.inline.}

Peek at the top value without removing it

Parameters:

  • value - Output parameter to receive the peeked value

Returns: true if successful, false if stack was empty

Example:

var val: int
if stack.peek(val):
  echo "Top value: ", val  # Stack unchanged

Source   Edit  
proc pop[N: static int; T](this: var Stack[N, T]; value: var T): bool {.inline.}

Pop a value from the stack

Parameters:

  • value - Output parameter to receive the popped value

Returns: true if successful, false if stack was empty

Example:

var val: int
if stack.pop(val):
  echo "Popped: ", val
else:
  echo "Stack empty"

Source   Edit  
proc push[N: static int; T](this: var Stack[N, T]; value: T): bool {.inline.}

Push a value onto the stack

Parameters:

  • value - Element to push

Returns: true if successful, false if stack was full

Example:

if not stack.push(42):
  echo "Stack full, push failed"

Source   Edit