Type Definition ndarray::ArrayViewMut
source · [−]Expand description
A read-write array view.
An array view represents an array or a part of it, created from an iterator, subview or slice of an array.
The ArrayViewMut<'a, A, D> is parameterized by 'a for the scope of the
borrow, A for the element type and D for the dimensionality.
Array views have all the methods of an array (see ArrayBase).
See also ArrayView.
Implementations
sourceimpl<'a, A, D> ArrayViewMut<'a, A, D> where
D: Dimension,
impl<'a, A, D> ArrayViewMut<'a, A, D> where
D: Dimension,
Methods for read-write array views.
sourcepub fn from_shape<Sh>(shape: Sh, xs: &'a mut [A]) -> Result<Self, ShapeError> where
Sh: Into<StrideShape<D>>,
pub fn from_shape<Sh>(shape: Sh, xs: &'a mut [A]) -> Result<Self, ShapeError> where
Sh: Into<StrideShape<D>>,
Create a read-write array view borrowing its data from a slice.
Checks whether dim and strides are compatible with the slice’s
length, returning an Err if not compatible.
use ndarray::ArrayViewMut;
use ndarray::arr3;
use ndarray::ShapeBuilder;
let mut s = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
let mut a = ArrayViewMut::from_shape((2, 3, 2).strides((1, 4, 2)),
&mut s).unwrap();
a[[0, 0, 0]] = 1;
assert!(
a == arr3(&[[[1, 2],
[4, 6],
[8, 10]],
[[1, 3],
[5, 7],
[9, 11]]])
);
assert!(a.strides() == &[1, 4, 2]);sourcepub unsafe fn from_shape_ptr<Sh>(shape: Sh, ptr: *mut A) -> Self where
Sh: Into<StrideShape<D>>,
pub unsafe fn from_shape_ptr<Sh>(shape: Sh, ptr: *mut A) -> Self where
Sh: Into<StrideShape<D>>,
Create an ArrayViewMut<A, D> from shape information and a
raw pointer to the elements.
Safety
The caller is responsible for ensuring all of the following:
-
The elements seen by moving
ptraccording to the shape and strides must live at least as long as'aand must not be aliased for the duration of'a. -
ptrmust be non-null and aligned, and it must be safe to.offset()ptrby zero. -
It must be safe to
.offset()the pointer repeatedly along all axes and calculate thecounts for the.offset()calls without overflow, even if the array is empty or the elements are zero-sized.In other words,
-
All possible pointers generated by moving along all axes must be in bounds or one byte past the end of a single allocation with element type
A. The only exceptions are if the array is empty or the element type is zero-sized. In these cases,ptrmay be dangling, but it must still be safe to.offset()the pointer along the axes. -
The offset in units of bytes between the least address and greatest address by moving along all axes must not exceed
isize::MAX. This constraint prevents the computed offset, in bytes, from overflowingisizeregardless of the starting point due to past offsets. -
The offset in units of
Abetween the least address and greatest address by moving along all axes must not exceedisize::MAX. This constraint prevents overflow when calculating thecountparameter to.offset()regardless of the starting point due to past offsets.
-
-
The product of non-zero axis lengths must not exceed
isize::MAX. -
Strides must be non-negative.
This function can use debug assertions to check some of these requirements, but it’s not a complete check.
sourcepub fn reborrow<'b>(self) -> ArrayViewMut<'b, A, D> where
'a: 'b,
pub fn reborrow<'b>(self) -> ArrayViewMut<'b, A, D> where
'a: 'b,
Convert the view into an ArrayViewMut<'b, A, D> where 'b is a lifetime
outlived by 'a'.
sourceimpl<'a, A> ArrayViewMut<'a, A, Ix0>
impl<'a, A> ArrayViewMut<'a, A, Ix0>
Methods specific to ArrayViewMut0.
See also all methods for ArrayViewMut and ArrayBase
sourcepub fn into_scalar(self) -> &'a mut A
pub fn into_scalar(self) -> &'a mut A
Consume the mutable view and return a mutable reference to the single element in the array.
The lifetime of the returned reference matches the lifetime of the data the array view was pointing to.
use ndarray::{arr0, Array0};
let mut array: Array0<f64> = arr0(5.);
let view = array.view_mut();
let scalar = view.into_scalar();
*scalar = 7.;
assert_eq!(scalar, &7.);
assert_eq!(array[()], 7.);sourceimpl<'a, A, D> ArrayViewMut<'a, A, D> where
D: Dimension,
impl<'a, A, D> ArrayViewMut<'a, A, D> where
D: Dimension,
Methods for read-write array views.
sourcepub fn into_slice(self) -> Option<&'a mut [A]>
pub fn into_slice(self) -> Option<&'a mut [A]>
Return the array’s data as a slice, if it is contiguous and in standard order.
Return None otherwise.
Note that while this is similar to ArrayBase::as_slice_mut(), this method transfers the
view’s lifetime to the slice.
sourcepub fn into_slice_memory_order(self) -> Option<&'a mut [A]>
pub fn into_slice_memory_order(self) -> Option<&'a mut [A]>
Return the array’s data as a slice, if it is contiguous.
Return None otherwise.
Note that while this is similar to
ArrayBase::as_slice_memory_order_mut(), this method transfers the
view’s lifetime to the slice.
sourcepub fn into_cell_view(self) -> ArrayView<'a, MathCell<A>, D>
pub fn into_cell_view(self) -> ArrayView<'a, MathCell<A>, D>
Return a shared view of the array with elements as if they were embedded in cells.
The cell view itself can be copied and accessed without exclusivity.
The view acts “as if” the elements are temporarily in cells, and elements can be changed through shared references using the regular cell methods.
sourceimpl<'a, A, D> ArrayViewMut<'a, A, D> where
D: Dimension,
impl<'a, A, D> ArrayViewMut<'a, A, D> where
D: Dimension,
Methods for read-write array views.
sourcepub fn split_at(self, axis: Axis, index: Ix) -> (Self, Self)
pub fn split_at(self, axis: Axis, index: Ix) -> (Self, Self)
Split the array view along axis and return one mutable view strictly
before the split and one mutable view after the split.
Panics if axis or index is out of bounds.
sourcepub fn multi_slice_move<M>(self, info: M) -> M::Output where
M: MultiSliceArg<'a, A, D>,
pub fn multi_slice_move<M>(self, info: M) -> M::Output where
M: MultiSliceArg<'a, A, D>,
Split the view into multiple disjoint slices.
This is similar to .multi_slice_mut(), but .multi_slice_move()
consumes self and produces views with lifetimes matching that of
self.
See Slicing for full documentation. See also
MultiSliceArg, s!, SliceArg, and
SliceInfo.
Panics if any of the following occur:
- if any of the views would intersect (i.e. if any element would appear in multiple slices)
- if an index is out of bounds or step size is zero
- if
DisIxDynandinfodoes not match the number of array axes
sourceimpl<'a, T, D> ArrayViewMut<'a, Complex<T>, D> where
D: Dimension,
impl<'a, T, D> ArrayViewMut<'a, Complex<T>, D> where
D: Dimension,
sourcepub fn split_complex(self) -> Complex<ArrayViewMut<'a, T, D>>
pub fn split_complex(self) -> Complex<ArrayViewMut<'a, T, D>>
Splits the view into views of the real and imaginary components of the elements.
use ndarray::prelude::*;
use num_complex::{Complex, Complex64};
let mut arr = array![
[Complex64::new(1., 2.), Complex64::new(3., 4.)],
[Complex64::new(5., 6.), Complex64::new(7., 8.)],
[Complex64::new(9., 10.), Complex64::new(11., 12.)],
];
let Complex { mut re, mut im } = arr.view_mut().split_complex();
assert_eq!(re, array![[1., 3.], [5., 7.], [9., 11.]]);
assert_eq!(im, array![[2., 4.], [6., 8.], [10., 12.]]);
re[[0, 1]] = 13.;
im[[2, 0]] = 14.;
assert_eq!(arr[[0, 1]], Complex64::new(13., 4.));
assert_eq!(arr[[2, 0]], Complex64::new(9., 14.));Trait Implementations
sourceimpl<'a, A, S, D> From<&'a mut ArrayBase<S, D>> for ArrayViewMut<'a, A, D> where
S: DataMut<Elem = A>,
D: Dimension,
impl<'a, A, S, D> From<&'a mut ArrayBase<S, D>> for ArrayViewMut<'a, A, D> where
S: DataMut<Elem = A>,
D: Dimension,
Implementation of ArrayViewMut::from(&mut A) where A is an array.
sourceimpl<'a, A, Slice: ?Sized> From<&'a mut Slice> for ArrayViewMut<'a, A, Ix1> where
Slice: AsMut<[A]>,
impl<'a, A, Slice: ?Sized> From<&'a mut Slice> for ArrayViewMut<'a, A, Ix1> where
Slice: AsMut<[A]>,
Implementation of ArrayViewMut::from(&mut S) where S is a slice or sliceable.
sourceimpl<'a, I, A, D> IndexLonger<I> for ArrayViewMut<'a, A, D> where
I: NdIndex<D>,
D: Dimension,
impl<'a, I, A, D> IndexLonger<I> for ArrayViewMut<'a, A, D> where
I: NdIndex<D>,
D: Dimension,
sourcefn index(self, index: I) -> &'a mut A
fn index(self, index: I) -> &'a mut A
Convert a mutable array view to a mutable reference of a element.
This method is like IndexMut::index_mut but with a longer lifetime
(matching the array view); which we can only do for the array view and
not in the Index trait.
See also the get_mut method which works for all arrays and array
views.
Panics if index is out of bounds.
sourcefn get(self, index: I) -> Option<&'a mut A>
fn get(self, index: I) -> Option<&'a mut A>
Convert a mutable array view to a mutable reference of a element, with checked access.
See also the get_mut method which works for all arrays and array
views.
sourceunsafe fn uget(self, index: I) -> &'a mut A
unsafe fn uget(self, index: I) -> &'a mut A
Convert a mutable array view to a mutable reference of a element without boundary check.
See also the uget_mut method which works for all arrays and array
views.
Note: only unchecked for non-debug builds of ndarray.
sourceimpl<'a, A, D> IntoIterator for ArrayViewMut<'a, A, D> where
D: Dimension,
impl<'a, A, D> IntoIterator for ArrayViewMut<'a, A, D> where
D: Dimension,
sourceimpl<'a, A, D> IntoParallelIterator for ArrayViewMut<'a, A, D> where
D: Dimension,
A: Sync + Send,
impl<'a, A, D> IntoParallelIterator for ArrayViewMut<'a, A, D> where
D: Dimension,
A: Sync + Send,
Requires crate feature rayon.
type Item = <ArrayBase<ViewRepr<&'a mut A>, D> as IntoIterator>::Item
type Item = <ArrayBase<ViewRepr<&'a mut A>, D> as IntoIterator>::Item
The type of item that the parallel iterator will produce.
type Iter = Parallel<ArrayBase<ViewRepr<&'a mut A>, D>>
type Iter = Parallel<ArrayBase<ViewRepr<&'a mut A>, D>>
The parallel iterator type that will be created.
sourcefn into_par_iter(self) -> Self::Iter
fn into_par_iter(self) -> Self::Iter
Converts self into a parallel iterator. Read more