aboutsummaryrefslogtreecommitdiff
path: root/src/bigint
diff options
context:
space:
mode:
Diffstat (limited to 'src/bigint')
-rw-r--r--src/bigint/addition.rs8
-rw-r--r--src/bigint/bits.rs16
-rw-r--r--src/bigint/convert.rs18
-rw-r--r--src/bigint/division.rs58
-rw-r--r--src/bigint/multiplication.rs20
-rw-r--r--src/bigint/power.rs6
-rw-r--r--src/bigint/shift.rs16
-rw-r--r--src/bigint/subtraction.rs8
8 files changed, 104 insertions, 46 deletions
diff --git a/src/bigint/addition.rs b/src/bigint/addition.rs
index b999f62..76aeb99 100644
--- a/src/bigint/addition.rs
+++ b/src/bigint/addition.rs
@@ -30,7 +30,7 @@ macro_rules! bigint_add {
};
}
-impl<'a, 'b> Add<&'b BigInt> for &'a BigInt {
+impl Add<&BigInt> for &BigInt {
type Output = BigInt;
#[inline]
@@ -46,7 +46,7 @@ impl<'a, 'b> Add<&'b BigInt> for &'a BigInt {
}
}
-impl<'a> Add<BigInt> for &'a BigInt {
+impl Add<BigInt> for &BigInt {
type Output = BigInt;
#[inline]
@@ -55,7 +55,7 @@ impl<'a> Add<BigInt> for &'a BigInt {
}
}
-impl<'a> Add<&'a BigInt> for BigInt {
+impl Add<&BigInt> for BigInt {
type Output = BigInt;
#[inline]
@@ -73,7 +73,7 @@ impl Add<BigInt> for BigInt {
}
}
-impl<'a> AddAssign<&'a BigInt> for BigInt {
+impl AddAssign<&BigInt> for BigInt {
#[inline]
fn add_assign(&mut self, other: &BigInt) {
let n = mem::replace(self, BigInt::zero());
diff --git a/src/bigint/bits.rs b/src/bigint/bits.rs
index 686def4..80f4e2c 100644
--- a/src/bigint/bits.rs
+++ b/src/bigint/bits.rs
@@ -108,7 +108,7 @@ forward_ref_val_binop!(impl BitAnd for BigInt, bitand);
// do not use forward_ref_ref_binop_commutative! for bitand so that we can
// clone as needed, avoiding over-allocation
-impl<'a, 'b> BitAnd<&'b BigInt> for &'a BigInt {
+impl BitAnd<&BigInt> for &BigInt {
type Output = BigInt;
#[inline]
@@ -130,7 +130,7 @@ impl<'a, 'b> BitAnd<&'b BigInt> for &'a BigInt {
}
}
-impl<'a> BitAnd<&'a BigInt> for BigInt {
+impl BitAnd<&BigInt> for BigInt {
type Output = BigInt;
#[inline]
@@ -142,7 +142,7 @@ impl<'a> BitAnd<&'a BigInt> for BigInt {
forward_val_assign!(impl BitAndAssign for BigInt, bitand_assign);
-impl<'a> BitAndAssign<&'a BigInt> for BigInt {
+impl BitAndAssign<&BigInt> for BigInt {
fn bitand_assign(&mut self, other: &BigInt) {
match (self.sign, other.sign) {
(NoSign, _) => {}
@@ -247,7 +247,7 @@ forward_ref_val_binop!(impl BitOr for BigInt, bitor);
// do not use forward_ref_ref_binop_commutative! for bitor so that we can
// clone as needed, avoiding over-allocation
-impl<'a, 'b> BitOr<&'b BigInt> for &'a BigInt {
+impl BitOr<&BigInt> for &BigInt {
type Output = BigInt;
#[inline]
@@ -270,7 +270,7 @@ impl<'a, 'b> BitOr<&'b BigInt> for &'a BigInt {
}
}
-impl<'a> BitOr<&'a BigInt> for BigInt {
+impl BitOr<&BigInt> for BigInt {
type Output = BigInt;
#[inline]
@@ -282,7 +282,7 @@ impl<'a> BitOr<&'a BigInt> for BigInt {
forward_val_assign!(impl BitOrAssign for BigInt, bitor_assign);
-impl<'a> BitOrAssign<&'a BigInt> for BigInt {
+impl BitOrAssign<&BigInt> for BigInt {
fn bitor_assign(&mut self, other: &BigInt) {
match (self.sign, other.sign) {
(_, NoSign) => {}
@@ -408,7 +408,7 @@ fn bitxor_neg_neg(a: &mut Vec<BigDigit>, b: &[BigDigit]) {
forward_all_binop_to_val_ref_commutative!(impl BitXor for BigInt, bitxor);
-impl<'a> BitXor<&'a BigInt> for BigInt {
+impl BitXor<&BigInt> for BigInt {
type Output = BigInt;
#[inline]
@@ -420,7 +420,7 @@ impl<'a> BitXor<&'a BigInt> for BigInt {
forward_val_assign!(impl BitXorAssign for BigInt, bitxor_assign);
-impl<'a> BitXorAssign<&'a BigInt> for BigInt {
+impl BitXorAssign<&BigInt> for BigInt {
fn bitxor_assign(&mut self, other: &BigInt) {
match (self.sign, other.sign) {
(_, NoSign) => {}
diff --git a/src/bigint/convert.rs b/src/bigint/convert.rs
index ff8e04e..c4f888b 100644
--- a/src/bigint/convert.rs
+++ b/src/bigint/convert.rs
@@ -10,7 +10,7 @@ use core::cmp::Ordering::{Equal, Greater, Less};
#[cfg(has_try_from)]
use core::convert::TryFrom;
use core::str::{self, FromStr};
-use num_traits::{FromPrimitive, Num, ToPrimitive, Zero};
+use num_traits::{FromPrimitive, Num, One, ToPrimitive, Zero};
impl FromStr for BigInt {
type Err = ParseBigIntError;
@@ -24,7 +24,7 @@ impl FromStr for BigInt {
impl Num for BigInt {
type FromStrRadixErr = ParseBigIntError;
- /// Creates and initializes a BigInt.
+ /// Creates and initializes a [`BigInt`].
#[inline]
fn from_str_radix(mut s: &str, radix: u32) -> Result<BigInt, ParseBigIntError> {
let sign = if s.starts_with('-') {
@@ -367,6 +367,16 @@ impl_to_bigint!(u128, FromPrimitive::from_u128);
impl_to_bigint!(f32, FromPrimitive::from_f32);
impl_to_bigint!(f64, FromPrimitive::from_f64);
+impl From<bool> for BigInt {
+ fn from(x: bool) -> Self {
+ if x {
+ One::one()
+ } else {
+ Zero::zero()
+ }
+ }
+}
+
#[inline]
pub(super) fn from_signed_bytes_be(digits: &[u8]) -> BigInt {
let sign = match digits.first() {
@@ -379,7 +389,7 @@ pub(super) fn from_signed_bytes_be(digits: &[u8]) -> BigInt {
// two's-complement the content to retrieve the magnitude
let mut digits = Vec::from(digits);
twos_complement_be(&mut digits);
- BigInt::from_biguint(sign, BigUint::from_bytes_be(&*digits))
+ BigInt::from_biguint(sign, BigUint::from_bytes_be(&digits))
} else {
BigInt::from_biguint(sign, BigUint::from_bytes_be(digits))
}
@@ -397,7 +407,7 @@ pub(super) fn from_signed_bytes_le(digits: &[u8]) -> BigInt {
// two's-complement the content to retrieve the magnitude
let mut digits = Vec::from(digits);
twos_complement_le(&mut digits);
- BigInt::from_biguint(sign, BigUint::from_bytes_le(&*digits))
+ BigInt::from_biguint(sign, BigUint::from_bytes_le(&digits))
} else {
BigInt::from_biguint(sign, BigUint::from_bytes_le(digits))
}
diff --git a/src/bigint/division.rs b/src/bigint/division.rs
index a702b8f..318d1fb 100644
--- a/src/bigint/division.rs
+++ b/src/bigint/division.rs
@@ -6,11 +6,11 @@ use crate::{IsizePromotion, UsizePromotion};
use core::ops::{Div, DivAssign, Rem, RemAssign};
use num_integer::Integer;
-use num_traits::{CheckedDiv, ToPrimitive, Zero};
+use num_traits::{CheckedDiv, CheckedEuclid, Euclid, Signed, ToPrimitive, Zero};
forward_all_binop_to_ref_ref!(impl Div for BigInt, div);
-impl<'a, 'b> Div<&'b BigInt> for &'a BigInt {
+impl Div<&BigInt> for &BigInt {
type Output = BigInt;
#[inline]
@@ -20,7 +20,7 @@ impl<'a, 'b> Div<&'b BigInt> for &'a BigInt {
}
}
-impl<'a> DivAssign<&'a BigInt> for BigInt {
+impl DivAssign<&BigInt> for BigInt {
#[inline]
fn div_assign(&mut self, other: &BigInt) {
*self = &*self / other;
@@ -235,7 +235,7 @@ impl Div<BigInt> for i128 {
forward_all_binop_to_ref_ref!(impl Rem for BigInt, rem);
-impl<'a, 'b> Rem<&'b BigInt> for &'a BigInt {
+impl Rem<&BigInt> for &BigInt {
type Output = BigInt;
#[inline]
@@ -251,7 +251,7 @@ impl<'a, 'b> Rem<&'b BigInt> for &'a BigInt {
}
}
-impl<'a> RemAssign<&'a BigInt> for BigInt {
+impl RemAssign<&BigInt> for BigInt {
#[inline]
fn rem_assign(&mut self, other: &BigInt) {
*self = &*self % other;
@@ -446,3 +446,51 @@ impl CheckedDiv for BigInt {
Some(self.div(v))
}
}
+
+impl CheckedEuclid for BigInt {
+ #[inline]
+ fn checked_div_euclid(&self, v: &BigInt) -> Option<BigInt> {
+ if v.is_zero() {
+ return None;
+ }
+ Some(self.div_euclid(v))
+ }
+
+ #[inline]
+ fn checked_rem_euclid(&self, v: &BigInt) -> Option<BigInt> {
+ if v.is_zero() {
+ return None;
+ }
+ Some(self.rem_euclid(v))
+ }
+}
+
+impl Euclid for BigInt {
+ #[inline]
+ fn div_euclid(&self, v: &BigInt) -> BigInt {
+ let (q, r) = self.div_rem(v);
+ if r.is_negative() {
+ if v.is_positive() {
+ q - 1
+ } else {
+ q + 1
+ }
+ } else {
+ q
+ }
+ }
+
+ #[inline]
+ fn rem_euclid(&self, v: &BigInt) -> BigInt {
+ let r = self % v;
+ if r.is_negative() {
+ if v.is_positive() {
+ r + v
+ } else {
+ r - v
+ }
+ } else {
+ r
+ }
+ }
+}
diff --git a/src/bigint/multiplication.rs b/src/bigint/multiplication.rs
index a2d9708..82e64c2 100644
--- a/src/bigint/multiplication.rs
+++ b/src/bigint/multiplication.rs
@@ -22,8 +22,8 @@ impl Mul<Sign> for Sign {
}
macro_rules! impl_mul {
- ($(impl<$($a:lifetime),*> Mul<$Other:ty> for $Self:ty;)*) => {$(
- impl<$($a),*> Mul<$Other> for $Self {
+ ($(impl Mul<$Other:ty> for $Self:ty;)*) => {$(
+ impl Mul<$Other> for $Self {
type Output = BigInt;
#[inline]
@@ -37,15 +37,15 @@ macro_rules! impl_mul {
)*}
}
impl_mul! {
- impl<> Mul<BigInt> for BigInt;
- impl<'b> Mul<&'b BigInt> for BigInt;
- impl<'a> Mul<BigInt> for &'a BigInt;
- impl<'a, 'b> Mul<&'b BigInt> for &'a BigInt;
+ impl Mul<BigInt> for BigInt;
+ impl Mul<BigInt> for &BigInt;
+ impl Mul<&BigInt> for BigInt;
+ impl Mul<&BigInt> for &BigInt;
}
macro_rules! impl_mul_assign {
- ($(impl<$($a:lifetime),*> MulAssign<$Other:ty> for BigInt;)*) => {$(
- impl<$($a),*> MulAssign<$Other> for BigInt {
+ ($(impl MulAssign<$Other:ty> for BigInt;)*) => {$(
+ impl MulAssign<$Other> for BigInt {
#[inline]
fn mul_assign(&mut self, other: $Other) {
// automatically match value/ref
@@ -61,8 +61,8 @@ macro_rules! impl_mul_assign {
)*}
}
impl_mul_assign! {
- impl<> MulAssign<BigInt> for BigInt;
- impl<'a> MulAssign<&'a BigInt> for BigInt;
+ impl MulAssign<BigInt> for BigInt;
+ impl MulAssign<&BigInt> for BigInt;
}
promote_all_scalars!(impl Mul for BigInt, mul);
diff --git a/src/bigint/power.rs b/src/bigint/power.rs
index a4dd806..4b41f4f 100644
--- a/src/bigint/power.rs
+++ b/src/bigint/power.rs
@@ -31,7 +31,7 @@ macro_rules! pow_impl {
}
}
- impl<'b> Pow<&'b $T> for BigInt {
+ impl Pow<&$T> for BigInt {
type Output = BigInt;
#[inline]
@@ -40,7 +40,7 @@ macro_rules! pow_impl {
}
}
- impl<'a> Pow<$T> for &'a BigInt {
+ impl Pow<$T> for &BigInt {
type Output = BigInt;
#[inline]
@@ -49,7 +49,7 @@ macro_rules! pow_impl {
}
}
- impl<'a, 'b> Pow<&'b $T> for &'a BigInt {
+ impl Pow<&$T> for &BigInt {
type Output = BigInt;
#[inline]
diff --git a/src/bigint/shift.rs b/src/bigint/shift.rs
index b816e12..22bb744 100644
--- a/src/bigint/shift.rs
+++ b/src/bigint/shift.rs
@@ -6,25 +6,25 @@ use num_traits::{PrimInt, Signed, Zero};
macro_rules! impl_shift {
(@ref $Shx:ident :: $shx:ident, $ShxAssign:ident :: $shx_assign:ident, $rhs:ty) => {
- impl<'b> $Shx<&'b $rhs> for BigInt {
+ impl $Shx<&$rhs> for BigInt {
type Output = BigInt;
#[inline]
- fn $shx(self, rhs: &'b $rhs) -> BigInt {
+ fn $shx(self, rhs: &$rhs) -> BigInt {
$Shx::$shx(self, *rhs)
}
}
- impl<'a, 'b> $Shx<&'b $rhs> for &'a BigInt {
+ impl $Shx<&$rhs> for &BigInt {
type Output = BigInt;
#[inline]
- fn $shx(self, rhs: &'b $rhs) -> BigInt {
+ fn $shx(self, rhs: &$rhs) -> BigInt {
$Shx::$shx(self, *rhs)
}
}
- impl<'b> $ShxAssign<&'b $rhs> for BigInt {
+ impl $ShxAssign<&$rhs> for BigInt {
#[inline]
- fn $shx_assign(&mut self, rhs: &'b $rhs) {
+ fn $shx_assign(&mut self, rhs: &$rhs) {
$ShxAssign::$shx_assign(self, *rhs);
}
}
@@ -38,7 +38,7 @@ macro_rules! impl_shift {
BigInt::from_biguint(self.sign, self.data << rhs)
}
}
- impl<'a> Shl<$rhs> for &'a BigInt {
+ impl Shl<$rhs> for &BigInt {
type Output = BigInt;
#[inline]
@@ -65,7 +65,7 @@ macro_rules! impl_shift {
BigInt::from_biguint(self.sign, data)
}
}
- impl<'a> Shr<$rhs> for &'a BigInt {
+ impl Shr<$rhs> for &BigInt {
type Output = BigInt;
#[inline]
diff --git a/src/bigint/subtraction.rs b/src/bigint/subtraction.rs
index a12a844..548f314 100644
--- a/src/bigint/subtraction.rs
+++ b/src/bigint/subtraction.rs
@@ -29,7 +29,7 @@ macro_rules! bigint_sub {
};
}
-impl<'a, 'b> Sub<&'b BigInt> for &'a BigInt {
+impl Sub<&BigInt> for &BigInt {
type Output = BigInt;
#[inline]
@@ -45,7 +45,7 @@ impl<'a, 'b> Sub<&'b BigInt> for &'a BigInt {
}
}
-impl<'a> Sub<BigInt> for &'a BigInt {
+impl Sub<BigInt> for &BigInt {
type Output = BigInt;
#[inline]
@@ -54,7 +54,7 @@ impl<'a> Sub<BigInt> for &'a BigInt {
}
}
-impl<'a> Sub<&'a BigInt> for BigInt {
+impl Sub<&BigInt> for BigInt {
type Output = BigInt;
#[inline]
@@ -72,7 +72,7 @@ impl Sub<BigInt> for BigInt {
}
}
-impl<'a> SubAssign<&'a BigInt> for BigInt {
+impl SubAssign<&BigInt> for BigInt {
#[inline]
fn sub_assign(&mut self, other: &BigInt) {
let n = mem::replace(self, BigInt::zero());