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 aren't usable out of the box. My attempts to wrap them have so far failed. However it seems that if I could wrap them then I could also implement Ord
in such a way that it would effectively make BinaryHeap
a min-heap.
Here's an example of a wrapper I tried:
#[derive(PartialEq, PartialOrd)]
struct MinNonNan(f64);
impl Eq for MinNonNan {}
impl Ord for MinNonNan {
fn cmp(&self, other: &MinNonNan) -> Ordering {
let ord = self.partial_cmp(other).unwrap();
match ord {
Ordering::Greater => Ordering::Less,
Ordering::Less => Ordering::Greater,
Ordering::Equal => ord
}
}
}
The problem is pop
returns the values as though it were a max-heap.
What exactly do I need to do to populate a BinaryHeap
with f64
values as a min-heap?
Working Example
Instead of writing your own
MinNonNan
, consider using the ordered-float crate + the std::cmp::Reverse type.Since you are
#[derive]
ingPartialOrd
, the.gt()
,.lt()
etc methods still compare normally, i.e.MinNonNan(42.0) < MinNonNan(47.0)
is still true. TheOrd
bound only restricts you to provide strictly-ordered types, it doesn't mean the implementation will use.cmp()
instead of<
/>
/<=
/>=
everywhere, nor the compiler will suddenly change those operators to use theOrd
implementation.If you want to flip the order, you need to reimplement
PartialOrd
as well.