SultanC
  1. Home
  2. Examples
  3. Collections

A binary heap ordered by a comparison function

smallest task first.

collections/priority_queue.sn
// A binary heap ordered by a comparison function: smallest task first.
import std.io.println;
import std.text.number;
import std.collections.heap;

def by_priority(left: &int, right: &int): int {
    return *right - *left;
}

def main(): int {
    let tasks: mutable heap<int> = new_heap(by_priority);
    heap_push(&mutable tasks, 5);
    heap_push(&mutable tasks, 1);
    heap_push(&mutable tasks, 9);
    heap_push(&mutable tasks, 3);
    let running: mutable bool = true;
    while (running) {
        match heap_pop(&mutable tasks) with
        | option.some(task) -> { println(int_to_text(task)); }
        | option.none -> { running := false; };
    }
    return 0;
}

Run it

With the interpreter, without compiling:

./build/sultanc run examples/en/collections/priority_queue.sn

Or compile it to an executable:

./build/sultanc examples/en/collections/priority_queue.sn -o build/example && ./build/example

Source on GitHub   All examples