19
19
//! ## Example
20
20
//!
21
21
//! ```rust
22
- //! use num::bigint::BigUint;
23
- //! use std::num::{Zero, One};
22
+ //! use num::{BigUint, Zero, One};
24
23
//! use std::mem::replace;
25
24
//!
26
25
//! // Calculate large fibonacci numbers.
@@ -61,14 +60,14 @@ use rand::Rng;
61
60
62
61
use std:: { cmp, fmt, hash} ;
63
62
use std:: default:: Default ;
64
- use std:: from_str:: FromStr ;
65
- use std:: num:: CheckedDiv ;
66
- use std:: num:: { ToPrimitive , FromPrimitive } ;
67
- use std:: num:: { Zero , One , FromStrRadix } ;
68
- use std:: str;
69
- use std:: string:: String ;
63
+ use std:: iter:: { AdditiveIterator , MultiplicativeIterator } ;
64
+ use std:: num:: { Int , ToPrimitive , FromPrimitive } ;
65
+ use std:: num:: FromStrRadix ;
66
+ use std:: str:: { mod, FromStr } ;
70
67
use std:: { i64, u64} ;
71
68
69
+ use { Num , Unsigned , CheckedAdd , CheckedSub , CheckedMul , CheckedDiv , Signed , Zero , One } ;
70
+
72
71
/// A `BigDigit` is a `BigUint`'s composing element.
73
72
pub type BigDigit = u32 ;
74
73
@@ -739,6 +738,20 @@ impl FromStrRadix for BigUint {
739
738
}
740
739
}
741
740
741
+ impl < T : Iterator < BigUint > > AdditiveIterator < BigUint > for T {
742
+ fn sum ( & mut self ) -> BigUint {
743
+ let init: BigUint = Zero :: zero ( ) ;
744
+ self . fold ( init, |acc, x| acc + x)
745
+ }
746
+ }
747
+
748
+ impl < T : Iterator < BigUint > > MultiplicativeIterator < BigUint > for T {
749
+ fn product ( & mut self ) -> BigUint {
750
+ let init: BigUint = One :: one ( ) ;
751
+ self . fold ( init, |acc, x| acc * x)
752
+ }
753
+ }
754
+
742
755
impl BigUint {
743
756
/// Creates and initializes a `BigUint`.
744
757
///
@@ -1084,7 +1097,6 @@ impl CheckedDiv for BigInt {
1084
1097
}
1085
1098
}
1086
1099
1087
-
1088
1100
impl Integer for BigInt {
1089
1101
#[ inline]
1090
1102
fn div_rem ( & self , other : & BigInt ) -> ( BigInt , BigInt ) {
@@ -1374,6 +1386,20 @@ impl<R: Rng> RandBigInt for R {
1374
1386
}
1375
1387
}
1376
1388
1389
+ impl < T : Iterator < BigInt > > AdditiveIterator < BigInt > for T {
1390
+ fn sum ( & mut self ) -> BigInt {
1391
+ let init: BigInt = Zero :: zero ( ) ;
1392
+ self . fold ( init, |acc, x| acc + x)
1393
+ }
1394
+ }
1395
+
1396
+ impl < T : Iterator < BigInt > > MultiplicativeIterator < BigInt > for T {
1397
+ fn product ( & mut self ) -> BigInt {
1398
+ let init: BigInt = One :: one ( ) ;
1399
+ self . fold ( init, |acc, x| acc * x)
1400
+ }
1401
+ }
1402
+
1377
1403
impl BigInt {
1378
1404
/// Creates and initializes a BigInt.
1379
1405
///
@@ -1416,6 +1442,29 @@ impl BigInt {
1416
1442
Minus => None
1417
1443
}
1418
1444
}
1445
+
1446
+ #[ inline]
1447
+ pub fn checked_add ( & self , v : & BigInt ) -> Option < BigInt > {
1448
+ return Some ( self . add ( v) ) ;
1449
+ }
1450
+
1451
+ #[ inline]
1452
+ pub fn checked_sub ( & self , v : & BigInt ) -> Option < BigInt > {
1453
+ return Some ( self . sub ( v) ) ;
1454
+ }
1455
+
1456
+ #[ inline]
1457
+ pub fn checked_mul ( & self , v : & BigInt ) -> Option < BigInt > {
1458
+ return Some ( self . mul ( v) ) ;
1459
+ }
1460
+
1461
+ #[ inline]
1462
+ pub fn checked_div ( & self , v : & BigInt ) -> Option < BigInt > {
1463
+ if v. is_zero ( ) {
1464
+ return None ;
1465
+ }
1466
+ return Some ( self . div ( v) ) ;
1467
+ }
1419
1468
}
1420
1469
1421
1470
#[ cfg( test) ]
@@ -1425,15 +1474,16 @@ mod biguint_tests {
1425
1474
use super :: { Plus , BigInt , RandBigInt , ToBigInt } ;
1426
1475
1427
1476
use std:: cmp:: { Less , Equal , Greater } ;
1428
- use std:: from_str :: FromStr ;
1477
+ use std:: str :: FromStr ;
1429
1478
use std:: i64;
1430
- use std:: num:: { Zero , One , FromStrRadix } ;
1479
+ use std:: num:: FromStrRadix ;
1431
1480
use std:: num:: { ToPrimitive , FromPrimitive } ;
1432
- use std:: num:: CheckedDiv ;
1433
1481
use std:: rand:: task_rng;
1434
1482
use std:: u64;
1435
1483
use std:: hash:: hash;
1436
1484
1485
+ use { Zero , One , CheckedAdd , CheckedSub , CheckedMul , CheckedDiv } ;
1486
+
1437
1487
#[ test]
1438
1488
fn test_from_slice ( ) {
1439
1489
fn check ( slice : & [ BigDigit ] , data : & [ BigDigit ] ) {
@@ -2289,13 +2339,14 @@ mod bigint_tests {
2289
2339
2290
2340
use std:: cmp:: { Less , Equal , Greater } ;
2291
2341
use std:: i64;
2292
- use std:: num:: CheckedDiv ;
2293
- use std:: num:: { Zero , One , FromStrRadix } ;
2342
+ use std:: num:: FromStrRadix ;
2294
2343
use std:: num:: { ToPrimitive , FromPrimitive } ;
2295
2344
use std:: rand:: task_rng;
2296
2345
use std:: u64;
2297
2346
use std:: hash:: hash;
2298
2347
2348
+ use { Zero , One , Signed } ;
2349
+
2299
2350
#[ test]
2300
2351
fn test_from_biguint ( ) {
2301
2352
fn check ( inp_s : Sign , inp_n : uint , ans_s : Sign , ans_n : uint ) {
@@ -2882,7 +2933,9 @@ mod bench {
2882
2933
use super :: BigUint ;
2883
2934
use std:: iter;
2884
2935
use std:: mem:: replace;
2885
- use std:: num:: { FromPrimitive , Zero , One } ;
2936
+ use std:: num:: FromPrimitive ;
2937
+
2938
+ use { Zero , One } ;
2886
2939
2887
2940
fn factorial ( n : uint ) -> BigUint {
2888
2941
let mut f: BigUint = One :: one ( ) ;
0 commit comments