From 06e85477a9d415e38c72148804e1d5219449cdab Mon Sep 17 00:00:00 2001 From: Tastaturtaste Date: Fri, 14 Apr 2023 20:30:20 +0200 Subject: [PATCH] Add RowVectorView type Add a view type for row vectors equivalent to the view type for column vectors. --- src/base/alias_view.rs | 134 ++++++++++++++++++++++++++++++++++++++++ src/base/conversion.rs | 28 ++++++++- src/base/matrix_view.rs | 19 ++++-- 3 files changed, 176 insertions(+), 5 deletions(-) diff --git a/src/base/alias_view.rs b/src/base/alias_view.rs index 19a6caec..02eba58d 100644 --- a/src/base/alias_view.rs +++ b/src/base/alias_view.rs @@ -438,6 +438,73 @@ pub type VectorView5<'a, T, RStride = U1, CStride = U5> = pub type VectorView6<'a, T, RStride = U1, CStride = U6> = Matrix>; +/// An immutable row vector view with dimensions known at compile-time. +/// +/// See [`RowVectorViewMut`] for a mutable version of this type. +/// +/// **Because this is an alias, not all its methods are listed here. See the [`Matrix`](crate::base::Matrix) type too.** +pub type RowVectorView<'a, T, D, RStride = U1, CStride = D> = + Matrix>; + +/// An immutable row vector view with dimensions known at compile-time. +/// +/// See [`RowSVectorViewMut`] for a mutable version of this type. +/// +/// **Because this is an alias, not all its methods are listed here. See the [`Matrix`](crate::base::Matrix) type too.** +pub type RowSVectorView<'a, T, const D: usize> = + Matrix, Const, ViewStorage<'a, T, Const<1>, Const, Const<1>, Const>>; + +/// An immutable row vector view dynamic numbers of rows and columns. +/// +/// See [`RowDVectorViewMut`] for a mutable version of this type. +/// +/// **Because this is an alias, not all its methods are listed here. See the [`Matrix`](crate::base::Matrix) type too.** +pub type RowDVectorView<'a, T, RStride = U1, CStride = Dyn> = + Matrix>; + +/// An immutable 1D row vector view. +/// +/// See [`RowVectorViewMut1`] for a mutable version of this type. +/// +/// **Because this is an alias, not all its methods are listed here. See the [`Matrix`](crate::base::Matrix) type too.** +pub type RowVectorView1<'a, T, RStride = U1, CStride = U1> = + Matrix>; +/// An immutable 2D row vector view. +/// +/// See [`RowVectorViewMut2`] for a mutable version of this type. +/// +/// **Because this is an alias, not all its methods are listed here. See the [`Matrix`](crate::base::Matrix) type too.** +pub type RowVectorView2<'a, T, RStride = U1, CStride = U2> = + Matrix>; +/// An immutable 3D row vector view. +/// +/// See [`RowVectorViewMut3`] for a mutable version of this type. +/// +/// **Because this is an alias, not all its methods are listed here. See the [`Matrix`](crate::base::Matrix) type too.** +pub type RowVectorView3<'a, T, RStride = U1, CStride = U2> = + Matrix>; +/// An immutable 4D row vector view. +/// +/// See [`RowVectorViewMut4`] for a mutable version of this type. +/// +/// **Because this is an alias, not all its methods are listed here. See the [`Matrix`](crate::base::Matrix) type too.** +pub type RowVectorView4<'a, T, RStride = U1, CStride = U4> = + Matrix>; +/// An immutable 5D row vector view. +/// +/// See [`RowVectorViewMut5`] for a mutable version of this type. +/// +/// **Because this is an alias, not all its methods are listed here. See the [`Matrix`](crate::base::Matrix) type too.** +pub type RowVectorView5<'a, T, RStride = U1, CStride = U5> = + Matrix>; +/// An immutable 6D row vector view. +/// +/// See [`RowVectorViewMut6`] for a mutable version of this type. +/// +/// **Because this is an alias, not all its methods are listed here. See the [`Matrix`](crate::base::Matrix) type too.** +pub type RowVectorView6<'a, T, RStride = U1, CStride = U6> = + Matrix>; + /* * * @@ -873,3 +940,70 @@ pub type VectorViewMut5<'a, T, RStride = U1, CStride = U5> = /// **Because this is an alias, not all its methods are listed here. See the [`Matrix`](crate::base::Matrix) type too.** pub type VectorViewMut6<'a, T, RStride = U1, CStride = U6> = Matrix>; + +/// A mutable row vector view with dimensions known at compile-time. +/// +/// See [`RowVectorView`] for an immutable version of this type. +/// +/// **Because this is an alias, not all its methods are listed here. See the [`Matrix`](crate::base::Matrix) type too.** +pub type RowVectorViewMut<'a, T, D, RStride = U1, CStride = Dyn> = + Matrix>; + +/// A mutable row vector view with dimensions known at compile-time. +/// +/// See [`RowSVectorView`] for an immutable version of this type. +/// +/// **Because this is an alias, not all its methods are listed here. See the [`Matrix`](crate::base::Matrix) type too.** +pub type RowSVectorViewMut<'a, T, const D: usize> = + Matrix, Const, ViewStorageMut<'a, T, Const<1>, Const, Const<1>, Const>>; + +/// A mutable row vector view dynamic numbers of rows and columns. +/// +/// See [`RowDVectorView`] for an immutable version of this type. +/// +/// **Because this is an alias, not all its methods are listed here. See the [`Matrix`](crate::base::Matrix) type too.** +pub type RowDVectorViewMut<'a, T, RStride = U1, CStride = Dyn> = + Matrix>; + +/// A mutable 1D row vector view. +/// +/// See [`RowVectorView1`] for an immutable version of this type. +/// +/// **Because this is an alias, not all its methods are listed here. See the [`Matrix`](crate::base::Matrix) type too.** +pub type RowVectorViewMut1<'a, T, RStride = U1, CStride = U1> = + Matrix>; +/// A mutable 2D row vector view. +/// +/// See [`RowVectorView2`] for an immutable version of this type. +/// +/// **Because this is an alias, not all its methods are listed here. See the [`Matrix`](crate::base::Matrix) type too.** +pub type RowVectorViewMut2<'a, T, RStride = U1, CStride = U2> = + Matrix>; +/// A mutable 3D row vector view. +/// +/// See [`RowVectorView3`] for an immutable version of this type. +/// +/// **Because this is an alias, not all its methods are listed here. See the [`Matrix`](crate::base::Matrix) type too.** +pub type RowVectorViewMut3<'a, T, RStride = U1, CStride = U3> = + Matrix>; +/// A mutable 4D row vector view. +/// +/// See [`RowVectorView4`] for an immutable version of this type. +/// +/// **Because this is an alias, not all its methods are listed here. See the [`Matrix`](crate::base::Matrix) type too.** +pub type RowVectorViewMut4<'a, T, RStride = U1, CStride = U4> = + Matrix>; +/// A mutable 5D row vector view. +/// +/// See [`RowVectorView5`] for an immutable version of this type. +/// +/// **Because this is an alias, not all its methods are listed here. See the [`Matrix`](crate::base::Matrix) type too.** +pub type RowVectorViewMut5<'a, T, RStride = U1, CStride = U5> = + Matrix>; +/// A mutable 6D row vector view. +/// +/// See [`RowVectorView6`] for an immutable version of this type. +/// +/// **Because this is an alias, not all its methods are listed here. See the [`Matrix`](crate::base::Matrix) type too.** +pub type RowVectorViewMut6<'a, T, RStride = U1, CStride = U6> = + Matrix>; diff --git a/src/base/conversion.rs b/src/base/conversion.rs index c29535ef..5c99180b 100644 --- a/src/base/conversion.rs +++ b/src/base/conversion.rs @@ -16,7 +16,7 @@ use crate::base::dimension::{DimName, Dyn}; use crate::base::iter::{MatrixIter, MatrixIterMut}; use crate::base::storage::{IsContiguous, RawStorage, RawStorageMut}; use crate::base::{ - ArrayStorage, DVectorView, DVectorViewMut, DefaultAllocator, Matrix, MatrixView, MatrixViewMut, + ArrayStorage, DVectorView, DVectorViewMut, RowDVectorView, RowDVectorViewMut, DefaultAllocator, Matrix, MatrixView, MatrixViewMut, OMatrix, Scalar, }; #[cfg(any(feature = "std", feature = "alloc"))] @@ -532,6 +532,32 @@ impl<'a, T: Scalar> From> for &'a mut [T] { } } +impl<'a, T: Scalar + Copy> From<&'a [T]> for RowDVectorView<'a, T, Dyn, U1>{ + #[inline] + fn from(slice: &'a [T]) -> Self { + Self::from_slice_with_strides_generic(slice, U1::name(), Dyn(slice.len()), Dyn(slice.len()), U1::name()) + } +} + +impl<'a, T: Scalar> From> for &'a [T] { + fn from(vec: RowDVectorView<'a, T, Dyn, U1>) -> &'a [T] { + vec.data.into_slice() + } +} + +impl<'a, T: Scalar + Copy> From<&'a mut [T]> for RowDVectorViewMut<'a, T, Dyn, U1> { + #[inline] + fn from(slice: &'a mut [T]) -> Self { + Self::from_slice_with_strides_generic(slice, U1::name(), Dyn(slice.len()), Dyn(slice.len()), U1::name()) + } +} + +impl<'a, T: Scalar> From> for &'a mut [T] { + fn from(vec: RowDVectorViewMut<'a, T, Dyn, U1>) -> &'a mut [T] { + vec.data.into_slice_mut() + } +} + impl From<[OMatrix; 2]> for OMatrix where diff --git a/src/base/matrix_view.rs b/src/base/matrix_view.rs index fa6f8f00..bd6fb8ba 100644 --- a/src/base/matrix_view.rs +++ b/src/base/matrix_view.rs @@ -247,17 +247,28 @@ unsafe impl<'a, T, R: Dim, C: Dim, RStride: Dim, CStride: Dim> RawStorageMut IsContiguous for ViewStorage<'a, T, R, U1, U1, CStride> {} -unsafe impl<'a, T, R: Dim, CStride: Dim> IsContiguous +unsafe impl<'a, T, R: Dim + IsNotStaticOne, CStride: Dim> IsContiguous for ViewStorage<'a, T, R, U1, U1, CStride> {} +unsafe impl<'a, T, R: Dim + IsNotStaticOne, CStride: Dim> IsContiguous for ViewStorageMut<'a, T, R, U1, U1, CStride> { } -unsafe impl<'a, T, R: DimName, C: Dim + IsNotStaticOne> IsContiguous +unsafe impl<'a, T, C: Dim + IsNotStaticOne, RStride: Dim> IsContiguous for ViewStorage<'a, T, U1, C, RStride, U1> {} +unsafe impl<'a, T, C: Dim + IsNotStaticOne, RStride: Dim> IsContiguous + for ViewStorageMut<'a, T, U1, C, RStride, U1> +{ +} + +unsafe impl<'a, T> IsContiguous + for ViewStorage<'a, T, U1, U1, U1, U1> +{ +} + +unsafe impl<'a, T, R: DimName + IsNotStaticOne, C: Dim + IsNotStaticOne> IsContiguous for ViewStorage<'a, T, R, C, U1, R> { } -unsafe impl<'a, T, R: DimName, C: Dim + IsNotStaticOne> IsContiguous +unsafe impl<'a, T, R: DimName + IsNotStaticOne, C: Dim + IsNotStaticOne> IsContiguous for ViewStorageMut<'a, T, R, C, U1, R> { }