Compare commits
No commits in common. "d6b0825b9a1a59aa4b98401f1b5aabbb4b959c67" and "cf262073dd27e6688368e6cbf7734a480dfbe4fe" have entirely different histories.
d6b0825b9a
...
cf262073dd
@ -6,15 +6,15 @@ use crate::num::*;
|
|||||||
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
||||||
extern crate alloc;
|
extern crate alloc;
|
||||||
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
||||||
use alloc::string::*;
|
|
||||||
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
|
||||||
use alloc::vec::*;
|
use alloc::vec::*;
|
||||||
|
#[cfg(all(feature = "alloc", not(feature = "std")))]
|
||||||
|
use alloc::string::*;
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
use std::prelude::v1::*;
|
use std::prelude::v1::*;
|
||||||
|
|
||||||
use core::cmp::{Ordering, PartialEq, PartialOrd};
|
use core::cmp::{Ordering, PartialOrd, PartialEq};
|
||||||
use core::convert::*;
|
use core::convert::TryInto;
|
||||||
use core::mem::replace;
|
use core::mem::replace;
|
||||||
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,
|
||||||
@ -465,36 +465,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;
|
todo!();
|
||||||
let target_radix: $s = (MAX as $s) + 1;
|
|
||||||
let sign = if n < 0 { Sign::Negative } else { Sign::Positive };
|
|
||||||
let n = n.abs();
|
|
||||||
|
|
||||||
let mut quotient = n / target_radix;
|
|
||||||
let mut rem = n % target_radix;
|
|
||||||
|
|
||||||
if quotient == 0 {
|
|
||||||
Self::from(rem as usize)
|
|
||||||
} else {
|
|
||||||
let mut inner: Vec<usize> = Vec::new();
|
|
||||||
inner.push(rem as usize);
|
|
||||||
|
|
||||||
loop {
|
|
||||||
rem = quotient % target_radix;
|
|
||||||
quotient = quotient / target_radix;
|
|
||||||
|
|
||||||
inner.push(rem as usize);
|
|
||||||
|
|
||||||
if (quotient == 0) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
BigInt {
|
|
||||||
inner,
|
|
||||||
sign,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -502,32 +473,14 @@ macro_rules! impl_from_larger {
|
|||||||
/// 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;
|
use core::usize::MAX;
|
||||||
let target_radix: $u = (MAX as $u) + 1;
|
|
||||||
|
|
||||||
let mut quotient = n / target_radix;
|
let base_usize_value = n / MAX as $u;
|
||||||
let mut rem = n % target_radix;
|
let rem = n % MAX as $u;
|
||||||
|
|
||||||
if quotient == 0 {
|
if base_usize_value == 0 {
|
||||||
Self::from(rem as usize)
|
Self::from(rem as usize)
|
||||||
} else {
|
} else {
|
||||||
let mut inner: Vec<usize> = Vec::new();
|
todo!();
|
||||||
inner.push(rem as usize);
|
|
||||||
|
|
||||||
loop {
|
|
||||||
rem = quotient % target_radix;
|
|
||||||
quotient = quotient / target_radix;
|
|
||||||
|
|
||||||
inner.push(rem as usize);
|
|
||||||
|
|
||||||
if (quotient == 0) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
BigInt {
|
|
||||||
inner,
|
|
||||||
sign: Sign::Positive
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -576,16 +529,13 @@ impl_from_larger!((i128, u128));
|
|||||||
impl_from_smaller!((i8, u8), (i16, u16), (i32, u32), (i64, u64));
|
impl_from_smaller!((i8, u8), (i16, u16), (i32, u32), (i64, u64));
|
||||||
|
|
||||||
// Implement PartialEq and PartialOrd to compare against BigInt values
|
// Implement PartialEq and PartialOrd to compare against BigInt values
|
||||||
impl_ord_literal!(i8, u8, i16, u16, i32, u32, i64, u64, i128, u128);
|
impl_ord_literal!(i8,u8,i16,u16,i32,u32,i64,u64,i128,u128);
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
#[cfg_attr(tarpaulin, skip)]
|
#[cfg_attr(tarpaulin, skip)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
const RADIX: u128 = core::usize::MAX as u128 + 1;
|
|
||||||
const I_RADIX: i128 = core::usize::MAX as i128 + 1;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn sanity_checks() {
|
fn sanity_checks() {
|
||||||
let int = BigInt::from(45u8);
|
let int = BigInt::from(45u8);
|
||||||
@ -605,14 +555,14 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_trim_zeros() {
|
fn test_trim_zeros() {
|
||||||
let mut lots_of_leading = BigInt {
|
let mut lotsoftrailing = BigInt {
|
||||||
inner: vec![1, 0, 0, 0, 0, 0, 0, 0, 0],
|
inner: vec![1, 0, 0, 0, 0, 0, 0, 0, 0],
|
||||||
sign: Positive,
|
sign: Positive,
|
||||||
};
|
};
|
||||||
|
|
||||||
lots_of_leading.trim_zeros();
|
lotsoftrailing.trim_zeros();
|
||||||
|
|
||||||
assert_eq!(BigInt::from(1), lots_of_leading);
|
assert_eq!(BigInt::from(1), lotsoftrailing);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@ -865,23 +815,15 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
fn test_from_large_unsigned() {
|
fn test_from_large_unsigned() {
|
||||||
let big_num: u128 = 9 * RADIX + 8;
|
BigInt::from(core::u128::MAX);
|
||||||
let res = BigInt::from(big_num);
|
|
||||||
|
|
||||||
assert_eq!(res.sign, Sign::Positive, "{:#?}", res);
|
|
||||||
assert_eq!(res.inner[0], 8usize, "{:#?}", res);
|
|
||||||
assert_eq!(res.inner[1], 9usize, "{:#?}", res);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
#[should_panic]
|
||||||
fn test_from_large_signed() {
|
fn test_from_large_signed() {
|
||||||
let big_num: i128 = 2 * I_RADIX + 3;
|
BigInt::from(128i128);
|
||||||
let res = BigInt::from(-big_num);
|
|
||||||
|
|
||||||
assert_eq!(res.sign, Sign::Negative, "{:#?}", res);
|
|
||||||
assert_eq!(res.inner[0], 3usize, "{:#?}", res);
|
|
||||||
assert_eq!(res.inner[1], 2usize, "{:#?}", res);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -65,7 +65,7 @@ impl PartialOrd for Sign {
|
|||||||
Self::Negative => match other {
|
Self::Negative => match other {
|
||||||
Self::Positive => Some(Ordering::Less),
|
Self::Positive => Some(Ordering::Less),
|
||||||
Self::Negative => Some(Ordering::Equal),
|
Self::Negative => Some(Ordering::Equal),
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user