From 182f73db270e764f54713e3937dc997a99864b6b Mon Sep 17 00:00:00 2001 From: erius Date: Mon, 27 May 2024 01:20:45 +0300 Subject: [PATCH] Started working on a new module - num, that will replace the current external num crate --- src/lib.rs | 1 + src/num.rs | 40 ++++++++++++++++++++++++++++++++++++++++ src/num/bigint.rs | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 src/num.rs create mode 100644 src/num/bigint.rs diff --git a/src/lib.rs b/src/lib.rs index 3bb3506..41b0a1d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1 +1,2 @@ +pub mod num; pub mod matrix; diff --git a/src/num.rs b/src/num.rs new file mode 100644 index 0000000..628c2a2 --- /dev/null +++ b/src/num.rs @@ -0,0 +1,40 @@ +mod bigint; + +use std::ops::*; + +pub trait Signed: Neg {} +pub trait NumOps: Sized + Add + Sub + Mul + Div {} +pub trait NumAssignOps: AddAssign + SubAssign + MulAssign + DivAssign {} +pub trait Num: PartialEq + Default + NumOps + NumAssignOps {} + +macro_rules! impl_num { + ($t:ty) => { + impl NumOps for $t {} + impl NumAssignOps for $t {} + impl Num for $t {} + }; +} + +macro_rules! impl_sign_num { + ($t:ty) => { + impl_num!($t); + impl Signed for $t {} + }; +} + +impl_num!(u8); +impl_num!(u16); +impl_num!(u32); +impl_num!(u64); +impl_num!(u128); +impl_num!(usize); + +impl_sign_num!(i8); +impl_sign_num!(i16); +impl_sign_num!(i32); +impl_sign_num!(i64); +impl_sign_num!(i128); +impl_sign_num!(isize); + +impl_sign_num!(f32); +impl_sign_num!(f64); diff --git a/src/num/bigint.rs b/src/num/bigint.rs new file mode 100644 index 0000000..867b4a7 --- /dev/null +++ b/src/num/bigint.rs @@ -0,0 +1,35 @@ +pub struct BigInt { + negative: bool, + num: BigUInt +} + +pub struct BigUInt { + data: Vec +} + +impl BigUInt { + pub fn new() -> Self { + Self::new_from(0) + } + + pub fn new_from(num: u64) -> Self { + Self { data: vec![num] } + } +} + +impl BigInt { + pub fn new() -> Self { + Self { negative: false, num: BigUInt::new() } + } + + pub fn new_from(num: i64) -> Self { + let (negative, num) = if num < 0 { (true, -num as u64) } + else { (false, num as u64) }; + Self { negative, num: BigUInt::new_from(num) } + } +} + +#[cfg(test)] +mod tests { + +}