Skip to content

Commit 2853d54

Browse files
committed
add strided constructor for ArrayView
1 parent 926ddbe commit 2853d54

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

src/lib.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -851,6 +851,45 @@ impl<'a, A, D> ArrayViewMut<'a, A, D>
851851
}
852852
}
853853

854+
/// Create an `ArrayView` borrowing its data from a slice.
855+
///
856+
/// Checks whether `dim` and `strides` are compatible with the slice's
857+
/// length, returning an `Err` if not compatible.
858+
///
859+
/// ```
860+
/// use ndarray::ArrayViewMut;
861+
/// use ndarray::arr3;
862+
///
863+
/// let s = &mut [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
864+
/// let mut a = ArrayViewMut::from_slice_dim_stride((2, 3, 2),
865+
/// (1, 4, 2),
866+
/// s).unwrap();
867+
///
868+
/// a[[0, 0, 0]] = 1;
869+
/// assert!(
870+
/// a == arr3(&[[[1, 2],
871+
/// [4, 6],
872+
/// [8, 10]],
873+
/// [[1, 3],
874+
/// [5, 7],
875+
/// [9, 11]]])
876+
/// );
877+
/// assert!(a.strides() == &[1, 4, 2]);
878+
/// ```
879+
pub fn from_slice_dim_stride(dim: D,
880+
strides: D,
881+
s: &'a mut [A]
882+
) -> Result<Self, StrideError>
883+
{
884+
dimension::can_index_slice(s,
885+
&dim,
886+
&strides).map(|_| {
887+
unsafe {
888+
Self::new_(s.as_mut_ptr(), dim, strides)
889+
}
890+
})
891+
}
892+
854893
#[inline]
855894
fn into_base_iter(self) -> Baseiter<'a, A, D> {
856895
unsafe {

0 commit comments

Comments
 (0)