use std::cmp::Ordering; /// `MinScored` holds a score `K` and a scored object `T` in /// a pair for use with a `BinaryHeap`. /// /// `MinScored` compares in reverse order by the score, so that we can /// use `BinaryHeap` as a min-heap to extract the score-value pair with the /// least score. /// /// **Note:** `MinScored` implements a total order (`Ord`), so that it is /// possible to use float types as scores. #[derive(Copy, Clone, Debug)] pub struct MinScored(pub K, pub T); impl PartialEq for MinScored { #[inline] fn eq(&self, other: &MinScored) -> bool { self.cmp(other) == Ordering::Equal } } impl Eq for MinScored {} impl PartialOrd for MinScored { #[inline] fn partial_cmp(&self, other: &MinScored) -> Option { Some(self.cmp(other)) } } impl Ord for MinScored { #[inline] fn cmp(&self, other: &MinScored) -> Ordering { let a = &self.0; let b = &other.0; if a == b { Ordering::Equal } else if a < b { Ordering::Greater } else if a > b { Ordering::Less } else if a.ne(a) && b.ne(b) { // these are the NaN cases Ordering::Equal } else if a.ne(a) { // Order NaN less, so that it is last in the MinScore order Ordering::Less } else { Ordering::Greater } } }