// 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;
}// كومة ثنائية مرتبة بدالة مقارنة: المهمة الأصغر أولا.
استورد std.io.println;
استورد std.text.number;
استورد std.collections.heap;
عرف حسب_الأولوية(اليسار: &عدد, اليمين: &عدد): عدد {
أرجع *اليمين - *اليسار;
}
عرف الرئيسية(): عدد {
دع المهام: متغير الكومة<عدد> = أنشئ_الكومة(حسب_الأولوية);
أضف_إلى_الكومة(&متغير المهام, 5);
أضف_إلى_الكومة(&متغير المهام, 1);
أضف_إلى_الكومة(&متغير المهام, 9);
أضف_إلى_الكومة(&متغير المهام, 3);
دع مستمر: متغير منطقي = صواب;
طالما (مستمر) {
طابق خذ_قمة_الكومة(&متغير المهام) مع
| اختياري.موجود(المهمة) -> { اطبع_سطر(عدد_إلى_نص(المهمة)); }
| اختياري.لا_شيء -> { مستمر := خطأ; };
}
أرجع 0;
}
Run it
With the interpreter, without compiling:
./build/sultanc run examples/en/collections/priority_queue.snOr compile it to an executable:
./build/sultanc examples/en/collections/priority_queue.sn -o build/example && ./build/example