Sat, 22 Oct 2022 22:28:04 +0300
Convert iteration utilities to GATs
| 0 | 1 | use crate::types::Float; | 
| 2 | use std::cmp::{PartialOrd,Ord,Ordering,Ordering::*}; | |
| 3 | ||
| 4 | /// A container for floating point numbers for sorting NaN as a least element | |
| 5 | /// (to essentially ignore errorneous computations producing NaNs). | |
| 6 | #[derive(Debug, Clone, Copy)] | |
| 7 | pub struct NaNLeast<F : Float>(pub F); | |
| 8 | ||
| 9 | /// Compare floating point numbers ordering nan as the least element. | |
| 10 | ||
| 11 | impl<F : Float> Ord for NaNLeast<F> { | |
| 12 | #[inline] | |
| 13 | fn cmp(&self, NaNLeast(b) : &Self) -> Ordering { | |
| 14 | let NaNLeast(a) = self; | |
| 15 | match a.partial_cmp(b) { | |
| 16 | None => match (a.is_nan(), b.is_nan()) { | |
| 17 | (true, false) => Less, | |
| 18 | (false, true) => Greater, | |
| 19 | _ => Equal // The case (true, true) should not occur! | |
| 20 | } | |
| 21 | Some(order) => order | |
| 22 | } | |
| 23 | } | |
| 24 | } | |
| 25 | ||
| 26 | impl<F : Float> PartialEq for NaNLeast<F> { | |
| 27 | #[inline] | |
| 28 | fn eq(&self, other : &Self) -> bool { self.cmp(other)==Equal } | |
| 29 | } | |
| 30 | ||
| 31 | impl<F : Float> Eq for NaNLeast<F> { } | |
| 32 | ||
| 33 | impl<F : Float> PartialOrd for NaNLeast<F> { | |
| 34 | #[inline] | |
| 35 | fn partial_cmp(&self, other : &Self) -> Option<Ordering> { Some(self.cmp(other)) } | |
| 36 | } |