Skip to main content

Module priority_queue

Module priority_queue 

Source
Expand description

PagedPriorityQueue: an indexed max-heap with a paged key index.

The greatest priority is returned first. Use core::cmp::Reverse for a min-queue. Equal priorities have unspecified order. Keys and priorities occupy parallel contiguous arrays in heap order; a four-way heap keeps insertion, priority changes and removal to O(log n) comparisons.

Lookups use PagedSparseSet’s paged index. With Flat they are O(1); Radix and Tree add their directory lookup costs to each index access. Only occupied key ranges allocate index pages.

A slot holds one key. push evicts another generation of that key; try_push refuses it. Lookups always check the whole key, including its generation.

§Panic safety and ordering

If a priority comparison or a closure passed to change_priority_with panics, all remaining entries stay findable and paired with their priorities. A mutation already made remains made, but heap order may be broken, so subsequent maximum and sorted-order results are unspecified. Changing a stored priority’s ordering through interior mutability is a logic error with the same consequence. These errors cannot cause undefined behaviour.

use paged_collections::priority_queue::PagedPriorityQueue;

let mut queue: PagedPriorityQueue<u32, i32> = PagedPriorityQueue::new();
assert_eq!(queue.push(7, 10), None);
assert_eq!(queue.push(3_000_000, 20), None);
assert_eq!(queue.change_priority(&7, 30), Some(10));
assert_eq!(queue.pop(), Some((7, 30)));
assert_eq!(queue.pop(), Some((3_000_000, 20)));

With the rayon feature, the shared parallel iterator is indexed and follows heap order.

use paged_collections::priority_queue::PagedPriorityQueue;
use rayon::prelude::*;

let mut queue: PagedPriorityQueue<u32, i32> = PagedPriorityQueue::new();
queue.push(7, 10);
queue.push(2, 20);
assert_eq!(queue.par_iter().map(|(&k, &p)| (k, p)).collect::<Vec<_>>(), [(2, 20), (7, 10)]);

Structs§

Drain
Iterator over eagerly removed entries, from drain. Dropping it drops any remaining entries; the queue is already empty.
IntoSortedIter
Owning iterator over entries in descending priority order, from into_sorted_iter.
PagedPriorityQueue
An indexed max-priority queue with a paged index and dense heap storage.

Type Aliases§

IntoIter
Owning iterator over entries in arbitrary heap order.
Iter
Borrowed iterator over entries in heap order, from iter.