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§
Source§impl<'a, T> HostTensorView<'a, T>
impl<'a, T> HostTensorView<'a, T>
Sourcepub fn from_slice(
shape: impl Into<ShapeVec>,
strides: impl Into<StrideVec>,
offset: isize,
data: &'a [T],
) -> Result<Self>
pub fn from_slice( shape: impl Into<ShapeVec>, strides: impl Into<StrideVec>, offset: isize, data: &'a [T], ) -> Result<Self>
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.
Sourcepub 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]);Sourcepub 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]);Sourcepub 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);Sourcepub 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);Sourcepub 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());Sourcepub fn is_compact_col_major(&self) -> Result<bool>
pub fn is_compact_col_major(&self) -> Result<bool>
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.
Sourcepub fn is_zero_offset_col_major(&self) -> Result<bool>
pub fn is_zero_offset_col_major(&self) -> Result<bool>
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.
Sourcepub fn as_slice(&self) -> Result<&'a [T]>
pub fn as_slice(&self) -> Result<&'a [T]>
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.
Sourcepub fn reshape_view(&self, shape: impl Into<ShapeVec>) -> Result<Self>
pub fn reshape_view(&self, shape: impl Into<ShapeVec>) -> Result<Self>
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.
Sourcepub fn transpose_view(&self, axes: &[usize]) -> Result<Self>
pub fn transpose_view(&self, axes: &[usize]) -> Result<Self>
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.
Sourcepub fn slice_view(&self, spec: &[SliceSpec]) -> Result<Self>
pub fn slice_view(&self, spec: &[SliceSpec]) -> Result<Self>
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§
Source§impl<'a, T: Clone> Clone for HostTensorView<'a, T>
impl<'a, T: Clone> Clone for HostTensorView<'a, T>
Source§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 more