rusty-numbers/src/bigint.rs

65 lines
1.0 KiB
Rust
Raw Normal View History

2020-02-12 22:29:57 -05:00
//! 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::{Sign, Unsigned};
2020-02-12 22:29:57 -05:00
#[derive(Debug)]
pub struct BigInt {
inner: Vec<usize>,
2020-02-12 23:10:08 -05:00
sign: Sign,
2020-02-12 22:29:57 -05:00
}
impl Default for BigInt {
fn default() -> Self {
Self {
inner: vec![],
2020-02-12 23:10:08 -05:00
sign: Sign::Positive,
2020-02-12 22:29:57 -05:00
}
}
}
2020-02-12 23:10:08 -05:00
impl<T: Unsigned> From<T> for BigInt {
2020-02-12 22:29:57 -05:00
fn from(n: T) -> Self {
let mut new = Self::default();
2020-02-12 23:10:08 -05:00
if n > T::max_value() {
new.inner = BigInt::split(n);
2020-02-12 22:29:57 -05:00
}
new
}
}
2020-02-12 23:10:08 -05:00
impl BigInt {
2020-02-12 22:29:57 -05:00
/// Split an unsigned number into BigInt parts
2020-02-12 23:10:08 -05:00
fn split<T: Unsigned>(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-12 22:29:57 -05:00
return vec![T::into()];
}
todo!();
}
}
impl BigInt {
pub fn new() -> Self {
Self::default()
}
}
#[cfg(test)]
mod tests {
}