@@ -207,7 +207,7 @@ impl Add<BigUint, BigUint> for BigUint {
207
207
let new_len = uint:: max ( self . data . len ( ) , other. data . len ( ) ) ;
208
208
209
209
let mut carry = 0 ;
210
- let sum = do vec:: from_fn ( new_len) |i| {
210
+ let mut sum = do vec:: from_fn ( new_len) |i| {
211
211
let ai = if i < self . data . len ( ) { self . data [ i] } else { 0 } ;
212
212
let bi = if i < other. data . len ( ) { other. data [ i] } else { 0 } ;
213
213
let ( hi, lo) = BigDigit :: from_uint (
@@ -216,8 +216,8 @@ impl Add<BigUint, BigUint> for BigUint {
216
216
carry = hi;
217
217
lo
218
218
} ;
219
- if carry == 0 { return BigUint :: new ( sum) } ;
220
- return BigUint :: new ( sum + [ carry ] ) ;
219
+ if carry != 0 { sum. push ( carry ) ; }
220
+ return BigUint :: new ( sum) ;
221
221
}
222
222
}
223
223
@@ -284,15 +284,15 @@ impl Mul<BigUint, BigUint> for BigUint {
284
284
if n == 1 { return copy * a; }
285
285
286
286
let mut carry = 0 ;
287
- let prod = do a. data . iter ( ) . transform |ai| {
287
+ let mut prod = do a. data . iter ( ) . transform |ai| {
288
288
let ( hi, lo) = BigDigit :: from_uint (
289
289
( * ai as uint ) * ( n as uint ) + ( carry as uint )
290
290
) ;
291
291
carry = hi;
292
292
lo
293
293
} . collect :: < ~[ BigDigit ] > ( ) ;
294
- if carry == 0 { return BigUint :: new ( prod) } ;
295
- return BigUint :: new ( prod + [ carry ] ) ;
294
+ if carry != 0 { prod. push ( carry ) ; }
295
+ return BigUint :: new ( prod) ;
296
296
}
297
297
298
298
@@ -520,10 +520,12 @@ impl ToStrRadix for BigUint {
520
520
521
521
fn fill_concat( v : & [ BigDigit ] , radix : uint , l : uint ) -> ~str {
522
522
if v. is_empty ( ) { return ~"0 " }
523
- let s = vec:: reversed ( v) . map ( |n| {
524
- let s = uint:: to_str_radix ( * n as uint , radix) ;
525
- str:: from_chars ( vec:: from_elem ( l - s. len ( ) , '0' ) ) + s
526
- } ) . concat ( ) ;
523
+ let mut s = str:: with_capacity ( v. len ( ) * l) ;
524
+ for v. rev_iter( ) . advance |n| {
525
+ let ss = uint:: to_str_radix( * n as uint, radix) ;
526
+ s. push_str( "0" . repeat( l - ss. len( ) ) ) ;
527
+ s. push_str( ss) ;
528
+ }
527
529
s. trim_left_chars( & '0' ) . to_owned( )
528
530
}
529
531
}
@@ -619,15 +621,15 @@ impl BigUint {
619
621
if n_bits == 0 || self . is_zero ( ) { return copy * self ; }
620
622
621
623
let mut carry = 0 ;
622
- let shifted = do self . data . iter ( ) . transform |elem| {
624
+ let mut shifted = do self . data . iter ( ) . transform |elem| {
623
625
let ( hi, lo) = BigDigit :: from_uint (
624
626
( * elem as uint ) << n_bits | ( carry as uint )
625
627
) ;
626
628
carry = hi;
627
629
lo
628
630
} . collect :: < ~[ BigDigit ] > ( ) ;
629
- if carry == 0 { return BigUint :: new ( shifted) ; }
630
- return BigUint :: new ( shifted + [ carry ] ) ;
631
+ if carry != 0 { shifted. push ( carry ) ; }
632
+ return BigUint :: new ( shifted) ;
631
633
}
632
634
633
635
@@ -1629,7 +1631,6 @@ mod bigint_tests {
1629
1631
use std:: int;
1630
1632
use std:: num:: { IntConvertible, Zero, One, FromStrRadix} ;
1631
1633
use std:: uint;
1632
- use std:: vec;
1633
1634
1634
1635
#[ test]
1635
1636
fn test_from_biguint ( ) {
@@ -1646,9 +1647,11 @@ mod bigint_tests {
1646
1647
1647
1648
#[ test]
1648
1649
fn test_cmp( ) {
1649
- let vs = [ & [ 2 ] , & [ 1 , 1 ] , & [ 2 , 1 ] , & [ 1 , 1 , 1 ] ] ;
1650
- let mut nums = vec:: reversed( vs)
1651
- . map( |s| BigInt :: from_slice( Minus , * s) ) ;
1650
+ let vs = [ & [ 2 as BigDigit ] , & [ 1 , 1 ] , & [ 2 , 1 ] , & [ 1 , 1 , 1 ] ] ;
1651
+ let mut nums = ~[ ] ;
1652
+ for vs. rev_iter( ) . advance |s| {
1653
+ nums. push( BigInt :: from_slice( Minus , * s) ) ;
1654
+ }
1652
1655
nums. push ( Zero :: zero ( ) ) ;
1653
1656
nums. push_all_move( vs. map( |s| BigInt :: from_slice( Plus , * s) ) ) ;
1654
1657
0 commit comments