From a0da6873c14d0914e1cc897912def0f8a8f6514b Mon Sep 17 00:00:00 2001 From: Quaternions Date: Thu, 5 Sep 2024 15:56:44 -0700 Subject: [PATCH] vector operators --- fixed_wide_vectors/src/macros/vector.rs | 48 +++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/fixed_wide_vectors/src/macros/vector.rs b/fixed_wide_vectors/src/macros/vector.rs index 732dbff..2596138 100644 --- a/fixed_wide_vectors/src/macros/vector.rs +++ b/fixed_wide_vectors/src/macros/vector.rs @@ -100,6 +100,54 @@ macro_rules! impl_vector { } } } + + // Impl arithmetic operators + $crate::impl_vector_assign_operator!(AddAssign, add_assign ); + $crate::impl_vector_operator!(Add, add ); + $crate::impl_vector_assign_operator!(SubAssign, sub_assign ); + $crate::impl_vector_operator!(Sub, sub ); + $crate::impl_vector_assign_operator!(MulAssign, mul_assign ); + $crate::impl_vector_operator!(Mul, mul ); + $crate::impl_vector_assign_operator!(DivAssign, div_assign ); + $crate::impl_vector_operator!(Div, div ); + $crate::impl_vector_assign_operator!(RemAssign, rem_assign ); + $crate::impl_vector_operator!(Rem, rem ); + + // Impl bitwise operators + $crate::impl_vector_assign_operator!(BitAndAssign, bitand_assign ); + $crate::impl_vector_operator!(BitAnd, bitand ); + $crate::impl_vector_assign_operator!(BitOrAssign, bitor_assign ); + $crate::impl_vector_operator!(BitOr, bitor ); + $crate::impl_vector_assign_operator!(BitXorAssign, bitxor_assign ); + $crate::impl_vector_operator!(BitXor, bitxor ); + + // Impl floating-point based methods + //#[cfg(feature="fixed_wide")] + //$crate::impl_wide_vector_operations!( $struct { $($field), + }, $size ); + } +} +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! impl_vector_operator { + ($trait: ident, $method: ident ) => { + impl,U,V> core::ops::$trait> for Vector{ + type Output=Vector; + fn $method(self,rhs:Vector)->Self::Output{ + self.map_zip(rhs,|(a,b)|a.$method(b)) + } + } + } +} +#[doc(hidden)] +#[macro_export(local_inner_macros)] +macro_rules! impl_vector_assign_operator { + ($trait: ident, $method: ident ) => { + impl,U> core::ops::$trait> for Vector{ + fn $method(&mut self,rhs:Vector){ + self.array.iter_mut().zip(rhs.array) + .for_each(|(a,b)|a.$method(b)) + } + } } }