Struct trait_based_collection::priority_queue::BinaryHeap
source · [−]pub struct BinaryHeap<T>where
T: Ord,{
pub mode: ExpansionMode,
/* private fields */
}
Expand description
An implementation of a priority queue using a binary heap. The binary heap is implemented using an array. The binary heap is a complete binary tree, meaning that every level of the tree is filled except for the last level, which is filled from left to right. The binary heap is stored using a function that determines the order of the elements in the heap.
The default heap function is equivalent to a min-heap, meaning that the element with the
smallest value is at the top of the heap. For a max-heap, the heap can be created using the
max_heap
function.
Examples
use trait_based_collection::{prelude::*, BinaryHeap};
let mut heap = BinaryHeap::min_heap(16, ExpansionMode::Expand(2.0));
heap.add(5);
heap.add(3);
heap.add(7);
heap.add(1);
assert_eq!(heap.remove(), Some(1));
assert_eq!(heap.remove(), Some(3));
assert_eq!(heap.remove(), Some(5));
assert_eq!(heap.remove(), Some(7));
assert_eq!(heap.remove(), None);
Fields
mode: ExpansionMode
The current mode of expansion of the deque that determinate the behaviour the collection is
full. See ExpansionMode
for more information.
Implementations
sourceimpl<T> BinaryHeap<T>where
T: Ord,
impl<T> BinaryHeap<T>where
T: Ord,
sourcepub fn new(
capacity: usize,
mode: ExpansionMode,
function: fn(_: &T, _: &T) -> Ordering
) -> Self
pub fn new(
capacity: usize,
mode: ExpansionMode,
function: fn(_: &T, _: &T) -> Ordering
) -> Self
Creates a new BinaryHeap
with the given capacity, expansion mode and function that
determines the order of the elements in the heap. The root of the heap is the element that
has Ordering::Less
when compared to the other elements in the heap.
This a generic constructor, if you want a max_heap
or a min_heap
, use the
corresponding functions.
Panics
This function panics if the capacity is 0.
Examples
use trait_based_collection::{prelude::*, BinaryHeap};
let mut heap: BinaryHeap<i32> = BinaryHeap::new(10,
ExpansionMode::Ignore,
|a, b| a.cmp(b)); // Min-heap.
sourcepub fn min_heap(capacity: usize, mode: ExpansionMode) -> Self
pub fn min_heap(capacity: usize, mode: ExpansionMode) -> Self
sourcepub fn max_heap(capacity: usize, mode: ExpansionMode) -> Self
pub fn max_heap(capacity: usize, mode: ExpansionMode) -> Self
Trait Implementations
sourceimpl<'a, T> Collection<'a, T> for BinaryHeap<T>where
T: 'a + Ord,
impl<'a, T> Collection<'a, T> for BinaryHeap<T>where
T: 'a + Ord,
sourcefn new_default() -> Self
fn new_default() -> Self
Collection
with a default capacity. Read moresourcefn with_capacity(capacity: usize) -> Self
fn with_capacity(capacity: usize) -> Self
Collection
with a specific capacity. Read moresourcefn with_approximate_capacity(approx_capacity: usize) -> Self
fn with_approximate_capacity(approx_capacity: usize) -> Self
Collection
with a capacity that is at least the specified capacity. Read moresourcefn add(&mut self, value: T)
fn add(&mut self, value: T)
Collection
. Read moresourcefn remove(&mut self) -> Option<T>
fn remove(&mut self) -> Option<T>
Collection
. Read moresourcefn clear(&mut self)
fn clear(&mut self)
Collection
while keeping the capacity. Read moresourcefn peek(&'a self) -> Option<Self::ItemRef>
fn peek(&'a self) -> Option<Self::ItemRef>
sourcefn peek_mut(&'a mut self) -> Option<Self::ItemMut>
fn peek_mut(&'a mut self) -> Option<Self::ItemMut>
sourcefn len(&self) -> usize
fn len(&self) -> usize
Collection
. Read moresourcefn get(&'a self, index: usize) -> Option<Self::ItemRef>
fn get(&'a self, index: usize) -> Option<Self::ItemRef>
Collection
. Read moresourcefn get_mut(&'a mut self, index: usize) -> Option<Self::ItemMut>
fn get_mut(&'a mut self, index: usize) -> Option<Self::ItemMut>
Collection
. Read moresourcefn find(&'a self, value: &T) -> Option<usize>where
T: PartialEq,
fn find(&'a self, value: &T) -> Option<usize>where
T: PartialEq,
Collection
and returns its index if found.
Returns None
if the item is not found. Read moresourcefn contains(&'a self, value: &T) -> boolwhere
T: PartialEq,
fn contains(&'a self, value: &T) -> boolwhere
T: PartialEq,
Collection
. Read moresourcefn is_empty(&self) -> bool
fn is_empty(&self) -> bool
Collection
is empty. Read moresourceimpl<T> Debug for BinaryHeap<T>where
T: Ord + Debug,
impl<T> Debug for BinaryHeap<T>where
T: Ord + Debug,
sourceimpl<T> Default for BinaryHeap<T>where
T: Ord,
impl<T> Default for BinaryHeap<T>where
T: Ord,
sourceimpl<T> Display for BinaryHeap<T>where
T: Ord,
T: Display,
impl<T> Display for BinaryHeap<T>where
T: Ord,
T: Display,
sourceimpl<T> Drop for BinaryHeap<T>where
T: Ord,
impl<T> Drop for BinaryHeap<T>where
T: Ord,
sourceimpl<T> Extend<T> for BinaryHeap<T>where
T: Ord,
impl<T> Extend<T> for BinaryHeap<T>where
T: Ord,
sourcefn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
fn extend<I: IntoIterator<Item = T>>(&mut self, iter: I)
sourcefn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)sourcefn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)sourceimpl<'a, T> FixedSizeCollection<'a, T> for BinaryHeap<T>where
T: 'a + Ord,
impl<'a, T> FixedSizeCollection<'a, T> for BinaryHeap<T>where
T: 'a + Ord,
sourcefn with_mode(capacity: usize, mode: ExpansionMode) -> Self
fn with_mode(capacity: usize, mode: ExpansionMode) -> Self
sourcefn capacity(&self) -> usize
fn capacity(&self) -> usize
sourcefn expand(&mut self, extra_size: usize)
fn expand(&mut self, extra_size: usize)
sourcefn mode(&self) -> &ExpansionMode
fn mode(&self) -> &ExpansionMode
ExpansionMode
of the collection . This is used to determine how the
collection will behave when it is full. Read moresourcefn is_full(&self) -> bool
fn is_full(&self) -> bool
FixedSizeCollection
is equal to the capacity. Read moresourceimpl<T> FromIterator<T> for BinaryHeap<T>where
T: Ord,
impl<T> FromIterator<T> for BinaryHeap<T>where
T: Ord,
sourcefn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
sourceimpl<'a, T> IntoIterator for &'a BinaryHeap<T>where
T: 'a + Ord,
impl<'a, T> IntoIterator for &'a BinaryHeap<T>where
T: 'a + Ord,
sourceimpl<'a, T> IntoIterator for &'a mut BinaryHeap<T>where
T: 'a + Ord,
impl<'a, T> IntoIterator for &'a mut BinaryHeap<T>where
T: 'a + Ord,
sourceimpl<T> IntoIterator for BinaryHeap<T>where
T: Ord,
impl<T> IntoIterator for BinaryHeap<T>where
T: Ord,
sourceimpl<'a, T> Iterators<'a, T> for BinaryHeap<T>where
T: 'a + Ord,
impl<'a, T> Iterators<'a, T> for BinaryHeap<T>where
T: 'a + Ord,
type ItemRef = &'a T
type ItemRef = &'a T
Iter
) iterates over the items in the
Collection
. The reference is only valid for the duration of the iteration. Read moretype ItemMut = PeekMut<'a, T>
type ItemMut = PeekMut<'a, T>
IterMut
) iterates over the items in
the Collection
. The reference is only valid for the duration of the iteration. Read moretype IterMut = BinaryHeapIterMut<'a, T>
type IterMut = BinaryHeapIterMut<'a, T>
sourcefn iter(&'a self) -> Self::Iter
fn iter(&'a self) -> Self::Iter
Collection
without consuming them. Read moresourcefn iter_mut(&'a mut self) -> Self::IterMut
fn iter_mut(&'a mut self) -> Self::IterMut
Collection
without consuming them. Read more