Skip to content

Queue

1 min read

Import: import system.collections;

Queue

FIFO queue backed by a ring buffer (T[] + head/count). Invariant: items.length > 0, 0 <= head < items.length, 0 <= count <= items.length, so any (head + k) % items.length with k >= 0 is a valid slot for Buffer.get_unchecked.

public class Queue<T>: Collection<T>

constructor

Creates an empty queue.

public constructor()

length

Number of elements currently stored.

public get length(): int

capacity

Current element capacity of the ring buffer.

public get capacity(): int

reserve

Reserves room for additional elements while preserving FIFO order and length.

public fun reserve(additional: int): Result<Unit, CapacityError>

enqueue

Appends value at the back of the queue.

public fun enqueue(value: T): void

dequeue

Removes and returns the front element, or None when empty. The vacated slot is zeroed so managed elements release immediately instead of staying retained until overwritten.

public fun dequeue(): Option<T>

peek

Returns the front element without removing it, or None when empty.

public fun peek(): Option<T>

clear

Removes every element. Live ring slots are overwritten with a zero value so managed elements release immediately instead of staying retained until later enqueues overwrite them; the ring is reset to front-aligned empty.

public fun clear(): void

clear

Unmanaged: no RC to release — rewind only.

public fun clear(): void where T: unmanaged

iterator

Iterates elements from front to back.

public fun iterator(): QueueIterator<T>