libstd: inlining almost every methods in bigint module.

This commit is contained in:
gifnksm 2013-04-28 10:58:19 +09:00
parent 01b3490a55
commit 92f0dc6b4b

View file

@ -53,15 +53,19 @@ pub mod BigDigit {
priv static hi_mask: uint = (-1 as uint) << bits; priv static hi_mask: uint = (-1 as uint) << bits;
priv static lo_mask: uint = (-1 as uint) >> bits; priv static lo_mask: uint = (-1 as uint) >> bits;
#[inline(always)]
priv fn get_hi(n: uint) -> BigDigit { (n >> bits) as BigDigit } priv fn get_hi(n: uint) -> BigDigit { (n >> bits) as BigDigit }
#[inline(always)]
priv fn get_lo(n: uint) -> BigDigit { (n & lo_mask) as BigDigit } priv fn get_lo(n: uint) -> BigDigit { (n & lo_mask) as BigDigit }
/// Split one machine sized unsigned integer into two BigDigits. /// Split one machine sized unsigned integer into two BigDigits.
#[inline(always)]
pub fn from_uint(n: uint) -> (BigDigit, BigDigit) { pub fn from_uint(n: uint) -> (BigDigit, BigDigit) {
(get_hi(n), get_lo(n)) (get_hi(n), get_lo(n))
} }
/// Join two BigDigits into one machine sized unsigned integer /// Join two BigDigits into one machine sized unsigned integer
#[inline(always)]
pub fn to_uint(hi: BigDigit, lo: BigDigit) -> uint { pub fn to_uint(hi: BigDigit, lo: BigDigit) -> uint {
(lo as uint) | ((hi as uint) << bits) (lo as uint) | ((hi as uint) << bits)
} }
@ -78,32 +82,40 @@ pub struct BigUint {
} }
impl Eq for BigUint { impl Eq for BigUint {
#[inline(always)]
fn eq(&self, other: &BigUint) -> bool { self.equals(other) } fn eq(&self, other: &BigUint) -> bool { self.equals(other) }
#[inline(always)]
fn ne(&self, other: &BigUint) -> bool { !self.equals(other) } fn ne(&self, other: &BigUint) -> bool { !self.equals(other) }
} }
impl TotalEq for BigUint { impl TotalEq for BigUint {
#[inline(always)]
fn equals(&self, other: &BigUint) -> bool { fn equals(&self, other: &BigUint) -> bool {
match self.cmp(other) { Equal => true, _ => false } match self.cmp(other) { Equal => true, _ => false }
} }
} }
impl Ord for BigUint { impl Ord for BigUint {
#[inline(always)]
fn lt(&self, other: &BigUint) -> bool { fn lt(&self, other: &BigUint) -> bool {
match self.cmp(other) { Less => true, _ => false} match self.cmp(other) { Less => true, _ => false}
} }
#[inline(always)]
fn le(&self, other: &BigUint) -> bool { fn le(&self, other: &BigUint) -> bool {
match self.cmp(other) { Less | Equal => true, _ => false } match self.cmp(other) { Less | Equal => true, _ => false }
} }
#[inline(always)]
fn ge(&self, other: &BigUint) -> bool { fn ge(&self, other: &BigUint) -> bool {
match self.cmp(other) { Greater | Equal => true, _ => false } match self.cmp(other) { Greater | Equal => true, _ => false }
} }
#[inline(always)]
fn gt(&self, other: &BigUint) -> bool { fn gt(&self, other: &BigUint) -> bool {
match self.cmp(other) { Greater => true, _ => false } match self.cmp(other) { Greater => true, _ => false }
} }
} }
impl TotalOrd for BigUint { impl TotalOrd for BigUint {
#[inline(always)]
fn cmp(&self, other: &BigUint) -> Ordering { fn cmp(&self, other: &BigUint) -> Ordering {
let s_len = self.data.len(), o_len = other.data.len(); let s_len = self.data.len(), o_len = other.data.len();
if s_len < o_len { return Less; } if s_len < o_len { return Less; }
@ -121,16 +133,19 @@ impl TotalOrd for BigUint {
} }
impl ToStr for BigUint { impl ToStr for BigUint {
#[inline(always)]
fn to_str(&self) -> ~str { self.to_str_radix(10) } fn to_str(&self) -> ~str { self.to_str_radix(10) }
} }
impl from_str::FromStr for BigUint { impl from_str::FromStr for BigUint {
#[inline(always)]
fn from_str(s: &str) -> Option<BigUint> { fn from_str(s: &str) -> Option<BigUint> {
FromStrRadix::from_str_radix(s, 10) FromStrRadix::from_str_radix(s, 10)
} }
} }
impl Shl<uint, BigUint> for BigUint { impl Shl<uint, BigUint> for BigUint {
#[inline(always)]
fn shl(&self, rhs: &uint) -> BigUint { fn shl(&self, rhs: &uint) -> BigUint {
let n_unit = *rhs / BigDigit::bits; let n_unit = *rhs / BigDigit::bits;
let n_bits = *rhs % BigDigit::bits; let n_bits = *rhs % BigDigit::bits;
@ -139,6 +154,7 @@ impl Shl<uint, BigUint> for BigUint {
} }
impl Shr<uint, BigUint> for BigUint { impl Shr<uint, BigUint> for BigUint {
#[inline(always)]
fn shr(&self, rhs: &uint) -> BigUint { fn shr(&self, rhs: &uint) -> BigUint {
let n_unit = *rhs / BigDigit::bits; let n_unit = *rhs / BigDigit::bits;
let n_bits = *rhs % BigDigit::bits; let n_bits = *rhs % BigDigit::bits;
@ -147,18 +163,22 @@ impl Shr<uint, BigUint> for BigUint {
} }
impl Zero for BigUint { impl Zero for BigUint {
#[inline(always)]
fn zero() -> BigUint { BigUint::new(~[]) } fn zero() -> BigUint { BigUint::new(~[]) }
#[inline(always)]
fn is_zero(&self) -> bool { self.data.is_empty() } fn is_zero(&self) -> bool { self.data.is_empty() }
} }
impl One for BigUint { impl One for BigUint {
#[inline(always)]
fn one() -> BigUint { BigUint::new(~[1]) } fn one() -> BigUint { BigUint::new(~[1]) }
} }
impl Unsigned for BigUint {} impl Unsigned for BigUint {}
impl Add<BigUint, BigUint> for BigUint { impl Add<BigUint, BigUint> for BigUint {
#[inline(always)]
fn add(&self, other: &BigUint) -> BigUint { fn add(&self, other: &BigUint) -> BigUint {
let new_len = uint::max(self.data.len(), other.data.len()); let new_len = uint::max(self.data.len(), other.data.len());
@ -178,6 +198,7 @@ impl Add<BigUint, BigUint> for BigUint {
} }
impl Sub<BigUint, BigUint> for BigUint { impl Sub<BigUint, BigUint> for BigUint {
#[inline(always)]
fn sub(&self, other: &BigUint) -> BigUint { fn sub(&self, other: &BigUint) -> BigUint {
let new_len = uint::max(self.data.len(), other.data.len()); let new_len = uint::max(self.data.len(), other.data.len());
@ -233,6 +254,7 @@ impl Mul<BigUint, BigUint> for BigUint {
return ll + mm.shl_unit(half_len) + hh.shl_unit(half_len * 2); return ll + mm.shl_unit(half_len) + hh.shl_unit(half_len * 2);
#[inline(always)]
fn mul_digit(a: &BigUint, n: BigDigit) -> BigUint { fn mul_digit(a: &BigUint, n: BigDigit) -> BigUint {
if n == 0 { return Zero::zero(); } if n == 0 { return Zero::zero(); }
if n == 1 { return copy *a; } if n == 1 { return copy *a; }
@ -249,6 +271,7 @@ impl Mul<BigUint, BigUint> for BigUint {
return BigUint::new(prod + [carry]); return BigUint::new(prod + [carry]);
} }
#[inline(always)]
fn cut_at(a: &BigUint, n: uint) -> (BigUint, BigUint) { fn cut_at(a: &BigUint, n: uint) -> (BigUint, BigUint) {
let mid = uint::min(a.data.len(), n); let mid = uint::min(a.data.len(), n);
return (BigUint::from_slice(vec::slice(a.data, mid, return (BigUint::from_slice(vec::slice(a.data, mid,
@ -256,6 +279,7 @@ impl Mul<BigUint, BigUint> for BigUint {
BigUint::from_slice(vec::slice(a.data, 0, mid))); BigUint::from_slice(vec::slice(a.data, 0, mid)));
} }
#[inline(always)]
fn sub_sign(a: BigUint, b: BigUint) -> (Ordering, BigUint) { fn sub_sign(a: BigUint, b: BigUint) -> (Ordering, BigUint) {
match a.cmp(&b) { match a.cmp(&b) {
Less => (Less, b - a), Less => (Less, b - a),
@ -267,6 +291,7 @@ impl Mul<BigUint, BigUint> for BigUint {
} }
impl Quot<BigUint, BigUint> for BigUint { impl Quot<BigUint, BigUint> for BigUint {
#[inline(always)]
fn quot(&self, other: &BigUint) -> BigUint { fn quot(&self, other: &BigUint) -> BigUint {
let (q, _) = self.quot_rem(other); let (q, _) = self.quot_rem(other);
return q; return q;
@ -274,6 +299,7 @@ impl Quot<BigUint, BigUint> for BigUint {
} }
impl Rem<BigUint, BigUint> for BigUint { impl Rem<BigUint, BigUint> for BigUint {
#[inline(always)]
fn rem(&self, other: &BigUint) -> BigUint { fn rem(&self, other: &BigUint) -> BigUint {
let (_, r) = self.quot_rem(other); let (_, r) = self.quot_rem(other);
return r; return r;
@ -281,6 +307,7 @@ impl Rem<BigUint, BigUint> for BigUint {
} }
impl Neg<BigUint> for BigUint { impl Neg<BigUint> for BigUint {
#[inline(always)]
fn neg(&self) -> BigUint { fail!() } fn neg(&self) -> BigUint { fail!() }
} }
@ -420,16 +447,19 @@ impl Integer for BigUint {
} }
impl IntConvertible for BigUint { impl IntConvertible for BigUint {
#[inline(always)]
fn to_int(&self) -> int { fn to_int(&self) -> int {
uint::min(self.to_uint(), int::max_value as uint) as int uint::min(self.to_uint(), int::max_value as uint) as int
} }
#[inline(always)]
fn from_int(n: int) -> BigUint { fn from_int(n: int) -> BigUint {
if (n < 0) { Zero::zero() } else { BigUint::from_uint(n as uint) } if (n < 0) { Zero::zero() } else { BigUint::from_uint(n as uint) }
} }
} }
impl ToStrRadix for BigUint { impl ToStrRadix for BigUint {
#[inline(always)]
fn to_str_radix(&self, radix: uint) -> ~str { fn to_str_radix(&self, radix: uint) -> ~str {
assert!(1 < radix && radix <= 16); assert!(1 < radix && radix <= 16);
let (base, max_len) = get_radix_base(radix); let (base, max_len) = get_radix_base(radix);
@ -438,6 +468,7 @@ impl ToStrRadix for BigUint {
} }
return fill_concat(convert_base(copy *self, base), radix, max_len); return fill_concat(convert_base(copy *self, base), radix, max_len);
#[inline(always)]
fn convert_base(n: BigUint, base: uint) -> ~[BigDigit] { fn convert_base(n: BigUint, base: uint) -> ~[BigDigit] {
let divider = BigUint::from_uint(base); let divider = BigUint::from_uint(base);
let mut result = ~[]; let mut result = ~[];
@ -453,6 +484,7 @@ impl ToStrRadix for BigUint {
return result; return result;
} }
#[inline(always)]
fn fill_concat(v: &[BigDigit], radix: uint, l: uint) -> ~str { fn fill_concat(v: &[BigDigit], radix: uint, l: uint) -> ~str {
if v.is_empty() { return ~"0" } if v.is_empty() { return ~"0" }
let s = str::concat(vec::reversed(v).map(|n| { let s = str::concat(vec::reversed(v).map(|n| {
@ -466,14 +498,16 @@ impl ToStrRadix for BigUint {
impl FromStrRadix for BigUint { impl FromStrRadix for BigUint {
/// Creates and initializes an BigUint. /// Creates and initializes an BigUint.
#[inline(always)]
pub fn from_str_radix(s: &str, radix: uint) pub fn from_str_radix(s: &str, radix: uint)
-> Option<BigUint> { -> Option<BigUint> {
BigUint::parse_bytes(str::to_bytes(s), radix) BigUint::parse_bytes(str::to_bytes(s), radix)
} }
} }
pub impl BigUint { impl BigUint {
/// Creates and initializes an BigUint. /// Creates and initializes an BigUint.
#[inline(always)]
pub fn new(v: ~[BigDigit]) -> BigUint { pub fn new(v: ~[BigDigit]) -> BigUint {
// omit trailing zeros // omit trailing zeros
let new_len = v.rposition(|n| *n != 0).map_default(0, |p| *p + 1); let new_len = v.rposition(|n| *n != 0).map_default(0, |p| *p + 1);
@ -485,6 +519,7 @@ pub impl BigUint {
} }
/// Creates and initializes an BigUint. /// Creates and initializes an BigUint.
#[inline(always)]
pub fn from_uint(n: uint) -> BigUint { pub fn from_uint(n: uint) -> BigUint {
match BigDigit::from_uint(n) { match BigDigit::from_uint(n) {
(0, 0) => Zero::zero(), (0, 0) => Zero::zero(),
@ -494,11 +529,13 @@ pub impl BigUint {
} }
/// Creates and initializes an BigUint. /// Creates and initializes an BigUint.
#[inline(always)]
pub fn from_slice(slice: &[BigDigit]) -> BigUint { pub fn from_slice(slice: &[BigDigit]) -> BigUint {
return BigUint::new(vec::from_slice(slice)); return BigUint::new(vec::from_slice(slice));
} }
/// Creates and initializes an BigUint. /// Creates and initializes an BigUint.
#[inline(always)]
pub fn parse_bytes(buf: &[u8], radix: uint) pub fn parse_bytes(buf: &[u8], radix: uint)
-> Option<BigUint> { -> Option<BigUint> {
let (base, unit_len) = get_radix_base(radix); let (base, unit_len) = get_radix_base(radix);
@ -521,6 +558,7 @@ pub impl BigUint {
} }
} }
#[inline(always)]
pub fn to_uint(&self) -> uint { pub fn to_uint(&self) -> uint {
match self.data.len() { match self.data.len() {
0 => 0, 0 => 0,
@ -530,12 +568,14 @@ pub impl BigUint {
} }
} }
#[inline(always)]
priv fn shl_unit(self, n_unit: uint) -> BigUint { priv fn shl_unit(self, n_unit: uint) -> BigUint {
if n_unit == 0 || self.is_zero() { return self; } if n_unit == 0 || self.is_zero() { return self; }
return BigUint::new(vec::from_elem(n_unit, 0) + self.data); return BigUint::new(vec::from_elem(n_unit, 0) + self.data);
} }
#[inline(always)]
priv fn shl_bits(self, n_bits: uint) -> BigUint { priv fn shl_bits(self, n_bits: uint) -> BigUint {
if n_bits == 0 || self.is_zero() { return self; } if n_bits == 0 || self.is_zero() { return self; }
@ -551,6 +591,7 @@ pub impl BigUint {
return BigUint::new(shifted + [carry]); return BigUint::new(shifted + [carry]);
} }
#[inline(always)]
priv fn shr_unit(self, n_unit: uint) -> BigUint { priv fn shr_unit(self, n_unit: uint) -> BigUint {
if n_unit == 0 { return self; } if n_unit == 0 { return self; }
if self.data.len() < n_unit { return Zero::zero(); } if self.data.len() < n_unit { return Zero::zero(); }
@ -559,6 +600,7 @@ pub impl BigUint {
); );
} }
#[inline(always)]
priv fn shr_bits(self, n_bits: uint) -> BigUint { priv fn shr_bits(self, n_bits: uint) -> BigUint {
if n_bits == 0 || self.data.is_empty() { return self; } if n_bits == 0 || self.data.is_empty() { return self; }
@ -573,6 +615,7 @@ pub impl BigUint {
} }
#[cfg(target_arch = "x86_64")] #[cfg(target_arch = "x86_64")]
#[inline(always)]
priv fn get_radix_base(radix: uint) -> (uint, uint) { priv fn get_radix_base(radix: uint) -> (uint, uint) {
assert!(1 < radix && radix <= 16); assert!(1 < radix && radix <= 16);
match radix { match radix {
@ -598,6 +641,7 @@ priv fn get_radix_base(radix: uint) -> (uint, uint) {
#[cfg(target_arch = "arm")] #[cfg(target_arch = "arm")]
#[cfg(target_arch = "x86")] #[cfg(target_arch = "x86")]
#[cfg(target_arch = "mips")] #[cfg(target_arch = "mips")]
#[inline(always)]
priv fn get_radix_base(radix: uint) -> (uint, uint) { priv fn get_radix_base(radix: uint) -> (uint, uint) {
assert!(1 < radix && radix <= 16); assert!(1 < radix && radix <= 16);
match radix { match radix {
@ -625,21 +669,26 @@ priv fn get_radix_base(radix: uint) -> (uint, uint) {
pub enum Sign { Minus, Zero, Plus } pub enum Sign { Minus, Zero, Plus }
impl Ord for Sign { impl Ord for Sign {
#[inline(always)]
fn lt(&self, other: &Sign) -> bool { fn lt(&self, other: &Sign) -> bool {
match self.cmp(other) { Less => true, _ => false} match self.cmp(other) { Less => true, _ => false}
} }
#[inline(always)]
fn le(&self, other: &Sign) -> bool { fn le(&self, other: &Sign) -> bool {
match self.cmp(other) { Less | Equal => true, _ => false } match self.cmp(other) { Less | Equal => true, _ => false }
} }
#[inline(always)]
fn ge(&self, other: &Sign) -> bool { fn ge(&self, other: &Sign) -> bool {
match self.cmp(other) { Greater | Equal => true, _ => false } match self.cmp(other) { Greater | Equal => true, _ => false }
} }
#[inline(always)]
fn gt(&self, other: &Sign) -> bool { fn gt(&self, other: &Sign) -> bool {
match self.cmp(other) { Greater => true, _ => false } match self.cmp(other) { Greater => true, _ => false }
} }
} }
impl TotalOrd for Sign { impl TotalOrd for Sign {
#[inline(always)]
fn cmp(&self, other: &Sign) -> Ordering { fn cmp(&self, other: &Sign) -> Ordering {
match (*self, *other) { match (*self, *other) {
(Minus, Minus) | (Zero, Zero) | (Plus, Plus) => Equal, (Minus, Minus) | (Zero, Zero) | (Plus, Plus) => Equal,
@ -651,6 +700,7 @@ impl TotalOrd for Sign {
impl Neg<Sign> for Sign { impl Neg<Sign> for Sign {
/// Negate Sign value. /// Negate Sign value.
#[inline(always)]
fn neg(&self) -> Sign { fn neg(&self) -> Sign {
match *self { match *self {
Minus => Plus, Minus => Plus,
@ -667,32 +717,40 @@ pub struct BigInt {
} }
impl Eq for BigInt { impl Eq for BigInt {
#[inline(always)]
fn eq(&self, other: &BigInt) -> bool { self.equals(other) } fn eq(&self, other: &BigInt) -> bool { self.equals(other) }
#[inline(always)]
fn ne(&self, other: &BigInt) -> bool { !self.equals(other) } fn ne(&self, other: &BigInt) -> bool { !self.equals(other) }
} }
impl TotalEq for BigInt { impl TotalEq for BigInt {
#[inline(always)]
fn equals(&self, other: &BigInt) -> bool { fn equals(&self, other: &BigInt) -> bool {
match self.cmp(other) { Equal => true, _ => false } match self.cmp(other) { Equal => true, _ => false }
} }
} }
impl Ord for BigInt { impl Ord for BigInt {
#[inline(always)]
fn lt(&self, other: &BigInt) -> bool { fn lt(&self, other: &BigInt) -> bool {
match self.cmp(other) { Less => true, _ => false} match self.cmp(other) { Less => true, _ => false}
} }
#[inline(always)]
fn le(&self, other: &BigInt) -> bool { fn le(&self, other: &BigInt) -> bool {
match self.cmp(other) { Less | Equal => true, _ => false } match self.cmp(other) { Less | Equal => true, _ => false }
} }
#[inline(always)]
fn ge(&self, other: &BigInt) -> bool { fn ge(&self, other: &BigInt) -> bool {
match self.cmp(other) { Greater | Equal => true, _ => false } match self.cmp(other) { Greater | Equal => true, _ => false }
} }
#[inline(always)]
fn gt(&self, other: &BigInt) -> bool { fn gt(&self, other: &BigInt) -> bool {
match self.cmp(other) { Greater => true, _ => false } match self.cmp(other) { Greater => true, _ => false }
} }
} }
impl TotalOrd for BigInt { impl TotalOrd for BigInt {
#[inline(always)]
fn cmp(&self, other: &BigInt) -> Ordering { fn cmp(&self, other: &BigInt) -> Ordering {
let scmp = self.sign.cmp(&other.sign); let scmp = self.sign.cmp(&other.sign);
if scmp != Equal { return scmp; } if scmp != Equal { return scmp; }
@ -706,42 +764,50 @@ impl TotalOrd for BigInt {
} }
impl ToStr for BigInt { impl ToStr for BigInt {
#[inline(always)]
fn to_str(&self) -> ~str { self.to_str_radix(10) } fn to_str(&self) -> ~str { self.to_str_radix(10) }
} }
impl from_str::FromStr for BigInt { impl from_str::FromStr for BigInt {
#[inline(always)]
fn from_str(s: &str) -> Option<BigInt> { fn from_str(s: &str) -> Option<BigInt> {
FromStrRadix::from_str_radix(s, 10) FromStrRadix::from_str_radix(s, 10)
} }
} }
impl Shl<uint, BigInt> for BigInt { impl Shl<uint, BigInt> for BigInt {
#[inline(always)]
fn shl(&self, rhs: &uint) -> BigInt { fn shl(&self, rhs: &uint) -> BigInt {
BigInt::from_biguint(self.sign, self.data << *rhs) BigInt::from_biguint(self.sign, self.data << *rhs)
} }
} }
impl Shr<uint, BigInt> for BigInt { impl Shr<uint, BigInt> for BigInt {
#[inline(always)]
fn shr(&self, rhs: &uint) -> BigInt { fn shr(&self, rhs: &uint) -> BigInt {
BigInt::from_biguint(self.sign, self.data >> *rhs) BigInt::from_biguint(self.sign, self.data >> *rhs)
} }
} }
impl Zero for BigInt { impl Zero for BigInt {
#[inline(always)]
fn zero() -> BigInt { fn zero() -> BigInt {
BigInt::from_biguint(Zero, Zero::zero()) BigInt::from_biguint(Zero, Zero::zero())
} }
#[inline(always)]
fn is_zero(&self) -> bool { self.sign == Zero } fn is_zero(&self) -> bool { self.sign == Zero }
} }
impl One for BigInt { impl One for BigInt {
#[inline(always)]
fn one() -> BigInt { fn one() -> BigInt {
BigInt::from_biguint(Plus, One::one()) BigInt::from_biguint(Plus, One::one())
} }
} }
impl Signed for BigInt { impl Signed for BigInt {
#[inline(always)]
fn abs(&self) -> BigInt { fn abs(&self) -> BigInt {
match self.sign { match self.sign {
Plus | Zero => copy *self, Plus | Zero => copy *self,
@ -749,6 +815,7 @@ impl Signed for BigInt {
} }
} }
#[inline(always)]
fn signum(&self) -> BigInt { fn signum(&self) -> BigInt {
match self.sign { match self.sign {
Plus => BigInt::from_biguint(Plus, One::one()), Plus => BigInt::from_biguint(Plus, One::one()),
@ -757,12 +824,15 @@ impl Signed for BigInt {
} }
} }
#[inline(always)]
fn is_positive(&self) -> bool { self.sign == Plus } fn is_positive(&self) -> bool { self.sign == Plus }
#[inline(always)]
fn is_negative(&self) -> bool { self.sign == Minus } fn is_negative(&self) -> bool { self.sign == Minus }
} }
impl Add<BigInt, BigInt> for BigInt { impl Add<BigInt, BigInt> for BigInt {
#[inline(always)]
fn add(&self, other: &BigInt) -> BigInt { fn add(&self, other: &BigInt) -> BigInt {
match (self.sign, other.sign) { match (self.sign, other.sign) {
(Zero, _) => copy *other, (Zero, _) => copy *other,
@ -777,6 +847,7 @@ impl Add<BigInt, BigInt> for BigInt {
} }
impl Sub<BigInt, BigInt> for BigInt { impl Sub<BigInt, BigInt> for BigInt {
#[inline(always)]
fn sub(&self, other: &BigInt) -> BigInt { fn sub(&self, other: &BigInt) -> BigInt {
match (self.sign, other.sign) { match (self.sign, other.sign) {
(Zero, _) => -other, (Zero, _) => -other,
@ -794,6 +865,7 @@ impl Sub<BigInt, BigInt> for BigInt {
} }
impl Mul<BigInt, BigInt> for BigInt { impl Mul<BigInt, BigInt> for BigInt {
#[inline(always)]
fn mul(&self, other: &BigInt) -> BigInt { fn mul(&self, other: &BigInt) -> BigInt {
match (self.sign, other.sign) { match (self.sign, other.sign) {
(Zero, _) | (_, Zero) => Zero::zero(), (Zero, _) | (_, Zero) => Zero::zero(),
@ -808,6 +880,7 @@ impl Mul<BigInt, BigInt> for BigInt {
} }
impl Quot<BigInt, BigInt> for BigInt { impl Quot<BigInt, BigInt> for BigInt {
#[inline(always)]
fn quot(&self, other: &BigInt) -> BigInt { fn quot(&self, other: &BigInt) -> BigInt {
let (q, _) = self.quot_rem(other); let (q, _) = self.quot_rem(other);
return q; return q;
@ -815,6 +888,7 @@ impl Quot<BigInt, BigInt> for BigInt {
} }
impl Rem<BigInt, BigInt> for BigInt { impl Rem<BigInt, BigInt> for BigInt {
#[inline(always)]
fn rem(&self, other: &BigInt) -> BigInt { fn rem(&self, other: &BigInt) -> BigInt {
let (_, r) = self.quot_rem(other); let (_, r) = self.quot_rem(other);
return r; return r;
@ -822,6 +896,7 @@ impl Rem<BigInt, BigInt> for BigInt {
} }
impl Neg<BigInt> for BigInt { impl Neg<BigInt> for BigInt {
#[inline(always)]
fn neg(&self) -> BigInt { fn neg(&self) -> BigInt {
BigInt::from_biguint(self.sign.neg(), copy self.data) BigInt::from_biguint(self.sign.neg(), copy self.data)
} }
@ -910,6 +985,7 @@ impl Integer for BigInt {
} }
impl IntConvertible for BigInt { impl IntConvertible for BigInt {
#[inline(always)]
fn to_int(&self) -> int { fn to_int(&self) -> int {
match self.sign { match self.sign {
Plus => uint::min(self.to_uint(), int::max_value as uint) as int, Plus => uint::min(self.to_uint(), int::max_value as uint) as int,
@ -919,6 +995,7 @@ impl IntConvertible for BigInt {
} }
} }
#[inline(always)]
fn from_int(n: int) -> BigInt { fn from_int(n: int) -> BigInt {
if n > 0 { if n > 0 {
return BigInt::from_biguint(Plus, BigUint::from_uint(n as uint)); return BigInt::from_biguint(Plus, BigUint::from_uint(n as uint));
@ -933,6 +1010,7 @@ impl IntConvertible for BigInt {
} }
impl ToStrRadix for BigInt { impl ToStrRadix for BigInt {
#[inline(always)]
fn to_str_radix(&self, radix: uint) -> ~str { fn to_str_radix(&self, radix: uint) -> ~str {
match self.sign { match self.sign {
Plus => self.data.to_str_radix(radix), Plus => self.data.to_str_radix(radix),
@ -944,6 +1022,7 @@ impl ToStrRadix for BigInt {
impl FromStrRadix for BigInt { impl FromStrRadix for BigInt {
/// Creates and initializes an BigInt. /// Creates and initializes an BigInt.
#[inline(always)]
fn from_str_radix(s: &str, radix: uint) fn from_str_radix(s: &str, radix: uint)
-> Option<BigInt> { -> Option<BigInt> {
BigInt::parse_bytes(str::to_bytes(s), radix) BigInt::parse_bytes(str::to_bytes(s), radix)
@ -952,11 +1031,13 @@ impl FromStrRadix for BigInt {
pub impl BigInt { pub impl BigInt {
/// Creates and initializes an BigInt. /// Creates and initializes an BigInt.
#[inline(always)]
pub fn new(sign: Sign, v: ~[BigDigit]) -> BigInt { pub fn new(sign: Sign, v: ~[BigDigit]) -> BigInt {
BigInt::from_biguint(sign, BigUint::new(v)) BigInt::from_biguint(sign, BigUint::new(v))
} }
/// Creates and initializes an BigInt. /// Creates and initializes an BigInt.
#[inline(always)]
pub fn from_biguint(sign: Sign, data: BigUint) -> BigInt { pub fn from_biguint(sign: Sign, data: BigUint) -> BigInt {
if sign == Zero || data.is_zero() { if sign == Zero || data.is_zero() {
return BigInt { sign: Zero, data: Zero::zero() }; return BigInt { sign: Zero, data: Zero::zero() };
@ -965,17 +1046,20 @@ pub impl BigInt {
} }
/// Creates and initializes an BigInt. /// Creates and initializes an BigInt.
#[inline(always)]
pub fn from_uint(n: uint) -> BigInt { pub fn from_uint(n: uint) -> BigInt {
if n == 0 { return Zero::zero(); } if n == 0 { return Zero::zero(); }
return BigInt::from_biguint(Plus, BigUint::from_uint(n)); return BigInt::from_biguint(Plus, BigUint::from_uint(n));
} }
/// Creates and initializes an BigInt. /// Creates and initializes an BigInt.
#[inline(always)]
pub fn from_slice(sign: Sign, slice: &[BigDigit]) -> BigInt { pub fn from_slice(sign: Sign, slice: &[BigDigit]) -> BigInt {
BigInt::from_biguint(sign, BigUint::from_slice(slice)) BigInt::from_biguint(sign, BigUint::from_slice(slice))
} }
/// Creates and initializes an BigInt. /// Creates and initializes an BigInt.
#[inline(always)]
pub fn parse_bytes(buf: &[u8], radix: uint) pub fn parse_bytes(buf: &[u8], radix: uint)
-> Option<BigInt> { -> Option<BigInt> {
if buf.is_empty() { return None; } if buf.is_empty() { return None; }
@ -989,6 +1073,7 @@ pub impl BigInt {
.map(|bu| BigInt::from_biguint(sign, *bu)); .map(|bu| BigInt::from_biguint(sign, *bu));
} }
#[inline(always)]
fn to_uint(&self) -> uint { fn to_uint(&self) -> uint {
match self.sign { match self.sign {
Plus => self.data.to_uint(), Plus => self.data.to_uint(),