use core::{cmp::Ordering, ops::Bound};
use super::{Domain, Iterable};
impl Domain for i128 {
const DISCRETE: bool = true;
#[must_use]
#[allow(clippy::arithmetic_side_effects)]
fn predecessor(&self) -> Option<Self> {
match *self {
Self::MIN => None,
_ => Some(self - 1),
}
}
#[must_use]
#[allow(clippy::arithmetic_side_effects)]
fn successor(&self) -> Option<Self> {
match *self {
Self::MAX => None,
_ => Some(self + 1),
}
}
#[must_use]
fn minimum() -> Bound<Self> {
Bound::Included(Self::MIN)
}
#[must_use]
fn maximum() -> Bound<Self> {
Bound::Included(Self::MAX)
}
#[must_use]
#[allow(clippy::shadow_reuse, clippy::arithmetic_side_effects, clippy::as_conversions)]
fn shares_neighbour_with(&self, other: &Self) -> bool {
let (big, small) = match self.cmp(other) {
Ordering::Less => (other, self),
Ordering::Equal => return false,
Ordering::Greater => (self, other),
};
#[allow(clippy::cast_sign_loss)]
let big = (big ^ Self::MIN) as u128;
#[allow(clippy::cast_sign_loss)]
let small = (small ^ Self::MIN) as u128;
big - small == 2
}
}
impl Iterable for i128 {
type Output = Self;
#[must_use]
fn next(&self) -> Option<Self::Output> {
if *self == Self::MAX {
None
} else {
#[allow(clippy::arithmetic_side_effects)]
Some(*self + 1)
}
}
}
#[cfg(test)]
mod tests {
use crate::Domain;
#[test]
fn is_next_to() {
assert!(i128::MIN.is_next_to(&(i128::MIN + 1)));
assert!((i128::MIN + 1).is_next_to(&i128::MIN));
assert!(!i128::MIN.is_next_to(&(i128::MIN + 2)));
assert!(!i128::MIN.is_next_to(&i128::MAX));
assert!(!i128::MAX.is_next_to(&i128::MIN));
}
#[test]
fn shares_neighbour_with() {
assert!(!i128::MIN.shares_neighbour_with(&i128::MIN));
assert!(!42_i128.shares_neighbour_with(&45));
assert!(!45_i128.shares_neighbour_with(&42));
assert!(42_i128.shares_neighbour_with(&44));
assert!(44_i128.shares_neighbour_with(&42));
assert!(!i128::MIN.shares_neighbour_with(&i128::MAX));
assert!(!i128::MAX.shares_neighbour_with(&i128::MIN));
}
}