use std::cmp::Ordering;
use std::hash::{Hash, Hasher};
#[derive(Clone, Copy)]
pub struct OrderedFloat<T>(pub T);
impl<T: Float + Copy> OrderedFloat<T> {
#[inline]
pub fn into_inner(self) -> T {
self.0
}
}
impl<T: std::fmt::Debug> std::fmt::Debug for OrderedFloat<T> {
#[inline]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f)
}
}
impl<T: Float> Eq for OrderedFloat<T> {}
impl<T: Float> PartialEq<Self> for OrderedFloat<T> {
#[inline]
fn eq(&self, other: &Self) -> bool {
if self.0.is_nan() {
other.0.is_nan()
} else {
self.0 == other.0
}
}
}
impl<T: Float> PartialOrd<Self> for OrderedFloat<T> {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl<T: Float> Ord for OrderedFloat<T> {
#[inline]
fn cmp(&self, other: &Self) -> Ordering {
match self.0.partial_cmp(&other.0) {
Some(ord) => ord,
None => self.0.is_nan().cmp(&other.0.is_nan()),
}
}
}
impl<T: Float> Hash for OrderedFloat<T> {
fn hash<H: Hasher>(&self, state: &mut H) {
self.0.hash(state);
}
}
impl<T> From<T> for OrderedFloat<T> {
#[inline]
fn from(val: T) -> Self {
Self(val)
}
}
pub trait Float: PartialOrd + PartialEq + private::FloatImpl {
fn ord(self) -> OrderedFloat<Self>
where
Self: Sized;
}
impl Float for f32 {
#[inline]
fn ord(self) -> OrderedFloat<Self> {
OrderedFloat(self)
}
}
impl Float for f64 {
#[inline]
fn ord(self) -> OrderedFloat<Self> {
OrderedFloat(self)
}
}
mod private {
use super::{Hash as _, Hasher};
pub trait FloatImpl {
fn is_nan(&self) -> bool;
fn hash<H: Hasher>(&self, state: &mut H);
}
impl FloatImpl for f32 {
#[inline]
fn is_nan(&self) -> bool {
Self::is_nan(*self)
}
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
let bits = if self.is_nan() {
0x7fc00000
} else {
(self + 0.0).to_bits()
};
bits.hash(state);
}
}
impl FloatImpl for f64 {
#[inline]
fn is_nan(&self) -> bool {
Self::is_nan(*self)
}
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
let bits = if self.is_nan() {
0x7ff8000000000000
} else {
(self + 0.0).to_bits()
};
bits.hash(state);
}
}
}