Type Definition ndarray::ArrayView

source ·
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.

Implementations§

Methods for read-only array views.

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;

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]);

Create an ArrayView<A, D> from shape information and a raw pointer to the elements.

Unsafe because 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 the counts 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 overflowing isize 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 exceed isize::MAX. This constraint prevents overflow when calculating the count parameter to .offset() regardless of the starting point due to past offsets.

  • The product of non-zero axis lengths must not exceed isize::MAX.

Convert the view into an ArrayView<'b, A, D> where 'b is a lifetime outlived by 'a'.

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.

Below, an illustration of .split_at(Axis(2), 2) on an array with shape 3 × 5 × 5.

Return the array’s data as a slice, if it is contiguous and in standard order. Return None otherwise.

Trait Implementations§

Implementation of ArrayView::from(&A) where A is an array.

Create a read-only array view of the array.

Converts to this type from the input type.

Implementation of ArrayView::from(&S) where S is a slice or slicable.

Create a one-dimensional read-only array view of the data in slice.

Panics if the slice length is greater than isize::MAX.

Converts to this type from the input type.

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.

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.

The type of the reference to the element that is produced, including its lifetime. Read more
Get a reference of a element through the view. Read more
The type of the elements being iterated over.
Which kind of iterator are we turning this into?
Creates an iterator from a value. Read more
The element produced per iteration.
Dimension type
This trait is private to implement; this method exists to make it impossible to implement outside the crate. Read more