Mark small matrix/vector "new" constractors as const-fn.

This commit is contained in:
Crozet Sébastien 2021-04-12 09:18:17 +02:00
parent ad5b18ac77
commit bedf48dbc2
2 changed files with 154 additions and 130 deletions

View File

@ -22,7 +22,9 @@ use simba::scalar::{ClosedAdd, ClosedMul};
use crate::base::allocator::Allocator;
use crate::base::dimension::{Dim, DimName, Dynamic, ToTypenum};
use crate::base::storage::Storage;
use crate::base::{Const, DefaultAllocator, Matrix, OMatrix, OVector, Scalar, Unit, Vector};
use crate::base::{
ArrayStorage, Const, DefaultAllocator, Matrix, OMatrix, OVector, Scalar, Unit, Vector,
};
/// When "no_unsound_assume_init" is enabled, expands to `unimplemented!()` instead of `new_uninitialized_generic().assume_init()`.
/// Intended as a placeholder, each callsite should be refactored to use uninitialized memory soundly
@ -877,22 +879,44 @@ where
* Constructors for small matrices and vectors.
*
*/
macro_rules! transpose_array(
[$($a: ident),*;] => {
[$([$a]),*]
};
[$($a: ident),*; $($b: ident),*;] => {
[$([$a, $b]),*];
};
[$($a: ident),*; $($b: ident),*; $($c: ident),*;] => {
[$([$a, $b, $c]),*];
};
[$($a: ident),*; $($b: ident),*; $($c: ident),*; $($d: ident),*;] => {
[$([$a, $b, $c, $d]),*];
};
[$($a: ident),*; $($b: ident),*; $($c: ident),*; $($d: ident),*; $($e: ident),*;] => {
[$([$a, $b, $c, $d, $e]),*];
};
[$($a: ident),*; $($b: ident),*; $($c: ident),*; $($d: ident),*; $($e: ident),*; $($f: ident),*;] => {
[$([$a, $b, $c, $d, $e, $f]),*];
};
);
macro_rules! componentwise_constructors_impl(
($($R: expr, $C: expr, $($args: ident:($irow: expr,$icol: expr)),*);* $(;)*) => {$(
impl<T> OMatrix<T, Const<$R>, Const<$C>>
where T: Scalar,
DefaultAllocator: Allocator<T, Const<$R>, Const<$C>> {
($($R: expr, $C: expr, [$($($args: ident),*);*] $(;)*)*) => {$(
impl<T> Matrix<T, Const<$R>, Const<$C>, ArrayStorage<T, $R, $C>> {
/// Initializes this matrix from its components.
#[inline]
pub fn new($($args: T),*) -> Self {
pub const fn new($($($args: T),*),*) -> Self {
unsafe {
#[cfg(feature="no_unsound_assume_init")]
let mut res: Self = unimplemented!();
#[cfg(not(feature="no_unsound_assume_init"))]
let mut res = Self::new_uninitialized().assume_init();
$( *res.get_unchecked_mut(($irow, $icol)) = $args; )*
res
Self::from_data_statically_unchecked(
ArrayStorage(
transpose_array![
$(
$($args),*
;)*
]
)
)
}
}
}
@ -903,145 +927,145 @@ componentwise_constructors_impl!(
/*
* Square matrices 1 .. 6.
*/
2, 2, m11:(0,0), m12:(0,1),
m21:(1,0), m22:(1,1);
3, 3, m11:(0,0), m12:(0,1), m13:(0,2),
m21:(1,0), m22:(1,1), m23:(1,2),
m31:(2,0), m32:(2,1), m33:(2,2);
4, 4, m11:(0,0), m12:(0,1), m13:(0,2), m14:(0,3),
m21:(1,0), m22:(1,1), m23:(1,2), m24:(1,3),
m31:(2,0), m32:(2,1), m33:(2,2), m34:(2,3),
m41:(3,0), m42:(3,1), m43:(3,2), m44:(3,3);
5, 5, m11:(0,0), m12:(0,1), m13:(0,2), m14:(0,3), m15:(0,4),
m21:(1,0), m22:(1,1), m23:(1,2), m24:(1,3), m25:(1,4),
m31:(2,0), m32:(2,1), m33:(2,2), m34:(2,3), m35:(2,4),
m41:(3,0), m42:(3,1), m43:(3,2), m44:(3,3), m45:(3,4),
m51:(4,0), m52:(4,1), m53:(4,2), m54:(4,3), m55:(4,4);
6, 6, m11:(0,0), m12:(0,1), m13:(0,2), m14:(0,3), m15:(0,4), m16:(0,5),
m21:(1,0), m22:(1,1), m23:(1,2), m24:(1,3), m25:(1,4), m26:(1,5),
m31:(2,0), m32:(2,1), m33:(2,2), m34:(2,3), m35:(2,4), m36:(2,5),
m41:(3,0), m42:(3,1), m43:(3,2), m44:(3,3), m45:(3,4), m46:(3,5),
m51:(4,0), m52:(4,1), m53:(4,2), m54:(4,3), m55:(4,4), m56:(4,5),
m61:(5,0), m62:(5,1), m63:(5,2), m64:(5,3), m65:(5,4), m66:(5,5);
2, 2, [m11, m12;
m21, m22];
3, 3, [m11, m12, m13;
m21, m22, m23;
m31, m32, m33];
4, 4, [m11, m12, m13, m14;
m21, m22, m23, m24;
m31, m32, m33, m34;
m41, m42, m43, m44];
5, 5, [m11, m12, m13, m14, m15;
m21, m22, m23, m24, m25;
m31, m32, m33, m34, m35;
m41, m42, m43, m44, m45;
m51, m52, m53, m54, m55];
6, 6, [m11, m12, m13, m14, m15, m16;
m21, m22, m23, m24, m25, m26;
m31, m32, m33, m34, m35, m36;
m41, m42, m43, m44, m45, m46;
m51, m52, m53, m54, m55, m56;
m61, m62, m63, m64, m65, m66];
/*
* Rectangular matrices with 2 rows.
*/
2, 3, m11:(0,0), m12:(0,1), m13:(0,2),
m21:(1,0), m22:(1,1), m23:(1,2);
2, 4, m11:(0,0), m12:(0,1), m13:(0,2), m14:(0,3),
m21:(1,0), m22:(1,1), m23:(1,2), m24:(1,3);
2, 5, m11:(0,0), m12:(0,1), m13:(0,2), m14:(0,3), m15:(0,4),
m21:(1,0), m22:(1,1), m23:(1,2), m24:(1,3), m25:(1,4);
2, 6, m11:(0,0), m12:(0,1), m13:(0,2), m14:(0,3), m15:(0,4), m16:(0,5),
m21:(1,0), m22:(1,1), m23:(1,2), m24:(1,3), m25:(1,4), m26:(1,5);
2, 3, [m11, m12, m13;
m21, m22, m23];
2, 4, [m11, m12, m13, m14;
m21, m22, m23, m24];
2, 5, [m11, m12, m13, m14, m15;
m21, m22, m23, m24, m25];
2, 6, [m11, m12, m13, m14, m15, m16;
m21, m22, m23, m24, m25, m26];
/*
* Rectangular matrices with 3 rows.
*/
3, 2, m11:(0,0), m12:(0,1),
m21:(1,0), m22:(1,1),
m31:(2,0), m32:(2,1);
3, 4, m11:(0,0), m12:(0,1), m13:(0,2), m14:(0,3),
m21:(1,0), m22:(1,1), m23:(1,2), m24:(1,3),
m31:(2,0), m32:(2,1), m33:(2,2), m34:(2,3);
3, 5, m11:(0,0), m12:(0,1), m13:(0,2), m14:(0,3), m15:(0,4),
m21:(1,0), m22:(1,1), m23:(1,2), m24:(1,3), m25:(1,4),
m31:(2,0), m32:(2,1), m33:(2,2), m34:(2,3), m35:(2,4);
3, 6, m11:(0,0), m12:(0,1), m13:(0,2), m14:(0,3), m15:(0,4), m16:(0,5),
m21:(1,0), m22:(1,1), m23:(1,2), m24:(1,3), m25:(1,4), m26:(1,5),
m31:(2,0), m32:(2,1), m33:(2,2), m34:(2,3), m35:(2,4), m36:(2,5);
3, 2, [m11, m12;
m21, m22;
m31, m32];
3, 4, [m11, m12, m13, m14;
m21, m22, m23, m24;
m31, m32, m33, m34];
3, 5, [m11, m12, m13, m14, m15;
m21, m22, m23, m24, m25;
m31, m32, m33, m34, m35];
3, 6, [m11, m12, m13, m14, m15, m16;
m21, m22, m23, m24, m25, m26;
m31, m32, m33, m34, m35, m36];
/*
* Rectangular matrices with 4 rows.
*/
4, 2, m11:(0,0), m12:(0,1),
m21:(1,0), m22:(1,1),
m31:(2,0), m32:(2,1),
m41:(3,0), m42:(3,1);
4, 3, m11:(0,0), m12:(0,1), m13:(0,2),
m21:(1,0), m22:(1,1), m23:(1,2),
m31:(2,0), m32:(2,1), m33:(2,2),
m41:(3,0), m42:(3,1), m43:(3,2);
4, 5, m11:(0,0), m12:(0,1), m13:(0,2), m14:(0,3), m15:(0,4),
m21:(1,0), m22:(1,1), m23:(1,2), m24:(1,3), m25:(1,4),
m31:(2,0), m32:(2,1), m33:(2,2), m34:(2,3), m35:(2,4),
m41:(3,0), m42:(3,1), m43:(3,2), m44:(3,3), m45:(3,4);
4, 6, m11:(0,0), m12:(0,1), m13:(0,2), m14:(0,3), m15:(0,4), m16:(0,5),
m21:(1,0), m22:(1,1), m23:(1,2), m24:(1,3), m25:(1,4), m26:(1,5),
m31:(2,0), m32:(2,1), m33:(2,2), m34:(2,3), m35:(2,4), m36:(2,5),
m41:(3,0), m42:(3,1), m43:(3,2), m44:(3,3), m45:(3,4), m46:(3,5);
4, 2, [m11, m12;
m21, m22;
m31, m32;
m41, m42];
4, 3, [m11, m12, m13;
m21, m22, m23;
m31, m32, m33;
m41, m42, m43];
4, 5, [m11, m12, m13, m14, m15;
m21, m22, m23, m24, m25;
m31, m32, m33, m34, m35;
m41, m42, m43, m44, m45];
4, 6, [m11, m12, m13, m14, m15, m16;
m21, m22, m23, m24, m25, m26;
m31, m32, m33, m34, m35, m36;
m41, m42, m43, m44, m45, m46];
/*
* Rectangular matrices with 5 rows.
*/
5, 2, m11:(0,0), m12:(0,1),
m21:(1,0), m22:(1,1),
m31:(2,0), m32:(2,1),
m41:(3,0), m42:(3,1),
m51:(4,0), m52:(4,1);
5, 3, m11:(0,0), m12:(0,1), m13:(0,2),
m21:(1,0), m22:(1,1), m23:(1,2),
m31:(2,0), m32:(2,1), m33:(2,2),
m41:(3,0), m42:(3,1), m43:(3,2),
m51:(4,0), m52:(4,1), m53:(4,2);
5, 4, m11:(0,0), m12:(0,1), m13:(0,2), m14:(0,3),
m21:(1,0), m22:(1,1), m23:(1,2), m24:(1,3),
m31:(2,0), m32:(2,1), m33:(2,2), m34:(2,3),
m41:(3,0), m42:(3,1), m43:(3,2), m44:(3,3),
m51:(4,0), m52:(4,1), m53:(4,2), m54:(4,3);
5, 6, m11:(0,0), m12:(0,1), m13:(0,2), m14:(0,3), m15:(0,4), m16:(0,5),
m21:(1,0), m22:(1,1), m23:(1,2), m24:(1,3), m25:(1,4), m26:(1,5),
m31:(2,0), m32:(2,1), m33:(2,2), m34:(2,3), m35:(2,4), m36:(2,5),
m41:(3,0), m42:(3,1), m43:(3,2), m44:(3,3), m45:(3,4), m46:(3,5),
m51:(4,0), m52:(4,1), m53:(4,2), m54:(4,3), m55:(4,4), m56:(4,5);
5, 2, [m11, m12;
m21, m22;
m31, m32;
m41, m42;
m51, m52];
5, 3, [m11, m12, m13;
m21, m22, m23;
m31, m32, m33;
m41, m42, m43;
m51, m52, m53];
5, 4, [m11, m12, m13, m14;
m21, m22, m23, m24;
m31, m32, m33, m34;
m41, m42, m43, m44;
m51, m52, m53, m54];
5, 6, [m11, m12, m13, m14, m15, m16;
m21, m22, m23, m24, m25, m26;
m31, m32, m33, m34, m35, m36;
m41, m42, m43, m44, m45, m46;
m51, m52, m53, m54, m55, m56];
/*
* Rectangular matrices with 6 rows.
*/
6, 2, m11:(0,0), m12:(0,1),
m21:(1,0), m22:(1,1),
m31:(2,0), m32:(2,1),
m41:(3,0), m42:(3,1),
m51:(4,0), m52:(4,1),
m61:(5,0), m62:(5,1);
6, 3, m11:(0,0), m12:(0,1), m13:(0,2),
m21:(1,0), m22:(1,1), m23:(1,2),
m31:(2,0), m32:(2,1), m33:(2,2),
m41:(3,0), m42:(3,1), m43:(3,2),
m51:(4,0), m52:(4,1), m53:(4,2),
m61:(5,0), m62:(5,1), m63:(5,2);
6, 4, m11:(0,0), m12:(0,1), m13:(0,2), m14:(0,3),
m21:(1,0), m22:(1,1), m23:(1,2), m24:(1,3),
m31:(2,0), m32:(2,1), m33:(2,2), m34:(2,3),
m41:(3,0), m42:(3,1), m43:(3,2), m44:(3,3),
m51:(4,0), m52:(4,1), m53:(4,2), m54:(4,3),
m61:(5,0), m62:(5,1), m63:(5,2), m64:(5,3);
6, 5, m11:(0,0), m12:(0,1), m13:(0,2), m14:(0,3), m15:(0,4),
m21:(1,0), m22:(1,1), m23:(1,2), m24:(1,3), m25:(1,4),
m31:(2,0), m32:(2,1), m33:(2,2), m34:(2,3), m35:(2,4),
m41:(3,0), m42:(3,1), m43:(3,2), m44:(3,3), m45:(3,4),
m51:(4,0), m52:(4,1), m53:(4,2), m54:(4,3), m55:(4,4),
m61:(5,0), m62:(5,1), m63:(5,2), m64:(5,3), m65:(5,4);
6, 2, [m11, m12;
m21, m22;
m31, m32;
m41, m42;
m51, m52;
m61, m62];
6, 3, [m11, m12, m13;
m21, m22, m23;
m31, m32, m33;
m41, m42, m43;
m51, m52, m53;
m61, m62, m63];
6, 4, [m11, m12, m13, m14;
m21, m22, m23, m24;
m31, m32, m33, m34;
m41, m42, m43, m44;
m51, m52, m53, m54;
m61, m62, m63, m64];
6, 5, [m11, m12, m13, m14, m15;
m21, m22, m23, m24, m25;
m31, m32, m33, m34, m35;
m41, m42, m43, m44, m45;
m51, m52, m53, m54, m55;
m61, m62, m63, m64, m65];
/*
* Row vectors 1 .. 6.
*/
1, 1, x:(0,0);
1, 2, x:(0,0), y:(0,1);
1, 3, x:(0,0), y:(0,1), z:(0,2);
1, 4, x:(0,0), y:(0,1), z:(0,2), w:(0,3);
1, 5, x:(0,0), y:(0,1), z:(0,2), w:(0,3), a:(0,4);
1, 6, x:(0,0), y:(0,1), z:(0,2), w:(0,3), a:(0,4), b:(0,5);
1, 1, [x];
1, 2, [x, y];
1, 3, [x, y, z];
1, 4, [x, y, z, w];
1, 5, [x, y, z, w, a];
1, 6, [x, y, z, w, a, b];
/*
* Column vectors 1 .. 6.
*/
2, 1, x:(0,0), y:(1,0);
3, 1, x:(0,0), y:(1,0), z:(2,0);
4, 1, x:(0,0), y:(1,0), z:(2,0), w:(3,0);
5, 1, x:(0,0), y:(1,0), z:(2,0), w:(3,0), a:(4,0);
6, 1, x:(0,0), y:(1,0), z:(2,0), w:(3,0), a:(4,0), b:(5,0);
2, 1, [x; y];
3, 1, [x; y; z];
4, 1, [x; y; z; w];
5, 1, [x; y; z; w; a];
6, 1, [x; y; z; w; a; b];
);
/*

View File

@ -150,7 +150,7 @@ pub type MatrixCross<T, R1, C1, R2, C2> =
/// some concrete types for `T` and a compatible data storage type `S`).
#[repr(C)]
#[derive(Clone, Copy)]
pub struct Matrix<T: Scalar, R: Dim, C: Dim, S> {
pub struct Matrix<T, R, C, S> {
/// The data storage that contains all the matrix components. Disappointed?
///
/// Well, if you came here to see how you can access the matrix components,
@ -179,7 +179,7 @@ pub struct Matrix<T: Scalar, R: Dim, C: Dim, S> {
_phantoms: PhantomData<(T, R, C)>,
}
impl<T: Scalar, R: Dim, C: Dim, S: fmt::Debug> fmt::Debug for Matrix<T, R, C, S> {
impl<T, R: Dim, C: Dim, S: fmt::Debug> fmt::Debug for Matrix<T, R, C, S> {
fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
formatter
.debug_struct("Matrix")
@ -295,11 +295,11 @@ where
{
}
impl<T: Scalar, R: Dim, C: Dim, S> Matrix<T, R, C, S> {
impl<T, R, C, S> Matrix<T, R, C, S> {
/// Creates a new matrix with the given data without statically checking that the matrix
/// dimension matches the storage dimension.
#[inline]
pub unsafe fn from_data_statically_unchecked(data: S) -> Matrix<T, R, C, S> {
#[inline(always)]
pub const unsafe fn from_data_statically_unchecked(data: S) -> Matrix<T, R, C, S> {
Matrix {
data,
_phantoms: PhantomData,
@ -309,7 +309,7 @@ impl<T: Scalar, R: Dim, C: Dim, S> Matrix<T, R, C, S> {
impl<T: Scalar, R: Dim, C: Dim, S: Storage<T, R, C>> Matrix<T, R, C, S> {
/// Creates a new matrix with the given data.
#[inline]
#[inline(always)]
pub fn from_data(data: S) -> Self {
unsafe { Self::from_data_statically_unchecked(data) }
}