Struct HeapType

Source
pub struct HeapType<T, E>
where T: Heap<E>,
{ pub data: T, /* private fields */ }
Expand description

A heap structure which holds a type which derives from Heap

Fields§

§data: T

Implementations§

Source§

impl<T: Heap<E>, E> HeapType<T, E>

Source

pub fn new(init: T) -> HeapType<T, E>
where T: Heap<E>,

Initialized a new heap from a Heap type

§Arguments
  • init: The Heap type to be initialized

returns: HeapType<T, E> which is your heap data structure

§Examples
use go_heap_rs::{HeapType, MinHeap};

let my_vec = MinHeap(vec![4, 3, 2, 1]); // see min heap implementation in Heap trait
let mut heap = HeapType::new(my_vec);
assert_eq!(heap.peak(), Some(&1));
Source

pub fn push(&mut self, x: E)

Pushes an element into heap

§Arguments
  • x: The element to push it into heap
Source

pub fn pop(&mut self) -> Option<E>

Removes the greatest item from the binary heap and returns it, or None if it is empty.

returns E: The first element in list

Source

pub fn remove(&mut self, i: usize) -> E

Removes an element from heap by it’s index in it’s underlying container

§Arguments
  • i: The index to remove

returns E: The element which as been removed

§Panics

This method might panic (based on implementation of swap) if i is bigger than len()

§Examples
use go_heap_rs::{HeapType, MinHeap};

let my_vec = MinHeap(vec![1, 4, 3]);
let mut heap = HeapType::new(my_vec); // [1, 4, 3]
assert_eq!(heap.remove(1), 4);
assert_eq!(heap.pop(), Some(1));
assert_eq!(heap.pop(), Some(3));
assert_eq!(heap.pop(), None);
Source

pub fn fix(&mut self, i: usize)

Fix re-establishes the heap ordering after the element at index i has changed its value. Changing the value of the element at index i and then calling Fix is equivalent to, but less expensive than, calling Remove(h, i) followed by a Push of the new value.

§Arguments
  • i: The index to fix
§Examples
use go_heap_rs::{HeapType, MinHeap};

let my_vec = MinHeap(vec![10, 4, 3]);
let mut heap = HeapType::new(my_vec); // [3, 4, 10]
heap.data.0[1] = 0;
heap.fix(0);
assert_eq!(heap.pop(), Some(0));
assert_eq!(heap.pop(), Some(3));
assert_eq!(heap.pop(), Some(10));
assert_eq!(heap.pop(), None);
Source

pub fn peak(&self) -> Option<&E>

peak will return the top of heap if available

Source

pub fn len(&self) -> usize

len will simply call self.data.len()

Auto Trait Implementations§

§

impl<T, E> Freeze for HeapType<T, E>
where T: Freeze,

§

impl<T, E> RefUnwindSafe for HeapType<T, E>

§

impl<T, E> Send for HeapType<T, E>
where T: Send, E: Send,

§

impl<T, E> Sync for HeapType<T, E>
where T: Sync, E: Sync,

§

impl<T, E> Unpin for HeapType<T, E>
where T: Unpin, E: Unpin,

§

impl<T, E> UnwindSafe for HeapType<T, E>
where T: UnwindSafe, E: 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, 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.