I want to populate a binary heap with floats--more specifically, I\'d like to implement a min-heap.
It seems that floats do not support Ord and thus are
use std::cmp::Ordering;
use std::collections::BinaryHeap;
#[derive(PartialEq)]
struct MinFloat(f64);
impl Eq for MinFloat {}
impl PartialOrd for MinFloat {
fn partial_cmp(&self, other: &Self) -> Option {
other.0.partial_cmp(&self.0)
}
}
impl Ord for MinFloat {
fn cmp(&self, other: &MinFloat) -> Ordering {
self.partial_cmp(other).unwrap()
}
}
fn main() {
let mut minheap = BinaryHeap::new();
minheap.push(MinFloat(2.0));
minheap.push(MinFloat(1.0));
minheap.push(MinFloat(42.0));
if let Some(MinFloat(root)) = minheap.pop() {
println!("{:?}", root);
}
}