pub struct PriorityQueue<T, P>{ /* private fields */ }Expand description
A keyed priority queue that pops by lowest priority first.
Backed by MinHeap with (P, T) pairs. Because Rust’s tuple ordering
is lexicographic, storing (priority, value) gives us priority ordering
for free.
§Use case
This is the queue Dijkstra’s algorithm uses:
push a (node, cost) pair and always pop the cheapest node next.
§Examples
use graph_collections::PriorityQueue;
let mut pq: PriorityQueue<&str, u32> = PriorityQueue::new();
pq.push("low", 10);
pq.push("high", 1);
pq.push("medium", 5);
assert_eq!(pq.peek_priority(), Some(&1));
assert_eq!(pq.pop(), Some(("high", 1)));
assert_eq!(pq.pop(), Some(("medium", 5)));
assert_eq!(pq.pop(), Some(("low", 10)));
assert!(pq.is_empty());Implementations§
Source§impl<T, P> PriorityQueue<T, P>
impl<T, P> PriorityQueue<T, P>
Sourcepub fn new() -> PriorityQueue<T, P>
pub fn new() -> PriorityQueue<T, P>
Creates a new, empty priority queue.
§Examples
use graph_collections::PriorityQueue;
let pq: PriorityQueue<&str, u32> = PriorityQueue::new();
assert!(pq.is_empty());Sourcepub fn with_capacity(capacity: usize) -> PriorityQueue<T, P>
pub fn with_capacity(capacity: usize) -> PriorityQueue<T, P>
Creates a new, empty priority queue with at least the given capacity pre-allocated.
§Examples
use graph_collections::PriorityQueue;
let pq: PriorityQueue<u32, u32> = PriorityQueue::with_capacity(128);
assert!(pq.is_empty());Source§impl<T, P> PriorityQueue<T, P>
impl<T, P> PriorityQueue<T, P>
Source§impl<T, P> PriorityQueue<T, P>
impl<T, P> PriorityQueue<T, P>
Sourcepub fn peek_priority(&self) -> Option<&P>
pub fn peek_priority(&self) -> Option<&P>
Returns a reference to the lowest priority value without removing it,
or None if empty.
§Examples
use graph_collections::PriorityQueue;
let mut pq: PriorityQueue<&str, u32> = PriorityQueue::new();
assert_eq!(pq.peek_priority(), None);
pq.push("a", 3);
pq.push("b", 1);
assert_eq!(pq.peek_priority(), Some(&1));
assert_eq!(pq.len(), 2); // unchangedSourcepub fn peek_value(&self) -> Option<&T>
pub fn peek_value(&self) -> Option<&T>
Returns a reference to the value with the lowest priority without
removing it, or None if empty.
§Examples
use graph_collections::PriorityQueue;
let mut pq: PriorityQueue<&str, u32> = PriorityQueue::new();
pq.push("high", 1);
pq.push("low", 9);
assert_eq!(pq.peek_value(), Some(&"high"));Source§impl<T, P> PriorityQueue<T, P>
impl<T, P> PriorityQueue<T, P>
Sourcepub fn push(&mut self, value: T, priority: P)
pub fn push(&mut self, value: T, priority: P)
Inserts value with the given priority in O(log n).
Duplicate priorities are allowed; ties are broken by the natural order
of T (because tuples are compared lexicographically).
§Examples
use graph_collections::PriorityQueue;
let mut pq: PriorityQueue<&str, u32> = PriorityQueue::new();
pq.push("task-a", 2);
pq.push("task-b", 1);
assert_eq!(pq.peek_priority(), Some(&1));Sourcepub fn pop(&mut self) -> Option<(T, P)>
pub fn pop(&mut self) -> Option<(T, P)>
Removes and returns (value, priority) for the element with the
lowest priority, or None if empty.
§Examples
use graph_collections::PriorityQueue;
let mut pq: PriorityQueue<&str, u32> = PriorityQueue::new();
assert_eq!(pq.pop(), None);
pq.push("a", 3);
pq.push("b", 1);
pq.push("c", 2);
assert_eq!(pq.pop(), Some(("b", 1)));
assert_eq!(pq.pop(), Some(("c", 2)));
assert_eq!(pq.pop(), Some(("a", 3)));Trait Implementations§
Source§impl<T, P> Clone for PriorityQueue<T, P>
impl<T, P> Clone for PriorityQueue<T, P>
Source§fn clone(&self) -> PriorityQueue<T, P>
fn clone(&self) -> PriorityQueue<T, P>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<T, P> Debug for PriorityQueue<T, P>
impl<T, P> Debug for PriorityQueue<T, P>
Source§impl<T, P> Default for PriorityQueue<T, P>
impl<T, P> Default for PriorityQueue<T, P>
Source§fn default() -> PriorityQueue<T, P>
fn default() -> PriorityQueue<T, P>
Source§impl<T, P> Extend<(T, P)> for PriorityQueue<T, P>
impl<T, P> Extend<(T, P)> for PriorityQueue<T, P>
Source§fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = (T, P)>,
fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = (T, P)>,
Pushes all (value, priority) pairs from the iterator into the queue.
§Examples
use graph_collections::PriorityQueue;
let mut pq: PriorityQueue<&str, u32> = PriorityQueue::new();
pq.push("seed", 5);
pq.extend([("a", 2u32), ("b", 8), ("c", 1)]);
assert_eq!(pq.peek_priority(), Some(&1));
assert_eq!(pq.len(), 4);Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)Source§impl<T, P> FromIterator<(T, P)> for PriorityQueue<T, P>
impl<T, P> FromIterator<(T, P)> for PriorityQueue<T, P>
Source§fn from_iter<I>(iter: I) -> PriorityQueue<T, P>where
I: IntoIterator<Item = (T, P)>,
fn from_iter<I>(iter: I) -> PriorityQueue<T, P>where
I: IntoIterator<Item = (T, P)>,
Builds a PriorityQueue from an iterator of (value, priority) pairs.
§Examples
use graph_collections::PriorityQueue;
let pq: PriorityQueue<&str, u32> = vec![
("slow", 10u32),
("fast", 1),
("medium", 5),
]
.into_iter()
.collect();
assert_eq!(pq.peek_priority(), Some(&1));
assert_eq!(pq.len(), 3);