use super::*;
pub trait Two: Sized
{
#[allow(non_snake_case)]
fn two() -> Self;
#[inline(always)]
fn is_two(&self) -> bool
where
Self: PartialEq<Self>,
{
self == &Self::two()
}
#[inline(always)]
fn is_non_two(&self) -> bool
where
Self: PartialEq<Self>,
{
!self.is_two()
}
}
impl<T> Two for T
where
T: NumericIdentity,
{
#[inline(always)]
fn two() -> Self { Self::ONE + Self::ONE }
}
pub trait Three: Sized
{
#[allow(non_snake_case)]
fn three() -> Self;
#[inline(always)]
fn is_three(&self) -> bool
where
Self: PartialEq<Self>,
{
self == &Self::three()
}
#[inline(always)]
fn is_non_three(&self) -> bool
where
Self: PartialEq<Self>,
{
!self.is_three()
}
}
impl<T> Three for T
where
T: One + Add<T, Output = T>,
{
#[inline(always)]
fn three() -> Self { Self::ONE + Self::ONE + Self::ONE }
}
pub trait OddOrEven: Sized
{
fn is_even(&self) -> bool;
#[inline(always)]
fn is_odd(&self) -> bool { !self.is_even() }
}
impl<T> OddOrEven for T
where
T: Integer,
{
fn is_even(&self) -> bool { (*self % Self::two()).is_zero() }
}
pub trait PositiveOrNegative: Zero + PartialOrd<Self> + Sized
{
fn is_positive_or_zero(&self) -> bool { self >= &Self::ZERO }
fn is_negative_or_zero(&self) -> bool { self <= &Self::ZERO }
fn is_strictly_positive(&self) -> bool { self > &Self::ZERO }
fn is_strictly_negative(&self) -> bool { self < &Self::ZERO }
}
impl<T: Zero + PartialOrd<T> + Sized> PositiveOrNegative for T {}
pub trait TakeHalf: Sized
{
fn take_half(self) -> Self;
}
impl<T> TakeHalf for T
where
T: NumericIdentity,
{
fn take_half(self) -> Self { self / Self::two() }
}
pub trait PartialOrdExtension: PartialOrd
{
#[inline(always)]
fn max_partial(self, other: Self) -> Self
where
Self: Sized,
{
if self >= other { self } else { other }
}
#[inline(always)]
fn min_partial(self, other: Self) -> Self
where
Self: Sized,
{
if self <= other { self } else { other }
}
#[inline(always)]
fn clamp_partial(self, min: Self, max: Self) -> Self
where
Self: Sized,
{
assert!(min <= max);
if self < min
{
min
}
else if self > max
{
max
}
else
{
self
}
}
}
impl<T: PartialOrd> PartialOrdExtension for T {}