Crates.io | go-heap-rs |
lib.rs | go-heap-rs |
version | 0.1.1 |
source | src |
created_at | 2021-07-24 06:22:10.411848 |
updated_at | 2021-08-05 15:34:06.074936 |
description | Golang's heap written in Rust |
homepage | |
repository | https://github.com/HirbodBehnam/go-heap-rs |
max_upload_size | |
id | 426652 |
size | 17,517 |
Golang's heap written in Rust
fix
and remove
methodsfrom
[dependencies]
go-heap-rs = "0.1"
struct MinHeap<T: Ord>(Vec<T>);
impl<T: Ord> Heap<T> for MinHeap<T> {
fn len(&self) -> usize {
self.0.len()
}
fn less(&self, i: usize, j: usize) -> bool {
self.0[i] < self.0[j]
}
fn swap(&mut self, i: usize, j: usize) {
self.0.swap(i, j);
}
fn push(&mut self, x: T) {
self.0.push(x);
}
fn pop(&mut self) -> T {
self.0.pop().expect("pop on an empty vec!")
}
fn peak(&self) -> Option<&T> {
self.0.get(0)
}
}
fn test() {
let my_vec = MinHeap(vec![2, 4, 3, 1]);
let mut heap = HeapType::new(my_vec);
assert_eq!(heap.peak(), Some(&1));
assert_eq!(heap.pop(), Some(1));
heap.push(-1);
assert_eq!(heap.pop(), Some(-1));
}