[−][src]Struct priority_queue::PriorityQueue
A priority queue with efficient change function to change the priority of an element.
The priority is of type P, that must implement std::cmp::Ord
.
The item is of type I, that must implement Hash
and Eq
.
Implemented as a heap of indexes, stores the items inside an IndexMap
to be able to retrieve them quickly.
Implementations
impl<I, P> PriorityQueue<I, P> where
P: Ord,
I: Hash + Eq,
[src]
P: Ord,
I: Hash + Eq,
pub fn new() -> Self
[src]
Creates an empty PriorityQueue
pub fn with_capacity(capacity: usize) -> Self
[src]
Creates an empty PriorityQueue
with the specified capacity.
impl<I, P, H> PriorityQueue<I, P, H> where
P: Ord,
I: Hash + Eq,
H: BuildHasher + Default,
[src]
P: Ord,
I: Hash + Eq,
H: BuildHasher + Default,
pub fn with_default_hasher() -> Self
[src]
Creates an empty PriorityQueue
with the default hasher
pub fn with_capacity_and_default_hasher(capacity: usize) -> Self
[src]
Creates an empty PriorityQueue
with the specified capacity and default hasher
impl<I, P, H> PriorityQueue<I, P, H> where
P: Ord,
I: Hash + Eq,
H: BuildHasher,
[src]
P: Ord,
I: Hash + Eq,
H: BuildHasher,
pub fn with_hasher(hash_builder: H) -> Self
[src]
Creates an empty PriorityQueue
with the specified hasher
pub fn with_capacity_and_hasher(capacity: usize, hash_builder: H) -> Self
[src]
Creates an empty PriorityQueue
with the specified capacity and hasher
The internal collections will be able to hold at least capacity
elements without reallocating.
If capacity
is 0, there will be no allocation.
pub fn iter(&self) -> Iter<'_, I, P>
[src]
Returns an iterator in arbitrary order over the (item, priority) elements in the queue
impl<I, P, H> PriorityQueue<I, P, H> where
P: Ord,
I: Hash + Eq,
[src]
P: Ord,
I: Hash + Eq,
pub fn iter_mut(&mut self) -> IterMut<'_, I, P, H>
[src]
Return an iterator in arbitrary order over the (item, priority) elements in the queue.
The item and the priority are mutable references, but it's a logic error
to modify the item in a way that change the result of Hash
or Eq
.
It's not an error, instead, to modify the priorities, because the heap
will be rebuilt once the IterMut
goes out of scope. It would be
rebuilt even if no priority value would have been modified, but the
procedure will not move anything, but just compare the priorities.
pub fn peek(&self) -> Option<(&I, &P)>
[src]
Returns the couple (item, priority) with the greatest priority in the queue, or None if it is empty.
Computes in O(1) time
pub fn peek_mut(&mut self) -> Option<(&mut I, &P)>
[src]
Returns the couple (item, priority) with the greatest priority in the queue, or None if it is empty.
The item is a mutable reference, but it's a logic error to modify it
in a way that change the result of Hash
or Eq
.
The priority cannot be modified with a call to this function.
To modify the priority use push
, change_priority
or
change_priority_by
.
Computes in O(1) time
pub fn capacity(&self) -> usize
[src]
Returns the number of elements the internal map can hold without reallocating.
This number is a lower bound; the map might be able to hold more, but is guaranteed to be able to hold at least this many.
impl<I, P, H> PriorityQueue<I, P, H> where
P: Ord,
I: Hash + Eq,
H: BuildHasher,
[src]
P: Ord,
I: Hash + Eq,
H: BuildHasher,
pub fn reserve(&mut self, additional: usize)
[src]
Reserves capacity for at least additional
more elements to be inserted
in the given PriorityQueue
. The collection may reserve more space to avoid
frequent reallocations. 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
.
impl<I, P, H> PriorityQueue<I, P, H> where
P: Ord,
I: Hash + Eq,
[src]
P: Ord,
I: Hash + Eq,
pub fn shrink_to_fit(&mut self)
[src]
Shrinks the capacity of the internal data structures that support this operation as much as possible.
pub fn pop(&mut self) -> Option<(I, P)>
[src]
Removes the item with the greatest priority from the priority queue and returns the pair (item, priority), or None if the queue is empty.
impl<I, P, H> PriorityQueue<I, P, H> where
P: Ord,
I: Hash + Eq,
H: BuildHasher,
[src]
P: Ord,
I: Hash + Eq,
H: BuildHasher,
pub fn push(&mut self, item: I, priority: P) -> Option<P>
[src]
Insert the item-priority pair into the queue.
If an element equal to item
was already into the queue,
it is updated and the old value of its priority returned in Some
;
otherwise, return None
.
Computes in O(log(N)) time.
pub fn push_increase(&mut self, item: I, priority: P) -> Option<P>
[src]
Increase the priority of an existing item in the queue, or insert it if not present.
If an element equal to item
is already in the queue with a
lower priority, its priority is increased to the new one
without replacing the element and the old priority is returned.
Otherwise, the new element is inserted into the queue.
Returns Some
if an element equal to item
is already in the
queue. If its priority is higher then priority
, the latter is returned back,
otherwise, the old priority is contained in the Option.
If the item is not in the queue, None
is returned.
Computes in O(log(N)) time.
pub fn push_decrease(&mut self, item: I, priority: P) -> Option<P>
[src]
Decrease the priority of an existing item in the queue, or insert it if not present.
If an element equal to item
is already in the queue with a
higher priority, its priority is decreased to the new one
without replacing the element and the old priority is returned.
Otherwise, the new element is inserted into the queue.
Returns Some
if an element equal to item
is already in the
queue. If its priority is lower then priority
, the latter is returned back,
otherwise, the old priority is contained in the Option.
If the item is not in the queue, None
is returned.
Computes in O(log(N)) time.
pub fn change_priority<Q: ?Sized>(
&mut self,
item: &Q,
new_priority: P
) -> Option<P> where
I: Borrow<Q>,
Q: Eq + Hash,
[src]
&mut self,
item: &Q,
new_priority: P
) -> Option<P> where
I: Borrow<Q>,
Q: Eq + Hash,
Change the priority of an Item returning the old value of priority,
or None
if the item wasn't in the queue.
The argument item
is only used for lookup, and is not used to overwrite the item's data
in the priority queue.
The item is found in O(1) thanks to the hash table. The operation is performed in O(log(N)) time.
pub fn change_priority_by<Q: ?Sized, F>(&mut self, item: &Q, priority_setter: F) where
I: Borrow<Q>,
Q: Eq + Hash,
F: FnOnce(&mut P),
[src]
I: Borrow<Q>,
Q: Eq + Hash,
F: FnOnce(&mut P),
Change the priority of an Item using the provided function. The item is found in O(1) thanks to the hash table. The operation is performed in O(log(N)) time (worst case).
pub fn get_priority<Q: ?Sized>(&self, item: &Q) -> Option<&P> where
I: Borrow<Q>,
Q: Eq + Hash,
[src]
I: Borrow<Q>,
Q: Eq + Hash,
Get the priority of an item, or None
, if the item is not in the queue
pub fn get<Q>(&self, item: &Q) -> Option<(&I, &P)> where
I: Borrow<Q>,
Q: Eq + Hash,
[src]
I: Borrow<Q>,
Q: Eq + Hash,
Get the couple (item, priority) of an arbitrary element, as reference
or None
if the item is not in the queue.
pub fn get_mut<Q>(&mut self, item: &Q) -> Option<(&mut I, &P)> where
I: Borrow<Q>,
Q: Eq + Hash,
[src]
I: Borrow<Q>,
Q: Eq + Hash,
Get the couple (item, priority) of an arbitrary element, or None
if the item was not in the queue.
The item is a mutable reference, but it's a logic error to modify it
in a way that change the result of Hash
or Eq
.
The priority cannot be modified with a call to this function.
To modify the priority use push
, change_priority
or
change_priority_by
.
pub fn remove<Q>(&mut self, item: &Q) -> Option<(I, P)> where
I: Borrow<Q>,
Q: Eq + Hash,
[src]
I: Borrow<Q>,
Q: Eq + Hash,
Remove an arbitrary element from the priority queue. Returns the (item, priority) couple or None if the item is not found in the queue.
The operation is performed in O(log(N)) time (worst case).
pub fn into_vec(self) -> Vec<I>
[src]
Returns the items not ordered
impl<I, P, H> PriorityQueue<I, P, H> where
P: Ord,
I: Hash + Eq,
[src]
P: Ord,
I: Hash + Eq,
pub fn into_sorted_vec(self) -> Vec<I>
[src]
Implements a HeapSort
pub fn len(&self) -> usize
[src]
Returns the number of elements in the priority queue.
pub fn is_empty(&self) -> bool
[src]
Returns true if the priority queue contains no elements.
impl<I, P, H> PriorityQueue<I, P, H> where
P: Ord,
I: Hash + Eq,
H: BuildHasher,
[src]
P: Ord,
I: Hash + Eq,
H: BuildHasher,
pub fn clear(&mut self)
[src]
Drops all items from the priority queue
pub fn append(&mut self, other: &mut Self)
[src]
Move all items of the other
queue to self
ignoring the items Eq to elements already in self
At the end, other
will be empty.
Note that at the end, the priority of the duplicated elements inside self may be the one of the elements in other, if other is longer than self
impl<I, P, H> PriorityQueue<I, P, H> where
P: Ord,
I: Hash + Eq,
[src]
P: Ord,
I: Hash + Eq,
pub fn into_sorted_iter(self) -> IntoSortedIter<I, P, H>
[src]
Generates a new iterator from self that will extract the elements from the one with the highest priority to the lowest one.
Trait Implementations
impl<I: Clone, P: Clone, H: Clone> Clone for PriorityQueue<I, P, H> where
I: Hash + Eq,
P: Ord,
[src]
I: Hash + Eq,
P: Ord,
pub fn clone(&self) -> PriorityQueue<I, P, H>
[src]
pub fn clone_from(&mut self, source: &Self)
1.0.0[src]
impl<I, P, H> Debug for PriorityQueue<I, P, H> where
I: Debug + Hash + Eq,
P: Debug + Ord,
[src]
I: Debug + Hash + Eq,
P: Debug + Ord,
impl<I, P, H> Default for PriorityQueue<I, P, H> where
I: Hash + Eq,
P: Ord,
H: BuildHasher + Default,
[src]
I: Hash + Eq,
P: Ord,
H: BuildHasher + Default,
impl<I, P, H> Eq for PriorityQueue<I, P, H> where
I: Hash + Eq,
P: Ord,
H: BuildHasher,
[src]
I: Hash + Eq,
P: Ord,
H: BuildHasher,
impl<I, P, H> Extend<(I, P)> for PriorityQueue<I, P, H> where
I: Hash + Eq,
P: Ord,
H: BuildHasher,
[src]
I: Hash + Eq,
P: Ord,
H: BuildHasher,
pub fn extend<T: IntoIterator<Item = (I, P)>>(&mut self, iter: T)
[src]
pub fn extend_one(&mut self, item: A)
[src]
pub fn extend_reserve(&mut self, additional: usize)
[src]
impl<I, P, H> From<Vec<(I, P), Global>> for PriorityQueue<I, P, H> where
I: Hash + Eq,
P: Ord,
H: BuildHasher + Default,
[src]
I: Hash + Eq,
P: Ord,
H: BuildHasher + Default,
impl<I, P, H> FromIterator<(I, P)> for PriorityQueue<I, P, H> where
I: Hash + Eq,
P: Ord,
H: BuildHasher + Default,
[src]
I: Hash + Eq,
P: Ord,
H: BuildHasher + Default,
pub fn from_iter<IT>(iter: IT) -> Self where
IT: IntoIterator<Item = (I, P)>,
[src]
IT: IntoIterator<Item = (I, P)>,
impl<I, P, H> IntoIterator for PriorityQueue<I, P, H> where
I: Hash + Eq,
P: Ord,
H: BuildHasher,
[src]
I: Hash + Eq,
P: Ord,
H: BuildHasher,
type Item = (I, P)
The type of the elements being iterated over.
type IntoIter = IntoIter<I, P>
Which kind of iterator are we turning this into?
pub fn into_iter(self) -> IntoIter<I, P>
[src]
impl<'a, I, P, H> IntoIterator for &'a PriorityQueue<I, P, H> where
I: Hash + Eq,
P: Ord,
H: BuildHasher,
[src]
I: Hash + Eq,
P: Ord,
H: BuildHasher,
type Item = (&'a I, &'a P)
The type of the elements being iterated over.
type IntoIter = Iter<'a, I, P>
Which kind of iterator are we turning this into?
pub fn into_iter(self) -> Iter<'a, I, P>
[src]
impl<'a, I, P, H> IntoIterator for &'a mut PriorityQueue<I, P, H> where
I: Hash + Eq,
P: Ord,
[src]
I: Hash + Eq,
P: Ord,
type Item = (&'a mut I, &'a mut P)
The type of the elements being iterated over.
type IntoIter = IterMut<'a, I, P, H>
Which kind of iterator are we turning this into?
pub fn into_iter(self) -> IterMut<'a, I, P, H>
[src]
impl<I, P1, H1, P2, H2> PartialEq<PriorityQueue<I, P2, H2>> for PriorityQueue<I, P1, H1> where
I: Hash + Eq,
P1: Ord,
P1: PartialEq<P2>,
Option<P1>: PartialEq<Option<P2>>,
P2: Ord,
H1: BuildHasher,
H2: BuildHasher,
[src]
I: Hash + Eq,
P1: Ord,
P1: PartialEq<P2>,
Option<P1>: PartialEq<Option<P2>>,
P2: Ord,
H1: BuildHasher,
H2: BuildHasher,
Auto Trait Implementations
impl<I, P, H> RefUnwindSafe for PriorityQueue<I, P, H> where
H: RefUnwindSafe,
I: RefUnwindSafe,
P: RefUnwindSafe,
[src]
H: RefUnwindSafe,
I: RefUnwindSafe,
P: RefUnwindSafe,
impl<I, P, H> Send for PriorityQueue<I, P, H> where
H: Send,
I: Send,
P: Send,
[src]
H: Send,
I: Send,
P: Send,
impl<I, P, H> Sync for PriorityQueue<I, P, H> where
H: Sync,
I: Sync,
P: Sync,
[src]
H: Sync,
I: Sync,
P: Sync,
impl<I, P, H> Unpin for PriorityQueue<I, P, H> where
H: Unpin,
I: Unpin,
P: Unpin,
[src]
H: Unpin,
I: Unpin,
P: Unpin,
impl<I, P, H> UnwindSafe for PriorityQueue<I, P, H> where
H: UnwindSafe,
I: UnwindSafe,
P: UnwindSafe,
[src]
H: UnwindSafe,
I: UnwindSafe,
P: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<Q, K> Equivalent<K> for Q where
K: Borrow<Q> + ?Sized,
Q: Eq + ?Sized,
[src]
K: Borrow<Q> + ?Sized,
Q: Eq + ?Sized,
pub fn equivalent(&self, key: &K) -> bool
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,