pub struct IntervalHeap<T, C: Compare<T> = Natural<T>> { /* private fields */ }
Expand description
A double-ended priority queue implemented with an interval heap.
It is a logic error for an item to be modified in such a way that the
item’s ordering relative to any other item, as determined by the heap’s
comparator, changes while it is in the heap. This is normally only
possible through Cell
, RefCell
, global state, I/O, or unsafe code.
Implementations§
Source§impl<T: Ord> IntervalHeap<T>
impl<T: Ord> IntervalHeap<T>
Sourcepub fn new() -> IntervalHeap<T>
pub fn new() -> IntervalHeap<T>
Returns an empty heap ordered according to the natural order of its items.
§Examples
use interval_heap::IntervalHeap;
let heap = IntervalHeap::<u32>::new();
assert!(heap.is_empty());
Sourcepub fn with_capacity(capacity: usize) -> IntervalHeap<T>
pub fn with_capacity(capacity: usize) -> IntervalHeap<T>
Returns an empty heap with the given capacity and ordered according to the natural order of its items.
The heap will be able to hold exactly capacity
items without reallocating.
§Examples
use interval_heap::IntervalHeap;
let heap = IntervalHeap::<u32>::with_capacity(5);
assert!(heap.is_empty());
assert!(heap.capacity() >= 5);
Source§impl<T, C: Compare<T>> IntervalHeap<T, C>
impl<T, C: Compare<T>> IntervalHeap<T, C>
Sourcepub fn with_comparator(cmp: C) -> IntervalHeap<T, C>
pub fn with_comparator(cmp: C) -> IntervalHeap<T, C>
Returns an empty heap ordered according to the given comparator.
Sourcepub fn with_capacity_and_comparator(
capacity: usize,
cmp: C,
) -> IntervalHeap<T, C>
pub fn with_capacity_and_comparator( capacity: usize, cmp: C, ) -> IntervalHeap<T, C>
Returns an empty heap with the given capacity and ordered according to the given comparator.
Sourcepub fn from_vec_and_comparator(vec: Vec<T>, cmp: C) -> IntervalHeap<T, C>
pub fn from_vec_and_comparator(vec: Vec<T>, cmp: C) -> IntervalHeap<T, C>
Returns a heap containing all the items of the given vector and ordered according to the given comparator.
Sourcepub fn iter(&self) -> Iter<'_, T> ⓘ
pub fn iter(&self) -> Iter<'_, T> ⓘ
Returns an iterator visiting all items in the heap in arbitrary order.
Sourcepub fn min(&self) -> Option<&T>
pub fn min(&self) -> Option<&T>
Returns a reference to the smallest item in the heap.
Returns None
if the heap is empty.
Sourcepub fn max(&self) -> Option<&T>
pub fn max(&self) -> Option<&T>
Returns a reference to the greatest item in the heap.
Returns None
if the heap is empty.
Sourcepub fn min_max(&self) -> Option<(&T, &T)>
pub fn min_max(&self) -> Option<(&T, &T)>
Returns references to the smallest and greatest items in the heap.
Returns None
if the heap is empty.
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
Returns the number of items the heap can hold without reallocation.
Sourcepub fn reserve_exact(&mut self, additional: usize)
pub fn reserve_exact(&mut self, additional: usize)
Reserves the minimum capacity for exactly additional
more items to be inserted into the
heap.
Does nothing if the capacity is already sufficient.
Note that the allocator may give the heap more space than it
requests. Therefore capacity can not be relied upon to be precisely
minimal. Prefer reserve
if future insertions are expected.
Sourcepub fn reserve(&mut self, additional: usize)
pub fn reserve(&mut self, additional: usize)
Reserves capacity for at least additional
more items to be inserted into the heap.
The heap may reserve more space to avoid frequent reallocations.
Sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Discards as much additional capacity from the heap as possible.
Sourcepub fn pop_min(&mut self) -> Option<T>
pub fn pop_min(&mut self) -> Option<T>
Removes the smallest item from the heap and returns it.
Returns None
if the heap was empty.
Sourcepub fn pop_max(&mut self) -> Option<T>
pub fn pop_max(&mut self) -> Option<T>
Removes the greatest item from the heap and returns it.
Returns None
if the heap was empty.
Sourcepub fn into_vec(self) -> Vec<T>
pub fn into_vec(self) -> Vec<T>
Consumes the heap and returns its items as a vector in arbitrary order.
Sourcepub fn into_sorted_vec(self) -> Vec<T>
pub fn into_sorted_vec(self) -> Vec<T>
Consumes the heap and returns its items as a vector in sorted (ascending) order.
Trait Implementations§
Source§impl<T: Clone, C: Clone + Compare<T>> Clone for IntervalHeap<T, C>
impl<T: Clone, C: Clone + Compare<T>> Clone for IntervalHeap<T, C>
Source§fn clone(&self) -> IntervalHeap<T, C>
fn clone(&self) -> IntervalHeap<T, C>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreSource§impl<T, C: Compare<T> + Default> Default for IntervalHeap<T, C>
impl<T, C: Compare<T> + Default> Default for IntervalHeap<T, C>
Source§fn default() -> IntervalHeap<T, C>
fn default() -> IntervalHeap<T, C>
Source§impl<'a, T: 'a + Copy, C: Compare<T>> Extend<&'a T> for IntervalHeap<T, C>
impl<'a, T: 'a + Copy, C: Compare<T>> Extend<&'a T> for IntervalHeap<T, C>
Source§fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = &'a T>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Source§impl<T, C: Compare<T>> Extend<T> for IntervalHeap<T, C>
impl<T, C: Compare<T>> Extend<T> for IntervalHeap<T, C>
Source§fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Source§impl<T: Ord> From<Vec<T>> for IntervalHeap<T>
impl<T: Ord> From<Vec<T>> for IntervalHeap<T>
Source§fn from(vec: Vec<T>) -> IntervalHeap<T>
fn from(vec: Vec<T>) -> IntervalHeap<T>
Returns a heap containing all the items of the given vector and ordered according to the natural order of its items.
§Examples
use interval_heap::IntervalHeap;
let heap = IntervalHeap::from(vec![5, 1, 6, 4]);
assert_eq!(heap.len(), 4);
assert_eq!(heap.min_max(), Some((&1, &6)));