Skip to content

PriorityQueue

1 min read

Import: import system.collections;

Read the usage guide for examples and common tasks. This reference lists the public declarations for this part of the library. See Reading API signatures for parameter and result notation.

PriorityQueue

Four-child min-heap: pop/peek always return the smallest element by the active comparator (natural ascending order via Comparable<T> by default, or a custom fun(T, T): int).

public class PriorityQueue<T>: Collection<T>

constructor

Empty queue ordered by T's natural Comparable order.

public constructor()where T: Comparable<T>

constructor

Empty queue ordered by a custom comparator (same contract as List.sort_by: negative when a should come out first, positive when b should).

public constructor(cmp: fun(T, T): int)

length

Number of elements currently stored.

public get length(): int

is_empty

True when the queue has no elements.

public fun is_empty(): bool

clear

Removes every element. Live slots are overwritten with a zero value so managed elements release immediately instead of staying retained until later pushes overwrite them.

public fun clear(): void

clear

Unmanaged: no RC to release — rewind only.

public fun clear(): void where T: unmanaged

push

Adds a value to the priority queue.

public fun push(value: T): void

pop

Removes the highest-priority value, or returns None when empty.

public fun pop(): Option<T>

peek

Returns the smallest element without removing it, or None when empty. O(1).

public fun peek(): Option<T>

iterator

Iterates elements in ascending order under the active comparator, over a snapshot taken now, so later pushes/pops do not affect the cursor.

public fun iterator(): PriorityQueueIterator<T>