use core::cmp::Ordering;
use core::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use super::{BigUint, ExactNumber};
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default)]
pub struct BigInt {
negative: bool,
magnitude: BigUint,
}
impl BigInt {
fn from_parts(negative: bool, magnitude: BigUint) -> Self {
let negative = negative && !magnitude.limbs().is_empty();
Self {
negative,
magnitude,
}
}
fn add_signed(&mut self, negative: bool, magnitude: &BigUint) {
if self.negative == negative {
self.magnitude += magnitude;
return;
}
if self.magnitude >= *magnitude {
self.magnitude.subtract(magnitude, false);
} else {
self.magnitude.subtract(magnitude, true);
self.negative = negative;
}
self.negative = self.negative && !self.magnitude.limbs().is_empty();
}
#[must_use]
pub fn is_negative(&self) -> bool {
self.negative
}
#[must_use]
pub fn magnitude(&self) -> &BigUint {
&self.magnitude
}
}
impl From<BigUint> for BigInt {
fn from(magnitude: BigUint) -> Self {
Self::from_parts(false, magnitude)
}
}
impl From<u128> for BigInt {
fn from(value: u128) -> Self {
Self::from(BigUint::from(value))
}
}
impl From<i128> for BigInt {
fn from(value: i128) -> Self {
Self::from_parts(value < 0, BigUint::from(value.unsigned_abs()))
}
}
impl Neg for BigInt {
type Output = Self;
fn neg(self) -> Self {
Self::from_parts(!self.negative, self.magnitude)
}
}
impl Neg for &BigInt {
type Output = BigInt;
fn neg(self) -> BigInt {
-self.clone()
}
}
impl AddAssign<&BigInt> for BigInt {
fn add_assign(&mut self, other: &BigInt) {
self.add_signed(other.negative, &other.magnitude);
}
}
impl AddAssign for BigInt {
fn add_assign(&mut self, other: Self) {
*self += &other;
}
}
impl SubAssign<&BigInt> for BigInt {
fn sub_assign(&mut self, other: &BigInt) {
let negative = !other.negative && !other.magnitude.limbs().is_empty();
self.add_signed(negative, &other.magnitude);
}
}
impl SubAssign for BigInt {
fn sub_assign(&mut self, other: Self) {
*self -= &other;
}
}
impl Add for BigInt {
type Output = Self;
fn add(mut self, other: Self) -> Self {
self += &other;
self
}
}
impl Add<&BigInt> for BigInt {
type Output = Self;
fn add(mut self, other: &BigInt) -> Self {
self += other;
self
}
}
impl Add for &BigInt {
type Output = BigInt;
fn add(self, other: Self) -> BigInt {
self.clone() + other
}
}
impl Sub for BigInt {
type Output = Self;
fn sub(mut self, other: Self) -> Self {
self -= &other;
self
}
}
impl Sub<&BigInt> for BigInt {
type Output = Self;
fn sub(mut self, other: &BigInt) -> Self {
self -= other;
self
}
}
impl Sub for &BigInt {
type Output = BigInt;
fn sub(self, other: Self) -> BigInt {
self.clone() - other
}
}
impl Mul for &BigInt {
type Output = BigInt;
fn mul(self, other: Self) -> BigInt {
BigInt::from_parts(
self.negative != other.negative,
&self.magnitude * &other.magnitude,
)
}
}
impl Mul for BigInt {
type Output = Self;
fn mul(self, other: Self) -> Self {
&self * &other
}
}
impl Mul<&BigInt> for BigInt {
type Output = Self;
fn mul(self, other: &BigInt) -> Self {
&self * other
}
}
impl MulAssign<&BigInt> for BigInt {
fn mul_assign(&mut self, other: &BigInt) {
*self = &*self * other;
}
}
impl MulAssign for BigInt {
fn mul_assign(&mut self, other: Self) {
*self *= &other;
}
}
impl PartialOrd for BigInt {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for BigInt {
fn cmp(&self, other: &Self) -> Ordering {
match (self.negative, other.negative) {
(false, true) => Ordering::Greater,
(true, false) => Ordering::Less,
(false, false) => self.magnitude.cmp(&other.magnitude),
(true, true) => other.magnitude.cmp(&self.magnitude),
}
}
}
impl ExactNumber for BigInt {
fn zero() -> Self {
Self::from(BigUint::zero())
}
fn one() -> Self {
Self::from(BigUint::one())
}
fn abs_diff(&self, other: &Self) -> Self {
let difference = self - other;
Self::from(difference.magnitude)
}
}
#[cfg(test)]
mod tests {
use proptest::prelude::*;
use super::{BigInt, BigUint, ExactNumber};
fn value(b: &BigInt) -> i128 {
let magnitude = b
.magnitude()
.limbs()
.iter()
.rev()
.fold(0_i128, |v, &l| (v << 32) | i128::from(l));
if b.is_negative() {
-magnitude
} else {
magnitude
}
}
#[test]
fn zero_is_not_negative() {
let five = BigInt::from(5_i128);
assert!(!(five.clone() - five.clone()).is_negative());
assert!(!(-BigInt::zero()).is_negative());
assert!(!(BigInt::from(-5_i128) * BigInt::zero()).is_negative());
assert_eq!(-BigInt::zero(), BigInt::zero());
}
#[test]
fn exceeds_i128() {
let max = BigInt::from(i128::MAX);
let min = BigInt::from(i128::MIN);
assert_eq!(max.clone() - min.clone() - max.clone(), -min.clone());
assert!(min.clone() * max.clone() < min);
assert_eq!(
BigInt::from(i128::MIN).magnitude(),
&BigUint::from(1_u128 << 127)
);
}
proptest! {
#[test]
fn matches_i128(a: i64, b: i64) {
let (x, y) = (BigInt::from(i128::from(a)), BigInt::from(i128::from(b)));
let (a, b) = (i128::from(a), i128::from(b));
prop_assert_eq!(value(&(x.clone() + y.clone())), a + b);
prop_assert_eq!(value(&(x.clone() - y.clone())), a - b);
prop_assert_eq!(value(&(x.clone() * y.clone())), a * b);
prop_assert_eq!(value(&x.abs_diff(&y)), (a - b).abs());
prop_assert_eq!(value(&-x.clone()), -a);
}
#[test]
fn by_reference_matches_owned(a: i128, b: i128, sa: bool, sb: bool) {
let sign = |v: BigInt, negative: bool| if negative { -v } else { v };
let x = sign(BigInt::from(a) * BigInt::from(a), sa) + BigInt::from(b);
let y = sign(BigInt::from(b) * BigInt::from(b), sb) - BigInt::from(a);
let sum = x.clone() + y.clone();
let difference = x.clone() - y.clone();
let product = x.clone() * y.clone();
prop_assert_eq!(&x + &y, sum.clone());
prop_assert_eq!(x.clone() + &y, sum.clone());
prop_assert_eq!(&x - &y, difference.clone());
prop_assert_eq!(x.clone() - &y, difference.clone());
prop_assert_eq!(&x * &y, product.clone());
prop_assert_eq!(x.clone() * &y, product.clone());
prop_assert_eq!(-&x, -x.clone());
let mut z = x.clone();
z += &y;
prop_assert_eq!(&z, &sum);
z = x.clone();
z += y.clone();
prop_assert_eq!(&z, &sum);
z = x.clone();
z -= &y;
prop_assert_eq!(&z, &difference);
z = x.clone();
z -= y.clone();
prop_assert_eq!(&z, &difference);
z = x.clone();
z *= &y;
prop_assert_eq!(&z, &product);
z = x.clone();
z *= y.clone();
prop_assert_eq!(&z, &product);
prop_assert_eq!(&sum - &y, x.clone());
prop_assert_eq!(&difference + &y, x.clone());
let mut zero = x.clone();
zero -= &x;
prop_assert!(!zero.is_negative());
prop_assert_eq!(zero, BigInt::zero());
}
#[test]
fn order_matches_i128(a: i128, b: i128) {
prop_assert_eq!(BigInt::from(a).cmp(&BigInt::from(b)), a.cmp(&b));
}
}
}