Trait PriorityQueue

Source
pub trait PriorityQueue<P: PartialOrd + PartialEq + Eq, T: PartialEq + Eq> {
Show 21 methods // Required methods fn enqueue(&mut self, priority: P, data: T); fn dequeue(&mut self) -> Option<(P, T)>; fn peek(&self) -> Option<(&P, &T)>; fn len(&self) -> usize; fn is_empty(&self) -> bool; fn capacity(&self) -> usize; fn contains(&self, data: &T) -> bool; fn contains_at(&self, priority: &P, data: &T) -> bool; fn remove(&mut self, data: &T) -> bool; fn remove_at(&mut self, priority: &P, data: &T) -> bool; fn with_capacity(capacity: usize) -> Self; fn append(&mut self, other: Self); fn extend<I>(&mut self, iter: I) where I: IntoIterator<Item = (P, T)>; fn reserve(&mut self, additional: usize); fn reserve_exact(&mut self, additional: usize); fn shrink_to_fit(&mut self); fn shrink_to(&mut self, capacity: usize); fn clear(&mut self); fn drain(&mut self) -> impl Iterator<Item = (P, T)> + '_; fn max_node(&self) -> Option<(&P, &T)>; fn min_node(&self) -> Option<(&P, &T)>;
}

Required Methods§

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 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 with_capacity(capacity: usize) -> Self

Create a new priority queue with a given capacity

§Example:

RustyPriorityQueue::<usize, &str>::with_capacity(10)

Source

fn append(&mut self, other: Self)

Append another priority queue into this one

  • Consumes the other priority queue
Source

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

Extend the priority queue with an IntoIterator of (P, T) => (Priority, Data) where P: PartialOrd + PartialEq + Eq, T: PartialEq + Eq

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.

§Panics

Panics if the new capacity overflows usize.

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.

§Panics

Panics if the new capacity overflows usize.

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.

The returned iterator keeps a mutable borrow on the heap to optimize its implementation.

§Examples

Basic usage:


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

assert!(!prio.is_empty());

for x in prio.drain() {
    println!("{x:?}");
}

assert!(prio.is_empty());
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.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§