Skip to content

Commit

Permalink
Merge RandBigUint and RandBigInt into single trait
Browse files Browse the repository at this point in the history
  • Loading branch information
dcrewi committed Sep 9, 2013
1 parent 54368af commit 4946e0e
Showing 1 changed file with 17 additions and 21 deletions.
38 changes: 17 additions & 21 deletions src/libextra/num/bigint.rs
Expand Up @@ -521,27 +521,6 @@ impl FromStrRadix for BigUint {
}
}

trait RandBigUInt {
/// Generate a random BigUint of the given bit size.
fn gen_biguint(&mut self, bit_size: uint) -> BigUint;
}

impl<R: Rng> RandBigUInt for R {
/// Generate a random BigUint of the given bit size.
fn gen_biguint(&mut self, bit_size: uint) -> BigUint {
let (digits, rem) = bit_size.div_rem(&BigDigit::bits);
let mut data = vec::with_capacity(digits+1);
for _ in range(0, digits) {
data.push(self.gen());
}
if rem > 0 {
let final_digit: BigDigit = self.gen();
data.push(final_digit >> (BigDigit::bits - rem));
}
return BigUint::new(data);
}
}

impl BigUint {
/// Creates and initializes an BigUint.
#[inline]
Expand Down Expand Up @@ -1074,12 +1053,29 @@ impl FromStrRadix for BigInt {
}
trait RandBigInt {
/// Generate a random BigUint of the given bit size.
fn gen_biguint(&mut self, bit_size: uint) -> BigUint;
/// Generate a random BigInt of the given bit size.
fn gen_bigint(&mut self, bit_size: uint) -> BigInt;
}
impl<R: Rng> RandBigInt for R {
/// Generate a random BigUint of the given bit size.
fn gen_biguint(&mut self, bit_size: uint) -> BigUint {
let (digits, rem) = bit_size.div_rem(&BigDigit::bits);
let mut data = vec::with_capacity(digits+1);
for _ in range(0, digits) {
data.push(self.gen());
}
if rem > 0 {
let final_digit: BigDigit = self.gen();
data.push(final_digit >> (BigDigit::bits - rem));
}
return BigUint::new(data);
}
/// Generate a random BigInt of the given bit size.
fn gen_bigint(&mut self, bit_size: uint) -> BigInt {
// Generate a random BigUint...
let biguint = self.gen_biguint(bit_size);
Expand Down

5 comments on commit 4946e0e

@bors
Copy link
Contributor

@bors bors commented on 4946e0e Sep 11, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from huonw
at dcrewi@4946e0e

@bors
Copy link
Contributor

@bors bors commented on 4946e0e Sep 11, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging dcrewi/rust/random-bigints = 4946e0e into auto

@bors
Copy link
Contributor

@bors bors commented on 4946e0e Sep 11, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dcrewi/rust/random-bigints = 4946e0e merged ok, testing candidate = 5bb8aef

@bors
Copy link
Contributor

@bors bors commented on 4946e0e Sep 11, 2013

@bors
Copy link
Contributor

@bors bors commented on 4946e0e Sep 11, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 5bb8aef

Please sign in to comment.