implement a bunch of stuff

This commit is contained in:
Quaternions 2024-09-05 15:41:39 -07:00
parent 990a923463
commit c362081003
4 changed files with 148 additions and 0 deletions

View File

@ -3,9 +3,55 @@
macro_rules! impl_matrix { macro_rules! impl_matrix {
() => { () => {
impl<const X:usize,const Y:usize,T> Matrix<X,Y,T>{ impl<const X:usize,const Y:usize,T> Matrix<X,Y,T>{
#[inline(always)]
pub const fn new(array:[[T;Y];X])->Self{ pub const fn new(array:[[T;Y];X])->Self{
Self{array} Self{array}
} }
#[inline(always)]
pub fn to_array(self)->[[T;Y];X]{
self.array
}
#[inline]
pub fn map<F,U>(self,f:F)->Matrix<X,Y,U>
where
F:Fn(T)->U
{
Matrix{
array:self.array.map(|inner|inner.map(&f)),
}
}
}
impl<const X:usize,const Y:usize,T> Matrix<X,Y,T>
where
T:Copy
{
pub const fn from_value(value:T)->Self{
Self{
array:[[value;Y];X]
}
}
}
impl<const X:usize,const Y:usize,T:PartialEq> PartialEq for Matrix<X,Y,T>{
fn eq(&self,other:&Self)->bool{
self.array==other.array
}
}
impl<const X:usize,const Y:usize,T:Eq> Eq for Matrix<X,Y,T>{}
impl<const X:usize,const Y:usize,T:core::hash::Hash> core::hash::Hash for Matrix<X,Y,T>{
fn hash<H:core::hash::Hasher>(&self,state:&mut H){
self.array.hash(state);
}
}
impl<const X:usize,const Y:usize,T:Default> Default for Matrix<X,Y,T>{
fn default()->Self{
Self{
array:core::array::from_fn(|_|core::array::from_fn(|_|Default::default()))
}
}
} }
} }
} }

View File

@ -3,17 +3,117 @@
macro_rules! impl_vector { macro_rules! impl_vector {
() => { () => {
impl<const N:usize,T> Vector<N,T>{ impl<const N:usize,T> Vector<N,T>{
#[inline(always)]
pub const fn new(array:[T;N])->Self{ pub const fn new(array:[T;N])->Self{
Self{array} Self{array}
} }
#[inline(always)]
pub fn to_array(self)->[T;N]{
self.array
}
#[inline]
pub fn map<F,U>(self,f:F)->Vector<N,U>
where
F:Fn(T)->U
{
Vector{
array:self.array.map(f),
}
}
#[inline]
pub fn map_zip<F,U,V>(self,other:Vector<N,U>,f:F)->Vector<N,V>
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<const N:usize,T:Copy> Vector<N,T>{ impl<const N:usize,T:Copy> Vector<N,T>{
#[inline(always)]
pub const fn from_value(value:T)->Self{ pub const fn from_value(value:T)->Self{
Self{ Self{
array:[value;N] array:[value;N]
} }
} }
} }
impl<const N:usize,T:PartialEq> PartialEq for Vector<N,T>{
fn eq(&self,other:&Self)->bool{
self.array==other.array
}
}
impl<const N:usize,T:Eq> Eq for Vector<N,T>{}
impl<const N:usize,T:core::hash::Hash> core::hash::Hash for Vector<N,T>{
fn hash<H:core::hash::Hasher>(&self,state:&mut H){
self.array.hash(state);
}
}
impl<const N:usize,T:Default> Default for Vector<N,T>{
fn default()->Self{
Self{
array:core::array::from_fn(|_|Default::default())
}
}
}
impl<const N:usize,T:Ord> Vector<N,T>{
#[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<N,core::cmp::Ordering>{
self.map_zip(rhs,|(a,b)|a.cmp(&b))
}
#[inline]
pub fn lt(self,rhs:Self)->Vector<N,bool>{
self.map_zip(rhs,|(a,b)|a.lt(&b))
}
#[inline]
pub fn gt(self,rhs:Self)->Vector<N,bool>{
self.map_zip(rhs,|(a,b)|a.gt(&b))
}
#[inline]
pub fn ge(self,rhs:Self)->Vector<N,bool>{
self.map_zip(rhs,|(a,b)|a.ge(&b))
}
#[inline]
pub fn le(self,rhs:Self)->Vector<N,bool>{
self.map_zip(rhs,|(a,b)|a.le(&b))
}
}
impl<const N:usize> Vector<N,bool>{
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<const N:usize,T:core::ops::Neg<Output=T>> core::ops::Neg for Vector<N,T>{
type Output=Self;
fn neg(self)->Self::Output{
Self{
array:self.array.map(|t|-t)
}
}
}
} }
} }

View File

@ -1,3 +1,4 @@
#[derive(Clone,Copy)]
pub struct Matrix<const X:usize,const Y:usize,T>{ pub struct Matrix<const X:usize,const Y:usize,T>{
pub(crate) array:[[T;Y];X], pub(crate) array:[[T;Y];X],
} }

View File

@ -1,3 +1,4 @@
#[derive(Clone,Copy)]
pub struct Vector<const N:usize,T>{ pub struct Vector<const N:usize,T>{
pub(crate) array:[T;N], pub(crate) array:[T;N],
} }