Struct HostTensorView
pub struct HostTensorView<'a, T> { /* private fields */ }Expand description
Borrowed host tensor view with shape, strides, and offset metadata.
This type intentionally does not implement PartialEq because view
equality is ambiguous between metadata identity, storage identity, and
logical element equality.
§Examples
use tenferro_tensor_core::HostTensor;
let tensor = HostTensor::from_vec_col_major(vec![2], vec![1.0_f64, 2.0])?;
let view = tensor.as_view();
assert_eq!(view.shape(), &[2]);let a = tensor.as_view();
let b = tensor.as_view();
let _ = a == b;Implementations§
§impl<'a, T> HostTensorView<'a, T>
impl<'a, T> HostTensorView<'a, T>
pub fn from_slice(
shape: impl Into<SmallVec<[usize; 8]>>,
strides: impl Into<SmallVec<[isize; 8]>>,
offset: isize,
data: &'a [T],
) -> Result<HostTensorView<'a, T>, ValidationError>
pub fn from_slice( shape: impl Into<SmallVec<[usize; 8]>>, strides: impl Into<SmallVec<[isize; 8]>>, offset: isize, data: &'a [T], ) -> Result<HostTensorView<'a, T>, ValidationError>
Create a typed view from explicit metadata and validate bounds eagerly.
§Examples
use tenferro_tensor_core::HostTensorView;
let data = [1.0_f64, 2.0, 3.0, 4.0];
let view = HostTensorView::from_slice(vec![2], vec![1], 1, &data)?;
assert_eq!(view.as_slice()?, &[2.0, 3.0]);§Errors
Returns ValidationError::RankMismatch for shape/stride rank
disagreement, ValidationError::ViewOutOfBounds for an unreachable
view, or ValidationError::IntegerOverflow when bounds arithmetic
overflows.
pub fn shape(&self) -> &[usize]
pub fn shape(&self) -> &[usize]
Borrow this view’s shape.
§Examples
use tenferro_tensor_core::HostTensor;
let tensor = HostTensor::from_vec_col_major(vec![2], vec![1.0_f64, 2.0])?;
assert_eq!(tensor.as_view().shape(), &[2]);pub fn strides(&self) -> &[isize]
pub fn strides(&self) -> &[isize]
Borrow this view’s signed element strides.
§Examples
use tenferro_tensor_core::HostTensor;
let tensor = HostTensor::from_vec_col_major(vec![2, 3], vec![0_i32; 6])?;
assert_eq!(tensor.as_view().strides(), &[1, 2]);pub fn offset(&self) -> isize
pub fn offset(&self) -> isize
Return this view’s signed element offset into the backing slice.
§Examples
use tenferro_tensor_core::HostTensor;
let tensor = HostTensor::from_vec_col_major(vec![1], vec![true])?;
assert_eq!(tensor.as_view().offset(), 0);pub fn rank(&self) -> usize
pub fn rank(&self) -> usize
Return the view rank.
§Examples
use tenferro_tensor_core::HostTensor;
let tensor = HostTensor::from_vec_col_major(vec![2, 1], vec![1.0_f64, 2.0])?;
assert_eq!(tensor.as_view().rank(), 2);pub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true when this view has zero logical elements.
§Examples
use tenferro_tensor_core::HostTensorView;
let data = [1.0_f64];
let view = HostTensorView::from_slice(vec![0], vec![1], 0, &data)?;
assert!(view.is_empty());pub fn is_compact_col_major(&self) -> Result<bool, ValidationError>
pub fn is_compact_col_major(&self) -> Result<bool, ValidationError>
Return whether this view has compact column-major logical strides.
§Examples
use tenferro_tensor_core::HostTensor;
let tensor = HostTensor::from_vec_col_major(vec![2, 2], vec![0_i32; 4])?;
assert!(tensor.as_view().is_compact_col_major()?);§Errors
Returns ValidationError::IntegerOverflow if compactness validation
overflows metadata arithmetic.
pub fn is_zero_offset_col_major(&self) -> Result<bool, ValidationError>
pub fn is_zero_offset_col_major(&self) -> Result<bool, ValidationError>
Return whether this view is compact column-major and starts at offset zero.
§Examples
use tenferro_tensor_core::HostTensor;
let tensor = HostTensor::from_vec_col_major(vec![1], vec![1_i64])?;
assert!(tensor.as_view().is_zero_offset_col_major()?);§Errors
Returns ValidationError::IntegerOverflow if compactness validation
overflows metadata arithmetic.
pub fn as_slice(&self) -> Result<&'a [T], ValidationError>
pub fn as_slice(&self) -> Result<&'a [T], ValidationError>
Borrow the slice-contiguous backing region for this view.
§Examples
use tenferro_tensor_core::HostTensorView;
let data = [1_i32, 2, 3, 4];
let view = HostTensorView::from_slice(vec![2], vec![1], 1, &data)?;
assert_eq!(view.as_slice()?, &[2, 3]);§Errors
Returns ValidationError::NonContiguousViewAsSlice for a view that
is not slice-contiguous, ValidationError::ViewOutOfBounds for an
invalid backing range, or ValidationError::IntegerOverflow when
range arithmetic overflows.
pub fn reshape_view(
&self,
shape: impl Into<SmallVec<[usize; 8]>>,
) -> Result<HostTensorView<'a, T>, ValidationError>
pub fn reshape_view( &self, shape: impl Into<SmallVec<[usize; 8]>>, ) -> Result<HostTensorView<'a, T>, ValidationError>
Return a metadata-only reshape of this compact column-major view.
§Examples
use tenferro_tensor_core::HostTensor;
let tensor = HostTensor::from_vec_col_major(vec![4], vec![1.0_f64, 2.0, 3.0, 4.0])?;
assert_eq!(tensor.as_view().reshape_view(vec![2, 2])?.shape(), &[2, 2]);§Errors
Returns ValidationError::NonContiguousViewAsSlice for a view that
is not slice-contiguous, ValidationError::ShapeMismatch for a
different element count, or ValidationError::IntegerOverflow when
shape arithmetic overflows.
pub fn transpose_view(
&self,
axes: &[usize],
) -> Result<HostTensorView<'a, T>, ValidationError>
pub fn transpose_view( &self, axes: &[usize], ) -> Result<HostTensorView<'a, T>, ValidationError>
Return a metadata-only transposed view with axes in the requested order.
§Examples
use tenferro_tensor_core::HostTensor;
let tensor = HostTensor::from_vec_col_major(vec![2, 3], vec![0_i32; 6])?;
let view = tensor.as_view().transpose_view(&[1, 0])?;
assert_eq!(view.shape(), &[3, 2]);
assert_eq!(view.strides(), &[2, 1]);§Errors
Returns ValidationError::InvalidPermutationLength,
ValidationError::AxisOutOfBounds, or
ValidationError::DuplicateAxis when axes is not a permutation of
the view rank; it may also return ValidationError::ViewOutOfBounds
or ValidationError::IntegerOverflow while validating the result.
pub fn slice_view(
&self,
spec: &[SliceSpec],
) -> Result<HostTensorView<'a, T>, ValidationError>
pub fn slice_view( &self, spec: &[SliceSpec], ) -> Result<HostTensorView<'a, T>, ValidationError>
Return a metadata-only positive-step slice of this view.
§Examples
use tenferro_tensor_core::{SliceSpec, HostTensor};
let tensor = HostTensor::from_vec_col_major(vec![4], vec![1_i64, 2, 3, 4])?;
let view = tensor
.as_view()
.slice_view(&[SliceSpec { start: 1, end: 4, step: 2 }])?;
assert_eq!(view.shape(), &[2]);§Errors
Returns ValidationError::RankMismatch when spec does not cover
every axis, ValidationError::InvalidSliceStep or
ValidationError::InvalidSliceBounds for invalid slice parameters,
or ValidationError::ViewOutOfBounds for an invalid result view.
Trait Implementations§
§impl<'a, T> Clone for HostTensorView<'a, T>where
T: Clone,
impl<'a, T> Clone for HostTensorView<'a, T>where
T: Clone,
§fn clone(&self) -> HostTensorView<'a, T>
fn clone(&self) -> HostTensorView<'a, T>
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl<'a, T> Freeze for HostTensorView<'a, T>
impl<'a, T> RefUnwindSafe for HostTensorView<'a, T>where
T: RefUnwindSafe,
impl<'a, T> Send for HostTensorView<'a, T>where
T: Sync,
impl<'a, T> Sync for HostTensorView<'a, T>where
T: Sync,
impl<'a, T> Unpin for HostTensorView<'a, T>
impl<'a, T> UnsafeUnpin for HostTensorView<'a, T>
impl<'a, T> UnwindSafe for HostTensorView<'a, T>where
T: RefUnwindSafe,
Blanket Implementations§
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> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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