Under heavy development. This site describes the intended 1.0 language. Current features and performance vary. Development status
Sn50SnThe SnAPI programming language
Development · Evolving reference

CreatePriorityQueue

Creates an allocation-free heap with a stored safe function policy.

Sn is under heavy development. This reference includes the intended 1.0 design; features may be incomplete in the current compiler. Read the status →

Module std.collections

import std.collections;

Public declaration contracts and authored examples. A page can document several related symbols. Implementation bodies are excluded.

CreatePriorityQueue

pub fn CreatePriorityQueue<T>(Compare: fn(readonly T&, readonly T&) -> i32)
    -> PriorityQueue<T>
    where T: Movable, MoveAssignable

Creates an allocation-free heap with a stored safe function policy. T: Movable and MoveAssignable item type. Compare: Safe readonly borrowed comparator; positive ranks Left higher. Return: Empty queue owning the policy and canonical allocator. Warning: Comparator must obey strict weak ordering; serialize queue mutation.

CreatePriorityQueue

pub fn CreatePriorityQueue<T, A>(Compare: fn(readonly T&, readonly T&) -> i32,
    mut AllocatorValue: A) -> PriorityQueue<T, PriorityComparison<T>, A>
    where T: Movable, MoveAssignable
    where A: Allocator, Defaultable, Movable

Creates an empty heap with a safe function and explicit allocator. T: Movable and MoveAssignable item type. A: Movable Defaultable structural allocator. Compare: Safe readonly borrowed comparator; positive ranks Left higher. AllocatorValue: Allocator moved into the returned queue without allocation. Return: Empty queue owning both policy and allocator. Warning: Comparator must obey strict weak ordering; serialize queue mutation.

CreatePriorityQueue

pub fn CreatePriorityQueue<T, C>(mut Compare: C) -> PriorityQueue<T, C>
    where T: Movable, MoveAssignable
    where C: BinaryCallable<readonly T&, readonly T&, i32>, Movable

Creates an allocation-free heap with one owned concrete comparator.

Callback storage remains concrete and is never boxed. Borrowed captures retain their original lifetime; owning the policy does not retain captured backing. T: Movable and MoveAssignable item type. C: Movable safe readonly BinaryCallable policy. Compare: Concrete policy moved into the returned queue. Return: Empty queue owning the policy and canonical allocator. Warning: Keep captured backing live and serialize queue mutation.

Example

module example.priority_queue_policy;
from std.collections import CreatePriorityQueue;
from std.memory import DefaultAllocator;
struct AscendingPriority {
    pub readonly fn Invoke(Left: readonly i32&, Right: readonly i32&) -> i32 {
        if Left < Right { return 1; }
        if Left > Right { return -1; }
        return 0;
    }
}
fn Main() -> i32 {
    var DefaultQueue = CreatePriorityQueue<i32>(default(AscendingPriority));
    var Queue = CreatePriorityQueue<i32>(default(AscendingPriority), default(DefaultAllocator));
    if DefaultQueue.TryPush(42) is .Err { Error = _ } { return 1; }
    if Queue.TryPush(20) is .Err { Error = _ } { return 2; }
    if Queue.TryPush(42) is .Err { Error = _ } { return 3; }
    if Queue.Pop().Unwrap() != 20 { return 4; }
    if DefaultQueue.Pop().Unwrap() != Queue.Pop().Unwrap() { return 5; }
    return 0;
}

CreatePriorityQueue

pub fn CreatePriorityQueue<T, C, A>(mut Compare: C, mut AllocatorValue: A)
    -> PriorityQueue<T, C, A>
    where T: Movable, MoveAssignable
    where C: BinaryCallable<readonly T&, readonly T&, i32>, Movable
    where A: Allocator, Defaultable, Movable

Creates an empty heap by transferring concrete policy and allocator owners. T: Movable and MoveAssignable item type. C: Movable safe readonly BinaryCallable policy. A: Movable Defaultable structural allocator. Compare: Concrete policy moved without boxing or allocation. AllocatorValue: Allocator transferred into the returned queue. Return: Empty queue owning both inputs without allocating. Warning: Captured backing must outlive queue uses; external synchronization is required.

Documentation snapshot: October 24, 2026 About versions ↗
Searching Development · language & standard library