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§
Source§impl<T: Clone + Default> StridedArray<T>
impl<T: Clone + Default> StridedArray<T>
Sourcepub fn col_major(dims: &[usize]) -> Self
pub fn col_major(dims: &[usize]) -> Self
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.
Sourcepub fn try_col_major(dims: &[usize]) -> Result<Self>
pub fn try_col_major(dims: &[usize]) -> Result<Self>
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)
));Sourcepub fn row_major(dims: &[usize]) -> Self
pub fn row_major(dims: &[usize]) -> Self
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.
Sourcepub fn try_row_major(dims: &[usize]) -> Result<Self>
pub fn try_row_major(dims: &[usize]) -> Result<Self>
Fallible Self::row_major.
§Errors
Returns StridedError::OffsetOverflow when the element count or a
stride does not fit isize.
Sourcepub fn from_fn_col_major(dims: &[usize], f: impl FnMut(&[usize]) -> T) -> Self
pub fn from_fn_col_major(dims: &[usize], f: impl FnMut(&[usize]) -> T) -> Self
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.
Sourcepub fn try_from_fn_col_major(
dims: &[usize],
f: impl FnMut(&[usize]) -> T,
) -> Result<Self>
pub fn try_from_fn_col_major( dims: &[usize], f: impl FnMut(&[usize]) -> T, ) -> Result<Self>
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.
Sourcepub fn from_fn_row_major(dims: &[usize], f: impl FnMut(&[usize]) -> T) -> Self
pub fn from_fn_row_major(dims: &[usize], f: impl FnMut(&[usize]) -> T) -> Self
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.
Sourcepub fn try_from_fn_row_major(
dims: &[usize],
f: impl FnMut(&[usize]) -> T,
) -> Result<Self>
pub fn try_from_fn_row_major( dims: &[usize], f: impl FnMut(&[usize]) -> T, ) -> Result<Self>
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.
Source§impl<T> StridedArray<T>
impl<T> StridedArray<T>
Sourcepub fn from_parts(
data: Vec<T>,
dims: &[usize],
strides: &[isize],
offset: isize,
) -> Result<Self>
pub fn from_parts( data: Vec<T>, dims: &[usize], strides: &[isize], offset: isize, ) -> Result<Self>
Create from raw parts.
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 view(&self) -> StridedView<'_, T>
pub fn view(&self) -> StridedView<'_, T>
Create an immutable view over this tensor.
Sourcepub fn view_mut(&mut self) -> StridedViewMut<'_, T>
pub fn view_mut(&mut self) -> StridedViewMut<'_, T>
Create a mutable view over this tensor.
Source§impl<T: Default> StridedArray<T>
impl<T: Default> StridedArray<T>
Sourcepub fn col_major_from_buffer(buf: Vec<T>, dims: &[usize]) -> Self
pub fn col_major_from_buffer(buf: Vec<T>, dims: &[usize]) -> Self
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.
Sourcepub fn try_col_major_from_buffer(buf: Vec<T>, dims: &[usize]) -> Result<Self>
pub fn try_col_major_from_buffer(buf: Vec<T>, dims: &[usize]) -> Result<Self>
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.
Source§impl<T: Copy> StridedArray<T>
impl<T: Copy> StridedArray<T>
Sourcepub unsafe fn col_major_uninit(dims: &[usize]) -> Self
pub unsafe fn col_major_uninit(dims: &[usize]) -> Self
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.
Sourcepub unsafe fn try_col_major_uninit(dims: &[usize]) -> Result<Self>
pub unsafe fn try_col_major_uninit(dims: &[usize]) -> Result<Self>
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.
Sourcepub unsafe fn col_major_from_buffer_uninit(buf: Vec<T>, dims: &[usize]) -> Self
pub unsafe fn col_major_from_buffer_uninit(buf: Vec<T>, dims: &[usize]) -> Self
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.
Sourcepub unsafe fn try_col_major_from_buffer_uninit(
buf: Vec<T>,
dims: &[usize],
) -> Result<Self>
pub unsafe fn try_col_major_from_buffer_uninit( buf: Vec<T>, dims: &[usize], ) -> Result<Self>
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.