1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
use crate::natural::InnerNatural::{Large, Small};
use crate::natural::Natural;
use crate::platform::Limb;
use malachite_base::num::basic::integers::PrimitiveInt;
use malachite_base::num::conversion::traits::WrappingFrom;
macro_rules! impl_partial_eq_limb {
($u: ident) => {
impl PartialEq<$u> for Natural {
/// Determines whether a [`Natural`] is equal to a [`Limb`](crate#limbs).
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// # Examples
/// See [here](super::partial_eq_primitive_int#partial_eq).
fn eq(&self, other: &$u) -> bool {
match *self {
Natural(Small(x)) => x == *other,
Natural(Large(_)) => false,
}
}
}
impl PartialEq<Natural> for $u {
/// Determines whether a [`Limb`](crate#limbs) is equal to a [`Natural`].
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// # Examples
/// See [here](super::partial_eq_primitive_int#partial_eq).
#[inline]
fn eq(&self, other: &Natural) -> bool {
other == self
}
}
};
}
macro_rules! impl_partial_eq_smaller_than_limb {
($u: ident) => {
impl PartialEq<$u> for Natural {
/// Determines whether a [`Natural`] is equal to a value of an unsigned primitive
/// integer type that's smaller than a [`Limb`](crate#limbs).
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// # Examples
/// See [here](super::partial_eq_primitive_int#partial_eq).
#[allow(clippy::cmp_owned)]
#[inline]
fn eq(&self, other: &$u) -> bool {
*self == Limb::from(*other)
}
}
impl PartialEq<Natural> for $u {
/// Determines whether a value of an unsigned primitive integer type that's smaller than
/// a [`Limb`](crate#limbs) is equal to a [`Natural`].
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// # Examples
/// See [here](super::partial_eq_primitive_int#partial_eq).
#[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 {
/// Determines whether a value of an unsigned primitive integer type that's larger than
/// a [`Limb`](crate#limbs) is equal to a [`Natural`].
///
/// This implementation is general enough to also work for [`usize`], regardless of
/// whether it is equal in width to [`Limb`](crate#limbs).
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// # Examples
/// See [here](super::partial_eq_primitive_int#partial_eq).
#[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 {
/// Determines whether a [`Natural`] is equal to a value of an unsigned primitive
/// integer type that's larger than a [`Limb`](crate#limbs).
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// # Examples
/// See [here](super::partial_eq_primitive_int#partial_eq).
#[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 {
/// Determines whether a [`Natural`] is equal to a signed primitive integer.
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// # Examples
/// See [here](super::partial_eq_primitive_int#partial_eq).
fn eq(&self, other: &$t) -> bool {
*other >= 0 && *self == other.unsigned_abs()
}
}
impl PartialEq<Natural> for $t {
/// Determines whether a signed primitive integer is equal to a [`Natural`].
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// # Examples
/// See [here](super::partial_eq_primitive_int#partial_eq).
#[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 {
/// Determines whether a [`Natural`] is equal to a [`usize`].
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// See [here](super::partial_eq_primitive_int#partial_eq).
#[inline]
fn eq(&self, other: &usize) -> bool {
if usize::WIDTH == u32::WIDTH {
*self == u32::wrapping_from(*other)
} else {
assert_eq!(usize::WIDTH, u64::WIDTH);
*self == u64::wrapping_from(*other)
}
}
}