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 bePartialOrd
,PartialEq
, andEq
.T
must bePartialEq
andEq
.
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>
impl<P: PartialOrd + PartialEq + Eq, T: PartialEq + Eq> RustyPriorityQueue<P, T>
Sourcepub const fn iter(&self) -> RustyPriorityQueueIterator<'_, P, T> ⓘ
pub const fn iter(&self) -> RustyPriorityQueueIterator<'_, P, T> ⓘ
Get an iterator over the priority queue
Sourcepub fn into_iter(self) -> RustyPriorityQueueIntoIterator<P, T> ⓘ
pub fn into_iter(self) -> RustyPriorityQueueIntoIterator<P, T> ⓘ
Convert the priority queue into an iterator
Sourcepub fn update(self, old: P, new: P, data: &T) -> Self
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 priorityP
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())));
Trait Implementations§
Source§impl<P: Debug + PartialOrd + PartialEq + Eq, T: Debug + PartialEq + Eq> Debug for RustyPriorityQueue<P, T>
impl<P: Debug + PartialOrd + PartialEq + Eq, T: Debug + PartialEq + Eq> Debug for RustyPriorityQueue<P, T>
Source§impl<P: PartialOrd + PartialEq + Eq, T: PartialEq + Eq> Default for RustyPriorityQueue<P, T>
impl<P: PartialOrd + PartialEq + Eq, T: PartialEq + Eq> Default for RustyPriorityQueue<P, T>
Source§impl<P: PartialOrd + PartialEq + Eq, T: PartialEq + Eq> FromIterator<(P, T)> for RustyPriorityQueue<P, T>
impl<P: PartialOrd + PartialEq + Eq, T: PartialEq + Eq> FromIterator<(P, T)> for RustyPriorityQueue<P, T>
Source§impl<'b, P: PartialOrd + PartialEq + Eq, T: PartialEq + Eq> IntoIterator for &'b RustyPriorityQueue<P, T>
impl<'b, P: PartialOrd + PartialEq + Eq, T: PartialEq + Eq> IntoIterator for &'b RustyPriorityQueue<P, T>
Source§impl<P: PartialOrd + PartialEq + Eq, T: PartialEq + Eq> IntoIterator for RustyPriorityQueue<P, T>
impl<P: PartialOrd + PartialEq + Eq, T: PartialEq + Eq> IntoIterator for RustyPriorityQueue<P, T>
Source§impl<P: PartialOrd + PartialEq + Eq, T: PartialEq + Eq> PriorityQueue<P, T> for RustyPriorityQueue<P, T>
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)>,
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
fn with_capacity(capacity: usize) -> Self
Create a new priority queue with a given capacity Read more
Source§fn reserve(&mut self, additional: usize)
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 moreSource§fn reserve_exact(&mut self, additional: usize)
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 moreSource§fn shrink_to_fit(&mut self)
fn shrink_to_fit(&mut self)
Discards as much additional capacity as possible.
Source§fn shrink_to(&mut self, capacity: usize)
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 drain(&mut self) -> impl Iterator<Item = (P, T)> + '_
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
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
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
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
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
Auto Trait Implementations§
impl<P, T> Freeze for RustyPriorityQueue<P, T>
impl<P, T> RefUnwindSafe for RustyPriorityQueue<P, T>where
P: RefUnwindSafe,
T: RefUnwindSafe,
impl<P, T> Send for RustyPriorityQueue<P, T>
impl<P, T> Sync for RustyPriorityQueue<P, T>
impl<P, T> Unpin for RustyPriorityQueue<P, T>
impl<P, T> UnwindSafe for RustyPriorityQueue<P, T>where
P: UnwindSafe,
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more