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)
Procs
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