Golang's heap written in Rust
fix
and remove
methodsfrom
toml
[dependencies]
go-heap-rs = "0.1"
```rust
struct MinHeap
impl
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 myvec = MinHeap(vec![2, 4, 3, 1]); let mut heap = HeapType::new(myvec); asserteq!(heap.peak(), Some(&1)); asserteq!(heap.pop(), Some(1)); heap.push(-1); assert_eq!(heap.pop(), Some(-1)); } ```