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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
use crate::natural::InnerNatural::{Large, Small};
use crate::natural::Natural;
use crate::platform::Limb;
use malachite_base::num::arithmetic::traits::ShrRound;
use malachite_base::num::basic::integers::PrimitiveInt;
use malachite_base::num::conversion::traits::WrappingFrom;
use malachite_base::num::logic::traits::SignificantBits;
use malachite_base::rounding_modes::RoundingMode;
use std::cmp::Ordering;
macro_rules! impl_partial_ord_limb {
($u: ident) => {
impl PartialOrd<$u> for Natural {
/// Compares a [`Natural`] to a [`Limb`](crate#limbs).
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// # Examples
/// See [here](super::partial_cmp_primitive_int#partial_cmp).
fn partial_cmp(&self, other: &$u) -> Option<Ordering> {
match *self {
Natural(Small(small)) => small.partial_cmp(other),
Natural(Large(_)) => Some(Ordering::Greater),
}
}
}
impl PartialOrd<Natural> for $u {
/// Compares a [`Limb`](crate#limbs) to a [`Natural`].
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// # Examples
/// See [here](super::partial_cmp_primitive_int#partial_cmp).
#[inline]
fn partial_cmp(&self, other: &Natural) -> Option<Ordering> {
other.partial_cmp(self).map(Ordering::reverse)
}
}
};
}
macro_rules! impl_partial_ord_smaller_than_limb {
($u: ident) => {
impl PartialOrd<$u> for Natural {
/// Compares a [`Natural`] 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_cmp_primitive_int#partial_cmp).
#[inline]
fn partial_cmp(&self, other: &$u) -> Option<Ordering> {
self.partial_cmp(&Limb::from(*other))
}
}
impl PartialOrd<Natural> for $u {
/// Compares a value of an unsigned primitive integer type that's smaller than a
/// [`Limb`](crate#limbs) to a [`Natural`].
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// # Examples
/// See [here](super::partial_cmp_primitive_int#partial_cmp).
#[inline]
fn partial_cmp(&self, other: &Natural) -> Option<Ordering> {
other.partial_cmp(self).map(Ordering::reverse)
}
}
};
}
macro_rules! impl_partial_ord_larger_than_limb_or_usize {
($u: ident) => {
impl PartialOrd<Natural> for $u {
/// Compares a value of an unsigned primitive integer type that's larger than a
/// [`Limb`](crate#limbs) 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_cmp_primitive_int#partial_cmp).
#[inline]
fn partial_cmp(&self, other: &Natural) -> Option<Ordering> {
other.partial_cmp(self).map(Ordering::reverse)
}
}
};
}
macro_rules! impl_partial_ord_larger_than_limb {
($u: ident) => {
impl_partial_ord_larger_than_limb_or_usize!($u);
impl PartialOrd<$u> for Natural {
/// Compares a [`Natural`] 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_cmp_primitive_int#partial_cmp).
#[inline]
fn partial_cmp(&self, other: &$u) -> Option<Ordering> {
let limb_count = other
.significant_bits()
.shr_round(Limb::LOG_WIDTH, RoundingMode::Ceiling);
let limb_count_cmp = self.limb_count().cmp(&limb_count);
if limb_count_cmp != Ordering::Equal || limb_count == 0 {
return Some(limb_count_cmp);
}
let width = Limb::WIDTH;
let mut i = limb_count << Limb::LOG_WIDTH;
let mut mask = $u::from(Limb::MAX) << (i - width);
for limb in self.limbs().rev() {
i -= width;
let limb_cmp = limb.cmp(&Limb::wrapping_from((other & mask) >> i));
if limb_cmp != Ordering::Equal {
return Some(limb_cmp);
}
mask >>= width;
}
Some(Ordering::Equal)
}
}
};
}
macro_rules! impl_signed {
($t: ident) => {
impl PartialOrd<$t> for Natural {
/// Compares a [`Natural`] to a signed primitive integer.
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// # Examples
/// See [here](super::partial_cmp_primitive_int#partial_cmp).
fn partial_cmp(&self, other: &$t) -> Option<Ordering> {
if *other < 0 {
Some(Ordering::Greater)
} else {
self.partial_cmp(&other.unsigned_abs())
}
}
}
impl PartialOrd<Natural> for $t {
/// Compares a signed primitive integer to a [`Natural`].
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// # Examples
/// See [here](super::partial_cmp_primitive_int#partial_cmp).
#[inline]
fn partial_cmp(&self, other: &Natural) -> Option<Ordering> {
other.partial_cmp(self).map(Ordering::reverse)
}
}
};
}
impl_partial_ord_smaller_than_limb!(u8);
impl_partial_ord_smaller_than_limb!(u16);
#[cfg(feature = "32_bit_limbs")]
impl_partial_ord_limb!(u32);
#[cfg(not(feature = "32_bit_limbs"))]
impl_partial_ord_smaller_than_limb!(u32);
#[cfg(feature = "32_bit_limbs")]
impl_partial_ord_larger_than_limb!(u64);
#[cfg(not(feature = "32_bit_limbs"))]
impl_partial_ord_limb!(u64);
impl_partial_ord_larger_than_limb!(u128);
impl_partial_ord_larger_than_limb_or_usize!(usize);
apply_to_signeds!(impl_signed);
impl PartialOrd<usize> for Natural {
/// Compares a [`Natural`] to a [`usize`].
///
/// # Worst-case complexity
/// Constant time and additional memory.
///
/// # Examples
/// See [here](super::partial_cmp_primitive_int#partial_cmp).
#[inline]
fn partial_cmp(&self, other: &usize) -> Option<Ordering> {
if usize::WIDTH == u32::WIDTH {
self.partial_cmp(&u32::wrapping_from(*other))
} else {
assert_eq!(usize::WIDTH, u64::WIDTH);
self.partial_cmp(&u64::wrapping_from(*other))
}
}
}