pub struct TensorView<'a, T, const N: usize, Layout = RowMajor, Ref = &'a [T]>where
T: 'a,{ /* private fields */ }Expand description
Zero-copy N-dimensional strided view over a borrowed slice.
§Type Parameters
'a— lifetime of the underlying data slice.T— element type.N— tensor rank (number of dimensions). Const generic; resolved at compile time.Layout— layout marker ZST (RowMajororColMajor).PhantomData; zero size.Ref— reference type-state (&'a [T]or&'a mut [T]).
§Invariants
strides[i] * shape[i]must not overflowusize.data.len() >= ∑ (shape[i]-1) * strides[i] + 1for any valid element access.newandwith_stridesboth verifyproduct(shape) <= data.len().
Implementations§
Source§impl<'a, 'b, T, L> TensorView<'a, T, 2, L, &'b [T]>
impl<'a, 'b, T, L> TensorView<'a, T, 2, L, &'b [T]>
Sourcepub fn row_view(
&self,
i: usize,
) -> Result<TensorView<'a, T, 1, RowMajor, &'b [T]>, TensorError>
pub fn row_view( &self, i: usize, ) -> Result<TensorView<'a, T, 1, RowMajor, &'b [T]>, TensorError>
Return a zero-copy 1-D view of row i.
§Errors
Returns TensorError::IndexOutOfBounds if i >= shape[0].
Sourcepub fn iter_rows(&self) -> Result<impl Iterator<Item = &'b [T]>, TensorError>
pub fn iter_rows(&self) -> Result<impl Iterator<Item = &'b [T]>, TensorError>
Iterate over row slices of a contiguous 2-D tensor.
§Errors
Returns TensorError::NotContiguous if the view is not row-major contiguous.
Sourcepub fn transpose_view(&self) -> TensorView<'a, T, 2, ColMajor, &'b [T]>
pub fn transpose_view(&self) -> TensorView<'a, T, 2, ColMajor, &'b [T]>
Return a zero-copy transposed view (swaps shape and strides).
The result is ColMajor-tagged. No allocation.
Sourcepub fn col_iter(&self, j: usize) -> Result<impl Iterator<Item = T>, TensorError>where
T: Copy,
pub fn col_iter(&self, j: usize) -> Result<impl Iterator<Item = T>, TensorError>where
T: Copy,
Iterate over elements of column j, one element per row.
Zero-copy: index arithmetic over the underlying slice.
§Errors
Returns TensorError::IndexOutOfBounds if j >= shape[1].
Source§impl<'a, T, L> TensorView<'a, T, 2, L, &mut [T]>
impl<'a, T, L> TensorView<'a, T, 2, L, &mut [T]>
Sourcepub fn row_view_mut(
&mut self,
i: usize,
) -> Result<TensorView<'a, T, 1, RowMajor, &mut [T]>, TensorError>
pub fn row_view_mut( &mut self, i: usize, ) -> Result<TensorView<'a, T, 1, RowMajor, &mut [T]>, TensorError>
Sourcepub fn iter_rows_mut(
&mut self,
) -> Result<impl Iterator<Item = &mut [T]>, TensorError>
pub fn iter_rows_mut( &mut self, ) -> Result<impl Iterator<Item = &mut [T]>, TensorError>
Iterate over mutable row slices of a contiguous 2-D tensor.
§Errors
Returns TensorError::NotContiguous if not row-major contiguous.
Source§impl<'a, T, L> TensorView<'a, T, 3, L>
impl<'a, T, L> TensorView<'a, T, 3, L>
Sourcepub fn matrix_at(&self, b: usize) -> Result<TensorView<'_, T, 2>, TensorError>
pub fn matrix_at(&self, b: usize) -> Result<TensorView<'_, T, 2>, TensorError>
Return a 2-D view of the b-th matrix in a batched tensor.
Source§impl<'a, T, L> TensorView<'a, T, 3, L, &'a mut [T]>
impl<'a, T, L> TensorView<'a, T, 3, L, &'a mut [T]>
Sourcepub fn matrix_at_mut(
&mut self,
b: usize,
) -> Result<TensorView<'_, T, 2, RowMajor, &mut [T]>, TensorError>
pub fn matrix_at_mut( &mut self, b: usize, ) -> Result<TensorView<'_, T, 2, RowMajor, &mut [T]>, TensorError>
Return a mutable 2-D view of the b-th matrix in a batched tensor.
Source§impl<'a, T, Ref> TensorView<'a, T, 2, RowMajor, Ref>
impl<'a, T, Ref> TensorView<'a, T, 2, RowMajor, Ref>
Sourcepub fn transpose(self) -> TensorView<'a, T, 2, ColMajor, Ref>
pub fn transpose(self) -> TensorView<'a, T, 2, ColMajor, Ref>
Transpose a row-major 2-D tensor view to column-major.
Source§impl<'a, T, Ref> TensorView<'a, T, 2, ColMajor, Ref>
impl<'a, T, Ref> TensorView<'a, T, 2, ColMajor, Ref>
Sourcepub fn transpose(self) -> TensorView<'a, T, 2, RowMajor, Ref>
pub fn transpose(self) -> TensorView<'a, T, 2, RowMajor, Ref>
Transpose a column-major 2-D tensor view to row-major.
Source§impl<'a, T> TensorView<'a, T, 1>where
T: NumericElement,
impl<'a, T> TensorView<'a, T, 1>where
T: NumericElement,
Sourcepub fn into_simd_view<Arch, Align>(
&self,
) -> Option<SimdView<'a, T, Arch, Align>>
pub fn into_simd_view<Arch, Align>( &self, ) -> Option<SimdView<'a, T, Arch, Align>>
Promote this contiguous rank-1 view into a typed SimdView.
Zero-copy: shares the same underlying slice. Returns None if the slice is empty
or if the alignment check fails for Align.
§Example
use hermes_simd_core::tensor::TensorView;
use hermes_simd_intrinsics::Scalar;
use hermes_simd_core::align::Unaligned;
let data = [1.0f32, 2.0, 3.0, 4.0];
let t = TensorView::<f32, 1>::new(&data, [4]).unwrap();
let view = t.into_simd_view::<Scalar, Unaligned>().unwrap();Source§impl<'a, 'b, T, const N: usize> TensorView<'a, T, N, RowMajor, &'b [T]>
impl<'a, 'b, T, const N: usize> TensorView<'a, T, N, RowMajor, &'b [T]>
Sourcepub fn new(
data: &'b [T],
shape: [usize; N],
) -> Result<TensorView<'a, T, N, RowMajor, &'b [T]>, TensorError>
pub fn new( data: &'b [T], shape: [usize; N], ) -> Result<TensorView<'a, T, N, RowMajor, &'b [T]>, TensorError>
Create a row-major tensor view over data with the given shape.
Strides are computed as strides[i] = ∏_{j=i+1..N} shape[j] (C-order).
§Errors
Returns TensorError::ShapeMismatch if ∏ shape > data.len().
§Examples
use hermes_simd_core::TensorView;
let data = [0, 1, 2, 3, 4, 5];
let view = TensorView::<i32, 2>::new(&data, [2, 3]).unwrap();
assert_eq!(view.shape(), [2, 3]);
assert_eq!(view.strides(), [3, 1]);
assert_eq!(view.get([1, 2]).unwrap(), 5);Source§impl<'a, 'b, T, const N: usize> TensorView<'a, T, N, RowMajor, &'b mut [T]>
impl<'a, 'b, T, const N: usize> TensorView<'a, T, N, RowMajor, &'b mut [T]>
Sourcepub fn new_mut(
data: &'b mut [T],
shape: [usize; N],
) -> Result<TensorView<'a, T, N, RowMajor, &'b mut [T]>, TensorError>
pub fn new_mut( data: &'b mut [T], shape: [usize; N], ) -> Result<TensorView<'a, T, N, RowMajor, &'b mut [T]>, TensorError>
Create a mutable row-major tensor view over data with the given shape.
§Errors
Returns TensorError::ShapeMismatch if ∏ shape > data.len().
Source§impl<'a, 'b, T, const N: usize, L> TensorView<'a, T, N, L, &'b [T]>where
L: Layout,
impl<'a, 'b, T, const N: usize, L> TensorView<'a, T, N, L, &'b [T]>where
L: Layout,
Sourcepub fn with_strides(
data: &'b [T],
shape: [usize; N],
strides: [usize; N],
) -> Result<TensorView<'a, T, N, L, &'b [T]>, TensorError>
pub fn with_strides( data: &'b [T], shape: [usize; N], strides: [usize; N], ) -> Result<TensorView<'a, T, N, L, &'b [T]>, TensorError>
Create a tensor view with explicit strides.
Allows column-major, blocked, or any custom layout.
§Errors
Returns TensorError::ShapeMismatch if ∏ shape > data.len().
Source§impl<'a, 'b, T, const N: usize, L> TensorView<'a, T, N, L, &'b mut [T]>where
L: Layout,
impl<'a, 'b, T, const N: usize, L> TensorView<'a, T, N, L, &'b mut [T]>where
L: Layout,
Sourcepub fn with_strides_mut(
data: &'b mut [T],
shape: [usize; N],
strides: [usize; N],
) -> Result<TensorView<'a, T, N, L, &'b mut [T]>, TensorError>
pub fn with_strides_mut( data: &'b mut [T], shape: [usize; N], strides: [usize; N], ) -> Result<TensorView<'a, T, N, L, &'b mut [T]>, TensorError>
Create a mutable tensor view with explicit strides.
§Errors
Returns TensorError::ShapeMismatch if ∏ shape > data.len().
Sourcepub fn downgrade(self) -> TensorView<'a, T, N, L, &'b [T]>
pub fn downgrade(self) -> TensorView<'a, T, N, L, &'b [T]>
Downgrade the exclusive mutable view to a shared read-only view.
Source§impl<'a, 'b, T> TensorView<'a, T, 2, ColMajor, &'b [T]>
impl<'a, 'b, T> TensorView<'a, T, 2, ColMajor, &'b [T]>
Sourcepub fn new_col_major(
data: &'b [T],
shape: [usize; 2],
) -> Result<TensorView<'a, T, 2, ColMajor, &'b [T]>, TensorError>
pub fn new_col_major( data: &'b [T], shape: [usize; 2], ) -> Result<TensorView<'a, T, 2, ColMajor, &'b [T]>, TensorError>
Create a column-major (Fortran-order) 2-D tensor view.
Fortran strides: strides[0] = 1, strides[1] = shape[0].
§Errors
Returns TensorError::ShapeMismatch if shape[0] * shape[1] > data.len().
Source§impl<'a, T, const N: usize, L, Ref> TensorView<'a, T, N, L, Ref>
impl<'a, T, const N: usize, L, Ref> TensorView<'a, T, N, L, Ref>
Sourcepub fn shape(&self) -> [usize; N]
pub fn shape(&self) -> [usize; N]
The logical shape of this tensor: number of elements per dimension.
Sourcepub fn num_elements(&self) -> usize
pub fn num_elements(&self) -> usize
Number of elements in this tensor: ∏ shape[i].
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if the tensor is empty (one of its dimensions is 0).
Sourcepub fn is_contiguous(&self) -> bool
pub fn is_contiguous(&self) -> bool
Whether this view is contiguous in row-major order.
Sourcepub fn get(&self, idx: [usize; N]) -> Result<T, TensorError>where
T: Copy,
pub fn get(&self, idx: [usize; N]) -> Result<T, TensorError>where
T: Copy,
Bounds-checked element access.
Sourcepub unsafe fn get_unchecked(&self, idx: [usize; N]) -> Twhere
T: Copy,
pub unsafe fn get_unchecked(&self, idx: [usize; N]) -> Twhere
T: Copy,
Sourcepub fn reshape<const M: usize>(
self,
new_shape: [usize; M],
) -> Result<TensorView<'a, T, M, RowMajor, Ref>, TensorError>
pub fn reshape<const M: usize>( self, new_shape: [usize; M], ) -> Result<TensorView<'a, T, M, RowMajor, Ref>, TensorError>
Reshape this view to a different rank M, reusing the same flat slice.
Source§impl<'a, 'b, T, const N: usize, L> TensorView<'a, T, N, L, &'b mut [T]>
impl<'a, 'b, T, const N: usize, L> TensorView<'a, T, N, L, &'b mut [T]>
Sourcepub fn as_slice_mut(&mut self) -> &mut [T]
pub fn as_slice_mut(&mut self) -> &mut [T]
Access the underlying flat mutable slice.
Sourcepub fn set(&mut self, idx: [usize; N], val: T) -> Result<(), TensorError>
pub fn set(&mut self, idx: [usize; N], val: T) -> Result<(), TensorError>
Bounds-checked element write access.
Sourcepub unsafe fn set_unchecked(&mut self, idx: [usize; N], val: T)
pub unsafe fn set_unchecked(&mut self, idx: [usize; N], val: T)
Trait Implementations§
Source§impl<'a, T, const N: usize, Layout> Clone for TensorView<'a, T, N, Layout>
impl<'a, T, const N: usize, Layout> Clone for TensorView<'a, T, N, Layout>
Source§fn clone(&self) -> TensorView<'a, T, N, Layout>
fn clone(&self) -> TensorView<'a, T, N, Layout>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreimpl<'a, T, const N: usize, Layout> Copy for TensorView<'a, T, N, Layout>
impl<'a, T, const N: usize, Layout, Ref> Send for TensorView<'a, T, N, Layout, Ref>where
Ref: Send,
impl<'a, T, const N: usize, Layout, Ref> Sync for TensorView<'a, T, N, Layout, Ref>where
Ref: Sync,
Auto Trait Implementations§
impl<'a, T, const N: usize, Layout, Ref> Freeze for TensorView<'a, T, N, Layout, Ref>
impl<'a, T, const N: usize, Layout, Ref> RefUnwindSafe for TensorView<'a, T, N, Layout, Ref>
impl<'a, T, const N: usize, Layout, Ref> Unpin for TensorView<'a, T, N, Layout, Ref>
impl<'a, T, const N: usize, Layout, Ref> UnsafeUnpin for TensorView<'a, T, N, Layout, Ref>
impl<'a, T, const N: usize, Layout, Ref> UnwindSafe for TensorView<'a, T, N, Layout, Ref>
Blanket Implementations§
Source§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
Source§type ArchivedMetadata = ()
type ArchivedMetadata = ()
Source§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
Source§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
Source§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
Source§unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
Source§fn resolve_niched(out: Place<NichedOption<T, N1>>)
fn resolve_niched(out: Place<NichedOption<T, N1>>)
out indicating that a T is niched.