use crate::natural::InnerNatural::{Large, Small};
use crate::natural::Natural;
use crate::platform::Limb;
use malachite_base::num::basic::integers::{PrimitiveInt, USIZE_IS_U32};
use malachite_base::num::conversion::traits::WrappingFrom;
macro_rules! impl_partial_eq_limb {
($u: ident) => {
impl PartialEq<$u> for Natural {
fn eq(&self, other: &$u) -> bool {
match self {
Natural(Small(x)) => *x == *other,
Natural(Large(_)) => false,
}
}
}
impl PartialEq<Natural> for $u {
#[inline]
fn eq(&self, other: &Natural) -> bool {
other == self
}
}
};
}
macro_rules! impl_partial_eq_smaller_than_limb {
($u: ident) => {
impl PartialEq<$u> for Natural {
#[allow(clippy::cmp_owned)]
#[inline]
fn eq(&self, other: &$u) -> bool {
*self == Limb::from(*other)
}
}
impl PartialEq<Natural> for $u {
#[allow(clippy::cmp_owned)]
#[inline]
fn eq(&self, other: &Natural) -> bool {
Limb::from(*self) == *other
}
}
};
}
macro_rules! impl_partial_eq_larger_than_limb_or_usize {
($u: ident) => {
impl PartialEq<Natural> for $u {
#[inline]
fn eq(&self, other: &Natural) -> bool {
other == self
}
}
};
}
macro_rules! impl_partial_eq_larger_than_limb {
($u: ident) => {
impl_partial_eq_larger_than_limb_or_usize!($u);
impl PartialEq<$u> for Natural {
#[inline]
fn eq(&self, other: &$u) -> bool {
let mut other = *other;
for limb in self.limbs() {
if other == 0 || limb != Limb::wrapping_from(other) {
return false;
}
other >>= Limb::WIDTH;
}
other == 0
}
}
};
}
macro_rules! impl_signed {
($t: ident) => {
impl PartialEq<$t> for Natural {
fn eq(&self, other: &$t) -> bool {
*other >= 0 && *self == other.unsigned_abs()
}
}
impl PartialEq<Natural> for $t {
#[inline]
fn eq(&self, other: &Natural) -> bool {
other == self
}
}
};
}
impl_partial_eq_smaller_than_limb!(u8);
impl_partial_eq_smaller_than_limb!(u16);
#[cfg(feature = "32_bit_limbs")]
impl_partial_eq_limb!(u32);
#[cfg(not(feature = "32_bit_limbs"))]
impl_partial_eq_smaller_than_limb!(u32);
#[cfg(feature = "32_bit_limbs")]
impl_partial_eq_larger_than_limb!(u64);
#[cfg(not(feature = "32_bit_limbs"))]
impl_partial_eq_limb!(u64);
impl_partial_eq_larger_than_limb!(u128);
impl_partial_eq_larger_than_limb_or_usize!(usize);
apply_to_signeds!(impl_signed);
impl PartialEq<usize> for Natural {
#[inline]
fn eq(&self, other: &usize) -> bool {
if USIZE_IS_U32 {
*self == u32::wrapping_from(*other)
} else {
assert_eq!(usize::WIDTH, u64::WIDTH);
*self == u64::wrapping_from(*other)
}
}
}