From 853afea0c7cd5152f22862ed1ad1a2acecff439e Mon Sep 17 00:00:00 2001 From: Jeroen van Rijn Date: Tue, 24 Feb 2026 13:18:56 +0100 Subject: [PATCH] [core:container/priority_queue] Make example visible on package overview --- core/container/priority_queue/doc.odin | 97 ++++++++++--------- .../priority_queue/priority_queue.odin | 3 +- 2 files changed, 52 insertions(+), 48 deletions(-) diff --git a/core/container/priority_queue/doc.odin b/core/container/priority_queue/doc.odin index e499afac9..7779e6e0a 100644 --- a/core/container/priority_queue/doc.odin +++ b/core/container/priority_queue/doc.odin @@ -1,50 +1,53 @@ -// A priority queue data structure. -package container_priority_queue - /* -import "base:runtime" -import pq "core:container/priority_queue" +A priority queue data structure. -main :: proc() { - Printer_Job :: struct { - user_id: u64, - weight: enum u8 {Highest, High, Normal, Low, Idle}, +Important: It needs to be initialized with `less` and `swap` procedures, see `init` and `init_from_dynamic_array`. + +Example: + import "base:runtime" + import pq "core:container/priority_queue" + + main :: proc() { + Printer_Job :: struct { + user_id: u64, + weight: enum u8 {Highest, High, Normal, Low, Idle}, + } + + q: pq.Priority_Queue(Printer_Job) + pq.init( + pq = &q, + less = proc(a, b: Printer_Job) -> bool { + // Jobs will be sorted in order of increasing weight + return a.weight < b.weight + }, + swap = pq.default_swap_proc(Printer_Job), + ) + defer pq.destroy(&q) + + // Add jobs with random weights + for _ in 0..<100 { + job: Printer_Job = --- + assert(runtime.random_generator_read_ptr(context.random_generator, &job, size_of(job))) + pq.push(&q, job) + } + + // Drain jobs in order of importance + last: Printer_Job + for pq.len(q) > 0 { + v := pq.pop(&q) + assert(v.weight >= last.weight) + last = v + } + + // Queue empty? + assert(pq.len(q) == 0) + + // Add one more job + pq.push(&q, Printer_Job{user_id = 42, weight = .Idle}) + + // Cancel all jobs + pq.clear(&q) + assert(pq.len(q) == 0) } - - q: pq.Priority_Queue(Printer_Job) - pq.init( - pq = &q, - less = proc(a, b: Printer_Job) -> bool { - // Jobs will be sorted in order of increasing weight - return a.weight < b.weight - }, - swap = pq.default_swap_proc(Printer_Job), - ) - defer pq.destroy(&q) - - // Add jobs with random weights - for _ in 0..<100 { - job: Printer_Job = --- - assert(runtime.random_generator_read_ptr(context.random_generator, &job, size_of(job))) - pq.push(&q, job) - } - - last: Printer_Job - for pq.len(q) > 0 { - // Drain jobs - v := pq.pop(&q) - assert(v.weight >= last.weight) - last = v - } - - // Queue empty? - assert(pq.len(q) == 0) - - // Add one more job - pq.push(&q, Printer_Job{user_id = 42, weight = .Idle}) - - // Cancel all jobs - pq.clear(&q) - assert(pq.len(q) == 0) -} -*/ \ No newline at end of file +*/ +package container_priority_queue \ No newline at end of file diff --git a/core/container/priority_queue/priority_queue.odin b/core/container/priority_queue/priority_queue.odin index d83750673..c7e9f61dc 100644 --- a/core/container/priority_queue/priority_queue.odin +++ b/core/container/priority_queue/priority_queue.odin @@ -5,7 +5,8 @@ import "base:runtime" // Priority Queue. // -// Important: Must be `init`ialized with `less` and `swap` procedures before use. See `doc.odin` for an example. +// Important: It needs to be initialized with `less` and `swap` procedures, see `init` and `init_from_dynamic_array`. +// See `doc.odin` for an example. Priority_Queue :: struct($T: typeid) { queue: [dynamic]T,