I want to fill the binary heap with floating point numbers - more precisely, I would like to implement a minimal heap.
It seems that floats do not support Ord and therefore cannot be used out of the box. My attempts to wrap them have so far been unsuccessful. However, it seems that if I could wrap them, I could also implement Ord so that it effectively turns BinaryHeap into a mini-heap.
Here is 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 that pop returns values as if it were the maximum heap.
What exactly do I need to do to populate the BinaryHeap f64 values as the minimum heap?
rust
maxcountryman
source share