Skip to main content

HostTensorView

Struct HostTensorView 

Source
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>

Source

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]);
Source

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]);
Source

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]);
Source

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);
Source

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);
Source

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());
Source

pub fn is_compact_col_major(&self) -> 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());
Source

pub fn is_zero_offset_col_major(&self) -> 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());
Source

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]);
Source

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]);
Source

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]);
Source

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]);

Trait Implementations§

Source§

impl<'a, T: Clone> Clone for HostTensorView<'a, T>

Source§

fn clone(&self) -> HostTensorView<'a, T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<'a, T: Debug> Debug for HostTensorView<'a, T>

Source§

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

Formats the value using the given formatter. Read more

Auto 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> 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> 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.