Skip to content

Commit

Permalink
Add RowVectorView type
Browse files Browse the repository at this point in the history
Add a view type for row vectors equivalent to the view type for column
vectors.
  • Loading branch information
Tastaturtaste committed Apr 14, 2023
1 parent f5af5db commit c79906c
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 5 deletions.
96 changes: 96 additions & 0 deletions src/base/alias_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,53 @@ pub type VectorView5<'a, T, RStride = U1, CStride = U5> =
pub type VectorView6<'a, T, RStride = U1, CStride = U6> =
Matrix<T, U6, U1, ViewStorage<'a, T, U6, U1, RStride, CStride>>;

/// A row vector view with dimensions known at compile-time.
///
/// **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<T, U1, D, ViewStorage<'a, T, U1, D, RStride, CStride>>;

/// A row vector view with dimensions known at compile-time.
///
/// **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<T, Const<1>, Const<D>, ViewStorage<'a, T, Const<1>, Const<D>, Const<1>, Const<D>>>;

/// A row vector view dynamic numbers of rows and columns.
pub type RowDVectorView<'a, T, RStride = U1, CStride = Dyn> =
Matrix<T, U1, Dyn, ViewStorage<'a, T, U1, Dyn, RStride, CStride>>;

/// A 1D row vector view.
///
/// **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<T, U1, U1, ViewStorage<'a, T, U1, U1, RStride, CStride>>;
/// A 2D row vector view.
///
/// **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<T, U1, U2, ViewStorage<'a, T, U1, U2, RStride, CStride>>;
/// A 3D row vector view.
///
/// **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<T, U1, U3, ViewStorage<'a, T, U1, U3, RStride, CStride>>;
/// A 4D row vector view.
///
/// **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<T, U1, U4, ViewStorage<'a, T, U1, U4, RStride, CStride>>;
/// A 5D row vector view.
///
/// **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<T, U1, U5, ViewStorage<'a, T, U1, U5, RStride, CStride>>;
/// A 6D row vector view.
///
/// **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<T, U1, U6, ViewStorage<'a, T, U1, U6, RStride, CStride>>;

/*
*
*
Expand Down Expand Up @@ -587,3 +634,52 @@ 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<T, U6, U1, ViewStorageMut<'a, T, U6, U1, RStride, CStride>>;

/// A row vector view with dimensions known at compile-time.
///
/// **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<T, D, U1, ViewStorageMut<'a, T, D, U1, RStride, CStride>>;

/// A row vector view with dimensions known at compile-time.
///
/// **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<T, Const<1>, Const<D>, ViewStorageMut<'a, T, Const<1>, Const<D>, Const<1>, Const<D>>>;

/// A row vector view dynamic numbers of rows and columns.
///
/// **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<T, U1, Dyn, ViewStorageMut<'a, T, U1, Dyn, RStride, CStride>>;

/// A 1D row vector view.
///
/// **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<T, U1, U1, ViewStorageMut<'a, T, U1, U1, RStride, CStride>>;
/// A 2D row vector view.
///
/// **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<T, U1, U2, ViewStorageMut<'a, T, U1, U2, RStride, CStride>>;
/// A 3D row vector view.
///
/// **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<T, U1, U3, ViewStorageMut<'a, T, U1, U3, RStride, CStride>>;
/// A 4D row vector view.
///
/// **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<T, U1, U4, ViewStorageMut<'a, T, U1, U4, RStride, CStride>>;
/// A 5D row vector view.
///
/// **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<T, U1, U5, ViewStorageMut<'a, T, U1, U5, RStride, CStride>>;
/// A 6D row vector view.
///
/// **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<T, U1, U6, ViewStorageMut<'a, T, U1, U6, RStride, CStride>>;
28 changes: 27 additions & 1 deletion src/base/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::base::dimension::{
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"))]
Expand Down Expand Up @@ -532,6 +532,32 @@ impl<'a, T: Scalar> From<DVectorViewMut<'a, T>> 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<RowDVectorView<'a, T, Dyn, U1>> 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<RowDVectorViewMut<'a, T, Dyn, U1>> for &'a mut [T] {
fn from(vec: RowDVectorViewMut<'a, T, Dyn, U1>) -> &'a mut [T] {
vec.data.into_slice_mut()
}
}

impl<T: Scalar + PrimitiveSimdValue, R: Dim, C: Dim> From<[OMatrix<T::Element, R, C>; 2]>
for OMatrix<T, R, C>
where
Expand Down
19 changes: 15 additions & 4 deletions src/base/matrix_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,17 +247,28 @@ unsafe impl<'a, T, R: Dim, C: Dim, RStride: Dim, CStride: Dim> RawStorageMut<T,
}
}

unsafe impl<'a, T, R: Dim, CStride: Dim> 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>
{
}
Expand Down

0 comments on commit c79906c

Please sign in to comment.