@@ -17,18 +17,16 @@ A `BigInt` is a combination of `BigUint` and `Sign`.
17
17
*/
18
18
19
19
use Integer ;
20
+ use rand:: Rng ;
20
21
21
- use std:: cmp;
22
+ use std:: { cmp, fmt } ;
22
23
use std:: default:: Default ;
23
- use std:: fmt;
24
24
use std:: from_str:: FromStr ;
25
25
use std:: num:: CheckedDiv ;
26
26
use std:: num:: { Bitwise , ToPrimitive , FromPrimitive } ;
27
27
use std:: num:: { Zero , One , ToStrRadix , FromStrRadix } ;
28
- use rand:: Rng ;
29
28
use std:: string:: String ;
30
- use std:: uint;
31
- use std:: { i64, u64} ;
29
+ use std:: { uint, i64, u64} ;
32
30
33
31
/**
34
32
A `BigDigit` is a `BigUint`'s composing element.
@@ -94,7 +92,7 @@ impl Eq for BigUint {}
94
92
impl PartialOrd for BigUint {
95
93
#[ inline]
96
94
fn lt ( & self , other : & BigUint ) -> bool {
97
- match self . cmp ( other) { Less => true , _ => false }
95
+ self . cmp ( other) == Less
98
96
}
99
97
}
100
98
@@ -115,7 +113,7 @@ impl Ord for BigUint {
115
113
116
114
impl Default for BigUint {
117
115
#[ inline]
118
- fn default ( ) -> BigUint { BigUint :: new ( Vec :: new ( ) ) }
116
+ fn default ( ) -> BigUint { Zero :: zero ( ) }
119
117
}
120
118
121
119
impl fmt:: Show for BigUint {
@@ -605,7 +603,7 @@ impl_to_biguint!(u64, FromPrimitive::from_u64)
605
603
606
604
impl ToStrRadix for BigUint {
607
605
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]" ) ;
609
607
let ( base, max_len) = get_radix_base ( radix) ;
610
608
if base == BigDigit :: base {
611
609
return fill_concat ( self . data . as_slice ( ) , radix, max_len)
@@ -645,8 +643,7 @@ impl ToStrRadix for BigUint {
645
643
impl FromStrRadix for BigUint {
646
644
/// Creates and initializes a `BigUint`.
647
645
#[ inline]
648
- fn from_str_radix ( s : & str , radix : uint )
649
- -> Option < BigUint > {
646
+ fn from_str_radix ( s : & str , radix : uint ) -> Option < BigUint > {
650
647
BigUint :: parse_bytes ( s. as_bytes ( ) , radix)
651
648
}
652
649
}
@@ -656,22 +653,19 @@ impl BigUint {
656
653
///
657
654
/// The digits are be in base 2^32.
658
655
#[ inline]
659
- pub fn new ( v : Vec < BigDigit > ) -> BigUint {
656
+ pub fn new ( mut digits : Vec < BigDigit > ) -> BigUint {
660
657
// 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 }
667
661
}
668
662
669
663
/// Creates and initializes a `BigUint`.
670
664
///
671
665
/// The digits are be in base 2^32.
672
666
#[ inline]
673
667
pub fn from_slice ( slice : & [ BigDigit ] ) -> BigUint {
674
- return BigUint :: new ( Vec :: from_slice ( slice) ) ;
668
+ BigUint :: new ( Vec :: from_slice ( slice) )
675
669
}
676
670
677
671
/// Creates and initializes a `BigUint`.
@@ -768,7 +762,6 @@ impl BigUint {
768
762
// `DoubleBigDigit` size dependent
769
763
#[ inline]
770
764
fn get_radix_base ( radix : uint ) -> ( DoubleBigDigit , uint ) {
771
- assert ! ( 1 < radix && radix <= 16 ) ;
772
765
match radix {
773
766
2 => ( 4294967296 , 32 ) ,
774
767
3 => ( 3486784401 , 20 ) ,
@@ -785,7 +778,7 @@ fn get_radix_base(radix: uint) -> (DoubleBigDigit, uint) {
785
778
14 => ( 1475789056 , 8 ) ,
786
779
15 => ( 2562890625 , 8 ) ,
787
780
16 => ( 4294967296 , 8 ) ,
788
- _ => fail ! ( )
781
+ _ => fail ! ( "The radix must be within (1, 16]" )
789
782
}
790
783
}
791
784
@@ -815,7 +808,7 @@ pub struct BigInt {
815
808
impl PartialEq for BigInt {
816
809
#[ inline]
817
810
fn eq ( & self , other : & BigInt ) -> bool {
818
- match self . cmp ( other) { Equal => true , _ => false }
811
+ self . cmp ( other) == Equal
819
812
}
820
813
}
821
814
@@ -824,7 +817,7 @@ impl Eq for BigInt {}
824
817
impl PartialOrd for BigInt {
825
818
#[ inline]
826
819
fn lt ( & self , other : & BigInt ) -> bool {
827
- match self . cmp ( other) { Less => true , _ => false }
820
+ self . cmp ( other) == Less
828
821
}
829
822
}
830
823
@@ -844,7 +837,7 @@ impl Ord for BigInt {
844
837
845
838
impl Default for BigInt {
846
839
#[ inline]
847
- fn default ( ) -> BigInt { BigInt :: new ( Zero , Vec :: new ( ) ) }
840
+ fn default ( ) -> BigInt { Zero :: zero ( ) }
848
841
}
849
842
850
843
impl fmt:: Show for BigInt {
@@ -929,8 +922,7 @@ impl Add<BigInt, BigInt> for BigInt {
929
922
match ( self . sign , other. sign ) {
930
923
( Zero , _) => other. clone ( ) ,
931
924
( _, 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 ) ,
934
926
( Plus , Minus ) => self - ( -* other) ,
935
927
( Minus , Plus ) => other - ( -* self ) ,
936
928
( Minus , Minus ) => -( ( -self ) + ( -* other) )
@@ -975,15 +967,15 @@ impl Div<BigInt, BigInt> for BigInt {
975
967
#[ inline]
976
968
fn div ( & self , other : & BigInt ) -> BigInt {
977
969
let ( q, _) = self . div_rem ( other) ;
978
- return q ;
970
+ q
979
971
}
980
972
}
981
973
982
974
impl Rem < BigInt , BigInt > for BigInt {
983
975
#[ inline]
984
976
fn rem ( & self , other : & BigInt ) -> BigInt {
985
977
let ( _, r) = self . div_rem ( other) ;
986
- return r ;
978
+ r
987
979
}
988
980
}
989
981
@@ -1045,13 +1037,13 @@ impl Integer for BigInt {
1045
1037
#[ inline]
1046
1038
fn div_floor ( & self , other : & BigInt ) -> BigInt {
1047
1039
let ( d, _) = self . div_mod_floor ( other) ;
1048
- return d ;
1040
+ d
1049
1041
}
1050
1042
1051
1043
#[ inline]
1052
1044
fn mod_floor ( & self , other : & BigInt ) -> BigInt {
1053
1045
let ( _, m) = self . div_mod_floor ( other) ;
1054
- return m ;
1046
+ m
1055
1047
}
1056
1048
1057
1049
fn div_mod_floor ( & self , other : & BigInt ) -> ( BigInt , BigInt ) {
@@ -1265,7 +1257,7 @@ impl<R: Rng> RandBigInt for R {
1265
1257
let final_digit: BigDigit = self . gen ( ) ;
1266
1258
data. push ( final_digit >> ( BigDigit :: bits - rem) ) ;
1267
1259
}
1268
- return BigUint :: new ( data) ;
1260
+ BigUint :: new ( data)
1269
1261
}
1270
1262
1271
1263
fn gen_bigint ( & mut self , bit_size : uint ) -> BigInt {
@@ -1287,7 +1279,7 @@ impl<R: Rng> RandBigInt for R {
1287
1279
} else {
1288
1280
Minus
1289
1281
} ;
1290
- return BigInt :: from_biguint ( sign, biguint) ;
1282
+ BigInt :: from_biguint ( sign, biguint)
1291
1283
}
1292
1284
1293
1285
fn gen_biguint_below ( & mut self , bound : & BigUint ) -> BigUint {
@@ -1322,8 +1314,8 @@ impl BigInt {
1322
1314
///
1323
1315
/// The digits are be in base 2^32.
1324
1316
#[ 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 ) )
1327
1319
}
1328
1320
1329
1321
/// Creates and initializes a `BigInt`.
@@ -1334,7 +1326,7 @@ impl BigInt {
1334
1326
if sign == Zero || data. is_zero ( ) {
1335
1327
return BigInt { sign : Zero , data : Zero :: zero ( ) } ;
1336
1328
}
1337
- return BigInt { sign : sign, data : data } ;
1329
+ BigInt { sign : sign, data : data }
1338
1330
}
1339
1331
1340
1332
/// Creates and initializes a `BigInt`.
@@ -1344,8 +1336,7 @@ impl BigInt {
1344
1336
}
1345
1337
1346
1338
/// 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 > {
1349
1340
if buf. is_empty ( ) { return None ; }
1350
1341
let mut sign = Plus ;
1351
1342
let mut start = 0 ;
0 commit comments