std/queue

Queue<T> is a generic First-In-First-Out (FIFO) queue implemented as a Ring Buffer (Circular Buffer).

Usage

import "std/queue.zc"
import "std/io.zc"

fn main() {
    let q = Queue<int>::new();
    
    q.push(1);
    q.push(2);
    q.push(3);
    
    // Pop returns an Option<T>
    if (q.pop().is_some()) {
        println(f"Popped: {q.pop().unwrap()}"); // 1
    }
}

Implementation Details

Struct Definition

struct Queue<T> {
    data: T*;
    cap: usize;
    head: usize;
    tail: usize;
    count: usize;
}

Methods

Construction

MethodSignatureDescription
newQueue<T>::new() -> Queue<T>Creates a new, empty queue.
cloneclone(self) -> Queue<T>Creates a deep copy of the queue.

Modification

MethodSignatureDescription
pushpush(self, value: T)Adds an element to the back of the queue.
poppop(self) -> Option<T>Removes and returns the element at the front. Returns None if empty.
clearclear(self)Removes all items from the queue.

Access & Query

MethodSignatureDescription
lengthlength(self) -> usizeReturns the number of items.
is_emptyis_empty(self) -> boolReturns true if the queue is empty.

Memory Management

MethodSignatureDescription
freefree(self)Frees the internal buffer.
Traitimpl Drop for QueueAutomatically calls free() when out of scope.