Skip to content

Commit d9287a4

Browse files
bors[bot]Speedy37Vincent Rouillécuviper
authored
Merge #192
192: Add iter_u32_digits , iter_u64_digits, to_u64_digits to both BigInt and BigUint r=cuviper a=cuviper This is a rebase of #158, and I also addressed my last review comments there. I did rename the iterators to `U32Digits` and `U64Digits`, but I left the `iter_` prefix on the methods so they're not confused with the similar `to_` methods. Co-authored-by: Vincent Rouillé <[email protected]> Co-authored-by: Vincent Rouillé <[email protected]> Co-authored-by: Vincent Rouillé <[email protected]> Co-authored-by: Josh Stone <[email protected]>
2 parents 7492eec + 0037a7f commit d9287a4

File tree

4 files changed

+428
-31
lines changed

4 files changed

+428
-31
lines changed

benches/bigint.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,3 +393,35 @@ fn modpow_even(b: &mut Bencher) {
393393

394394
b.iter(|| base.modpow(&e, &m));
395395
}
396+
397+
#[bench]
398+
fn to_u32_digits(b: &mut Bencher) {
399+
let mut rng = get_rng();
400+
let n = rng.gen_biguint(2048);
401+
402+
b.iter(|| n.to_u32_digits());
403+
}
404+
405+
#[bench]
406+
fn iter_u32_digits(b: &mut Bencher) {
407+
let mut rng = get_rng();
408+
let n = rng.gen_biguint(2048);
409+
410+
b.iter(|| n.iter_u32_digits().max());
411+
}
412+
413+
#[bench]
414+
fn to_u64_digits(b: &mut Bencher) {
415+
let mut rng = get_rng();
416+
let n = rng.gen_biguint(2048);
417+
418+
b.iter(|| n.to_u64_digits());
419+
}
420+
421+
#[bench]
422+
fn iter_u64_digits(b: &mut Bencher) {
423+
let mut rng = get_rng();
424+
let n = rng.gen_biguint(2048);
425+
426+
b.iter(|| n.iter_u64_digits().max());
427+
}

src/bigint.rs

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use self::Sign::{Minus, NoSign, Plus};
3131
use crate::big_digit::{self, BigDigit, DoubleBigDigit};
3232
use crate::biguint;
3333
use crate::biguint::to_str_radix_reversed;
34-
use crate::biguint::{BigUint, IntDigits};
34+
use crate::biguint::{BigUint, IntDigits, U32Digits, U64Digits};
3535
use crate::ParseBigIntError;
3636
#[cfg(has_try_from)]
3737
use crate::TryFromBigIntError;
@@ -2981,6 +2981,65 @@ impl BigInt {
29812981
(self.sign, self.data.to_u32_digits())
29822982
}
29832983

2984+
/// Returns the sign and the `u64` digits representation of the `BigInt` ordered least
2985+
/// significant digit first.
2986+
///
2987+
/// # Examples
2988+
///
2989+
/// ```
2990+
/// use num_bigint::{BigInt, Sign};
2991+
///
2992+
/// assert_eq!(BigInt::from(-1125).to_u64_digits(), (Sign::Minus, vec![1125]));
2993+
/// assert_eq!(BigInt::from(4294967295u32).to_u64_digits(), (Sign::Plus, vec![4294967295]));
2994+
/// assert_eq!(BigInt::from(4294967296u64).to_u64_digits(), (Sign::Plus, vec![4294967296]));
2995+
/// assert_eq!(BigInt::from(-112500000000i64).to_u64_digits(), (Sign::Minus, vec![112500000000]));
2996+
/// assert_eq!(BigInt::from(112500000000i64).to_u64_digits(), (Sign::Plus, vec![112500000000]));
2997+
/// assert_eq!(BigInt::from(1u128 << 64).to_u64_digits(), (Sign::Plus, vec![0, 1]));
2998+
/// ```
2999+
#[inline]
3000+
pub fn to_u64_digits(&self) -> (Sign, Vec<u64>) {
3001+
(self.sign, self.data.to_u64_digits())
3002+
}
3003+
3004+
/// Returns an iterator of `u32` digits representation of the `BigInt` ordered least
3005+
/// significant digit first.
3006+
///
3007+
/// # Examples
3008+
///
3009+
/// ```
3010+
/// use num_bigint::BigInt;
3011+
///
3012+
/// assert_eq!(BigInt::from(-1125).iter_u32_digits().collect::<Vec<u32>>(), vec![1125]);
3013+
/// assert_eq!(BigInt::from(4294967295u32).iter_u32_digits().collect::<Vec<u32>>(), vec![4294967295]);
3014+
/// assert_eq!(BigInt::from(4294967296u64).iter_u32_digits().collect::<Vec<u32>>(), vec![0, 1]);
3015+
/// assert_eq!(BigInt::from(-112500000000i64).iter_u32_digits().collect::<Vec<u32>>(), vec![830850304, 26]);
3016+
/// assert_eq!(BigInt::from(112500000000i64).iter_u32_digits().collect::<Vec<u32>>(), vec![830850304, 26]);
3017+
/// ```
3018+
#[inline]
3019+
pub fn iter_u32_digits(&self) -> U32Digits<'_> {
3020+
self.data.iter_u32_digits()
3021+
}
3022+
3023+
/// Returns an iterator of `u64` digits representation of the `BigInt` ordered least
3024+
/// significant digit first.
3025+
///
3026+
/// # Examples
3027+
///
3028+
/// ```
3029+
/// use num_bigint::BigInt;
3030+
///
3031+
/// assert_eq!(BigInt::from(-1125).iter_u64_digits().collect::<Vec<u64>>(), vec![1125u64]);
3032+
/// assert_eq!(BigInt::from(4294967295u32).iter_u64_digits().collect::<Vec<u64>>(), vec![4294967295u64]);
3033+
/// assert_eq!(BigInt::from(4294967296u64).iter_u64_digits().collect::<Vec<u64>>(), vec![4294967296u64]);
3034+
/// assert_eq!(BigInt::from(-112500000000i64).iter_u64_digits().collect::<Vec<u64>>(), vec![112500000000u64]);
3035+
/// assert_eq!(BigInt::from(112500000000i64).iter_u64_digits().collect::<Vec<u64>>(), vec![112500000000u64]);
3036+
/// assert_eq!(BigInt::from(1u128 << 64).iter_u64_digits().collect::<Vec<u64>>(), vec![0, 1]);
3037+
/// ```
3038+
#[inline]
3039+
pub fn iter_u64_digits(&self) -> U64Digits<'_> {
3040+
self.data.iter_u64_digits()
3041+
}
3042+
29843043
/// Returns the two's-complement byte representation of the `BigInt` in big-endian byte order.
29853044
///
29863045
/// # Examples

0 commit comments

Comments
 (0)