Struct maelstrom_util::heap::Heap

source ·
pub struct Heap<DepsT: HeapDeps>(/* private fields */);
Expand description

A min-heap implementation that provides some features necessary for us that are missing from std::collections::BinaryHeap:

  • The values (priorities) of elements can be stored outside of the heap itself. This supports a common use case where the heap just contains IDs for a specific data structure, and the actual data structures are stored elsewhere, usually in a map of some sort.
  • The heap can be notified when Individual elements’ values change, via Heap::sift_up and Heap::sift_down, both of which are O(log(n)).
  • If all or most of the elements’ values change, the heap can be fixed up using Heap::rebuild. This is O(n).
  • Arbitrary elements can be removed from the heap in O(log(n)) time.

This heap accomplishes this by telling the elements what their heap indices are. Those heap indices are then used in the per-element methods.

Since the values of elements are stored externally from the heap, the heap relies on the client notifying it whenever a value changes. If the client doesn’t do this, the heap condition will be violated and Heap::peek will no longer necessarily provide the smallest element.

The modifying methods of this struct take a &mut HeapDeps, instead of a Heap constructor taking the dependencies and storing them in the heap. This is done to satisfy the borrow checker so that clients of the heap don’t need to use a bunch of std::rc::Rc<Refcell<_>>s.

Implementations§

source§

impl<DepsT: HeapDeps> Heap<DepsT>

source

pub fn peek(&self) -> Option<&DepsT::Element>

Return an Option containing an element with the smallest value in the heap, or None if the heap is empty. Note that multiple elements in the heap may have the smallest value. In this case, an arbitrary element will be returned. O(1).

source

pub fn pop(&mut self, deps: &mut DepsT) -> Option<DepsT::Element>

Remove the element with the smallest value in the heap, or None if the heap is empty. Note that multiple elements in the heap may have the smallest value. In this case, an arbitrary element will be returned. O(log(n)).

source

pub fn push(&mut self, deps: &mut DepsT, elem: DepsT::Element)

Add a new element to the heap. O(log(n)).

source

pub fn remove(&mut self, deps: &mut DepsT, idx: HeapIndex)

Remove the element at the given index from the heap. If the index is out of range, the function will panic. O(log(n)).

source

pub fn sift_up(&mut self, deps: &mut DepsT, idx: HeapIndex)

Notify the heap an element’s value has decreased. The heap will see if the element needs to be sifted up the heap. If the index is out of range, the function will panic. O(log(n)).

source

pub fn sift_down(&mut self, deps: &mut DepsT, idx: HeapIndex)

Notify the heap an element’s value has increased. The heap will see if the element needs to be sifted down the heap. If the index is out of range, the function will panic. O(log(n)).

source

pub fn rebuild(&mut self, deps: &mut DepsT)

Assume all elements’ values have changed and rebuild the heap accordingly. If one is changing the values of most of the elements in the heap, this method is faster than calling Heap::sift_up or Heap::sift_down on each element. O(n).

Trait Implementations§

source§

impl<DepsT: HeapDeps> Default for Heap<DepsT>

source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<DepsT> Freeze for Heap<DepsT>

§

impl<DepsT> RefUnwindSafe for Heap<DepsT>
where <DepsT as HeapDeps>::Element: RefUnwindSafe,

§

impl<DepsT> Send for Heap<DepsT>
where <DepsT as HeapDeps>::Element: Send,

§

impl<DepsT> Sync for Heap<DepsT>
where <DepsT as HeapDeps>::Element: Sync,

§

impl<DepsT> Unpin for Heap<DepsT>
where <DepsT as HeapDeps>::Element: Unpin,

§

impl<DepsT> UnwindSafe for Heap<DepsT>
where <DepsT as HeapDeps>::Element: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

source§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> SendSyncUnwindSafe for T
where T: Send + Sync + UnwindSafe + ?Sized,