Skip to content

Commit 0eb858b

Browse files
committed
Cleanup bigint
1 parent 3ca00ec commit 0eb858b

File tree

1 file changed

+27
-36
lines changed

1 file changed

+27
-36
lines changed

src/libnum/bigint.rs

+27-36
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,16 @@ A `BigInt` is a combination of `BigUint` and `Sign`.
1717
*/
1818

1919
use Integer;
20+
use rand::Rng;
2021

21-
use std::cmp;
22+
use std::{cmp, fmt};
2223
use std::default::Default;
23-
use std::fmt;
2424
use std::from_str::FromStr;
2525
use std::num::CheckedDiv;
2626
use std::num::{Bitwise, ToPrimitive, FromPrimitive};
2727
use std::num::{Zero, One, ToStrRadix, FromStrRadix};
28-
use rand::Rng;
2928
use std::string::String;
30-
use std::uint;
31-
use std::{i64, u64};
29+
use std::{uint, i64, u64};
3230

3331
/**
3432
A `BigDigit` is a `BigUint`'s composing element.
@@ -94,7 +92,7 @@ impl Eq for BigUint {}
9492
impl PartialOrd for BigUint {
9593
#[inline]
9694
fn lt(&self, other: &BigUint) -> bool {
97-
match self.cmp(other) { Less => true, _ => false}
95+
self.cmp(other) == Less
9896
}
9997
}
10098

@@ -115,7 +113,7 @@ impl Ord for BigUint {
115113

116114
impl Default for BigUint {
117115
#[inline]
118-
fn default() -> BigUint { BigUint::new(Vec::new()) }
116+
fn default() -> BigUint { Zero::zero() }
119117
}
120118

121119
impl fmt::Show for BigUint {
@@ -605,7 +603,7 @@ impl_to_biguint!(u64, FromPrimitive::from_u64)
605603

606604
impl ToStrRadix for BigUint {
607605
fn to_str_radix(&self, radix: uint) -> String {
608-
assert!(1 < radix && radix <= 16);
606+
assert!(1 < radix && radix <= 16, "The radix must be within (1, 16]");
609607
let (base, max_len) = get_radix_base(radix);
610608
if base == BigDigit::base {
611609
return fill_concat(self.data.as_slice(), radix, max_len)
@@ -645,8 +643,7 @@ impl ToStrRadix for BigUint {
645643
impl FromStrRadix for BigUint {
646644
/// Creates and initializes a `BigUint`.
647645
#[inline]
648-
fn from_str_radix(s: &str, radix: uint)
649-
-> Option<BigUint> {
646+
fn from_str_radix(s: &str, radix: uint) -> Option<BigUint> {
650647
BigUint::parse_bytes(s.as_bytes(), radix)
651648
}
652649
}
@@ -656,22 +653,19 @@ impl BigUint {
656653
///
657654
/// The digits are be in base 2^32.
658655
#[inline]
659-
pub fn new(v: Vec<BigDigit>) -> BigUint {
656+
pub fn new(mut digits: Vec<BigDigit>) -> BigUint {
660657
// omit trailing zeros
661-
let new_len = v.iter().rposition(|n| *n != 0).map_or(0, |p| p + 1);
662-
663-
if new_len == v.len() { return BigUint { data: v }; }
664-
let mut v = v;
665-
v.truncate(new_len);
666-
return BigUint { data: v };
658+
let new_len = digits.iter().rposition(|n| *n != 0).map_or(0, |p| p + 1);
659+
digits.truncate(new_len);
660+
BigUint { data: digits }
667661
}
668662

669663
/// Creates and initializes a `BigUint`.
670664
///
671665
/// The digits are be in base 2^32.
672666
#[inline]
673667
pub fn from_slice(slice: &[BigDigit]) -> BigUint {
674-
return BigUint::new(Vec::from_slice(slice));
668+
BigUint::new(Vec::from_slice(slice))
675669
}
676670

677671
/// Creates and initializes a `BigUint`.
@@ -768,7 +762,6 @@ impl BigUint {
768762
// `DoubleBigDigit` size dependent
769763
#[inline]
770764
fn get_radix_base(radix: uint) -> (DoubleBigDigit, uint) {
771-
assert!(1 < radix && radix <= 16);
772765
match radix {
773766
2 => (4294967296, 32),
774767
3 => (3486784401, 20),
@@ -785,7 +778,7 @@ fn get_radix_base(radix: uint) -> (DoubleBigDigit, uint) {
785778
14 => (1475789056, 8),
786779
15 => (2562890625, 8),
787780
16 => (4294967296, 8),
788-
_ => fail!()
781+
_ => fail!("The radix must be within (1, 16]")
789782
}
790783
}
791784

@@ -815,7 +808,7 @@ pub struct BigInt {
815808
impl PartialEq for BigInt {
816809
#[inline]
817810
fn eq(&self, other: &BigInt) -> bool {
818-
match self.cmp(other) { Equal => true, _ => false }
811+
self.cmp(other) == Equal
819812
}
820813
}
821814

@@ -824,7 +817,7 @@ impl Eq for BigInt {}
824817
impl PartialOrd for BigInt {
825818
#[inline]
826819
fn lt(&self, other: &BigInt) -> bool {
827-
match self.cmp(other) { Less => true, _ => false}
820+
self.cmp(other) == Less
828821
}
829822
}
830823

@@ -844,7 +837,7 @@ impl Ord for BigInt {
844837

845838
impl Default for BigInt {
846839
#[inline]
847-
fn default() -> BigInt { BigInt::new(Zero, Vec::new()) }
840+
fn default() -> BigInt { Zero::zero() }
848841
}
849842

850843
impl fmt::Show for BigInt {
@@ -929,8 +922,7 @@ impl Add<BigInt, BigInt> for BigInt {
929922
match (self.sign, other.sign) {
930923
(Zero, _) => other.clone(),
931924
(_, Zero) => self.clone(),
932-
(Plus, Plus) => BigInt::from_biguint(Plus,
933-
self.data + other.data),
925+
(Plus, Plus) => BigInt::from_biguint(Plus, self.data + other.data),
934926
(Plus, Minus) => self - (-*other),
935927
(Minus, Plus) => other - (-*self),
936928
(Minus, Minus) => -((-self) + (-*other))
@@ -975,15 +967,15 @@ impl Div<BigInt, BigInt> for BigInt {
975967
#[inline]
976968
fn div(&self, other: &BigInt) -> BigInt {
977969
let (q, _) = self.div_rem(other);
978-
return q;
970+
q
979971
}
980972
}
981973

982974
impl Rem<BigInt, BigInt> for BigInt {
983975
#[inline]
984976
fn rem(&self, other: &BigInt) -> BigInt {
985977
let (_, r) = self.div_rem(other);
986-
return r;
978+
r
987979
}
988980
}
989981

@@ -1045,13 +1037,13 @@ impl Integer for BigInt {
10451037
#[inline]
10461038
fn div_floor(&self, other: &BigInt) -> BigInt {
10471039
let (d, _) = self.div_mod_floor(other);
1048-
return d;
1040+
d
10491041
}
10501042

10511043
#[inline]
10521044
fn mod_floor(&self, other: &BigInt) -> BigInt {
10531045
let (_, m) = self.div_mod_floor(other);
1054-
return m;
1046+
m
10551047
}
10561048

10571049
fn div_mod_floor(&self, other: &BigInt) -> (BigInt, BigInt) {
@@ -1265,7 +1257,7 @@ impl<R: Rng> RandBigInt for R {
12651257
let final_digit: BigDigit = self.gen();
12661258
data.push(final_digit >> (BigDigit::bits - rem));
12671259
}
1268-
return BigUint::new(data);
1260+
BigUint::new(data)
12691261
}
12701262

12711263
fn gen_bigint(&mut self, bit_size: uint) -> BigInt {
@@ -1287,7 +1279,7 @@ impl<R: Rng> RandBigInt for R {
12871279
} else {
12881280
Minus
12891281
};
1290-
return BigInt::from_biguint(sign, biguint);
1282+
BigInt::from_biguint(sign, biguint)
12911283
}
12921284

12931285
fn gen_biguint_below(&mut self, bound: &BigUint) -> BigUint {
@@ -1322,8 +1314,8 @@ impl BigInt {
13221314
///
13231315
/// The digits are be in base 2^32.
13241316
#[inline]
1325-
pub fn new(sign: Sign, v: Vec<BigDigit>) -> BigInt {
1326-
BigInt::from_biguint(sign, BigUint::new(v))
1317+
pub fn new(sign: Sign, digits: Vec<BigDigit>) -> BigInt {
1318+
BigInt::from_biguint(sign, BigUint::new(digits))
13271319
}
13281320

13291321
/// Creates and initializes a `BigInt`.
@@ -1334,7 +1326,7 @@ impl BigInt {
13341326
if sign == Zero || data.is_zero() {
13351327
return BigInt { sign: Zero, data: Zero::zero() };
13361328
}
1337-
return BigInt { sign: sign, data: data };
1329+
BigInt { sign: sign, data: data }
13381330
}
13391331

13401332
/// Creates and initializes a `BigInt`.
@@ -1344,8 +1336,7 @@ impl BigInt {
13441336
}
13451337

13461338
/// Creates and initializes a `BigInt`.
1347-
pub fn parse_bytes(buf: &[u8], radix: uint)
1348-
-> Option<BigInt> {
1339+
pub fn parse_bytes(buf: &[u8], radix: uint) -> Option<BigInt> {
13491340
if buf.is_empty() { return None; }
13501341
let mut sign = Plus;
13511342
let mut start = 0;

0 commit comments

Comments
 (0)