bigint: inlining small functions
This commit is contained in:
parent
b247d17629
commit
17c8f8bd0c
@ -58,19 +58,19 @@ pub mod BigDigit {
|
||||
static hi_mask: uint = (-1 as uint) << bits;
|
||||
static lo_mask: uint = (-1 as uint) >> bits;
|
||||
|
||||
|
||||
#[inline]
|
||||
fn get_hi(n: uint) -> BigDigit { (n >> bits) as BigDigit }
|
||||
|
||||
#[inline]
|
||||
fn get_lo(n: uint) -> BigDigit { (n & lo_mask) as BigDigit }
|
||||
|
||||
/// Split one machine sized unsigned integer into two BigDigits.
|
||||
|
||||
#[inline]
|
||||
pub fn from_uint(n: uint) -> (BigDigit, BigDigit) {
|
||||
(get_hi(n), get_lo(n))
|
||||
}
|
||||
|
||||
/// Join two BigDigits into one machine sized unsigned integer
|
||||
|
||||
#[inline]
|
||||
pub fn to_uint(hi: BigDigit, lo: BigDigit) -> uint {
|
||||
(lo as uint) | ((hi as uint) << bits)
|
||||
}
|
||||
@ -88,24 +88,26 @@ pub struct BigUint {
|
||||
}
|
||||
|
||||
impl Eq for BigUint {
|
||||
#[inline]
|
||||
fn eq(&self, other: &BigUint) -> bool { self.equals(other) }
|
||||
}
|
||||
|
||||
impl TotalEq for BigUint {
|
||||
#[inline]
|
||||
fn equals(&self, other: &BigUint) -> bool {
|
||||
match self.cmp(other) { Equal => true, _ => false }
|
||||
}
|
||||
}
|
||||
|
||||
impl Ord for BigUint {
|
||||
|
||||
#[inline]
|
||||
fn lt(&self, other: &BigUint) -> bool {
|
||||
match self.cmp(other) { Less => true, _ => false}
|
||||
}
|
||||
}
|
||||
|
||||
impl TotalOrd for BigUint {
|
||||
|
||||
#[inline]
|
||||
fn cmp(&self, other: &BigUint) -> Ordering {
|
||||
let (s_len, o_len) = (self.data.len(), other.data.len());
|
||||
if s_len < o_len { return Less; }
|
||||
@ -120,12 +122,12 @@ fn cmp(&self, other: &BigUint) -> Ordering {
|
||||
}
|
||||
|
||||
impl ToStr for BigUint {
|
||||
|
||||
#[inline]
|
||||
fn to_str(&self) -> ~str { self.to_str_radix(10) }
|
||||
}
|
||||
|
||||
impl FromStr for BigUint {
|
||||
|
||||
#[inline]
|
||||
fn from_str(s: &str) -> Option<BigUint> {
|
||||
FromStrRadix::from_str_radix(s, 10)
|
||||
}
|
||||
@ -134,17 +136,17 @@ fn from_str(s: &str) -> Option<BigUint> {
|
||||
impl Num for BigUint {}
|
||||
|
||||
impl Orderable for BigUint {
|
||||
|
||||
#[inline]
|
||||
fn min(&self, other: &BigUint) -> BigUint {
|
||||
if self < other { self.clone() } else { other.clone() }
|
||||
}
|
||||
|
||||
|
||||
#[inline]
|
||||
fn max(&self, other: &BigUint) -> BigUint {
|
||||
if self > other { self.clone() } else { other.clone() }
|
||||
}
|
||||
|
||||
|
||||
#[inline]
|
||||
fn clamp(&self, mn: &BigUint, mx: &BigUint) -> BigUint {
|
||||
if self > mx { mx.clone() } else
|
||||
if self < mn { mn.clone() } else { self.clone() }
|
||||
@ -152,7 +154,7 @@ fn clamp(&self, mn: &BigUint, mx: &BigUint) -> BigUint {
|
||||
}
|
||||
|
||||
impl Shl<uint, BigUint> for BigUint {
|
||||
|
||||
#[inline]
|
||||
fn shl(&self, rhs: &uint) -> BigUint {
|
||||
let n_unit = *rhs / BigDigit::bits;
|
||||
let n_bits = *rhs % BigDigit::bits;
|
||||
@ -161,7 +163,7 @@ fn shl(&self, rhs: &uint) -> BigUint {
|
||||
}
|
||||
|
||||
impl Shr<uint, BigUint> for BigUint {
|
||||
|
||||
#[inline]
|
||||
fn shr(&self, rhs: &uint) -> BigUint {
|
||||
let n_unit = *rhs / BigDigit::bits;
|
||||
let n_bits = *rhs % BigDigit::bits;
|
||||
@ -170,22 +172,21 @@ fn shr(&self, rhs: &uint) -> BigUint {
|
||||
}
|
||||
|
||||
impl Zero for BigUint {
|
||||
|
||||
#[inline]
|
||||
fn zero() -> BigUint { BigUint::new(~[]) }
|
||||
|
||||
|
||||
#[inline]
|
||||
fn is_zero(&self) -> bool { self.data.is_empty() }
|
||||
}
|
||||
|
||||
impl One for BigUint {
|
||||
|
||||
#[inline]
|
||||
fn one() -> BigUint { BigUint::new(~[1]) }
|
||||
}
|
||||
|
||||
impl Unsigned for BigUint {}
|
||||
|
||||
impl Add<BigUint, BigUint> for BigUint {
|
||||
|
||||
fn add(&self, other: &BigUint) -> BigUint {
|
||||
let new_len = num::max(self.data.len(), other.data.len());
|
||||
|
||||
@ -205,7 +206,6 @@ fn add(&self, other: &BigUint) -> BigUint {
|
||||
}
|
||||
|
||||
impl Sub<BigUint, BigUint> for BigUint {
|
||||
|
||||
fn sub(&self, other: &BigUint) -> BigUint {
|
||||
let new_len = num::max(self.data.len(), other.data.len());
|
||||
|
||||
@ -278,14 +278,14 @@ fn mul_digit(a: &BigUint, n: BigDigit) -> BigUint {
|
||||
return BigUint::new(prod);
|
||||
}
|
||||
|
||||
|
||||
#[inline]
|
||||
fn cut_at(a: &BigUint, n: uint) -> (BigUint, BigUint) {
|
||||
let mid = num::min(a.data.len(), n);
|
||||
return (BigUint::from_slice(a.data.slice(mid, a.data.len())),
|
||||
BigUint::from_slice(a.data.slice(0, mid)));
|
||||
}
|
||||
|
||||
|
||||
#[inline]
|
||||
fn sub_sign(a: BigUint, b: BigUint) -> (Ordering, BigUint) {
|
||||
match a.cmp(&b) {
|
||||
Less => (Less, b - a),
|
||||
@ -297,7 +297,7 @@ fn sub_sign(a: BigUint, b: BigUint) -> (Ordering, BigUint) {
|
||||
}
|
||||
|
||||
impl Div<BigUint, BigUint> for BigUint {
|
||||
|
||||
#[inline]
|
||||
fn div(&self, other: &BigUint) -> BigUint {
|
||||
let (q, _) = self.div_rem(other);
|
||||
return q;
|
||||
@ -305,7 +305,7 @@ fn div(&self, other: &BigUint) -> BigUint {
|
||||
}
|
||||
|
||||
impl Rem<BigUint, BigUint> for BigUint {
|
||||
|
||||
#[inline]
|
||||
fn rem(&self, other: &BigUint) -> BigUint {
|
||||
let (_, r) = self.div_rem(other);
|
||||
return r;
|
||||
@ -313,29 +313,28 @@ fn rem(&self, other: &BigUint) -> BigUint {
|
||||
}
|
||||
|
||||
impl Neg<BigUint> for BigUint {
|
||||
|
||||
#[inline]
|
||||
fn neg(&self) -> BigUint { fail!() }
|
||||
}
|
||||
|
||||
impl Integer for BigUint {
|
||||
|
||||
#[inline]
|
||||
fn div_rem(&self, other: &BigUint) -> (BigUint, BigUint) {
|
||||
self.div_mod_floor(other)
|
||||
}
|
||||
|
||||
|
||||
#[inline]
|
||||
fn div_floor(&self, other: &BigUint) -> BigUint {
|
||||
let (d, _) = self.div_mod_floor(other);
|
||||
return d;
|
||||
}
|
||||
|
||||
|
||||
#[inline]
|
||||
fn mod_floor(&self, other: &BigUint) -> BigUint {
|
||||
let (_, m) = self.div_mod_floor(other);
|
||||
return m;
|
||||
}
|
||||
|
||||
|
||||
fn div_mod_floor(&self, other: &BigUint) -> (BigUint, BigUint) {
|
||||
if other.is_zero() { fail!() }
|
||||
if self.is_zero() { return (Zero::zero(), Zero::zero()); }
|
||||
@ -423,7 +422,7 @@ fn div_estimate(a: &BigUint, b: &BigUint, n: uint)
|
||||
*
|
||||
* The result is always positive
|
||||
*/
|
||||
|
||||
#[inline]
|
||||
fn gcd(&self, other: &BigUint) -> BigUint {
|
||||
// Use Euclid's algorithm
|
||||
let mut m = (*self).clone();
|
||||
@ -439,15 +438,15 @@ fn gcd(&self, other: &BigUint) -> BigUint {
|
||||
/**
|
||||
* Calculates the Lowest Common Multiple (LCM) of the number and `other`
|
||||
*/
|
||||
|
||||
#[inline]
|
||||
fn lcm(&self, other: &BigUint) -> BigUint { ((*self * *other) / self.gcd(other)) }
|
||||
|
||||
/// Returns `true` if the number can be divided by `other` without leaving a remainder
|
||||
|
||||
#[inline]
|
||||
fn is_multiple_of(&self, other: &BigUint) -> bool { (*self % *other).is_zero() }
|
||||
|
||||
/// Returns `true` if the number is divisible by `2`
|
||||
|
||||
#[inline]
|
||||
fn is_even(&self) -> bool {
|
||||
// Considering only the last digit.
|
||||
if self.data.is_empty() {
|
||||
@ -458,24 +457,23 @@ fn is_even(&self) -> bool {
|
||||
}
|
||||
|
||||
/// Returns `true` if the number is not divisible by `2`
|
||||
|
||||
#[inline]
|
||||
fn is_odd(&self) -> bool { !self.is_even() }
|
||||
}
|
||||
|
||||
impl IntConvertible for BigUint {
|
||||
|
||||
#[inline]
|
||||
fn to_int(&self) -> int {
|
||||
num::min(self.to_uint(), int::max_value as uint) as int
|
||||
}
|
||||
|
||||
|
||||
#[inline]
|
||||
fn from_int(n: int) -> BigUint {
|
||||
if (n < 0) { Zero::zero() } else { BigUint::from_uint(n as uint) }
|
||||
}
|
||||
}
|
||||
|
||||
impl ToStrRadix for BigUint {
|
||||
|
||||
fn to_str_radix(&self, radix: uint) -> ~str {
|
||||
assert!(1 < radix && radix <= 16);
|
||||
let (base, max_len) = get_radix_base(radix);
|
||||
@ -484,7 +482,6 @@ fn to_str_radix(&self, radix: uint) -> ~str {
|
||||
}
|
||||
return fill_concat(convert_base((*self).clone(), base), radix, max_len);
|
||||
|
||||
|
||||
fn convert_base(n: BigUint, base: uint) -> ~[BigDigit] {
|
||||
let divider = BigUint::from_uint(base);
|
||||
let mut result = ~[];
|
||||
@ -500,7 +497,6 @@ fn convert_base(n: BigUint, base: uint) -> ~[BigDigit] {
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
fn fill_concat(v: &[BigDigit], radix: uint, l: uint) -> ~str {
|
||||
if v.is_empty() { return ~"0" }
|
||||
let mut s = str::with_capacity(v.len() * l);
|
||||
@ -516,7 +512,7 @@ fn fill_concat(v: &[BigDigit], radix: uint, l: uint) -> ~str {
|
||||
|
||||
impl FromStrRadix for BigUint {
|
||||
/// Creates and initializes an BigUint.
|
||||
|
||||
#[inline]
|
||||
fn from_str_radix(s: &str, radix: uint)
|
||||
-> Option<BigUint> {
|
||||
BigUint::parse_bytes(s.as_bytes(), radix)
|
||||
@ -525,7 +521,7 @@ fn from_str_radix(s: &str, radix: uint)
|
||||
|
||||
impl BigUint {
|
||||
/// Creates and initializes an BigUint.
|
||||
|
||||
#[inline]
|
||||
pub fn new(v: ~[BigDigit]) -> BigUint {
|
||||
// omit trailing zeros
|
||||
let new_len = v.rposition(|n| *n != 0).map_move_default(0, |p| p + 1);
|
||||
@ -537,7 +533,7 @@ pub fn new(v: ~[BigDigit]) -> BigUint {
|
||||
}
|
||||
|
||||
/// Creates and initializes an BigUint.
|
||||
|
||||
#[inline]
|
||||
pub fn from_uint(n: uint) -> BigUint {
|
||||
match BigDigit::from_uint(n) {
|
||||
(0, 0) => Zero::zero(),
|
||||
@ -547,13 +543,12 @@ pub fn from_uint(n: uint) -> BigUint {
|
||||
}
|
||||
|
||||
/// Creates and initializes an BigUint.
|
||||
|
||||
#[inline]
|
||||
pub fn from_slice(slice: &[BigDigit]) -> BigUint {
|
||||
return BigUint::new(slice.to_owned());
|
||||
}
|
||||
|
||||
/// Creates and initializes an BigUint.
|
||||
|
||||
pub fn parse_bytes(buf: &[u8], radix: uint)
|
||||
-> Option<BigUint> {
|
||||
let (base, unit_len) = get_radix_base(radix);
|
||||
@ -583,6 +578,7 @@ pub fn parse_bytes(buf: &[u8], radix: uint)
|
||||
|
||||
/// Converts this big integer into a uint, returning the uint::max_value if
|
||||
/// it's too large to fit in a uint.
|
||||
#[inline]
|
||||
pub fn to_uint(&self) -> uint {
|
||||
match self.data.len() {
|
||||
0 => 0,
|
||||
@ -592,7 +588,7 @@ pub fn to_uint(&self) -> uint {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[inline]
|
||||
fn shl_unit(&self, n_unit: uint) -> BigUint {
|
||||
if n_unit == 0 || self.is_zero() { return (*self).clone(); }
|
||||
|
||||
@ -600,7 +596,7 @@ fn shl_unit(&self, n_unit: uint) -> BigUint {
|
||||
+ self.data);
|
||||
}
|
||||
|
||||
|
||||
#[inline]
|
||||
fn shl_bits(&self, n_bits: uint) -> BigUint {
|
||||
if n_bits == 0 || self.is_zero() { return (*self).clone(); }
|
||||
|
||||
@ -616,7 +612,7 @@ fn shl_bits(&self, n_bits: uint) -> BigUint {
|
||||
return BigUint::new(shifted);
|
||||
}
|
||||
|
||||
|
||||
#[inline]
|
||||
fn shr_unit(&self, n_unit: uint) -> BigUint {
|
||||
if n_unit == 0 { return (*self).clone(); }
|
||||
if self.data.len() < n_unit { return Zero::zero(); }
|
||||
@ -625,7 +621,7 @@ fn shr_unit(&self, n_unit: uint) -> BigUint {
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
#[inline]
|
||||
fn shr_bits(&self, n_bits: uint) -> BigUint {
|
||||
if n_bits == 0 || self.data.is_empty() { return (*self).clone(); }
|
||||
|
||||
@ -640,6 +636,7 @@ fn shr_bits(&self, n_bits: uint) -> BigUint {
|
||||
}
|
||||
|
||||
#[cfg(target_word_size = "64")]
|
||||
#[inline]
|
||||
fn get_radix_base(radix: uint) -> (uint, uint) {
|
||||
assert!(1 < radix && radix <= 16);
|
||||
match radix {
|
||||
@ -663,6 +660,7 @@ fn get_radix_base(radix: uint) -> (uint, uint) {
|
||||
}
|
||||
|
||||
#[cfg(target_word_size = "32")]
|
||||
#[inline]
|
||||
fn get_radix_base(radix: uint) -> (uint, uint) {
|
||||
assert!(1 < radix && radix <= 16);
|
||||
match radix {
|
||||
@ -690,19 +688,18 @@ fn get_radix_base(radix: uint) -> (uint, uint) {
|
||||
pub enum Sign { Minus, Zero, Plus }
|
||||
|
||||
impl Ord for Sign {
|
||||
|
||||
#[inline]
|
||||
fn lt(&self, other: &Sign) -> bool {
|
||||
match self.cmp(other) { Less => true, _ => false}
|
||||
}
|
||||
}
|
||||
|
||||
impl TotalEq for Sign {
|
||||
fn equals(&self, other: &Sign) -> bool {
|
||||
*self == *other
|
||||
}
|
||||
#[inline]
|
||||
fn equals(&self, other: &Sign) -> bool { *self == *other }
|
||||
}
|
||||
impl TotalOrd for Sign {
|
||||
|
||||
#[inline]
|
||||
fn cmp(&self, other: &Sign) -> Ordering {
|
||||
match (*self, *other) {
|
||||
(Minus, Minus) | (Zero, Zero) | (Plus, Plus) => Equal,
|
||||
@ -714,7 +711,7 @@ fn cmp(&self, other: &Sign) -> Ordering {
|
||||
|
||||
impl Neg<Sign> for Sign {
|
||||
/// Negate Sign value.
|
||||
|
||||
#[inline]
|
||||
fn neg(&self) -> Sign {
|
||||
match *self {
|
||||
Minus => Plus,
|
||||
@ -732,26 +729,26 @@ pub struct BigInt {
|
||||
}
|
||||
|
||||
impl Eq for BigInt {
|
||||
|
||||
#[inline]
|
||||
fn eq(&self, other: &BigInt) -> bool { self.equals(other) }
|
||||
}
|
||||
|
||||
impl TotalEq for BigInt {
|
||||
|
||||
#[inline]
|
||||
fn equals(&self, other: &BigInt) -> bool {
|
||||
match self.cmp(other) { Equal => true, _ => false }
|
||||
}
|
||||
}
|
||||
|
||||
impl Ord for BigInt {
|
||||
|
||||
#[inline]
|
||||
fn lt(&self, other: &BigInt) -> bool {
|
||||
match self.cmp(other) { Less => true, _ => false}
|
||||
}
|
||||
}
|
||||
|
||||
impl TotalOrd for BigInt {
|
||||
|
||||
#[inline]
|
||||
fn cmp(&self, other: &BigInt) -> Ordering {
|
||||
let scmp = self.sign.cmp(&other.sign);
|
||||
if scmp != Equal { return scmp; }
|
||||
@ -765,12 +762,12 @@ fn cmp(&self, other: &BigInt) -> Ordering {
|
||||
}
|
||||
|
||||
impl ToStr for BigInt {
|
||||
|
||||
#[inline]
|
||||
fn to_str(&self) -> ~str { self.to_str_radix(10) }
|
||||
}
|
||||
|
||||
impl FromStr for BigInt {
|
||||
|
||||
#[inline]
|
||||
fn from_str(s: &str) -> Option<BigInt> {
|
||||
FromStrRadix::from_str_radix(s, 10)
|
||||
}
|
||||
@ -779,17 +776,17 @@ fn from_str(s: &str) -> Option<BigInt> {
|
||||
impl Num for BigInt {}
|
||||
|
||||
impl Orderable for BigInt {
|
||||
|
||||
#[inline]
|
||||
fn min(&self, other: &BigInt) -> BigInt {
|
||||
if self < other { self.clone() } else { other.clone() }
|
||||
}
|
||||
|
||||
|
||||
#[inline]
|
||||
fn max(&self, other: &BigInt) -> BigInt {
|
||||
if self > other { self.clone() } else { other.clone() }
|
||||
}
|
||||
|
||||
|
||||
#[inline]
|
||||
fn clamp(&self, mn: &BigInt, mx: &BigInt) -> BigInt {
|
||||
if self > mx { mx.clone() } else
|
||||
if self < mn { mn.clone() } else { self.clone() }
|
||||
@ -797,38 +794,38 @@ fn clamp(&self, mn: &BigInt, mx: &BigInt) -> BigInt {
|
||||
}
|
||||
|
||||
impl Shl<uint, BigInt> for BigInt {
|
||||
|
||||
#[inline]
|
||||
fn shl(&self, rhs: &uint) -> BigInt {
|
||||
BigInt::from_biguint(self.sign, self.data << *rhs)
|
||||
}
|
||||
}
|
||||
|
||||
impl Shr<uint, BigInt> for BigInt {
|
||||
|
||||
#[inline]
|
||||
fn shr(&self, rhs: &uint) -> BigInt {
|
||||
BigInt::from_biguint(self.sign, self.data >> *rhs)
|
||||
}
|
||||
}
|
||||
|
||||
impl Zero for BigInt {
|
||||
|
||||
#[inline]
|
||||
fn zero() -> BigInt {
|
||||
BigInt::from_biguint(Zero, Zero::zero())
|
||||
}
|
||||
|
||||
|
||||
#[inline]
|
||||
fn is_zero(&self) -> bool { self.sign == Zero }
|
||||
}
|
||||
|
||||
impl One for BigInt {
|
||||
|
||||
#[inline]
|
||||
fn one() -> BigInt {
|
||||
BigInt::from_biguint(Plus, One::one())
|
||||
}
|
||||
}
|
||||
|
||||
impl Signed for BigInt {
|
||||
|
||||
#[inline]
|
||||
fn abs(&self) -> BigInt {
|
||||
match self.sign {
|
||||
Plus | Zero => self.clone(),
|
||||
@ -836,12 +833,12 @@ fn abs(&self) -> BigInt {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[inline]
|
||||
fn abs_sub(&self, other: &BigInt) -> BigInt {
|
||||
if *self <= *other { Zero::zero() } else { *self - *other }
|
||||
}
|
||||
|
||||
|
||||
#[inline]
|
||||
fn signum(&self) -> BigInt {
|
||||
match self.sign {
|
||||
Plus => BigInt::from_biguint(Plus, One::one()),
|
||||
@ -850,15 +847,15 @@ fn signum(&self) -> BigInt {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[inline]
|
||||
fn is_positive(&self) -> bool { self.sign == Plus }
|
||||
|
||||
|
||||
#[inline]
|
||||
fn is_negative(&self) -> bool { self.sign == Minus }
|
||||
}
|
||||
|
||||
impl Add<BigInt, BigInt> for BigInt {
|
||||
|
||||
#[inline]
|
||||
fn add(&self, other: &BigInt) -> BigInt {
|
||||
match (self.sign, other.sign) {
|
||||
(Zero, _) => other.clone(),
|
||||
@ -873,7 +870,7 @@ fn add(&self, other: &BigInt) -> BigInt {
|
||||
}
|
||||
|
||||
impl Sub<BigInt, BigInt> for BigInt {
|
||||
|
||||
#[inline]
|
||||
fn sub(&self, other: &BigInt) -> BigInt {
|
||||
match (self.sign, other.sign) {
|
||||
(Zero, _) => -other,
|
||||
@ -891,7 +888,7 @@ fn sub(&self, other: &BigInt) -> BigInt {
|
||||
}
|
||||
|
||||
impl Mul<BigInt, BigInt> for BigInt {
|
||||
|
||||
#[inline]
|
||||
fn mul(&self, other: &BigInt) -> BigInt {
|
||||
match (self.sign, other.sign) {
|
||||
(Zero, _) | (_, Zero) => Zero::zero(),
|
||||
@ -906,7 +903,7 @@ fn mul(&self, other: &BigInt) -> BigInt {
|
||||
}
|
||||
|
||||
impl Div<BigInt, BigInt> for BigInt {
|
||||
|
||||
#[inline]
|
||||
fn div(&self, other: &BigInt) -> BigInt {
|
||||
let (q, _) = self.div_rem(other);
|
||||
return q;
|
||||
@ -914,7 +911,7 @@ fn div(&self, other: &BigInt) -> BigInt {
|
||||
}
|
||||
|
||||
impl Rem<BigInt, BigInt> for BigInt {
|
||||
|
||||
#[inline]
|
||||
fn rem(&self, other: &BigInt) -> BigInt {
|
||||
let (_, r) = self.div_rem(other);
|
||||
return r;
|
||||
@ -922,14 +919,14 @@ fn rem(&self, other: &BigInt) -> BigInt {
|
||||
}
|
||||
|
||||
impl Neg<BigInt> for BigInt {
|
||||
|
||||
#[inline]
|
||||
fn neg(&self) -> BigInt {
|
||||
BigInt::from_biguint(self.sign.neg(), self.data.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl Integer for BigInt {
|
||||
|
||||
#[inline]
|
||||
fn div_rem(&self, other: &BigInt) -> (BigInt, BigInt) {
|
||||
// r.sign == self.sign
|
||||
let (d_ui, r_ui) = self.data.div_mod_floor(&other.data);
|
||||
@ -944,19 +941,18 @@ fn div_rem(&self, other: &BigInt) -> (BigInt, BigInt) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[inline]
|
||||
fn div_floor(&self, other: &BigInt) -> BigInt {
|
||||
let (d, _) = self.div_mod_floor(other);
|
||||
return d;
|
||||
}
|
||||
|
||||
|
||||
#[inline]
|
||||
fn mod_floor(&self, other: &BigInt) -> BigInt {
|
||||
let (_, m) = self.div_mod_floor(other);
|
||||
return m;
|
||||
}
|
||||
|
||||
|
||||
fn div_mod_floor(&self, other: &BigInt) -> (BigInt, BigInt) {
|
||||
// m.sign == other.sign
|
||||
let (d_ui, m_ui) = self.data.div_rem(&other.data);
|
||||
@ -984,7 +980,7 @@ fn div_mod_floor(&self, other: &BigInt) -> (BigInt, BigInt) {
|
||||
*
|
||||
* The result is always positive
|
||||
*/
|
||||
|
||||
#[inline]
|
||||
fn gcd(&self, other: &BigInt) -> BigInt {
|
||||
BigInt::from_biguint(Plus, self.data.gcd(&other.data))
|
||||
}
|
||||
@ -992,26 +988,26 @@ fn gcd(&self, other: &BigInt) -> BigInt {
|
||||
/**
|
||||
* Calculates the Lowest Common Multiple (LCM) of the number and `other`
|
||||
*/
|
||||
|
||||
#[inline]
|
||||
fn lcm(&self, other: &BigInt) -> BigInt {
|
||||
BigInt::from_biguint(Plus, self.data.lcm(&other.data))
|
||||
}
|
||||
|
||||
/// Returns `true` if the number can be divided by `other` without leaving a remainder
|
||||
|
||||
#[inline]
|
||||
fn is_multiple_of(&self, other: &BigInt) -> bool { self.data.is_multiple_of(&other.data) }
|
||||
|
||||
/// Returns `true` if the number is divisible by `2`
|
||||
|
||||
#[inline]
|
||||
fn is_even(&self) -> bool { self.data.is_even() }
|
||||
|
||||
/// Returns `true` if the number is not divisible by `2`
|
||||
|
||||
#[inline]
|
||||
fn is_odd(&self) -> bool { self.data.is_odd() }
|
||||
}
|
||||
|
||||
impl IntConvertible for BigInt {
|
||||
|
||||
#[inline]
|
||||
fn to_int(&self) -> int {
|
||||
match self.sign {
|
||||
Plus => num::min(self.to_uint(), int::max_value as uint) as int,
|
||||
@ -1021,7 +1017,7 @@ fn to_int(&self) -> int {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[inline]
|
||||
fn from_int(n: int) -> BigInt {
|
||||
if n > 0 {
|
||||
return BigInt::from_biguint(Plus, BigUint::from_uint(n as uint));
|
||||
@ -1036,7 +1032,7 @@ fn from_int(n: int) -> BigInt {
|
||||
}
|
||||
|
||||
impl ToStrRadix for BigInt {
|
||||
|
||||
#[inline]
|
||||
fn to_str_radix(&self, radix: uint) -> ~str {
|
||||
match self.sign {
|
||||
Plus => self.data.to_str_radix(radix),
|
||||
@ -1048,21 +1044,21 @@ fn to_str_radix(&self, radix: uint) -> ~str {
|
||||
|
||||
impl FromStrRadix for BigInt {
|
||||
/// Creates and initializes an BigInt.
|
||||
|
||||
fn from_str_radix(s: &str, radix: uint)
|
||||
-> Option<BigInt> {
|
||||
#[inline]
|
||||
fn from_str_radix(s: &str, radix: uint) -> Option<BigInt> {
|
||||
BigInt::parse_bytes(s.as_bytes(), radix)
|
||||
}
|
||||
}
|
||||
|
||||
impl BigInt {
|
||||
/// Creates and initializes an BigInt.
|
||||
#[inline]
|
||||
pub fn new(sign: Sign, v: ~[BigDigit]) -> BigInt {
|
||||
BigInt::from_biguint(sign, BigUint::new(v))
|
||||
}
|
||||
|
||||
/// Creates and initializes an BigInt.
|
||||
|
||||
#[inline]
|
||||
pub fn from_biguint(sign: Sign, data: BigUint) -> BigInt {
|
||||
if sign == Zero || data.is_zero() {
|
||||
return BigInt { sign: Zero, data: Zero::zero() };
|
||||
@ -1071,20 +1067,19 @@ pub fn from_biguint(sign: Sign, data: BigUint) -> BigInt {
|
||||
}
|
||||
|
||||
/// Creates and initializes an BigInt.
|
||||
|
||||
#[inline]
|
||||
pub fn from_uint(n: uint) -> BigInt {
|
||||
if n == 0 { return Zero::zero(); }
|
||||
return BigInt::from_biguint(Plus, BigUint::from_uint(n));
|
||||
}
|
||||
|
||||
/// Creates and initializes an BigInt.
|
||||
|
||||
#[inline]
|
||||
pub fn from_slice(sign: Sign, slice: &[BigDigit]) -> BigInt {
|
||||
BigInt::from_biguint(sign, BigUint::from_slice(slice))
|
||||
}
|
||||
|
||||
/// Creates and initializes an BigInt.
|
||||
|
||||
pub fn parse_bytes(buf: &[u8], radix: uint)
|
||||
-> Option<BigInt> {
|
||||
if buf.is_empty() { return None; }
|
||||
@ -1098,6 +1093,7 @@ pub fn parse_bytes(buf: &[u8], radix: uint)
|
||||
.map_move(|bu| BigInt::from_biguint(sign, bu));
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn to_uint(&self) -> uint {
|
||||
match self.sign {
|
||||
Plus => self.data.to_uint(),
|
||||
@ -1577,7 +1573,6 @@ fn check(n: uint, s: &str) {
|
||||
|
||||
#[cfg(test)]
|
||||
mod bigint_tests {
|
||||
|
||||
use super::*;
|
||||
|
||||
use std::cmp::{Less, Equal, Greater};
|
||||
|
Loading…
Reference in New Issue
Block a user