Struct RustyPriorityQueue

Source
pub struct RustyPriorityQueue<P: PartialOrd + PartialEq + Eq, T: PartialEq + Eq> { /* private fields */ }
Expand description

A priority queue implementation based on Rust’s BinaryHeap. Templated with P for priority and T for data.

  • P must be PartialOrd, PartialEq, and Eq.
  • T must be PartialEq and Eq.

If Node A and Node B have the same priority, but Node A was added before Node B, then Node A will be prioritized over Node B. See PriorityQueue for more information.

§Examples


let mut prio = RustyPriorityQueue::<usize, String>::default();
prio.enqueue(2, "hello".to_string());
prio.enqueue(3, "julia".to_string());
prio.enqueue(1, "world".to_string());
prio.enqueue(3, "naomi".to_string());

let mut new_prio: RustyPriorityQueue<usize, String> = prio
    .into_iter()
    .map(|(priority, data)| (priority, data.to_owned() + " wow"))
    .collect();

assert_eq!(new_prio.dequeue(), Some((3, "julia wow".to_string())));
assert_eq!(new_prio.dequeue(), Some((3, "naomi wow".to_string())));
assert_eq!(new_prio.dequeue(), Some((2, "hello wow".to_string())));
assert_eq!(new_prio.dequeue(), Some((1, "world wow".to_string())));

Implementations§

Source§

impl<P: PartialOrd + PartialEq + Eq, T: PartialEq + Eq> RustyPriorityQueue<P, T>

Source

pub const fn iter(&self) -> RustyPriorityQueueIterator<'_, P, T>

Get an iterator over the priority queue

Source

pub fn into_iter(self) -> RustyPriorityQueueIntoIterator<P, T>

Convert the priority queue into an iterator

Source

pub fn update(self, old: P, new: P, data: &T) -> Self

Update the priority of an item in the queue.

This is a very slow operation!

  • it uses unsafe std::mem::transmute_copy under the hhod to guarantee allocation of priority P on every possible scenario.
§Examples

let mut prio = RustyPriorityQueue::<usize, String>::default();
prio.enqueue(5, "julia".to_string());
prio.enqueue(2, "hello".to_string());
prio.enqueue(3, "julia".to_string());
prio.enqueue(1, "world".to_string());
prio.enqueue(3, "naomi".to_string());
let ref_str = "julia".to_string();
let mut new = prio.update(3, 7,&ref_str);
assert_eq!(new.dequeue(), Some((7, "julia".to_string())));
assert_eq!(new.dequeue(), Some((5, "julia".to_string())));
assert_eq!(new.dequeue(), Some((3, "naomi".to_string())));
assert_eq!(new.dequeue(), Some((2, "hello".to_string())));
assert_eq!(new.dequeue(), Some((1, "world".to_string())));
Source

pub fn update_as(&mut self, old: P, new: P, data: T)

Update the priority of an item in the queue with a reference to the data.

Trait Implementations§

Source§

impl<P: Debug + PartialOrd + PartialEq + Eq, T: Debug + PartialEq + Eq> Debug for RustyPriorityQueue<P, T>

Source§

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

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

impl<P: PartialOrd + PartialEq + Eq, T: PartialEq + Eq> Default for RustyPriorityQueue<P, T>

Source§

fn default() -> Self

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

impl<P: PartialOrd + PartialEq + Eq, T: PartialEq + Eq> FromIterator<(P, T)> for RustyPriorityQueue<P, T>

Source§

fn from_iter<I: IntoIterator<Item = (P, T)>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<'b, P: PartialOrd + PartialEq + Eq, T: PartialEq + Eq> IntoIterator for &'b RustyPriorityQueue<P, T>

Source§

type IntoIter = RustyPriorityQueueIterator<'b, P, T>

Which kind of iterator are we turning this into?
Source§

type Item = (&'b P, &'b T)

The type of the elements being iterated over.
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<P: PartialOrd + PartialEq + Eq, T: PartialEq + Eq> IntoIterator for RustyPriorityQueue<P, T>

Source§

type Item = (P, T)

The type of the elements being iterated over.
Source§

type IntoIter = RustyPriorityQueueIntoIterator<P, T>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<P: PartialOrd + PartialEq + Eq, T: PartialEq + Eq> PriorityQueue<P, T> for RustyPriorityQueue<P, T>

Source§

fn extend<I>(&mut self, iter: I)
where I: IntoIterator<Item = (P, T)>,

Extend the priority queue with an iterator

§Examples

let mut prio = RustyPriorityQueue::<usize, String>::default();
prio.extend(vec![(2, "world".to_string()), (3, "hello".to_string())]);
assert_eq!(prio.dequeue(), Some((3, "hello".to_string())));
assert_eq!(prio.dequeue(), Some((2, "world".to_string())));
assert_eq!(prio.dequeue(), None);
Source§

fn with_capacity(capacity: usize) -> Self

Create a new priority queue with a given capacity Read more
Source§

fn enqueue(&mut self, priority: P, data: T)

Add an item to the queue
Source§

fn dequeue(&mut self) -> Option<(P, T)>

Remove an item from the queue
Source§

fn peek(&self) -> Option<(&P, &T)>

Peeks the next item in the queue
Source§

fn len(&self) -> usize

Queue length
Source§

fn is_empty(&self) -> bool

Boolean if the queue is empty
Source§

fn capacity(&self) -> usize

Queue capacity
Source§

fn append(&mut self, other: Self)

Append another priority queue into this one Read more
Source§

fn reserve(&mut self, additional: usize)

Reserves capacity for at least additional elements more than the current length. The allocator may reserve more space to speculatively avoid frequent allocations. After calling reserve, capacity will be greater than or equal to self.len() + additional. Does nothing if capacity is already sufficient. Read more
Source§

fn reserve_exact(&mut self, additional: usize)

Reserves the minimum capacity for at least additional elements more than the current length. Unlike reserve, this will not deliberately over-allocate to speculatively avoid frequent allocations. After calling reserve_exact, capacity will be greater than or equal to self.len() + additional. Does nothing if the capacity is already sufficient. Read more
Source§

fn shrink_to_fit(&mut self)

Discards as much additional capacity as possible.
Source§

fn shrink_to(&mut self, capacity: usize)

Discards capacity with a lower bound. The capacity will remain at least as large as both the length and the supplied value. If the current capacity is less than the lower limit, this is a no-op.
Source§

fn clear(&mut self)

Removes all items from the queue
Source§

fn drain(&mut self) -> impl Iterator<Item = (P, T)> + '_

Clears the binary heap, returning an iterator over the removed elements in arbitrary order. If the iterator is dropped before being fully consumed, it drops the remaining elements in arbitrary order. Read more
Source§

fn contains(&self, data: &T) -> bool

Check if an item exists in the queue true if it exists, false if it does not
Source§

fn contains_at(&self, priority: &P, data: &T) -> bool

Check if an item exists in the queue at a certain priority true if it exists, false if it does not
Source§

fn remove(&mut self, data: &T) -> bool

Remove all existing items from the queue true if it was removed, false if it was not
Source§

fn remove_at(&mut self, priority: &P, data: &T) -> bool

Remove an item from the queue if exists at a certain priority true if it was removed, false if it was not
Source§

fn max_node(&self) -> Option<(&P, &T)>

Returns the node the the highest priority in the queue. If no nodes exist, None is returned.
Source§

fn min_node(&self) -> Option<(&P, &T)>

Returns the node the the lowest priority in the queue. If no nodes exist, None is returned.

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<P, T> UnwindSafe for RustyPriorityQueue<P, T>
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> 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, 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.