package core:container/priority_queue

⌘K
Ctrl+K
or
/

    Overview

    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)
    }
    

    Index

    Types (1)
    Constants (1)
    Variables (0)

    This section is empty.

    Procedure Groups (0)

    This section is empty.

    Types

    Priority_Queue ¶

    Priority_Queue :: struct($T: typeid) {
    	… // See source for fields
    }
     

    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.

    Related Procedures With Parameters

    Constants

    DEFAULT_CAPACITY ¶

    DEFAULT_CAPACITY: int : 16

    Variables

    This section is empty.

    Procedures

    cap ¶

    cap :: proc(pq: $Q/Priority_Queue($T)) -> int {…}

    clear ¶

    clear :: proc(pq: ^$Q/Priority_Queue($T)) {…}

    default_swap_proc ¶

    default_swap_proc :: proc($T: typeid) -> proc([]typeid, i, int) {…}

    destroy ¶

    destroy :: proc(pq: ^$Q/Priority_Queue($T)) {…}

    fix ¶

    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 ¶

    init :: proc(pq: ^$Q/Priority_Queue($T), less: proc(a, b: $T) -> bool, swap: proc(q: $S/[]$T, i, j: int), capacity: int = DEFAULT_CAPACITY, allocator := context.allocator) -> (err: runtime.Allocator_Error) {…}

    init_from_dynamic_array ¶

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

    len ¶

    len :: proc(pq: $Q/Priority_Queue($T)) -> int {…}

    peek ¶

    peek :: proc(pq: $Q/Priority_Queue($T), loc := #caller_location) -> (res: $T) {…}

    peek_safe ¶

    peek_safe :: proc(pq: $Q/Priority_Queue($T), loc := #caller_location) -> (res: $T, ok: bool) {…}

    pop ¶

    pop :: proc(pq: ^$Q/Priority_Queue($T), loc := #caller_location) -> (value: $T) {…}

    pop_safe ¶

    pop_safe :: proc(pq: ^$Q/Priority_Queue($T), loc := #caller_location) -> (value: $T, ok: bool) {…}

    push ¶

    push :: proc(pq: ^$Q/Priority_Queue($T), value: $T) -> (err: runtime.Allocator_Error) {…}

    remove ¶

    remove :: proc(pq: ^$Q/Priority_Queue($T), i: int) -> (value: $T, ok: bool) {…}

    reserve ¶

    reserve :: proc(pq: ^$Q/Priority_Queue($T), capacity: int) -> (err: runtime.Allocator_Error) {…}

    Procedure Groups

    This section is empty.

    Source Files

    Generation Information

    Generated with odin version dev-2026-02 (vendor "odin") Windows_amd64 @ 2026-02-28 21:12:56.376164400 +0000 UTC