A priority queue data structure. 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) }

Collection Info

View Source
Collection
core
Path
container/priority_queue
Entries
17

Source Files

Constants

1

Types

1

Priority_Queue #

Source
Priority_Queue :: Priority_Queue

Priority Queue. Important: It needs to be initialized with `less` and `swap` procedures, see `init` and `init_from_dynamic_array`. See `doc.odin` for an example.

Procedures

15

fix #

Source
fix :: proc(pq: ^$Q/Priority_Queue($T), i: int) {…}

NOTE(bill): When an element at index 'i' has changed its value, this will fix the the heap ordering. This is using a basic "heapsort" with shift up and a shift down parts.

init_from_dynamic_array #

Source
init_from_dynamic_array :: proc(pq: ^$Q/Priority_Queue($T), queue: [dynamic]$T, less: proc(a, b: $T) -> bool, swap: proc(q: []$T, i, j: int)) {…}