pub type ArrayView<'a, A, D> = ArrayBase<ViewRepr<&'a A>, D>;
Expand description
A read-only array view.
An array view represents an array or a part of it, created from an iterator, subview or slice of an array.
The ArrayView<'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 ArrayViewMut
.
Aliased Type§
struct ArrayView<'a, A, D> { /* private fields */ }
Implementations§
source§impl<'a, A, D> ArrayView<'a, A, D>where
D: Dimension,
impl<'a, A, D> ArrayView<'a, A, D>where
D: Dimension,
Methods for read-only array views.
sourcepub fn from_shape<Sh>(shape: Sh, xs: &'a [A]) -> Result<Self, ShapeError>where
Sh: Into<StrideShape<D>>,
pub fn from_shape<Sh>(shape: Sh, xs: &'a [A]) -> Result<Self, ShapeError>where
Sh: Into<StrideShape<D>>,
Create a read-only array view borrowing its data from a slice.
Checks whether shape
are compatible with the slice’s
length, returning an Err
if not compatible.
use ndarray::ArrayView;
use ndarray::arr3;
use ndarray::ShapeBuilder;
// advanced example where we are even specifying exact strides to use (which is optional).
let s = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
let a = ArrayView::from_shape((2, 3, 2).strides((1, 4, 2)),
&s).unwrap();
assert!(
a == arr3(&[[[0, 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: *const A) -> Selfwhere
Sh: Into<StrideShape<D>>,
pub unsafe fn from_shape_ptr<Sh>(shape: Sh, ptr: *const A) -> Selfwhere
Sh: Into<StrideShape<D>>,
Create an ArrayView<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
ptr
according to the shape and strides must live at least as long as'a
and must not be not mutably aliased for the duration of'a
. -
ptr
must be non-null and aligned, and it must be safe to.offset()
ptr
by zero. -
It must be safe to
.offset()
the pointer repeatedly along all axes and calculate thecount
s 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,ptr
may 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 overflowingisize
regardless of the starting point due to past offsets. -
The offset in units of
A
between the least address and greatest address by moving along all axes must not exceedisize::MAX
. This constraint prevents overflow when calculating thecount
parameter 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.
source§impl<'a, A, D> ArrayView<'a, A, D>where
D: Dimension,
impl<'a, A, D> ArrayView<'a, A, D>where
D: Dimension,
Methods for read-only array views.
sourcepub fn reborrow<'b>(self) -> ArrayView<'b, A, D>where
'a: 'b,
pub fn reborrow<'b>(self) -> ArrayView<'b, A, D>where
'a: 'b,
Convert the view into an ArrayView<'b, A, D>
where 'b
is a lifetime
outlived by 'a'
.
sourcepub fn to_slice(&self) -> Option<&'a [A]>
pub fn to_slice(&self) -> Option<&'a [A]>
Return the array’s data as a slice, if it is contiguous and in standard order.
Return None
otherwise.
Note that while the method is similar to ArrayBase::as_slice()
, this method transfers
the view’s lifetime to the slice, so it is a bit more powerful.
sourcepub fn to_slice_memory_order(&self) -> Option<&'a [A]>
pub fn to_slice_memory_order(&self) -> Option<&'a [A]>
Return the array’s data as a slice, if it is contiguous.
Return None
otherwise.
Note that while the method is similar to
ArrayBase::as_slice_memory_order()
, this method transfers the view’s
lifetime to the slice, so it is a bit more powerful.
source§impl<'a, A> ArrayView<'a, A, Ix0>
impl<'a, A> ArrayView<'a, A, Ix0>
sourcepub fn into_scalar(self) -> &'a A
pub fn into_scalar(self) -> &'a A
Consume the view and return a 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};
// `Foo` doesn't implement `Clone`.
#[derive(Debug, Eq, PartialEq)]
struct Foo;
let array: Array0<Foo> = arr0(Foo);
let view = array.view();
let scalar: &Foo = view.into_scalar();
assert_eq!(scalar, &Foo);
source§impl<'a, A, D> ArrayView<'a, A, D>where
D: Dimension,
impl<'a, A, D> ArrayView<'a, A, D>where
D: Dimension,
Methods for read-only 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 view strictly before the
split and one view after the split.
Panics if axis
or index
is out of bounds.
Examples:
let a = aview2(&[[0, 1, 2, 3],
[4, 5, 6, 7],
[8, 9, 0, 1]]);
The array view a
has two axes and shape 3 × 4:
──▶ Axis(1)
┌─────┬─────┬─────┬─────┐ 0
│ │ a₀₀ │ a₀₁ │ a₀₂ │ a₀₃ │
▼ ├─────┼─────┼─────┼─────┤ 1
Axis(0)│ a₁₀ │ a₁₁ │ a₁₂ │ a₁₃ │
├─────┼─────┼─────┼─────┤ 2
│ a₂₀ │ a₂₁ │ a₂₂ │ a₂₃ │
└─────┴─────┴─────┴─────┘ 3 ↑
0 1 2 3 4 ← possible split_at indices.
Row indices increase along Axis(0)
, and column indices increase along
Axis(1)
. Note that we split “before” an element index, and that
both 0 and the endpoint are valid split indices.
Example 1: Split a
along the first axis, in this case the rows, at
index 2.
This produces views v1 and v2 of shapes 2 × 4 and 1 × 4:
let (v1, v2) = a.split_at(Axis(0), 2);
┌─────┬─────┬─────┬─────┐ 0 ↓ indices
│ a₀₀ │ a₀₁ │ a₀₂ │ a₀₃ │ along Axis(0)
├─────┼─────┼─────┼─────┤ v1 1
│ a₁₀ │ a₁₁ │ a₁₂ │ a₁₃ │
└─────┴─────┴─────┴─────┘
┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄ 2
┌─────┬─────┬─────┬─────┐
│ a₂₀ │ a₂₁ │ a₂₂ │ a₂₃ │ v2
└─────┴─────┴─────┴─────┘ 3
Example 2: Split a
along the second axis, in this case the
columns, at index 2.
This produces views u1 and u2 of shapes 3 × 2 and 3 × 2:
let (u1, u2) = a.split_at(Axis(1), 2);
u1 u2
┌─────┬─────┐┊┌─────┬─────┐
│ a₀₀ │ a₀₁ │┊│ a₀₂ │ a₀₃ │
├─────┼─────┤┊├─────┼─────┤
│ a₁₀ │ a₁₁ │┊│ a₁₂ │ a₁₃ │
├─────┼─────┤┊├─────┼─────┤
│ a₂₀ │ a₂₁ │┊│ a₂₂ │ a₂₃ │
└─────┴─────┘┊└─────┴─────┘
0 1 2 3 4 indices →
along Axis(1)
source§impl<'a, T, D> ArrayView<'a, Complex<T>, D>where
D: Dimension,
impl<'a, T, D> ArrayView<'a, Complex<T>, D>where
D: Dimension,
sourcepub fn split_complex(self) -> Complex<ArrayView<'a, T, D>>
pub fn split_complex(self) -> Complex<ArrayView<'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 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 { re, im } = arr.view().split_complex();
assert_eq!(re, array![[1., 3.], [5., 7.], [9., 11.]]);
assert_eq!(im, array![[2., 4.], [6., 8.], [10., 12.]]);
Trait Implementations§
source§impl<'a, A, const N: usize> From<&'a [[A; N]]> for ArrayView<'a, A, Ix2>
impl<'a, A, const N: usize> From<&'a [[A; N]]> for ArrayView<'a, A, Ix2>
Implementation of ArrayView2::from(&[[A; N]])
Panics if the product of non-zero axis lengths overflows isize
. (This
can only occur if A is zero-sized or if N
is zero, because slices cannot
contain more than isize::MAX
number of bytes.)
source§impl<'a, A, const M: usize, const N: usize> From<&'a [[A; N]; M]> for ArrayView<'a, A, Ix2>
impl<'a, A, const M: usize, const N: usize> From<&'a [[A; N]; M]> for ArrayView<'a, A, Ix2>
Implementation of ArrayView2::from(&[[A; N]; M])
Panics if the product of non-zero axis lengths overflows isize
(This can only occur if A
is zero-sized because slices cannot contain more than isize::MAX
number of bytes).
Panics if N == 0 and the number of rows is greater than isize::MAX.
source§impl<'a, A, S, D> From<&'a ArrayBase<S, D>> for ArrayView<'a, A, D>
impl<'a, A, S, D> From<&'a ArrayBase<S, D>> for ArrayView<'a, A, D>
Implementation of ArrayView::from(&A)
where A
is an array.
source§impl<'a, A, Slice> From<&'a Slice> for ArrayView<'a, A, Ix1>
impl<'a, A, Slice> From<&'a Slice> for ArrayView<'a, A, Ix1>
Implementation of ArrayView::from(&S)
where S
is a slice or sliceable.
Panics if the length of the slice overflows isize
. (This can only
occur if A
is zero-sized, because slices cannot contain more than
isize::MAX
number of bytes.)
source§impl<'a, 'b, I, A, D> IndexLonger<I> for &'b ArrayView<'a, A, D>
impl<'a, 'b, I, A, D> IndexLonger<I> for &'b ArrayView<'a, A, D>
source§fn index(self, index: I) -> &'a A
fn index(self, index: I) -> &'a A
Get a reference of a element through the view.
This method is like Index::index
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
method which works for all arrays and array
views.
Panics if index is out of bounds.
source§unsafe fn uget(self, index: I) -> &'a A
unsafe fn uget(self, index: I) -> &'a A
Get a reference of a element through the view without boundary check
This method is like elem
with a longer lifetime (matching the array
view); which we can’t do for general arrays.
See also the uget
method which works for all arrays and array
views.
Note: only unchecked for non-debug builds of ndarray.
source§impl<'a, A, D> IntoIterator for ArrayView<'a, A, D>where
D: Dimension,
impl<'a, A, D> IntoIterator for ArrayView<'a, A, D>where
D: Dimension,
source§impl<'a, A, D> IntoParallelIterator for ArrayView<'a, A, D>
impl<'a, A, D> IntoParallelIterator for ArrayView<'a, A, D>
Requires crate feature rayon
.
§type Item = <ArrayBase<ViewRepr<&'a A>, D> as IntoIterator>::Item
type Item = <ArrayBase<ViewRepr<&'a A>, D> as IntoIterator>::Item
§type Iter = Parallel<ArrayBase<ViewRepr<&'a A>, D>>
type Iter = Parallel<ArrayBase<ViewRepr<&'a A>, D>>
source§fn into_par_iter(self) -> Self::Iter
fn into_par_iter(self) -> Self::Iter
self
into a parallel iterator. Read more