Add unsafe slice construction.

This commit is contained in:
Sébastien Crozet 2018-02-02 12:26:18 +01:00
parent a35e40b38e
commit 4cb43498a4

View File

@ -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> { 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] #[inline]
pub fn new_slice_with_strides_generic(data: &'a [N], nrows: R, ncols: C, rstride: RStride, cstride: CStride) -> Self { 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 // 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, ncols.value() * cstride.value() + nrows.value() * rstride.value() + 1,
"Matrix slice: input data buffer to small."); "Matrix slice: input data buffer to small.");
let data = unsafe { unsafe {
SliceStorage::from_raw_parts(data.as_ptr(), (nrows, ncols), (rstride, cstride)) Self::new_slice_with_strides_generic_unchecked(data, 0, nrows, ncols, rstride, cstride)
}; }
Self::from_data(data)
} }
} }
impl<'a, N: Scalar, R: Dim, C: Dim, RStride: Dim, CStride: Dim> MatrixSliceMutMN<'a, N, R, C, 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] #[inline]
pub fn new_slice_with_strides_mut_generic(data: &'a mut [N], nrows: R, ncols: C, rstride: RStride, cstride: CStride) -> Self { 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 // 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, ncols.value() * cstride.value() + nrows.value() * rstride.value() + 1,
"Matrix slice: input data buffer to small."); "Matrix slice: input data buffer to small.");
let data = unsafe { unsafe {
SliceStorageMut::from_raw_parts(data.as_mut_ptr(), (nrows, ncols), (rstride, cstride)) Self::new_slice_with_strides_mut_generic_unchecked(data, 0, nrows, ncols, rstride, cstride)
}; }
Self::from_data(data)
} }
} }
impl<'a, N: Scalar, R: Dim, C: Dim> MatrixSliceMN<'a, N, R, C> { 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] #[inline]
pub fn new_slice_generic(data: &'a [N], nrows: R, ncols: C) -> Self { pub fn new_slice_generic(data: &'a [N], nrows: R, ncols: C) -> Self {
Self::new_slice_with_strides_generic(data, nrows, ncols, U1, nrows) 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> { 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] #[inline]
pub fn new_slice_mut_generic(data: &'a mut [N], nrows: R, ncols: C) -> Self { 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) 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 { pub fn new(data: &'a [N], $($args: usize),*) -> Self {
Self::new_slice_generic(data, $($gargs),*) 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> { 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 { 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)) 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 { pub fn new(data: &'a mut [N], $($args: usize),*) -> Self {
Self::new_slice_mut_generic(data, $($gargs),*) 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> { 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( Self::new_slice_with_strides_mut_generic(
data, $($gargs,)* Dynamic::new(rstride), Dynamic::new(cstride)) 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))
}
} }
} }
); );