Refactoring for a more convenient BigInt api
timw4mail/rusty-numbers/pipeline/head This commit looks good Details

This commit is contained in:
Timothy Warren 2022-02-15 10:20:16 -05:00
parent 12c43bf9ab
commit 52ce18b897
2 changed files with 89 additions and 90 deletions

View File

@ -10,8 +10,8 @@ coverage:
# Remove generated files # Remove generated files
clean: clean:
cargo clean cargo clean
rm cobertura.xml rm -f cobertura.xml
rm coverage.html rm -f coverage.html
# Check code style # Check code style
lint: lint:

View File

@ -13,7 +13,7 @@ use core::convert::TryInto;
use core::ops::{ use core::ops::{
Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Not, Rem, RemAssign, Sub, SubAssign, Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Not, Rem, RemAssign, Sub, SubAssign,
}; };
use core::usize; use core::prelude::v1::*;
#[cfg(feature = "std")] #[cfg(feature = "std")]
use std::prelude::v1::*; use std::prelude::v1::*;
@ -28,10 +28,10 @@ pub struct BigInt {
#[macro_export] #[macro_export]
macro_rules! big_int { macro_rules! big_int {
($w:literal) => { ($w:literal) => {
$crate::bigint::BigInt::from($w) $crate::bigint::BigInt::new($w)
}; };
(- $x:literal) => { (- $x:literal) => {
$crate::bigint::BigInt::from(-$x) $crate::bigint::BigInt::new(-$x)
}; };
} }
@ -55,25 +55,26 @@ impl From<usize> for BigInt {
impl From<&str> for BigInt { impl From<&str> for BigInt {
fn from(s: &str) -> Self { fn from(s: &str) -> Self {
Self::from_str_radix(s, 10) Self::from_str(s, 10)
} }
} }
impl From<String> for BigInt { impl From<String> for BigInt {
fn from(s: String) -> Self { fn from(s: String) -> Self {
Self::from_str_radix(&s, 10) Self::from_str(&s, 10)
} }
} }
impl BigInt { impl BigInt {
/// Create a new Bigint, of value 0 /// Create a new Bigint, of value 0
/// ///
/// The various `From` implementations are more useful in most cases /// In most cases, you probably want to use [new](`BigInt::new`) instead
#[must_use] #[must_use]
pub fn zero() -> Self { pub fn zero() -> Self {
Self::default() Self::default()
} }
/// Create a new `BigInt` from an existing Rust primitive type
pub fn new(initial: impl Into<BigInt>) -> Self { pub fn new(initial: impl Into<BigInt>) -> Self {
initial.into() initial.into()
} }
@ -130,7 +131,7 @@ impl BigInt {
/// Convert a `&str` or a `String` representing a number in the specified radix to a Bigint. /// Convert a `&str` or a `String` representing a number in the specified radix to a Bigint.
/// ///
/// For radix 10, use the `from` associated function instead. /// For radix 10, use the `new` or `from` associated function instead.
/// ///
/// Radix must be between 1 and 36, inclusive, with radix higher /// Radix must be between 1 and 36, inclusive, with radix higher
/// than 11 represented by A-Z /// than 11 represented by A-Z
@ -141,7 +142,7 @@ impl BigInt {
/// # Panics /// # Panics
/// * If radix is not between 1 and 36 inclusive /// * If radix is not between 1 and 36 inclusive
/// * Some branches are not yet implemented /// * Some branches are not yet implemented
pub fn from_str_radix<T: ToString + ?Sized>(s: &T, radix: usize) -> BigInt { pub fn from_str<T: ToString + ?Sized>(s: &T, radix: usize) -> BigInt {
// Two lines due to borrow checker complaints // Two lines due to borrow checker complaints
let input = s.to_string().to_ascii_uppercase(); let input = s.to_string().to_ascii_uppercase();
let input = input.trim(); let input = input.trim();
@ -162,12 +163,12 @@ impl BigInt {
} }
} }
return BigInt::from(raw_digits.len()); return BigInt::new(raw_digits.len());
} }
// If the number fits in a usize, try the easy way // If the number fits in a usize, try the easy way
if let Ok(easy_res) = usize::from_str_radix(input, radix.try_into().unwrap()) { if let Ok(easy_res) = usize::from_str_radix(input, radix.try_into().unwrap()) {
return BigInt::from(easy_res); return BigInt::new(easy_res);
} }
// TODO: consider parsing out the error, to tell if the // TODO: consider parsing out the error, to tell if the
@ -289,7 +290,7 @@ impl Add for BigInt {
if overflowed { if overflowed {
out.inner.push(res + carry); out.inner.push(res + carry);
carry = 1; carry = 1;
} else if res < core::usize::MAX { } else if res < usize::MAX {
out.inner.push(res + carry); out.inner.push(res + carry);
carry = 0; carry = 0;
} else { } else {
@ -339,7 +340,7 @@ impl Sub for BigInt {
// In base ten, this would be like: // In base ten, this would be like:
// 15 - 8 = (9 - 8) + (5 + 1) // 15 - 8 = (9 - 8) + (5 + 1)
let rem = (a + 1) - borrow; let rem = (a + 1) - borrow;
let res = (core::usize::MAX - b) + rem; let res = (usize::MAX - b) + rem;
out.inner.push(res); out.inner.push(res);
borrow = 1; borrow = 1;
@ -527,8 +528,7 @@ macro_rules! impl_from_larger {
impl From<$s> for BigInt { impl From<$s> for BigInt {
/// Create a `BigInt` from a signed integer primitive /// Create a `BigInt` from a signed integer primitive
fn from(n: $s) -> Self { fn from(n: $s) -> Self {
use core::usize::MAX; let target_radix: $s = (usize::MAX as $s) + 1;
let target_radix: $s = (MAX as $s) + 1;
let sign = if n < 0 { Sign::Negative } else { Sign::Positive }; let sign = if n < 0 { Sign::Negative } else { Sign::Positive };
let n = n.abs(); let n = n.abs();
@ -562,8 +562,7 @@ macro_rules! impl_from_larger {
impl From<$u> for BigInt { impl From<$u> for BigInt {
/// Create a `BigInt` from an unsigned integer primitive /// Create a `BigInt` from an unsigned integer primitive
fn from(n: $u) -> Self { fn from(n: $u) -> Self {
use core::usize::MAX; let target_radix: $u = (usize::MAX as $u) + 1;
let target_radix: $u = (MAX as $u) + 1;
let mut quotient = n / target_radix; let mut quotient = n / target_radix;
let mut rem = n % target_radix; let mut rem = n % target_radix;
@ -601,28 +600,28 @@ macro_rules! impl_ord_literal {
impl PartialEq<$prim> for BigInt { impl PartialEq<$prim> for BigInt {
#[must_use] #[must_use]
fn eq(&self, other: &$prim) -> bool { fn eq(&self, other: &$prim) -> bool {
self == &BigInt::from(*other) self == &BigInt::new(*other)
} }
} }
impl PartialEq<BigInt> for $prim { impl PartialEq<BigInt> for $prim {
#[must_use] #[must_use]
fn eq(&self, other: &BigInt) -> bool { fn eq(&self, other: &BigInt) -> bool {
&BigInt::from(*self) == other &BigInt::new(*self) == other
} }
} }
impl PartialOrd<$prim> for BigInt { impl PartialOrd<$prim> for BigInt {
#[must_use] #[must_use]
fn partial_cmp(&self, other: &$prim) -> Option<Ordering> { fn partial_cmp(&self, other: &$prim) -> Option<Ordering> {
self.partial_cmp(&BigInt::from(*other)) self.partial_cmp(&BigInt::new(*other))
} }
} }
impl PartialOrd<BigInt> for $prim { impl PartialOrd<BigInt> for $prim {
#[must_use] #[must_use]
fn partial_cmp(&self, other: &BigInt) -> Option<Ordering> { fn partial_cmp(&self, other: &BigInt) -> Option<Ordering> {
(&BigInt::from(*self)).partial_cmp(other) (&BigInt::new(*self)).partial_cmp(other)
} }
} }
)+ )+
@ -647,23 +646,23 @@ impl_ord_literal!(i8, u8, i16, u16, i32, u32, i64, u64, i128, u128);
mod tests { mod tests {
use super::*; use super::*;
const RADIX: u128 = core::usize::MAX as u128 + 1; const RADIX: u128 = usize::MAX as u128 + 1;
const I_RADIX: i128 = core::usize::MAX as i128 + 1; const I_RADIX: i128 = usize::MAX as i128 + 1;
#[test] #[test]
fn sanity_checks() { fn sanity_checks() {
let int = BigInt::from(45u8); let int = BigInt::new(45u8);
assert_eq!(int.inner[0], 45usize) assert_eq!(int.inner[0], 45usize)
} }
#[test] #[test]
fn test_macro() { fn test_macro() {
let a = big_int!(75); let a = big_int!(75);
let b = BigInt::from(75); let b = BigInt::new(75);
assert_eq!(a, b); assert_eq!(a, b);
let a = big_int!(-75); let a = big_int!(-75);
let b = BigInt::from(-75); let b = BigInt::new(-75);
assert_eq!(a, b); assert_eq!(a, b);
} }
@ -676,7 +675,7 @@ mod tests {
lots_of_leading.trim_zeros(); lots_of_leading.trim_zeros();
assert_eq!(BigInt::from(1), lots_of_leading); assert_eq!(BigInt::new(1), lots_of_leading);
} }
#[test] #[test]
@ -685,32 +684,32 @@ mod tests {
// so the sum should be // so the sum should be
// [MAX -1, 1] // [MAX -1, 1]
// Compare base 10: 9 + 9 = 18 // Compare base 10: 9 + 9 = 18
let a = BigInt::from(core::usize::MAX); let a = BigInt::new(usize::MAX);
let b = BigInt::from(core::usize::MAX); let b = BigInt::new(usize::MAX);
let sum = a + b; let sum = a + b;
assert_eq!( assert_eq!(
sum.inner[0], sum.inner[0],
core::usize::MAX - 1, usize::MAX - 1,
"least significant place should be MAX - 1" "least significant place should be MAX - 1"
); );
assert_eq!(sum.inner[1], 1usize, "most significant place should be 1"); assert_eq!(sum.inner[1], 1usize, "most significant place should be 1");
let a = BigInt::from(core::usize::MAX); let a = BigInt::new(usize::MAX);
let b = BigInt::from(1usize); let b = BigInt::new(1usize);
let sum = a + b; let sum = a + b;
assert_eq!(sum.inner[0], 0_usize); assert_eq!(sum.inner[0], 0_usize);
assert_eq!(sum.inner[1], 1usize); assert_eq!(sum.inner[1], 1usize);
let a = BigInt::from(10); let a = BigInt::new(10);
let b = -BigInt::from(5); let b = -BigInt::new(5);
let sum = a + b; let sum = a + b;
assert_eq!(sum.inner[0], 5usize); assert_eq!(sum.inner[0], 5usize);
assert_eq!(sum.sign, Positive); assert_eq!(sum.sign, Positive);
let a = -BigInt::from(5); let a = -BigInt::new(5);
let b = BigInt::from(10); let b = BigInt::new(10);
let sum = a + b; let sum = a + b;
assert_eq!(sum.inner[0], 5usize); assert_eq!(sum.inner[0], 5usize);
assert_eq!(sum.sign, Positive); assert_eq!(sum.sign, Positive);
@ -718,14 +717,14 @@ mod tests {
#[test] #[test]
fn test_add_assign() { fn test_add_assign() {
let mut a = BigInt::from(core::usize::MAX); let mut a = BigInt::new(usize::MAX);
let b = BigInt::from(core::usize::MAX); let b = BigInt::new(usize::MAX);
a += b; a += b;
assert_eq!( assert_eq!(
a.inner[0], a.inner[0],
core::usize::MAX - 1, usize::MAX - 1,
"least significant place should be MAX - 1" "least significant place should be MAX - 1"
); );
assert_eq!(a.inner[1], 1usize, "most significant place should be 1"); assert_eq!(a.inner[1], 1usize, "most significant place should be 1");
@ -733,24 +732,24 @@ mod tests {
#[test] #[test]
fn test_sub() { fn test_sub() {
let a = BigInt::from(core::usize::MAX); let a = BigInt::new(usize::MAX);
let b = BigInt::from(core::u16::MAX); let b = BigInt::new(core::u16::MAX);
let diff = a - b; let diff = a - b;
assert_eq!( assert_eq!(
diff.clone().inner[0], diff.clone().inner[0],
core::usize::MAX - core::u16::MAX as usize usize::MAX - core::u16::MAX as usize
); );
assert_eq!(diff.inner.len(), 1); assert_eq!(diff.inner.len(), 1);
let a = BigInt::from(5); let a = BigInt::new(5);
let b = -BigInt::from(3); let b = -BigInt::new(3);
let diff = a - b; let diff = a - b;
assert_eq!(diff.sign, Positive); assert_eq!(diff.sign, Positive);
assert_eq!(diff.inner[0], 8usize); assert_eq!(diff.inner[0], 8usize);
let a = -BigInt::from(5); let a = -BigInt::new(5);
let b = BigInt::from(3); let b = BigInt::new(3);
let diff = a - b; let diff = a - b;
assert_eq!(diff.sign, Negative); assert_eq!(diff.sign, Negative);
assert_eq!(diff.inner[0], 8usize); assert_eq!(diff.inner[0], 8usize);
@ -763,10 +762,10 @@ mod tests {
sign: Positive, sign: Positive,
}; };
let b = BigInt::from(2); let b = BigInt::new(2);
let diff = a - b; let diff = a - b;
assert_eq!(diff.clone().inner.len(), 1, "{:#?}", diff.clone()); assert_eq!(diff.clone().inner.len(), 1, "{:#?}", diff.clone());
assert_eq!(diff.inner[0], core::usize::MAX - 1); assert_eq!(diff.inner[0], usize::MAX - 1);
} }
#[test] #[test]
@ -775,7 +774,7 @@ mod tests {
inner: vec![1, 0, 1], inner: vec![1, 0, 1],
sign: Positive, sign: Positive,
}; };
let b = BigInt::from(2); let b = BigInt::new(2);
a -= b; a -= b;
@ -784,8 +783,8 @@ mod tests {
#[test] #[test]
fn test_mul() { fn test_mul() {
let a = BigInt::from(65536); let a = BigInt::new(65536);
let b = BigInt::from(4); let b = BigInt::new(4);
let product = a * b; let product = a * b;
assert_eq!(product.inner[0], 65536usize * 4); assert_eq!(product.inner[0], 65536usize * 4);
@ -793,26 +792,26 @@ mod tests {
#[test] #[test]
fn test_mul_signs() { fn test_mul_signs() {
let a = BigInt::from(2); let a = BigInt::new(2);
let b = BigInt::from(-2); let b = BigInt::new(-2);
let product = a * b; let product = a * b;
assert_eq!(product.inner[0], 4usize); assert_eq!(product.inner[0], 4usize);
assert_eq!(product.sign, Negative); assert_eq!(product.sign, Negative);
let a = -BigInt::from(2); let a = -BigInt::new(2);
let b = BigInt::from(2); let b = BigInt::new(2);
let product = a * b; let product = a * b;
assert_eq!(product.inner[0], 4usize); assert_eq!(product.inner[0], 4usize);
assert_eq!(product.sign, Negative); assert_eq!(product.sign, Negative);
let a = BigInt::from(-2); let a = BigInt::new(-2);
let b = BigInt::from(-2); let b = BigInt::new(-2);
let product = a * b; let product = a * b;
assert_eq!(product.inner[0], 4usize); assert_eq!(product.inner[0], 4usize);
assert_eq!(product.sign, Positive); assert_eq!(product.sign, Positive);
let a = BigInt::from(2); let a = BigInt::new(2);
let b = BigInt::from(2); let b = BigInt::new(2);
let product = a * b; let product = a * b;
assert_eq!(product.inner[0], 4usize); assert_eq!(product.inner[0], 4usize);
assert_eq!(product.sign, Positive); assert_eq!(product.sign, Positive);
@ -821,8 +820,8 @@ mod tests {
#[test] #[test]
#[should_panic] #[should_panic]
fn test_mul_overflow() { fn test_mul_overflow() {
let a = BigInt::from(core::usize::MAX); let a = BigInt::new(usize::MAX);
let b = BigInt::from(5); let b = BigInt::new(5);
let _product = a * b; let _product = a * b;
} }
@ -830,8 +829,8 @@ mod tests {
#[test] #[test]
#[should_panic] #[should_panic]
fn test_mul_assign_overflow() { fn test_mul_assign_overflow() {
let mut a = BigInt::from(core::usize::MAX); let mut a = BigInt::new(usize::MAX);
let b = BigInt::from(5); let b = BigInt::new(5);
a *= b; a *= b;
} }
@ -839,8 +838,8 @@ mod tests {
#[test] #[test]
#[should_panic] #[should_panic]
fn test_div() { fn test_div() {
let a = BigInt::from(128); let a = BigInt::new(128);
let b = BigInt::from(32); let b = BigInt::new(32);
let _quotient = a / b; let _quotient = a / b;
} }
@ -848,8 +847,8 @@ mod tests {
#[test] #[test]
#[should_panic] #[should_panic]
fn test_div_assign() { fn test_div_assign() {
let mut a = BigInt::from(128); let mut a = BigInt::new(128);
let b = BigInt::from(32); let b = BigInt::new(32);
a /= b; a /= b;
} }
@ -857,8 +856,8 @@ mod tests {
#[test] #[test]
#[should_panic] #[should_panic]
fn test_rem() { fn test_rem() {
let a = BigInt::from(5); let a = BigInt::new(5);
let b = BigInt::from(2); let b = BigInt::new(2);
let _rem = a % b; let _rem = a % b;
} }
@ -866,8 +865,8 @@ mod tests {
#[test] #[test]
#[should_panic] #[should_panic]
fn test_rem_assign() { fn test_rem_assign() {
let mut a = BigInt::from(5); let mut a = BigInt::new(5);
let b = BigInt::from(2); let b = BigInt::new(2);
a %= b; a %= b;
} }
@ -888,16 +887,16 @@ mod tests {
#[test] #[test]
fn test_not() { fn test_not() {
let a = BigInt::from(0_u8); let a = BigInt::new(0_u8);
let b = !a; let b = !a;
assert_eq!(b.inner[0], core::usize::MAX); assert_eq!(b.inner[0], usize::MAX);
} }
#[test] #[test]
fn test_partial_eq() { fn test_partial_eq() {
let a = 12345_u16; let a = 12345_u16;
let b = BigInt::from(a); let b = BigInt::new(a);
assert!(a.eq(&b)); assert!(a.eq(&b));
assert!(b.eq(&a)); assert!(b.eq(&a));
@ -906,7 +905,7 @@ mod tests {
#[test] #[test]
fn test_partial_ord() { fn test_partial_ord() {
let a = 12345_u32; let a = 12345_u32;
let b = BigInt::from(a); let b = BigInt::new(a);
let c = 3_u8; let c = 3_u8;
assert_eq!(a.partial_cmp(&b), Some(Ordering::Equal)); assert_eq!(a.partial_cmp(&b), Some(Ordering::Equal));
@ -922,16 +921,16 @@ mod tests {
#[test] #[test]
fn test_from() { fn test_from() {
// Signed numbers // Signed numbers
assert_eq!(-BigInt::from(2), BigInt::from(-2)); assert_eq!(-BigInt::new(2), BigInt::new(-2));
// Larger than usize // Larger than usize
assert_eq!(BigInt::from(45_u128), BigInt::from(45_usize)); assert_eq!(BigInt::new(45_u128), BigInt::new(45_usize));
} }
#[test] #[test]
fn test_from_large_unsigned() { fn test_from_large_unsigned() {
let big_num: u128 = 9 * RADIX + 8; let big_num: u128 = 9 * RADIX + 8;
let res = BigInt::from(big_num); let res = BigInt::new(big_num);
assert_eq!(res.sign, Sign::Positive, "{:#?}", res); assert_eq!(res.sign, Sign::Positive, "{:#?}", res);
assert_eq!(res.inner[0], 8_usize, "{:#?}", res); assert_eq!(res.inner[0], 8_usize, "{:#?}", res);
@ -941,7 +940,7 @@ mod tests {
#[test] #[test]
fn test_from_large_signed() { fn test_from_large_signed() {
let big_num: i128 = 2 * I_RADIX + 3; let big_num: i128 = 2 * I_RADIX + 3;
let res = BigInt::from(-big_num); let res = BigInt::new(-big_num);
assert_eq!(res.sign, Sign::Negative, "{:#?}", res); assert_eq!(res.sign, Sign::Negative, "{:#?}", res);
assert_eq!(res.inner[0], 3_usize, "{:#?}", res); assert_eq!(res.inner[0], 3_usize, "{:#?}", res);
@ -952,13 +951,13 @@ mod tests {
#[should_panic] #[should_panic]
fn test_from_str_large() { fn test_from_str_large() {
let str = "ZYXWVUTSRQPONMLKJIHGFEDCBA987654321"; let str = "ZYXWVUTSRQPONMLKJIHGFEDCBA987654321";
let _ = BigInt::from(str); let _ = BigInt::new(str);
} }
#[test] #[test]
fn test_from_str_small() { fn test_from_str_small() {
let str = "012345"; let str = "012345";
let num = BigInt::from(str); let num = BigInt::new(str);
assert_eq!(num.inner[0], 12345_usize); assert_eq!(num.inner[0], 12345_usize);
} }
@ -966,20 +965,20 @@ mod tests {
#[should_panic] #[should_panic]
fn test_from_string_large() { fn test_from_string_large() {
let str = String::from("ZYXWVUTSRQPONMLKJIHGFEDCBA987654321"); let str = String::from("ZYXWVUTSRQPONMLKJIHGFEDCBA987654321");
let _ = BigInt::from(str); let _ = BigInt::new(str);
} }
#[test] #[test]
fn test_from_string_small() { fn test_from_string_small() {
let str = String::from("012345"); let str = String::from("012345");
let num = BigInt::from(str); let num = BigInt::new(str);
assert_eq!(num.inner[0], 12345_usize); assert_eq!(num.inner[0], 12345_usize);
} }
#[test] #[test]
fn test_from_str_radix_1() { fn test_from_str_radix_1() {
let s = "1".repeat(32); let s = "1".repeat(32);
let num = BigInt::from_str_radix(&s, 1); let num = BigInt::from_str(&s, 1);
assert_eq!(num.inner[0], 32_usize); assert_eq!(num.inner[0], 32_usize);
} }
@ -989,25 +988,25 @@ mod tests {
let zeroes = "0".repeat(24); let zeroes = "0".repeat(24);
let s = ones + &zeroes; let s = ones + &zeroes;
let num = BigInt::from_str_radix(&s, 1); let num = BigInt::from_str(&s, 1);
assert_eq!(num.inner[0], 32_usize); assert_eq!(num.inner[0], 32_usize);
} }
#[test] #[test]
#[should_panic] #[should_panic]
fn test_from_str_radix_invalid() { fn test_from_str_radix_invalid() {
let _ = BigInt::from_str_radix("foobar0", 50); let _ = BigInt::from_str("foobar0", 50);
} }
#[test] #[test]
#[should_panic] #[should_panic]
fn test_from_str_radix_large() { fn test_from_str_radix_large() {
let _ = BigInt::from_str_radix("ZYXWVUTSRQPONMLKJIHGFEDCBA987654321", 36); let _ = BigInt::from_str("ZYXWVUTSRQPONMLKJIHGFEDCBA987654321", 36);
} }
#[test] #[test]
fn test_from_str_radix_small() { fn test_from_str_radix_small() {
let num = BigInt::from_str_radix("FEDCBA", 16); let num = BigInt::from_str("FEDCBA", 16);
assert!(num.inner[0] > 0, "Number is not greater than 0"); assert!(num.inner[0] > 0, "Number is not greater than 0");
assert!(num.inner[0] < usize::MAX, "Result is larger than usize"); assert!(num.inner[0] < usize::MAX, "Result is larger than usize");
assert_eq!(num.inner[0], 0xFEDCBA_usize); assert_eq!(num.inner[0], 0xFEDCBA_usize);
@ -1015,7 +1014,7 @@ mod tests {
#[test] #[test]
fn test_from_str_radix_lowercase() { fn test_from_str_radix_lowercase() {
let num = BigInt::from_str_radix("fedcba", 16); let num = BigInt::from_str("fedcba", 16);
assert_eq!(num.inner[0], 0xFEDCBA_usize); assert_eq!(num.inner[0], 0xFEDCBA_usize);
} }
} }