Cleanup bigint
This commit is contained in:
parent
1e931918c8
commit
9faf5a3483
|
@ -17,18 +17,16 @@ A `BigInt` is a combination of `BigUint` and `Sign`.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use Integer;
|
use Integer;
|
||||||
|
use rand::Rng;
|
||||||
|
|
||||||
use std::cmp;
|
use std::{cmp, fmt};
|
||||||
use std::default::Default;
|
use std::default::Default;
|
||||||
use std::fmt;
|
|
||||||
use std::from_str::FromStr;
|
use std::from_str::FromStr;
|
||||||
use std::num::CheckedDiv;
|
use std::num::CheckedDiv;
|
||||||
use std::num::{Bitwise, ToPrimitive, FromPrimitive};
|
use std::num::{Bitwise, ToPrimitive, FromPrimitive};
|
||||||
use std::num::{Zero, One, ToStrRadix, FromStrRadix};
|
use std::num::{Zero, One, ToStrRadix, FromStrRadix};
|
||||||
use rand::Rng;
|
|
||||||
use std::string::String;
|
use std::string::String;
|
||||||
use std::uint;
|
use std::{uint, i64, u64};
|
||||||
use std::{i64, u64};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
A `BigDigit` is a `BigUint`'s composing element.
|
A `BigDigit` is a `BigUint`'s composing element.
|
||||||
|
@ -94,7 +92,7 @@ impl Eq for BigUint {}
|
||||||
impl PartialOrd for BigUint {
|
impl PartialOrd for BigUint {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn lt(&self, other: &BigUint) -> bool {
|
fn lt(&self, other: &BigUint) -> bool {
|
||||||
match self.cmp(other) { Less => true, _ => false}
|
self.cmp(other) == Less
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -115,7 +113,7 @@ impl Ord for BigUint {
|
||||||
|
|
||||||
impl Default for BigUint {
|
impl Default for BigUint {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn default() -> BigUint { BigUint::new(Vec::new()) }
|
fn default() -> BigUint { Zero::zero() }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Show for BigUint {
|
impl fmt::Show for BigUint {
|
||||||
|
@ -605,7 +603,7 @@ impl_to_biguint!(u64, FromPrimitive::from_u64)
|
||||||
|
|
||||||
impl ToStrRadix for BigUint {
|
impl ToStrRadix for BigUint {
|
||||||
fn to_str_radix(&self, radix: uint) -> String {
|
fn to_str_radix(&self, radix: uint) -> String {
|
||||||
assert!(1 < radix && radix <= 16);
|
assert!(1 < radix && radix <= 16, "The radix must be within (1, 16]");
|
||||||
let (base, max_len) = get_radix_base(radix);
|
let (base, max_len) = get_radix_base(radix);
|
||||||
if base == BigDigit::base {
|
if base == BigDigit::base {
|
||||||
return fill_concat(self.data.as_slice(), radix, max_len)
|
return fill_concat(self.data.as_slice(), radix, max_len)
|
||||||
|
@ -645,8 +643,7 @@ impl ToStrRadix for BigUint {
|
||||||
impl FromStrRadix for BigUint {
|
impl FromStrRadix for BigUint {
|
||||||
/// Creates and initializes a `BigUint`.
|
/// Creates and initializes a `BigUint`.
|
||||||
#[inline]
|
#[inline]
|
||||||
fn from_str_radix(s: &str, radix: uint)
|
fn from_str_radix(s: &str, radix: uint) -> Option<BigUint> {
|
||||||
-> Option<BigUint> {
|
|
||||||
BigUint::parse_bytes(s.as_bytes(), radix)
|
BigUint::parse_bytes(s.as_bytes(), radix)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -656,14 +653,11 @@ impl BigUint {
|
||||||
///
|
///
|
||||||
/// The digits are be in base 2^32.
|
/// The digits are be in base 2^32.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn new(v: Vec<BigDigit>) -> BigUint {
|
pub fn new(mut digits: Vec<BigDigit>) -> BigUint {
|
||||||
// omit trailing zeros
|
// omit trailing zeros
|
||||||
let new_len = v.iter().rposition(|n| *n != 0).map_or(0, |p| p + 1);
|
let new_len = digits.iter().rposition(|n| *n != 0).map_or(0, |p| p + 1);
|
||||||
|
digits.truncate(new_len);
|
||||||
if new_len == v.len() { return BigUint { data: v }; }
|
BigUint { data: digits }
|
||||||
let mut v = v;
|
|
||||||
v.truncate(new_len);
|
|
||||||
return BigUint { data: v };
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates and initializes a `BigUint`.
|
/// Creates and initializes a `BigUint`.
|
||||||
|
@ -671,7 +665,7 @@ impl BigUint {
|
||||||
/// The digits are be in base 2^32.
|
/// The digits are be in base 2^32.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn from_slice(slice: &[BigDigit]) -> BigUint {
|
pub fn from_slice(slice: &[BigDigit]) -> BigUint {
|
||||||
return BigUint::new(Vec::from_slice(slice));
|
BigUint::new(Vec::from_slice(slice))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates and initializes a `BigUint`.
|
/// Creates and initializes a `BigUint`.
|
||||||
|
@ -768,7 +762,6 @@ impl BigUint {
|
||||||
// `DoubleBigDigit` size dependent
|
// `DoubleBigDigit` size dependent
|
||||||
#[inline]
|
#[inline]
|
||||||
fn get_radix_base(radix: uint) -> (DoubleBigDigit, uint) {
|
fn get_radix_base(radix: uint) -> (DoubleBigDigit, uint) {
|
||||||
assert!(1 < radix && radix <= 16);
|
|
||||||
match radix {
|
match radix {
|
||||||
2 => (4294967296, 32),
|
2 => (4294967296, 32),
|
||||||
3 => (3486784401, 20),
|
3 => (3486784401, 20),
|
||||||
|
@ -785,7 +778,7 @@ fn get_radix_base(radix: uint) -> (DoubleBigDigit, uint) {
|
||||||
14 => (1475789056, 8),
|
14 => (1475789056, 8),
|
||||||
15 => (2562890625, 8),
|
15 => (2562890625, 8),
|
||||||
16 => (4294967296, 8),
|
16 => (4294967296, 8),
|
||||||
_ => fail!()
|
_ => fail!("The radix must be within (1, 16]")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -815,7 +808,7 @@ pub struct BigInt {
|
||||||
impl PartialEq for BigInt {
|
impl PartialEq for BigInt {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn eq(&self, other: &BigInt) -> bool {
|
fn eq(&self, other: &BigInt) -> bool {
|
||||||
match self.cmp(other) { Equal => true, _ => false }
|
self.cmp(other) == Equal
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -824,7 +817,7 @@ impl Eq for BigInt {}
|
||||||
impl PartialOrd for BigInt {
|
impl PartialOrd for BigInt {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn lt(&self, other: &BigInt) -> bool {
|
fn lt(&self, other: &BigInt) -> bool {
|
||||||
match self.cmp(other) { Less => true, _ => false}
|
self.cmp(other) == Less
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -844,7 +837,7 @@ impl Ord for BigInt {
|
||||||
|
|
||||||
impl Default for BigInt {
|
impl Default for BigInt {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn default() -> BigInt { BigInt::new(Zero, Vec::new()) }
|
fn default() -> BigInt { Zero::zero() }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Show for BigInt {
|
impl fmt::Show for BigInt {
|
||||||
|
@ -929,8 +922,7 @@ impl Add<BigInt, BigInt> for BigInt {
|
||||||
match (self.sign, other.sign) {
|
match (self.sign, other.sign) {
|
||||||
(Zero, _) => other.clone(),
|
(Zero, _) => other.clone(),
|
||||||
(_, Zero) => self.clone(),
|
(_, Zero) => self.clone(),
|
||||||
(Plus, Plus) => BigInt::from_biguint(Plus,
|
(Plus, Plus) => BigInt::from_biguint(Plus, self.data + other.data),
|
||||||
self.data + other.data),
|
|
||||||
(Plus, Minus) => self - (-*other),
|
(Plus, Minus) => self - (-*other),
|
||||||
(Minus, Plus) => other - (-*self),
|
(Minus, Plus) => other - (-*self),
|
||||||
(Minus, Minus) => -((-self) + (-*other))
|
(Minus, Minus) => -((-self) + (-*other))
|
||||||
|
@ -975,7 +967,7 @@ impl Div<BigInt, BigInt> for BigInt {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn div(&self, other: &BigInt) -> BigInt {
|
fn div(&self, other: &BigInt) -> BigInt {
|
||||||
let (q, _) = self.div_rem(other);
|
let (q, _) = self.div_rem(other);
|
||||||
return q;
|
q
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -983,7 +975,7 @@ impl Rem<BigInt, BigInt> for BigInt {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn rem(&self, other: &BigInt) -> BigInt {
|
fn rem(&self, other: &BigInt) -> BigInt {
|
||||||
let (_, r) = self.div_rem(other);
|
let (_, r) = self.div_rem(other);
|
||||||
return r;
|
r
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1045,13 +1037,13 @@ impl Integer for BigInt {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn div_floor(&self, other: &BigInt) -> BigInt {
|
fn div_floor(&self, other: &BigInt) -> BigInt {
|
||||||
let (d, _) = self.div_mod_floor(other);
|
let (d, _) = self.div_mod_floor(other);
|
||||||
return d;
|
d
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn mod_floor(&self, other: &BigInt) -> BigInt {
|
fn mod_floor(&self, other: &BigInt) -> BigInt {
|
||||||
let (_, m) = self.div_mod_floor(other);
|
let (_, m) = self.div_mod_floor(other);
|
||||||
return m;
|
m
|
||||||
}
|
}
|
||||||
|
|
||||||
fn div_mod_floor(&self, other: &BigInt) -> (BigInt, BigInt) {
|
fn div_mod_floor(&self, other: &BigInt) -> (BigInt, BigInt) {
|
||||||
|
@ -1265,7 +1257,7 @@ impl<R: Rng> RandBigInt for R {
|
||||||
let final_digit: BigDigit = self.gen();
|
let final_digit: BigDigit = self.gen();
|
||||||
data.push(final_digit >> (BigDigit::bits - rem));
|
data.push(final_digit >> (BigDigit::bits - rem));
|
||||||
}
|
}
|
||||||
return BigUint::new(data);
|
BigUint::new(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn gen_bigint(&mut self, bit_size: uint) -> BigInt {
|
fn gen_bigint(&mut self, bit_size: uint) -> BigInt {
|
||||||
|
@ -1287,7 +1279,7 @@ impl<R: Rng> RandBigInt for R {
|
||||||
} else {
|
} else {
|
||||||
Minus
|
Minus
|
||||||
};
|
};
|
||||||
return BigInt::from_biguint(sign, biguint);
|
BigInt::from_biguint(sign, biguint)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn gen_biguint_below(&mut self, bound: &BigUint) -> BigUint {
|
fn gen_biguint_below(&mut self, bound: &BigUint) -> BigUint {
|
||||||
|
@ -1322,8 +1314,8 @@ impl BigInt {
|
||||||
///
|
///
|
||||||
/// The digits are be in base 2^32.
|
/// The digits are be in base 2^32.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn new(sign: Sign, v: Vec<BigDigit>) -> BigInt {
|
pub fn new(sign: Sign, digits: Vec<BigDigit>) -> BigInt {
|
||||||
BigInt::from_biguint(sign, BigUint::new(v))
|
BigInt::from_biguint(sign, BigUint::new(digits))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates and initializes a `BigInt`.
|
/// Creates and initializes a `BigInt`.
|
||||||
|
@ -1334,7 +1326,7 @@ impl 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() };
|
||||||
}
|
}
|
||||||
return BigInt { sign: sign, data: data };
|
BigInt { sign: sign, data: data }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates and initializes a `BigInt`.
|
/// Creates and initializes a `BigInt`.
|
||||||
|
@ -1344,8 +1336,7 @@ impl BigInt {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates and initializes a `BigInt`.
|
/// Creates and initializes a `BigInt`.
|
||||||
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; }
|
||||||
let mut sign = Plus;
|
let mut sign = Plus;
|
||||||
let mut start = 0;
|
let mut start = 0;
|
||||||
|
|
Loading…
Reference in New Issue