nimphea/nimphea_fifo

Source   Edit  

FIFO Queue (First-In-First-Out)

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

Features:

  • Fixed capacity at compile time (no dynamic allocation)
  • Thread-safe for single producer/single consumer
  • Audio-rate safe (no locks, predictable performance)
  • Generic type support via templates

Use Cases:

  • Event queues
  • Message passing between threads
  • Audio sample buffering
  • Command queues

Memory:

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

Usage:

import nimphea_fifo

# Create FIFO for 16 integers
var queue: Fifo[16, int]
queue.init()

# Push values
discard queue.push(42)
discard queue.push(100)

# Pop values (FIFO order)
var value: int
if queue.pop(value):
  echo value  # 42 (first in, first out)

# Check state
echo queue.len()      # Current count
echo queue.isEmpty()  # false
echo queue.isFull()   # false

Types

Fifo[N; T] = object

Fixed-capacity FIFO queue

Generic Parameters:

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

Fields:

  • data - Fixed array storage
  • head - Read position
  • tail - Write position
  • count - Current number of elements
Source   Edit  

Procs

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

Convert FIFO to string representation (for debugging)

Example:

echo queue  # "Fifo[16, int](count=5/16)"

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

Get maximum capacity

Returns: Maximum capacity (compile-time constant N)

Example:

echo queue.capacity()  # 32

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

Clear all elements from the queue

Example:

queue.clear()

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

Initialize the FIFO queue

Example:

var queue: Fifo[32, float32]
queue.init()

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

Check if queue is empty

Returns: true if empty, false otherwise

Example:

if queue.isEmpty():
  echo "Queue is empty"

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

Check if queue is full

Returns: true if full, false otherwise

Example:

if queue.isFull():
  echo "Queue is full, cannot push"

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

Get current number of elements

Returns: Number of elements (0 to N)

Example:

echo queue.len()  # e.g., 5

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

Peek at the next value without removing it

Parameters:

  • value - Output parameter to receive the peeked value

Returns: true if successful, false if queue was empty

Example:

var val: int
if queue.peek(val):
  echo "Next value: ", val  # Queue unchanged

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

Pop a value from the queue (from head)

Parameters:

  • value - Output parameter to receive the popped value

Returns: true if successful, false if queue was empty

Example:

var val: int
if queue.pop(val):
  echo "Popped: ", val
else:
  echo "Queue empty"

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

Push a value onto the queue (at tail)

Parameters:

  • value - Element to push

Returns: true if successful, false if queue was full

Example:

if not queue.push(42):
  echo "Queue full, push failed"

Source   Edit