rusty-numbers/src/bigint.rs

173 lines
3.4 KiB
Rust
Raw Normal View History

//! \[WIP\] Arbitrarily large integers
2020-02-12 23:10:08 -05:00
//!
//! Traits to implement:
//! * Add
//! * AddAssign
//! * Div
//! * DivAssign
//! * Mul
//! * MulAssign
//! * Neg
//! * Rem
//! * RemAssign
//! * Sub
//! * SubAssign
use crate::num::*;
2020-02-12 22:29:57 -05:00
#[derive(Clone, Debug)]
2020-02-12 22:29:57 -05:00
pub struct BigInt {
inner: Vec<usize>,
2020-02-12 23:10:08 -05:00
sign: Sign,
2020-02-12 22:29:57 -05:00
}
2020-03-03 16:33:21 -05:00
macro_rules! impl_from_smaller {
($($Type: ty),* ) => {
$(
impl From<$Type> for BigInt {
fn from(n: $Type) -> Self {
let mut new = Self::default();
new.inner[0] = n as usize;
new
}
}
)*
}
}
#[cfg(target_pointer_width = "16")]
impl_from_smaller!(u8, u16);
#[cfg(target_pointer_width = "32")]
impl_from_smaller!(u8, u16, u32);
#[cfg(target_pointer_width = "64")]
impl_from_smaller!(u8, u16, u32, u64);
#[cfg(target_pointer_width = "16")]
static BITS:usize = 16;
#[cfg(target_pointer_width = "32")]
static BITS:usize = 32;
#[cfg(target_pointer_width = "64")]
static BITS:usize = 64;
fn add(u: BigInt, v: BigInt) -> BigInt {
let mut out = BigInt::default();
let u_digits = u.inner.len();
let v_digits = v.inner.len();
let digits = if v_digits > u_digits {
v_digits
} else {
u_digits
};
let mut carry = 0usize;
for i in 0..=digits {
let a = *u.inner.get(i).unwrap_or(&0usize);
let b = *v.inner.get(i).unwrap_or(&0usize);
let (res, overflowed) = a.overflowing_add(b);
// assert!(!overflowed, "Expected to overflow: {:#?}", (res, overflowed));
if overflowed {
if i == 0 {
out.inner[i] = res;
} else {
out.inner.push(res);
}
carry = core::usize::MAX - res + 1;
} else {
if res < core::usize::MAX {
out.inner.push(res + carry);
carry = 0;
} else {
out.inner.push(0usize);
carry = 1;
}
carry = 0;
}
}
out
}
2020-02-12 22:29:57 -05:00
impl Default for BigInt {
fn default() -> Self {
Self {
2020-03-03 16:33:21 -05:00
inner: vec![0],
2020-02-12 23:10:08 -05:00
sign: Sign::Positive,
2020-02-12 22:29:57 -05:00
}
}
}
2020-03-03 16:33:21 -05:00
impl From<usize> for BigInt {
fn from(n: usize) -> Self {
Self {
inner: vec![n],
sign: Sign::Positive,
2020-02-12 22:29:57 -05:00
}
}
}
impl From<&str> for BigInt {
fn from(_: &str) -> Self {
unimplemented!()
}
}
impl From<String> for BigInt {
fn from(_: String) -> Self {
unimplemented!()
}
}
2020-02-12 23:10:08 -05:00
impl BigInt {
2020-02-13 17:13:25 -05:00
pub fn new() -> Self {
Self::default()
}
2020-02-24 16:30:59 -05:00
pub fn shrink_to_fit(&mut self) {
todo!();
}
pub fn from_str_radix() {
todo!();
}
2020-02-12 22:29:57 -05:00
/// Split an unsigned number into BigInt parts
2020-02-13 17:13:25 -05:00
fn split<T: Unsigned>(&mut self, num: T) -> Vec<usize> {
2020-02-12 22:29:57 -05:00
// Pretty easy if you don't actually need to split the value!
2020-02-12 23:10:08 -05:00
if num < T::max_value() {
2020-02-13 17:13:25 -05:00
todo!();
// return vec![num as usize];
2020-02-12 22:29:57 -05:00
}
todo!();
}
}
#[cfg(test)]
2020-02-24 16:30:59 -05:00
mod tests {
use super::*;
#[test]
fn sanity_checks() {
2020-03-03 16:33:21 -05:00
let int = BigInt::from(45u8);
assert_eq!(int.inner[0], 45usize)
2020-02-24 16:30:59 -05:00
}
2020-03-03 16:33:21 -05:00
/* #[test]
fn test_add() {
let a = BigInt::from(core::u64::MAX);
let b = BigInt::from(core::u64::MAX);
let sum = add(a, b);
assert!(false, "{:#?} should have inner equal to {:?}", sum, core::u64::MAX as u128 + core::u64::MAX as u128);
} */
2020-02-24 16:30:59 -05:00
}