From 4cb43498a413b6ba2a7f3ced631767cf20ba07b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Crozet?= Date: Fri, 2 Feb 2018 12:26:18 +0100 Subject: [PATCH] Add unsafe slice construction. --- src/core/construction_slice.rs | 63 +++++++++++++++++++++++++++++----- 1 file changed, 55 insertions(+), 8 deletions(-) diff --git a/src/core/construction_slice.rs b/src/core/construction_slice.rs index 992d7792..8d161044 100644 --- a/src/core/construction_slice.rs +++ b/src/core/construction_slice.rs @@ -23,6 +23,15 @@ use core::matrix_slice::{SliceStorage, SliceStorageMut}; * */ impl<'a, N: Scalar, R: Dim, C: Dim, RStride: Dim, CStride: Dim> MatrixSliceMN<'a, N, R, C, RStride, CStride> { + #[inline] + pub unsafe fn new_slice_with_strides_generic_unchecked( + data: &'a [N], start: usize, nrows: R, ncols: C, rstride: RStride, cstride: CStride) -> Self { + let data = unsafe { + SliceStorage::from_raw_parts(data.as_ptr().offset(start as isize), (nrows, ncols), (rstride, cstride)) + }; + Self::from_data(data) + } + #[inline] pub fn new_slice_with_strides_generic(data: &'a [N], nrows: R, ncols: C, rstride: RStride, cstride: CStride) -> Self { // NOTE: The assertion implements the following formula, but without subtractions to avoid @@ -32,14 +41,22 @@ impl<'a, N: Scalar, R: Dim, C: Dim, RStride: Dim, CStride: Dim> MatrixSliceMN<'a ncols.value() * cstride.value() + nrows.value() * rstride.value() + 1, "Matrix slice: input data buffer to small."); - let data = unsafe { - SliceStorage::from_raw_parts(data.as_ptr(), (nrows, ncols), (rstride, cstride)) - }; - Self::from_data(data) + unsafe { + Self::new_slice_with_strides_generic_unchecked(data, 0, nrows, ncols, rstride, cstride) + } } } impl<'a, N: Scalar, R: Dim, C: Dim, RStride: Dim, CStride: Dim> MatrixSliceMutMN<'a, N, R, C, RStride, CStride> { + #[inline] + pub unsafe fn new_slice_with_strides_mut_generic_unchecked( + data: &'a mut [N], start: usize, nrows: R, ncols: C, rstride: RStride, cstride: CStride) -> Self { + let data = unsafe { + SliceStorageMut::from_raw_parts(data.as_mut_ptr().offset(start as isize), (nrows, ncols), (rstride, cstride)) + }; + Self::from_data(data) + } + #[inline] pub fn new_slice_with_strides_mut_generic(data: &'a mut [N], nrows: R, ncols: C, rstride: RStride, cstride: CStride) -> Self { // NOTE: The assertion implements the following formula, but without subtractions to avoid @@ -49,14 +66,18 @@ impl<'a, N: Scalar, R: Dim, C: Dim, RStride: Dim, CStride: Dim> MatrixSliceMutMN ncols.value() * cstride.value() + nrows.value() * rstride.value() + 1, "Matrix slice: input data buffer to small."); - let data = unsafe { - SliceStorageMut::from_raw_parts(data.as_mut_ptr(), (nrows, ncols), (rstride, cstride)) - }; - Self::from_data(data) + unsafe { + Self::new_slice_with_strides_mut_generic_unchecked(data, 0, nrows, ncols, rstride, cstride) + } } } impl<'a, N: Scalar, R: Dim, C: Dim> MatrixSliceMN<'a, N, R, C> { + #[inline] + pub unsafe fn new_slice_generic_unchecked(data: &'a [N], start: usize, nrows: R, ncols: C) -> Self { + Self::new_slice_with_strides_generic_unchecked(data, start, nrows, ncols, U1, nrows) + } + #[inline] pub fn new_slice_generic(data: &'a [N], nrows: R, ncols: C) -> Self { Self::new_slice_with_strides_generic(data, nrows, ncols, U1, nrows) @@ -64,6 +85,11 @@ impl<'a, N: Scalar, R: Dim, C: Dim> MatrixSliceMN<'a, N, R, C> { } impl<'a, N: Scalar, R: Dim, C: Dim> MatrixSliceMutMN<'a, N, R, C> { + #[inline] + pub unsafe fn new_slice_mut_generic_unchecked(data: &'a mut [N], start: usize, nrows: R, ncols: C) -> Self { + Self::new_slice_with_strides_mut_generic_unchecked(data, start, nrows, ncols, U1, nrows) + } + #[inline] pub fn new_slice_mut_generic(data: &'a mut [N], nrows: R, ncols: C) -> Self { Self::new_slice_with_strides_mut_generic(data, nrows, ncols, U1, nrows) @@ -77,6 +103,11 @@ macro_rules! impl_constructors( pub fn new(data: &'a [N], $($args: usize),*) -> Self { Self::new_slice_generic(data, $($gargs),*) } + + #[inline] + pub unsafe fn new_unchecked(data: &'a [N], start: usize, $($args: usize),*) -> Self { + Self::new_slice_generic_unchecked(data, start, $($gargs),*) + } } impl<'a, N: Scalar, $($DimIdent: $DimBound, )*> MatrixSliceMN<'a, N, $($Dims,)* Dynamic, Dynamic> { @@ -84,6 +115,11 @@ macro_rules! impl_constructors( pub fn new_with_strides(data: &'a [N], $($args: usize,)* rstride: usize, cstride: usize) -> Self { Self::new_slice_with_strides_generic(data, $($gargs,)* Dynamic::new(rstride), Dynamic::new(cstride)) } + + #[inline] + pub unsafe fn new_with_strides_unchecked(data: &'a [N], start: usize, $($args: usize,)* rstride: usize, cstride: usize) -> Self { + Self::new_slice_with_strides_generic_unchecked(data, start, $($gargs,)* Dynamic::new(rstride), Dynamic::new(cstride)) + } } } ); @@ -118,6 +154,11 @@ macro_rules! impl_constructors_mut( pub fn new(data: &'a mut [N], $($args: usize),*) -> Self { Self::new_slice_mut_generic(data, $($gargs),*) } + + #[inline] + pub unsafe fn new_unchecked(data: &'a mut [N], start: usize, $($args: usize),*) -> Self { + Self::new_slice_mut_generic_unchecked(data, start, $($gargs),*) + } } impl<'a, N: Scalar, $($DimIdent: $DimBound, )*> MatrixSliceMutMN<'a, N, $($Dims,)* Dynamic, Dynamic> { @@ -126,6 +167,12 @@ macro_rules! impl_constructors_mut( Self::new_slice_with_strides_mut_generic( data, $($gargs,)* Dynamic::new(rstride), Dynamic::new(cstride)) } + + #[inline] + pub unsafe fn new_with_strides_unchecked(data: &'a mut [N], start: usize, $($args: usize,)* rstride: usize, cstride: usize) -> Self { + Self::new_slice_with_strides_mut_generic_unchecked( + data, start, $($gargs,)* Dynamic::new(rstride), Dynamic::new(cstride)) + } } } );