use std::cmp::Ordering;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum IntegerOrInfinity {
PositiveInfinity,
Integer(i64),
NegativeInfinity,
}
impl IntegerOrInfinity {
#[must_use]
pub fn clamp_finite(self, min: i64, max: i64) -> i64 {
match self {
Self::Integer(i) => i.clamp(min, max),
Self::PositiveInfinity => max,
Self::NegativeInfinity => min,
}
}
#[must_use]
pub const fn as_integer(self) -> Option<i64> {
match self {
Self::Integer(i) => Some(i),
_ => None,
}
}
}
impl From<f64> for IntegerOrInfinity {
fn from(number: f64) -> Self {
if number.is_nan() || number == 0.0 {
Self::Integer(0)
} else if number == f64::INFINITY {
Self::PositiveInfinity
} else if number == f64::NEG_INFINITY {
Self::NegativeInfinity
} else {
let integer = number.abs().floor().copysign(number) as i64;
Self::Integer(integer)
}
}
}
impl PartialEq<i64> for IntegerOrInfinity {
fn eq(&self, other: &i64) -> bool {
match self {
Self::Integer(i) => i == other,
_ => false,
}
}
}
impl PartialEq<IntegerOrInfinity> for i64 {
fn eq(&self, other: &IntegerOrInfinity) -> bool {
match other {
IntegerOrInfinity::Integer(i) => i == other,
_ => false,
}
}
}
impl PartialOrd<i64> for IntegerOrInfinity {
fn partial_cmp(&self, other: &i64) -> Option<Ordering> {
match self {
Self::PositiveInfinity => Some(Ordering::Greater),
Self::Integer(i) => i.partial_cmp(other),
Self::NegativeInfinity => Some(Ordering::Less),
}
}
}
impl PartialOrd<IntegerOrInfinity> for i64 {
fn partial_cmp(&self, other: &IntegerOrInfinity) -> Option<Ordering> {
match other {
IntegerOrInfinity::PositiveInfinity => Some(Ordering::Less),
IntegerOrInfinity::Integer(i) => self.partial_cmp(i),
IntegerOrInfinity::NegativeInfinity => Some(Ordering::Greater),
}
}
}