From e52c32ccaab2e9017f643a3fc25564726898053f Mon Sep 17 00:00:00 2001 From: Corwin Kuiper Date: Sat, 20 Mar 2021 01:47:46 +0000 Subject: [PATCH] assign impls --- src/number.rs | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/src/number.rs b/src/number.rs index 3e411106..aa8ecc0c 100644 --- a/src/number.rs +++ b/src/number.rs @@ -1,6 +1,6 @@ use core::{ fmt::Display, - ops::{Add, Div, Mul, Neg, Sub}, + ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign}, }; #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] @@ -19,6 +19,12 @@ impl Add for Num { } } +impl AddAssign for Num { + fn add_assign(&mut self, rhs: Self) { + self.0 += rhs.0 + } +} + impl Sub for Num { type Output = Self; fn sub(self, rhs: Num) -> Self::Output { @@ -26,6 +32,12 @@ impl Sub for Num { } } +impl SubAssign for Num { + fn sub_assign(&mut self, rhs: Self) { + self.0 -= rhs.0 + } +} + impl Mul for Num { type Output = Self; fn mul(self, rhs: Num) -> Self::Output { @@ -37,6 +49,16 @@ impl Mul for Num { } } +impl MulAssign for Num { + fn mul_assign(&mut self, rhs: Self) { + if N % 2 == 0 { + self.0 = (self.0 >> (N / 2)) * (rhs.0 >> (N / 2)) + } else { + self.0 = (self.0 >> (1 + N / 2)) * (rhs.0 >> (N / 2)) + } + } +} + impl Div for Num { type Output = Self; fn div(self, rhs: Num) -> Self::Output { @@ -48,6 +70,16 @@ impl Div for Num { } } +impl DivAssign for Num { + fn div_assign(&mut self, rhs: Self) { + if N % 2 == 0 { + self.0 = (self.0 << (N / 2)) / (rhs.0 >> (N / 2)) + } else { + self.0 = (self.0 << (1 + N / 2)) / (rhs.0 >> (N / 2)) + } + } +} + impl Neg for Num { type Output = Self; fn neg(self) -> Self::Output { @@ -62,6 +94,14 @@ impl Num { pub fn min() -> Self { Num(i32::MIN) } + + pub fn int(&self) -> i32 { + self.0 >> N + } + + pub fn new(integral: i32) -> Self { + Self(integral << N) + } } impl Display for Num {