Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add iter_u32_digits , iter_u64_digits, to_u64_digits to both BigInt and BigUint #158

Closed
wants to merge 14 commits into from
Closed
32 changes: 32 additions & 0 deletions benches/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -393,3 +393,35 @@ fn modpow_even(b: &mut Bencher) {

b.iter(|| base.modpow(&e, &m));
}

#[bench]
fn to_u32_digits(b: &mut Bencher) {
let mut rng = get_rng();
let n = rng.gen_biguint(2048);

b.iter(|| n.to_u32_digits());
}

#[bench]
fn iter_u32_digits(b: &mut Bencher) {
let mut rng = get_rng();
let n = rng.gen_biguint(2048);

b.iter(|| n.iter_u32_digits().max());
}

#[bench]
fn to_u64_digits(b: &mut Bencher) {
let mut rng = get_rng();
let n = rng.gen_biguint(2048);

b.iter(|| n.to_u64_digits());
}

#[bench]
fn iter_u64_digits(b: &mut Bencher) {
let mut rng = get_rng();
let n = rng.gen_biguint(2048);

b.iter(|| n.iter_u64_digits().max());
}
59 changes: 58 additions & 1 deletion src/bigint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use self::Sign::{Minus, NoSign, Plus};
use crate::big_digit::{self, BigDigit, DoubleBigDigit};
use crate::biguint;
use crate::biguint::to_str_radix_reversed;
use crate::biguint::{BigUint, IntDigits};
use crate::biguint::{BigUint, IntDigits, IterU32Digits, IterU64Digits};
use crate::ParseBigIntError;
#[cfg(has_try_from)]
use crate::TryFromBigIntError;
Expand Down Expand Up @@ -2988,6 +2988,63 @@ impl BigInt {
(self.sign, self.data.to_u32_digits())
}

/// Returns the sign and the `u32` digits representation of the `BigInt` ordered least
Speedy37 marked this conversation as resolved.
Show resolved Hide resolved
/// significant digit first.
///
/// # Examples
///
/// ```
/// use num_bigint::{BigInt, Sign};
///
/// assert_eq!(BigInt::from(-1125).to_u64_digits(), (Sign::Minus, vec![1125]));
/// assert_eq!(BigInt::from(4294967295u32).to_u64_digits(), (Sign::Plus, vec![4294967295]));
/// assert_eq!(BigInt::from(4294967296u64).to_u64_digits(), (Sign::Plus, vec![4294967296]));
/// assert_eq!(BigInt::from(-112500000000i64).to_u64_digits(), (Sign::Minus, vec![112500000000]));
/// assert_eq!(BigInt::from(112500000000i64).to_u64_digits(), (Sign::Plus, vec![112500000000]));
/// ```
#[inline]
pub fn to_u64_digits(&self) -> (Sign, Vec<u64>) {
(self.sign, self.data.to_u64_digits())
}

/// Returns the `u32` digits representation of the `BigUint` ordered least significant digit
/// first.
///
/// # Examples
///
/// ```
/// use num_bigint::BigInt;
///
/// assert_eq!(BigInt::from(-1125).iter_u32_digits().collect::<Vec<u32>>(), vec![1125]);
/// assert_eq!(BigInt::from(4294967295u32).iter_u32_digits().collect::<Vec<u32>>(), vec![4294967295]);
/// assert_eq!(BigInt::from(4294967296u64).iter_u32_digits().collect::<Vec<u32>>(), vec![0, 1]);
/// assert_eq!(BigInt::from(-112500000000i64).iter_u32_digits().collect::<Vec<u32>>(), vec![830850304, 26]);
/// assert_eq!(BigInt::from(112500000000i64).iter_u32_digits().collect::<Vec<u32>>(), vec![830850304, 26]);
/// ```
#[inline]
pub fn iter_u32_digits<'a>(&'a self) -> IterU32Digits<'a> {
cuviper marked this conversation as resolved.
Show resolved Hide resolved
self.data.iter_u32_digits()
}

/// Returns the `u32` digits representation of the `BigUint` ordered least significant digit
Speedy37 marked this conversation as resolved.
Show resolved Hide resolved
/// first.
///
/// # Examples
///
/// ```
/// use num_bigint::BigInt;
///
/// assert_eq!(BigInt::from(-1125).iter_u64_digits().collect::<Vec<u64>>(), vec![1125u64]);
/// assert_eq!(BigInt::from(4294967295u32).iter_u64_digits().collect::<Vec<u64>>(), vec![4294967295u64]);
/// assert_eq!(BigInt::from(4294967296u64).iter_u64_digits().collect::<Vec<u64>>(), vec![4294967296u64]);
/// assert_eq!(BigInt::from(-112500000000i64).iter_u64_digits().collect::<Vec<u64>>(), vec![112500000000u64]);
/// assert_eq!(BigInt::from(112500000000i64).iter_u64_digits().collect::<Vec<u64>>(), vec![112500000000u64]);
/// ```
#[inline]
pub fn iter_u64_digits<'a>(&'a self) -> IterU64Digits<'a> {
self.data.iter_u64_digits()
}

/// Returns the two's-complement byte representation of the `BigInt` in big-endian byte order.
///
/// # Examples
Expand Down