pub struct StridedViewMut<'a, T> { /* private fields */ }Expand description
Dynamic-rank mutable strided view.
Always uses Identity element operation for write simplicity.
Julia typically applies ops on the read side.
Implementations§
Source§impl<'a, T> StridedViewMut<'a, T>
impl<'a, T> StridedViewMut<'a, T>
Sourcepub fn new(
data: &'a mut [T],
dims: &[usize],
strides: &[isize],
offset: isize,
) -> Result<Self>
pub fn new( data: &'a mut [T], dims: &[usize], strides: &[isize], offset: isize, ) -> Result<Self>
Create a mutable view, copying the caller’s metadata inline.
The layout is copied, so it does not need to outlive the view and no
heap allocation happens at rank <= INLINE_RANK.
§Errors
Returns StridedError::StrideLengthMismatch when dims and
strides have different lengths, and
StridedError::OffsetOverflow when a reachable offset is not
representable: dim - 1 of a non-unit extent does not fit isize, a
stride times that last index or the accumulated offsets overflow
isize or go negative, or the highest reachable offset is not below
the buffer length. A layout with a zero extent skips this check.
§Examples
use strided_view::StridedViewMut;
let mut data = [0.0f64; 4];
let mut view = StridedViewMut::<f64>::new(&mut data, &[2, 2], &[1, 2], 0).unwrap();
view.set(&[1, 1], 5.0);
assert_eq!(data[3], 5.0);Sourcepub unsafe fn new_unchecked(
data: &'a mut [T],
dims: &[usize],
strides: &[isize],
offset: isize,
) -> Self
pub unsafe fn new_unchecked( data: &'a mut [T], dims: &[usize], strides: &[isize], offset: isize, ) -> Self
Create without bounds checking, copying the caller’s metadata.
§Safety
Caller must ensure all index combinations stay within bounds.
§Examples
use strided_view::StridedViewMut;
let mut data = [0.0f64; 4];
let view = unsafe { StridedViewMut::<f64>::new_unchecked(&mut data, &[2, 2], &[1, 2], 0) };
assert_eq!(view.ndim(), 2);Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the total number of elements.
§Panics
Panics when the element count does not fit usize, which only a
broadcast (stride-0) layout can reach.
Sourcepub fn as_mut_ptr(&self) -> *mut T
pub fn as_mut_ptr(&self) -> *mut T
Raw mutable pointer to element at the view’s base offset.
Sourcepub fn data(&self) -> &[T]
pub fn data(&self) -> &[T]
Returns a reference to the backing data slice.
The length is the backing slice’s, not the shape’s: a view whose shape has a zero extent still reports the whole buffer.
Sourcepub fn data_mut(&mut self) -> &mut [T]
pub fn data_mut(&mut self) -> &mut [T]
Returns a mutable reference to the backing data slice.
The slice is re-derived from the view’s storage handle, so ptr, get,
and set stay valid after the returned borrow ends.
Sourcepub fn permute(self, perm: &[usize]) -> Result<StridedViewMut<'a, T>>
pub fn permute(self, perm: &[usize]) -> Result<StridedViewMut<'a, T>>
Permute dimensions, consuming the mutable view.
Returns a new mutable view with reordered dimensions and strides.
Takes self by value to prevent aliasing of mutable views.
§Errors
Returns StridedError::RankMismatch when perm does not have one
entry per axis, and StridedError::InvalidAxis when an entry
names an axis outside the rank or repeats an axis.
Sourcepub fn as_view(&self) -> StridedView<'_, T, Identity>
pub fn as_view(&self) -> StridedView<'_, T, Identity>
Reborrow as an immutable view.
The reborrow copies this view’s metadata into the immutable view’s
inline storage, which allocates nothing up to INLINE_RANK and
spills above it (the mutable owner always keeps its own copy).
§Examples
use strided_view::StridedViewMut;
let mut data = [1.0f64, 2.0, 3.0];
let view = StridedViewMut::<f64>::new(&mut data, &[3], &[1], 0).unwrap();
let read = view.as_view();
assert_eq!(read.get(&[2]), 3.0);