Skip to main content

StridedArray

Struct StridedArray 

pub struct StridedArray<T> { /* private fields */ }
Expand description

Owned strided multidimensional array.

Supports both column-major (Julia default) and row-major (C default) layouts.

Implementations§

§

impl<T> StridedArray<T>
where T: Clone + Default,

pub fn col_major(dims: &[usize]) -> StridedArray<T>

Create a column-major (Julia default) tensor filled with Default values.

The shape and strides are stored inline, so a rank <= INLINE_RANK tensor allocates only its element buffer.

§Examples
use strided_view::StridedArray;

let a = StridedArray::<f64>::col_major(&[2, 3]);
assert_eq!(a.dims(), &[2, 3]);
assert_eq!(a.strides(), &[1, 2]);
§Panics

Panics when the element count or a stride does not fit isize; use Self::try_col_major to get StridedError::OffsetOverflow instead.

pub fn try_col_major(dims: &[usize]) -> Result<StridedArray<T>, StridedError>

Fallible Self::col_major.

§Errors

Returns StridedError::OffsetOverflow when the element count or a stride does not fit isize.

§Examples
use strided_view::{StridedArray, StridedError};

assert_eq!(StridedArray::<f64>::try_col_major(&[2, 3]).unwrap().strides(), &[1, 2]);
assert!(matches!(
    StridedArray::<f64>::try_col_major(&[1 << 62, 4]),
    Err(StridedError::OffsetOverflow)
));

pub fn row_major(dims: &[usize]) -> StridedArray<T>

Create a row-major (C default) tensor filled with Default values.

The shape and strides are stored inline, so a rank <= INLINE_RANK tensor allocates only its element buffer.

§Examples
use strided_view::StridedArray;

let a = StridedArray::<f64>::row_major(&[2, 3]);
assert_eq!(a.dims(), &[2, 3]);
assert_eq!(a.strides(), &[3, 1]);
§Panics

Panics when the element count or a stride does not fit isize; use Self::try_row_major to get StridedError::OffsetOverflow instead.

pub fn try_row_major(dims: &[usize]) -> Result<StridedArray<T>, StridedError>

Fallible Self::row_major.

§Errors

Returns StridedError::OffsetOverflow when the element count or a stride does not fit isize.

pub fn from_fn_col_major( dims: &[usize], f: impl FnMut(&[usize]) -> T, ) -> StridedArray<T>

Create a column-major tensor with values produced by a function.

The function is called with indices in column-major iteration order.

§Panics

Panics when the element count or a stride does not fit isize; use Self::try_from_fn_col_major to get StridedError::OffsetOverflow instead.

pub fn try_from_fn_col_major( dims: &[usize], f: impl FnMut(&[usize]) -> T, ) -> Result<StridedArray<T>, StridedError>

Fallible Self::from_fn_col_major; f is not called on error.

§Errors

Returns StridedError::OffsetOverflow when the element count or a stride does not fit isize.

pub fn from_fn_row_major( dims: &[usize], f: impl FnMut(&[usize]) -> T, ) -> StridedArray<T>

Create a row-major tensor with values produced by a function.

The function is called with indices in row-major iteration order.

§Panics

Panics when the element count or a stride does not fit isize; use Self::try_from_fn_row_major to get StridedError::OffsetOverflow instead.

pub fn try_from_fn_row_major( dims: &[usize], f: impl FnMut(&[usize]) -> T, ) -> Result<StridedArray<T>, StridedError>

Fallible Self::from_fn_row_major; f is not called on error.

§Errors

Returns StridedError::OffsetOverflow when the element count or a stride does not fit isize.

§

impl<T> StridedArray<T>

pub fn from_parts( data: Vec<T>, dims: &[usize], strides: &[isize], offset: isize, ) -> Result<StridedArray<T>, StridedError>

Create from raw parts.

pub fn dims(&self) -> &[usize]

Returns the shape (dimension sizes) of this array.

pub fn strides(&self) -> &[isize]

Returns the strides (in units of T) for each dimension.

pub fn ndim(&self) -> usize

Returns the number of dimensions (rank).

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.

pub fn is_empty(&self) -> bool

Returns true if any dimension is zero.

pub fn data(&self) -> &[T]

Returns a reference to the backing data slice.

pub fn data_mut(&mut self) -> &mut [T]

Returns a mutable reference to the backing data slice.

pub fn view(&self) -> StridedView<'_, T>

Create an immutable view over this tensor.

pub fn view_mut(&mut self) -> StridedViewMut<'_, T>

Create a mutable view over this tensor.

pub fn permuted(self, perm: &[usize]) -> Result<StridedArray<T>, StridedError>

Permute dimensions (metadata-only reorder, no data copy).

Returns a new array with reordered dims and strides. The underlying data buffer is not touched.

pub fn into_data(self) -> Vec<T>

Consume the array and return the backing Vec<T>.

pub fn iter(&self) -> Iter<'_, T> ⓘ

Iterate over all elements in memory order.

pub fn iter_mut(&mut self) -> IterMut<'_, T> ⓘ

Mutable iteration over all elements in memory order.

§

impl<T> StridedArray<T>
where T: Default,

pub fn col_major_from_buffer(buf: Vec<T>, dims: &[usize]) -> StridedArray<T>

Create a column-major tensor reusing an existing buffer.

If buf has at least product(dims) elements, it is truncated and zeroed. Otherwise a fresh buffer is allocated.

§Panics

Panics when the element count or a stride does not fit isize; use Self::try_col_major_from_buffer to get StridedError::OffsetOverflow instead.

pub fn try_col_major_from_buffer( buf: Vec<T>, dims: &[usize], ) -> Result<StridedArray<T>, StridedError>

Fallible Self::col_major_from_buffer; buf is dropped on error.

§Errors

Returns StridedError::OffsetOverflow when the element count or a stride does not fit isize.

§

impl<T> StridedArray<T>
where T: Copy,

pub unsafe fn col_major_uninit(dims: &[usize]) -> StridedArray<T>

Create a column-major tensor with uninitialized data.

§Safety

Caller must write every element before reading.

§Panics

Panics when the element count or a stride does not fit isize; use Self::try_col_major_uninit to get StridedError::OffsetOverflow instead.

pub unsafe fn try_col_major_uninit( dims: &[usize], ) -> Result<StridedArray<T>, StridedError>

Fallible Self::col_major_uninit.

§Safety

Caller must write every element before reading.

§Errors

Returns StridedError::OffsetOverflow when the element count or a stride does not fit isize.

pub unsafe fn col_major_from_buffer_uninit( buf: Vec<T>, dims: &[usize], ) -> StridedArray<T>

Reuse an existing buffer as a column-major tensor without zeroing.

§Safety

Caller must write every element before reading.

§Panics

Panics when the element count or a stride does not fit isize; use Self::try_col_major_from_buffer_uninit to get StridedError::OffsetOverflow instead.

pub unsafe fn try_col_major_from_buffer_uninit( buf: Vec<T>, dims: &[usize], ) -> Result<StridedArray<T>, StridedError>

Fallible Self::col_major_from_buffer_uninit; buf is dropped on error.

§Safety

Caller must write every element before reading.

§Errors

Returns StridedError::OffsetOverflow when the element count or a stride does not fit isize.

§

impl<T> StridedArray<T>
where T: Copy,

pub fn get(&self, indices: &[usize]) -> T

Get an element by multi-dimensional index.

§Panics

Panics if indices.len() is not the array’s rank, an index is out of bounds, or the indexed offset does not fit isize.

pub fn set(&mut self, indices: &[usize], value: T)

Set an element by multi-dimensional index.

§Panics

Panics if indices.len() is not the array’s rank, an index is out of bounds, or the indexed offset does not fit isize.

Trait Implementations§

§

impl<T> Clone for StridedArray<T>
where T: Clone,

§

fn clone(&self) -> StridedArray<T>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
§

impl<T> Debug for StridedArray<T>
where T: Debug,

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl<T> Index<&[usize]> for StridedArray<T>
where T: Copy,

§

type Output = T

The returned type after indexing.
§

fn index(&self, indices: &[usize]) -> &T

Performs the indexing (container[index]) operation. Read more
§

impl<T> IndexMut<&[usize]> for StridedArray<T>
where T: Copy,

§

fn index_mut(&mut self, indices: &[usize]) -> &mut T

Performs the mutable indexing (container[index]) operation. Read more

Auto Trait Implementations§

§

impl<T> Freeze for StridedArray<T>

§

impl<T> RefUnwindSafe for StridedArray<T>
where T: RefUnwindSafe,

§

impl<T> Send for StridedArray<T>
where T: Send,

§

impl<T> Sync for StridedArray<T>
where T: Sync,

§

impl<T> Unpin for StridedArray<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for StridedArray<T>

§

impl<T> UnwindSafe for StridedArray<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T, S> SimdFrom<T, S> for T
where S: Simd,

§

fn simd_from(_simd: S, value: T) -> T

§

impl<F, T, S> SimdInto<T, S> for F
where T: SimdFrom<F, S>, S: Simd,

§

fn simd_into(self, simd: S) -> T

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.