PriorityQueue

Struct PriorityQueue 

Source
pub struct PriorityQueue<T, P>
where T: Ord, P: Ord,
{ /* 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>
where T: Ord, P: Ord,

Source

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());
Source

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>
where T: Ord, P: Ord,

Source

pub fn is_empty(&self) -> bool

Returns true if the queue contains no elements.

§Examples
use graph_collections::PriorityQueue;

let mut pq: PriorityQueue<u32, u32> = PriorityQueue::new();
assert!(pq.is_empty());
pq.push(1, 10);
assert!(!pq.is_empty());
Source

pub fn len(&self) -> usize

Returns the number of elements in the queue.

§Examples
use graph_collections::PriorityQueue;

let mut pq: PriorityQueue<u32, u32> = PriorityQueue::new();
pq.push(1, 5);
pq.push(2, 3);
assert_eq!(pq.len(), 2);
Source§

impl<T, P> PriorityQueue<T, P>
where T: Ord, P: Ord,

Source

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); // unchanged
Source

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>
where T: Ord, P: Ord,

Source

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));
Source

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)));
Source

pub fn clear(&mut self)

Removes all elements from the queue.

§Examples
use graph_collections::PriorityQueue;

let mut pq: PriorityQueue<u32, u32> = PriorityQueue::new();
pq.push(1, 10);
pq.push(2, 5);
pq.clear();
assert!(pq.is_empty());

Trait Implementations§

Source§

impl<T, P> Clone for PriorityQueue<T, P>
where T: Clone + Ord, P: Clone + Ord,

Source§

fn clone(&self) -> PriorityQueue<T, P>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T, P> Debug for PriorityQueue<T, P>
where T: Debug + Ord, P: Debug + Ord,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<T, P> Default for PriorityQueue<T, P>
where T: Ord, P: Ord,

Source§

fn default() -> PriorityQueue<T, P>

Returns the “default value” for a type. Read more
Source§

impl<T, P> Extend<(T, P)> for PriorityQueue<T, P>
where T: Ord, P: Ord,

Source§

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)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<T, P> FromIterator<(T, P)> for PriorityQueue<T, P>
where T: Ord, P: Ord,

Source§

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

Auto Trait Implementations§

§

impl<T, P> Freeze for PriorityQueue<T, P>

§

impl<T, P> RefUnwindSafe for PriorityQueue<T, P>

§

impl<T, P> Send for PriorityQueue<T, P>
where P: Send, T: Send,

§

impl<T, P> Sync for PriorityQueue<T, P>
where P: Sync, T: Sync,

§

impl<T, P> Unpin for PriorityQueue<T, P>
where P: Unpin, T: Unpin,

§

impl<T, P> UnwindSafe for PriorityQueue<T, P>
where P: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.