Struct crater::fheap::FibHeap

source ·
pub struct FibHeap<'a, T: Node<'a> + ?Sized, U> { /* private fields */ }
Expand description

A fibonacci heap is a type of data structure optimized for use as a priority queue. It is commonly useful in pathfinding algorithms like Dijkstra’s and A*. Fib heaps have the following amortized time complexities:

  • find-min: O(1)
  • extract-min: O(logn)
  • insert: O(1), compare to O(logn) in a binary heap
  • decrease-key: O(1), compare to O(logn) in a binary heap / pairing heap
  • meld: O(1), compare to O(n) in a binary heap Extracting (or deleting) an arbitrary element is also O(logn). Increase-key is not directly supported, but can be done by removing and reinserting. Fib heaps will generally be a better choice than other heaps when:
  • The number of elements extracted from the heap is proportional to the number inserted and/or large, not fixed and small
  • The keys/priorities of elements need to be decreased frequently
  • The keys/priorities of elements don’t need to be increased frequently (also the comparison function doesn’t need to be changed)

Implementations§

source§

impl<'a, T: Node<'a> + ?Sized + 'a, U> FibHeap<'a, T, U>

source

pub fn new(_container: &'a U) -> Self

Create a new fibonacci heap whose backing storage will live at least as long as _container. Typically, if the nodes are stored in a Vec or a crate::kdtree::KdTree or so on, this backing container will have to remain effectively pinned for as long as the fibonacci heap exists, so that references to nodes remain valid.

Conceptually, only the lifespan of _container matters: by passing it in here and holding a PhantomData reference with the same lifespan, we force the shared reference to the container to outlive the fibonacci heap and prevent any mutable references to it from existing for that time.

Internally, RawNode uses UnsafeCell so that nodes can be mutated through shared reference. Otherwise, correctly mutating nodes is excessively complicated (requires using raw pointers throughout, and requires the backing storage to have some way to get a mutable subreference that doesn’t overlap with any node so we can assume it is effectively pinned, most of the time this would be a mutable reference to an empty slice).

source

pub fn peek_min(&self) -> Option<&'a T>

Get the minimal element if it exists, returning a reference to it without removing it. It is safe to decrease the key of the result, but not to increase it if doing so could cause it to no longer be minimal (the key probably lives behind an UnsafeCell)

source

pub fn pop_min(&mut self) -> Option<&'a T>

Get and remove the minimal element if it exists, returning a reference to it. The key of the result may be freely modified

source

pub unsafe fn push(&mut self, ent: &'a T)

Add a node to the heap. This is unsafe because the node must not already be in the heap, nor in a different heap. It is not strictly required for all nodes to have the same backing store as long as the borrow checker confirms they outlive the heap, but most of the time all references will be to elements of the backing store.

source

pub unsafe fn decrease_key(&mut self, ent: &'a T)

Called immediately AFTER a node’s key is decreased, to ensure that the heap invariants are maintained. This is unsafe because the node must be an element of the heap.

source

pub unsafe fn remove(&mut self, node: &'a T)

Remove a node from the heap by reference. Since the heap is intrusive and does not own its nodes, nothing is returned because the caller already has a reference to the removed node. This is unsafe because the node must be an element of the heap.

Auto Trait Implementations§

§

impl<'a, T: ?Sized, U> RefUnwindSafe for FibHeap<'a, T, U>

§

impl<'a, T: ?Sized, U> Send for FibHeap<'a, T, U>
where T: Sync, U: Send,

§

impl<'a, T: ?Sized, U> Sync for FibHeap<'a, T, U>
where T: Sync, U: Sync,

§

impl<'a, T: ?Sized, U> Unpin for FibHeap<'a, T, U>

§

impl<'a, T, U> !UnwindSafe for FibHeap<'a, T, U>

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, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V