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>
impl<T: Heap<E>, E> HeapType<T, E>
Sourcepub fn new(init: T) -> HeapType<T, E>where
T: Heap<E>,
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));
Sourcepub fn pop(&mut self) -> Option<E>
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
Sourcepub fn remove(&mut self, i: usize) -> E
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);
Sourcepub fn fix(&mut self, i: usize)
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);
Auto Trait Implementations§
impl<T, E> Freeze for HeapType<T, E>where
T: Freeze,
impl<T, E> RefUnwindSafe for HeapType<T, E>where
T: RefUnwindSafe,
E: RefUnwindSafe,
impl<T, E> Send for HeapType<T, E>
impl<T, E> Sync for HeapType<T, E>
impl<T, E> Unpin for HeapType<T, E>
impl<T, E> UnwindSafe for HeapType<T, E>where
T: UnwindSafe,
E: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more