Struct mut_binary_heap::BinaryHeap
source · pub struct BinaryHeap<K, T, C = MaxComparator> { /* private fields */ }
Expand description
A priority queue implemented with a binary heap storing key-value pairs.
Unlike the implementation of BinaryHeap in the
standard library, it is ok to modify values while in the heap. This is possible
through the BinaryHeap::get_mut()
method. Updating a value through RefCell
or global state, etc however will still result in an invalid heap as the heap
won’t get updated automatically.
Examples
use mut_binary_heap::BinaryHeap;
// Type inference lets us omit an explicit type signature (which
// would be `BinaryHeap<i32, i32, MaxComparator>` in this example).
let mut heap: BinaryHeap<_, _> = BinaryHeap::new();
// We can use peek to look at the next item in the heap. In this case,
// there's no items in there yet so we get None.
assert_eq!(heap.peek(), None);
// Let's add some scores...
heap.push(1, 1);
heap.push(2, 5);
heap.push(3, 2);
// Now peek shows the most important item in the heap.
assert_eq!(heap.peek(), Some(&5));
// We can check the length of a heap.
assert_eq!(heap.len(), 3);
// We can iterate over the items in the heap, although they are returned in
// a random order.
for x in &heap {
println!("key {}, value {}", x.0, x.1);
}
// If we instead pop these scores, they should come back in order.
assert_eq!(heap.pop(), Some(5));
assert_eq!(heap.pop(), Some(2));
assert_eq!(heap.pop(), Some(1));
assert_eq!(heap.pop(), None);
// We can clear the heap of any remaining items.
heap.clear();
// The heap should now be empty.
assert!(heap.is_empty())
A BinaryHeap
with a known list of items can be initialized from an iterator
and a key selection function
use mut_binary_heap::BinaryHeap;
// This will create a max-heap.
let heap: BinaryHeap<_, _> = BinaryHeap::from([1, 5, 2].iter(), |v| v.clone());
Min-heap
BinaryHeap
can also act as a min-heap without requiring Reverse
or a custom Ord
implementation.
use mut_binary_heap::BinaryHeap;
let mut heap = BinaryHeap::new_min();
// There is no need to wrap values in `Reverse`
heap.push(1, 1);
heap.push(2, 5);
heap.push(3, 2);
// If we pop these scores now, they should come back in the reverse order.
assert_eq!(heap.pop(), Some(1));
assert_eq!(heap.pop(), Some(2));
assert_eq!(heap.pop(), Some(5));
assert_eq!(heap.pop(), None);
Time complexity
The value for push
is an expected cost; the method documentation gives a
more detailed analysis.
The cost for get_mut
contains the cost of dropping the RefMut
returned
by the function. Getting access is O(1).
Implementations§
source§impl<K: Hash + Eq, T, C: Compare<T> + Default> BinaryHeap<K, T, C>
impl<K: Hash + Eq, T, C: Compare<T> + Default> BinaryHeap<K, T, C>
sourcepub fn new() -> Self
pub fn new() -> Self
Creates an empty BinaryHeap
.
This default version will create a max-heap.
Examples
Basic usage:
use mut_binary_heap::BinaryHeap;
let mut heap: BinaryHeap<i32, i32> = BinaryHeap::new();
heap.push(0, 3);
heap.push(1, 1);
heap.push(2, 5);
assert_eq!(heap.pop(), Some(5));
sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates an empty BinaryHeap
with a specific capacity.
This preallocates enough memory for capacity
elements,
so that the BinaryHeap
does not have to be reallocated
until it contains at least that many values.
This default version will create a max-heap.
Examples
Basic usage:
use mut_binary_heap::BinaryHeap;
let mut heap: BinaryHeap<i32, i32> = BinaryHeap::with_capacity(10);
assert!(heap.capacity_min() >= 10);
heap.push(0, 3);
heap.push(1, 1);
heap.push(2, 5);
assert_eq!(heap.pop(), Some(5));
source§impl<K: Hash + Eq + Clone, T, C: Compare<T> + Default> BinaryHeap<K, T, C>
impl<K: Hash + Eq + Clone, T, C: Compare<T> + Default> BinaryHeap<K, T, C>
pub fn from<I: IntoIterator<Item = T>, F: Fn(&T) -> K>( values: I, key_selector: F ) -> Self
source§impl<K: Hash + Eq, T: Ord> BinaryHeap<K, T, MinComparator>
impl<K: Hash + Eq, T: Ord> BinaryHeap<K, T, MinComparator>
sourcepub fn new_min() -> Self
pub fn new_min() -> Self
Creates an empty BinaryHeap
.
The _min()
version will create a min-heap.
Examples
Basic usage:
use mut_binary_heap::BinaryHeap;
let mut heap = BinaryHeap::new_min();
heap.push(0, 3);
heap.push(1, 1);
heap.push(2, 5);
assert_eq!(heap.pop(), Some(1));
sourcepub fn with_capacity_min(capacity: usize) -> Self
pub fn with_capacity_min(capacity: usize) -> Self
Creates an empty BinaryHeap
with a specific capacity.
This preallocates enough memory for capacity
elements,
so that the BinaryHeap
does not have to be reallocated
until it contains at least that many values.
The _min()
version will create a min-heap.
Examples
Basic usage:
use mut_binary_heap::BinaryHeap;
let mut heap = BinaryHeap::with_capacity_min(10);
assert!(heap.capacity_min() >= 10);
heap.push(0, 3);
heap.push(1, 1);
heap.push(2, 5);
assert_eq!(heap.pop(), Some(1));
source§impl<K: Hash + Eq, T, F> BinaryHeap<K, T, FnComparator<F>>where
F: Fn(&T, &T) -> Ordering,
impl<K: Hash + Eq, T, F> BinaryHeap<K, T, FnComparator<F>>where F: Fn(&T, &T) -> Ordering,
sourcepub fn new_by(f: F) -> Self
pub fn new_by(f: F) -> Self
Creates an empty BinaryHeap
.
The _by()
version will create a heap ordered by given closure.
Examples
Basic usage:
use mut_binary_heap::BinaryHeap;
let mut heap = BinaryHeap::new_by(|a: &i32, b: &i32| b.cmp(a));
heap.push(0, 3);
heap.push(1, 1);
heap.push(2, 5);
assert_eq!(heap.pop(), Some(1));
sourcepub fn with_capacity_by(capacity: usize, f: F) -> Self
pub fn with_capacity_by(capacity: usize, f: F) -> Self
Creates an empty BinaryHeap
with a specific capacity.
This preallocates enough memory for capacity
elements,
so that the BinaryHeap
does not have to be reallocated
until it contains at least that many values.
The _by()
version will create a heap ordered by given closure.
Examples
Basic usage:
use mut_binary_heap::BinaryHeap;
let mut heap = BinaryHeap::with_capacity_by(10, |a: &i32, b: &i32| b.cmp(a));
assert!(heap.capacity_min() >= 10);
heap.push(0, 3);
heap.push(1, 1);
heap.push(2, 5);
assert_eq!(heap.pop(), Some(1));
source§impl<K: Hash + Eq, T, F, C: Ord> BinaryHeap<K, T, KeyComparator<F>>where
F: Fn(&T) -> C,
impl<K: Hash + Eq, T, F, C: Ord> BinaryHeap<K, T, KeyComparator<F>>where F: Fn(&T) -> C,
sourcepub fn new_by_sort_key(f: F) -> Self
pub fn new_by_sort_key(f: F) -> Self
Creates an empty BinaryHeap
.
The _by_sort_key()
version will create a heap ordered by
key converted by given closure.
Examples
Basic usage:
use mut_binary_heap::BinaryHeap;
let mut heap = BinaryHeap::new_by_sort_key(|a: &i32| a % 4);
heap.push(0, 3);
heap.push(1, 1);
heap.push(2, 5);
assert_eq!(heap.pop(), Some(3));
sourcepub fn with_capacity_by_sort_key(capacity: usize, f: F) -> Self
pub fn with_capacity_by_sort_key(capacity: usize, f: F) -> Self
Creates an empty BinaryHeap
with a specific capacity.
This preallocates enough memory for capacity
elements,
so that the BinaryHeap
does not have to be reallocated
until it contains at least that many values.
The _by_sort_key()
version will create a heap ordered by
key coverted by given closure.
Examples
Basic usage:
use mut_binary_heap::BinaryHeap;
let mut heap = BinaryHeap::with_capacity_by_sort_key(10, |a: &i32| a % 4);
assert!(heap.capacity_min() >= 10);
heap.push(0, 3);
heap.push(1, 1);
heap.push(2, 5);
assert_eq!(heap.pop(), Some(3));
source§impl<K: Hash + Eq + Clone, T, C: Compare<T>> BinaryHeap<K, T, C>
impl<K: Hash + Eq + Clone, T, C: Compare<T>> BinaryHeap<K, T, C>
sourcepub fn push(&mut self, key: K, item: T) -> Option<T>
pub fn push(&mut self, key: K, item: T) -> Option<T>
Pushes an item onto the binary heap.
If the heap did not have this key present, None is returned.
If the heap did have this key present, the value is updated, and the old
value is returned. The key is not updated, though; this matters for
types that can be ==
without being identical. For more information see
the documentation of HashMap::insert.
Examples
Basic usage:
use mut_binary_heap::BinaryHeap;
let mut heap: BinaryHeap<i32, i32> = BinaryHeap::new();
heap.push(0, 3);
heap.push(1, 5);
heap.push(2, 1);
assert_eq!(heap.len(), 3);
assert_eq!(heap.peek(), Some(&5));
Time complexity
The expected cost of push
, averaged over every possible ordering of
the elements being pushed, and over a sufficiently large number of
pushes, is O(1). This is the most meaningful cost metric when pushing
elements that are not already in any sorted pattern.
The time complexity degrades if elements are pushed in predominantly ascending order. In the worst case, elements are pushed in ascending sorted order and the amortized cost per push is O(log(n)) against a heap containing n elements.
The worst case cost of a single call to push
is O(n). The worst case
occurs when capacity is exhausted and needs a resize. The resize cost
has been amortized in the previous figures.
source§impl<K: Hash + Eq, T, C: Compare<T>> BinaryHeap<K, T, C>
impl<K: Hash + Eq, T, C: Compare<T>> BinaryHeap<K, T, C>
sourcepub fn peek_mut(&mut self) -> Option<PeekMut<'_, K, T, C>>
pub fn peek_mut(&mut self) -> Option<PeekMut<'_, K, T, C>>
Returns a mutable reference to the first item in the binary heap, or
None
if it is empty.
Note: If the PeekMut
value is leaked, the heap may be in an
inconsistent state.
Examples
Basic usage:
use mut_binary_heap::BinaryHeap;
let mut heap: BinaryHeap<i32, i32> = BinaryHeap::new();
assert!(heap.peek_mut().is_none());
heap.push(0, 1);
heap.push(1, 5);
heap.push(2, 2);
{
let mut val = heap.peek_mut().unwrap();
assert_eq!(*val, 5);
*val = 0;
}
assert_eq!(heap.peek(), Some(&2));
Time complexity
If the item is modified then the worst case time complexity is O(log(n)), otherwise it’s O(1).
sourcepub fn pop(&mut self) -> Option<T>
pub fn pop(&mut self) -> Option<T>
Removes the greatest item from the binary heap and returns it, or None
if it
is empty.
Examples
Basic usage:
use mut_binary_heap::BinaryHeap;
let mut heap = BinaryHeap::<_, _>::from([1, 3], |v| v.clone());
assert_eq!(heap.pop(), Some(3));
assert_eq!(heap.pop(), Some(1));
assert_eq!(heap.pop(), None);
Time complexity
The worst case cost of pop
on a heap containing n elements is O(log(n)).
sourcepub fn pop_with_key(&mut self) -> Option<(K, T)>
pub fn pop_with_key(&mut self) -> Option<(K, T)>
Removes the greatest item from the binary heap and returns it as a key-value pair,
or None
if it is empty.
Examples
Basic usage:
use mut_binary_heap::BinaryHeap;
let mut heap = BinaryHeap::<_,_>::from(vec![1, 3], |v| v.clone());
assert_eq!(heap.pop_with_key(), Some((3, 3)));
assert_eq!(heap.pop_with_key(), Some((1, 1)));
assert_eq!(heap.pop_with_key(), None);
Time complexity
The worst case cost of pop
on a heap containing n elements is O(log(n)).
sourcepub fn contains_key(&self, key: &K) -> bool
pub fn contains_key(&self, key: &K) -> bool
Returns true
if the heap contains a value for the given key.
Examples
use mut_binary_heap::BinaryHeap;
let mut heap = BinaryHeap::<_,_>::from([1, 3], |v| v.clone());
assert!(heap.contains_key(&1));
assert!(heap.contains_key(&3));
assert!(!heap.contains_key(&2));
Time complexity
This method runs in O(1) time.
sourcepub fn get(&self, key: &K) -> Option<&T>
pub fn get(&self, key: &K) -> Option<&T>
Returns a reference to the value for a given key or None if the key does not exist.
Examples
use mut_binary_heap::BinaryHeap;
let mut heap = BinaryHeap::<_,_>::from(vec![1, 3], |v| v.clone());
assert_eq!(heap.get(&1), Some(&1));
assert_eq!(heap.get(&2), None);
Time complecity
This method runs in O(1) time.
sourcepub fn get_mut<'a>(&'a mut self, key: &'a K) -> Option<RefMut<'a, K, T, C>>
pub fn get_mut<'a>(&'a mut self, key: &'a K) -> Option<RefMut<'a, K, T, C>>
Returns a mutable reference to the value for a given key or None if the key does not exist.
The heap is updated when RefMut is dropped.
Examples
use mut_binary_heap::BinaryHeap;
let mut heap = BinaryHeap::<i32, i32>::from(vec![1, 3], |v| v.clone());
{
let mut v = heap.get_mut(&1).unwrap();
assert_eq!(*v, 1);
*v = 5;
// Drop updates the heap
}
assert_eq!(heap.peek_with_key(), Some((&1, &5)));
assert_eq!(heap.get(&2), None);
Time complecity
sourcepub fn remove(&mut self, key: &K) -> Option<(K, T)>
pub fn remove(&mut self, key: &K) -> Option<(K, T)>
Removes a key from the heap, returning the (key, value)
if the key
was previously in the heap.
The key may be any borrowed form of the map’s key type, but Hash and Eq on the borrowed form must match those for the key type.
Example
use mut_binary_heap::BinaryHeap;
let mut heap: BinaryHeap<_, _> = BinaryHeap::new();
heap.push(0, 5);
heap.push(1, 3);
heap.push(2, 6);
assert_eq!(heap.remove(&0), Some((0, 5)));
assert_eq!(heap.remove(&3), None);
assert_eq!(heap.len(), 2);
assert_eq!(heap.pop(), Some(6));
assert_eq!(heap.pop(), Some(3));
sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Consumes the BinaryHeap
and returns a vector in sorted
(ascending) order.
Examples
Basic usage:
use mut_binary_heap::BinaryHeap;
let mut heap = BinaryHeap::<_, _>::from([1, 2, 4, 5, 7], |v| v.clone());
heap.push(0, 6);
heap.push(1, 3);
// let vec = heap.into_sorted_vec();
// assert_eq!(vec, [1, 2, 3, 4, 5, 6, 7]);
Reserves capacity for at least additional
more elements to be inserted in the
BinaryHeap
. The collection may reserve more space to avoid frequent reallocations.
Panics
Panics if the new capacity overflows usize
.
Examples
Basic usage:
use mut_binary_heap::BinaryHeap;
let mut heap: BinaryHeap<_, _> = BinaryHeap::new();
heap.reserve(100);
assert!(heap.capacity_min() >= 100);
heap.push(0, 4);
sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Discards as much additional capacity as possible. The implementation of Vec and HashMap the exact value of the new capacity.
Examples
Basic usage:
use mut_binary_heap::BinaryHeap;
let mut heap: BinaryHeap<i32, i32> = BinaryHeap::with_capacity(100);
assert!(heap.capacity_min() >= 100);
heap.shrink_to_fit();
assert!(heap.capacity_min() >= 0);
sourcepub fn shrink_to(&mut self, min_capacity: usize)
pub fn shrink_to(&mut self, min_capacity: usize)
Discards capacity with a lower bound. The implementation of Vec and HashMap the exact value of the new capacity.
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.
Examples
use mut_binary_heap::BinaryHeap;
let mut heap: BinaryHeap<i32, i32> = BinaryHeap::with_capacity(100);
assert!(heap.capacity_min() >= 100);
heap.shrink_to(10);
assert!(heap.capacity_min() >= 10);
source§impl<K, T, C> BinaryHeap<K, T, C>
impl<K, T, C> BinaryHeap<K, T, C>
sourcepub fn iter(&self) -> Iter<'_, K, T> ⓘ
pub fn iter(&self) -> Iter<'_, K, T> ⓘ
Returns an iterator visiting all key-value pairs in the underlying vector, in arbitrary order.
Examples
Basic usage:
use mut_binary_heap::BinaryHeap;
let heap = BinaryHeap::<_,_>::from([1, 2, 3, 4], |v| v.clone());
// Print (1, 1), (2, 2), (3, 3), (4, 4) in arbitrary order
for x in heap.iter() {
println!("key {}, value {}", x.0, x.1);
}
sourcepub fn iter_values(&self) -> IterValues<'_, K, T> ⓘ
pub fn iter_values(&self) -> IterValues<'_, K, T> ⓘ
Returns an iterator visiting all values in the underlying vector, in arbitrary order.
Examples
Basic usage:
use mut_binary_heap::BinaryHeap;
let heap = BinaryHeap::<_,_>::from([1, 2, 3, 4], |v| v.clone());
// Print 1, 2, 3, 4 in arbitrary order
for x in heap.iter_values() {
println!("{}", x);
}
sourcepub fn iter_keys(&self) -> IterKeys<'_, K, T> ⓘ
pub fn iter_keys(&self) -> IterKeys<'_, K, T> ⓘ
Returns an iterator visiting all keys in the underlying vector, in arbitrary order.
Examples
Basic usage:
use mut_binary_heap::BinaryHeap;
let heap = BinaryHeap::<_,_>::from([1, 2, 3, 4], |v| v.clone());
// Print 1, 2, 3, 4 in arbitrary order
for x in heap.iter_keys() {
println!("{}", x);
}
sourcepub fn into_values(self) -> IntoValues<K, T> ⓘ
pub fn into_values(self) -> IntoValues<K, T> ⓘ
Creates a consuming iterator, that is, one that moves each value out of the heap in arbitrary order. The heap cannot be used after calling this.
sourcepub fn into_keys(self) -> IntoKeys<K, T> ⓘ
pub fn into_keys(self) -> IntoKeys<K, T> ⓘ
Creates a consuming iterator, that is, one that moves each key out of the heap in arbitrary order. The heap cannot be used after calling this.
sourcepub fn into_iter_sorted(self) -> IntoIterSorted<K, T, C> ⓘ
pub fn into_iter_sorted(self) -> IntoIterSorted<K, T, C> ⓘ
Returns an iterator which retrieves elements in heap order. This method consumes the original heap.
Examples
Basic usage:
use mut_binary_heap::BinaryHeap;
let heap = BinaryHeap::<_,_>::from([1, 2, 3, 4, 5], |v| v.clone());
assert_eq!(heap.into_iter_sorted().take(2).collect::<Vec<_>>(), [5, 4]);
sourcepub fn peek(&self) -> Option<&T>
pub fn peek(&self) -> Option<&T>
Returns the greatest item in the binary heap, or None
if it is empty.
Examples
Basic usage:
use mut_binary_heap::BinaryHeap;
let mut heap: BinaryHeap<_, _> = BinaryHeap::new();
assert_eq!(heap.peek(), None);
heap.push(1, 1);
heap.push(2, 5);
heap.push(3, 2);
assert_eq!(heap.peek(), Some(&5));
Time complexity
Cost is O(1) in the worst case.
sourcepub fn peek_with_key(&self) -> Option<(&K, &T)>
pub fn peek_with_key(&self) -> Option<(&K, &T)>
Returns the greatest item in the binary heap as a key-value pair,
or None
if it is empty.
Examples
Basic usage:
use mut_binary_heap::BinaryHeap;
let mut heap: BinaryHeap<_, _> = BinaryHeap::new();
assert_eq!(heap.peek(), None);
heap.push(1, 1);
heap.push(2, 5);
heap.push(3, 2);
assert!(heap.peek_mut().is_some());
assert_eq!(heap.peek_mut().unwrap().key_value(), (&2, &5));
Time complexity
Cost is O(1) in the worst case.
sourcepub fn capacity(&self) -> (usize, usize)
pub fn capacity(&self) -> (usize, usize)
Returns the number of elements the binary heap can hold without reallocating. Returns a touple with the capacity of the internal vector and hashmap.
Examples
Basic usage:
use mut_binary_heap::BinaryHeap;
let mut heap: BinaryHeap<_, _> = BinaryHeap::with_capacity(100);
assert!(heap.capacity().0 >= 100);
assert!(heap.capacity().1 >= 100);
heap.push(0, 4);
sourcepub fn capacity_min(&self) -> usize
pub fn capacity_min(&self) -> usize
Returns the minimum number of elements the binary heap can hold without reallocating.
Examples
Basic usage:
use mut_binary_heap::BinaryHeap;
let mut heap: BinaryHeap<_, _> = BinaryHeap::with_capacity(100);
assert!(heap.capacity_min() >= 100);
heap.push(0, 4);
sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Consumes the BinaryHeap
and returns the underlying vector
in arbitrary order.
Examples
Basic usage:
use mut_binary_heap::BinaryHeap;
let heap = BinaryHeap::<_,_>::from([1, 2, 3, 4, 5, 6, 7], |v| v.clone());
// let vec = heap.into_vec();
// Will print in some order
// for x in vec {
// println!("{}", x);
// }
Returns the length of the binary heap.
Examples
Basic usage:
use mut_binary_heap::BinaryHeap;
let heap = BinaryHeap::<_,_>::from([1, 3].iter(), |v| v.clone());
assert_eq!(heap.len(), 2);
sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Checks if the binary heap is empty.
Examples
Basic usage:
use mut_binary_heap::BinaryHeap;
let mut heap: BinaryHeap<_, _> = BinaryHeap::new();
assert!(heap.is_empty());
heap.push(0, 3);
heap.push(1, 5);
heap.push(2, 1);
assert!(!heap.is_empty());
sourcepub fn drain(&mut self) -> Drain<'_, (K, T)> ⓘ
pub fn drain(&mut self) -> Drain<'_, (K, 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:
use mut_binary_heap::BinaryHeap;
let mut heap = BinaryHeap::<_,_>::from([1, 3].iter(), |v| v.clone());
assert!(!heap.is_empty());
for x in heap.drain() {
println!("key {}, value {}", x.0, x.1);
}
assert!(heap.is_empty());
Trait Implementations§
source§impl<K: Hash + Eq, T, C: Compare<T> + Default> Default for BinaryHeap<K, T, C>
impl<K: Hash + Eq, T, C: Compare<T> + Default> Default for BinaryHeap<K, T, C>
source§fn default() -> BinaryHeap<K, T, C>
fn default() -> BinaryHeap<K, T, C>
Creates an empty BinaryHeap<K, T>
.
source§impl<K: Hash + Eq + Clone, T, C: Compare<T> + Default> FromIterator<(K, T)> for BinaryHeap<K, T, C>
impl<K: Hash + Eq + Clone, T, C: Compare<T> + Default> FromIterator<(K, T)> for BinaryHeap<K, T, C>
source§impl<'a, K, T, C> IntoIterator for &'a BinaryHeap<K, T, C>
impl<'a, K, T, C> IntoIterator for &'a BinaryHeap<K, T, C>
source§impl<'a, K: Hash + Eq, T, C: Compare<T>> IntoIterator for &'a mut BinaryHeap<K, T, C>
impl<'a, K: Hash + Eq, T, C: Compare<T>> IntoIterator for &'a mut BinaryHeap<K, T, C>
source§impl<K, T, C> IntoIterator for BinaryHeap<K, T, C>
impl<K, T, C> IntoIterator for BinaryHeap<K, T, C>
source§fn into_iter(self) -> IntoIter<K, T> ⓘ
fn into_iter(self) -> IntoIter<K, T> ⓘ
Creates a consuming iterator, that is, one that moves each key-value pair out of the binary heap in arbitrary order. The binary heap cannot be used after calling this.
Examples
Basic usage:
use mut_binary_heap::BinaryHeap;
let heap = BinaryHeap::<_,_>::from([1, 2, 3, 4].iter(), |v| v.clone());
// Print 1, 2, 3, 4 in arbitrary order
for x in heap.into_iter() {
// x has type (i32, i32), not (&i32, &i32)
println!("key {}, value {}", x.0, x.1);
}