diff --git a/fixed_wide_vectors/src/macros/matrix.rs b/fixed_wide_vectors/src/macros/matrix.rs index 71ec8e7..05a213f 100644 --- a/fixed_wide_vectors/src/macros/matrix.rs +++ b/fixed_wide_vectors/src/macros/matrix.rs @@ -3,9 +3,55 @@ macro_rules! impl_matrix { () => { impl Matrix{ + #[inline(always)] pub const fn new(array:[[T;Y];X])->Self{ Self{array} } + #[inline(always)] + pub fn to_array(self)->[[T;Y];X]{ + self.array + } + #[inline] + pub fn map(self,f:F)->Matrix + where + F:Fn(T)->U + { + Matrix{ + array:self.array.map(|inner|inner.map(&f)), + } + } + } + impl Matrix + where + T:Copy + { + pub const fn from_value(value:T)->Self{ + Self{ + array:[[value;Y];X] + } + } + } + + impl PartialEq for Matrix{ + fn eq(&self,other:&Self)->bool{ + self.array==other.array + } + } + + impl Eq for Matrix{} + + impl core::hash::Hash for Matrix{ + fn hash(&self,state:&mut H){ + self.array.hash(state); + } + } + + impl Default for Matrix{ + fn default()->Self{ + Self{ + array:core::array::from_fn(|_|core::array::from_fn(|_|Default::default())) + } + } } } } diff --git a/fixed_wide_vectors/src/macros/vector.rs b/fixed_wide_vectors/src/macros/vector.rs index 8a0fe4e..24fe199 100644 --- a/fixed_wide_vectors/src/macros/vector.rs +++ b/fixed_wide_vectors/src/macros/vector.rs @@ -3,17 +3,117 @@ macro_rules! impl_vector { () => { impl Vector{ + #[inline(always)] pub const fn new(array:[T;N])->Self{ Self{array} } + #[inline(always)] + pub fn to_array(self)->[T;N]{ + self.array + } + #[inline] + pub fn map(self,f:F)->Vector + where + F:Fn(T)->U + { + Vector{ + array:self.array.map(f), + } + } + #[inline] + pub fn map_zip(self,other:Vector,f:F)->Vector + where + F:Fn((T,U))->V, + { + let mut iter=self.array.into_iter().zip(other.array); + Vector{ + array:core::array::from_fn(|_|f(iter.next().unwrap())), + } + } } impl Vector{ + #[inline(always)] pub const fn from_value(value:T)->Self{ Self{ array:[value;N] } } } + + impl PartialEq for Vector{ + fn eq(&self,other:&Self)->bool{ + self.array==other.array + } + } + + impl Eq for Vector{} + + impl core::hash::Hash for Vector{ + fn hash(&self,state:&mut H){ + self.array.hash(state); + } + } + + impl Default for Vector{ + fn default()->Self{ + Self{ + array:core::array::from_fn(|_|Default::default()) + } + } + } + + impl Vector{ + #[inline] + pub fn min(self,rhs:Self)->Self{ + self.map_zip(rhs,|(a,b)|a.min(b)) + } + #[inline] + pub fn max(self,rhs:Self)->Self{ + self.map_zip(rhs,|(a,b)|a.max(b)) + } + #[inline] + pub fn cmp(self,rhs:Self)->Vector{ + self.map_zip(rhs,|(a,b)|a.cmp(&b)) + } + #[inline] + pub fn lt(self,rhs:Self)->Vector{ + self.map_zip(rhs,|(a,b)|a.lt(&b)) + } + #[inline] + pub fn gt(self,rhs:Self)->Vector{ + self.map_zip(rhs,|(a,b)|a.gt(&b)) + } + #[inline] + pub fn ge(self,rhs:Self)->Vector{ + self.map_zip(rhs,|(a,b)|a.ge(&b)) + } + #[inline] + pub fn le(self,rhs:Self)->Vector{ + self.map_zip(rhs,|(a,b)|a.le(&b)) + } + } + + impl Vector{ + const ALL:[bool;N]=[true;N]; + const NONE:[bool;N]=[false;N]; + #[inline] + pub fn all(&self)->bool{ + core::matches!(self.array,ALL) + } + #[inline] + pub fn any(&self)->bool{ + !core::matches!(self.array,NONE) + } + } + + impl> core::ops::Neg for Vector{ + type Output=Self; + fn neg(self)->Self::Output{ + Self{ + array:self.array.map(|t|-t) + } + } + } } } diff --git a/fixed_wide_vectors/src/matrix.rs b/fixed_wide_vectors/src/matrix.rs index 331424b..a4ec526 100644 --- a/fixed_wide_vectors/src/matrix.rs +++ b/fixed_wide_vectors/src/matrix.rs @@ -1,3 +1,4 @@ +#[derive(Clone,Copy)] pub struct Matrix{ pub(crate) array:[[T;Y];X], } diff --git a/fixed_wide_vectors/src/vector.rs b/fixed_wide_vectors/src/vector.rs index 3d3e956..b706670 100644 --- a/fixed_wide_vectors/src/vector.rs +++ b/fixed_wide_vectors/src/vector.rs @@ -1,3 +1,4 @@ +#[derive(Clone,Copy)] pub struct Vector{ pub(crate) array:[T;N], }