pub struct Layout { /* private fields */ }Expand description
Layout describes how to interpret a linear buffer as an N-dimensional tensor.
Stores shape, strides (in elements, can be negative for flipped dimensions), and a start offset for views/slices.
Implementations§
Source§impl Layout
impl Layout
Sourcepub fn contiguous(shape: Shape) -> Self
pub fn contiguous(shape: Shape) -> Self
Create a new contiguous layout (row-major/C-order).
Sourcepub fn new(shape: Shape, strides: Vec<isize>, start_offset: usize) -> Self
pub fn new(shape: Shape, strides: Vec<isize>, start_offset: usize) -> Self
Create a layout with explicit strides.
Sourcepub fn strides(&self) -> &[isize]
pub fn strides(&self) -> &[isize]
The strides in elements (can be negative for flipped dimensions).
Sourcepub fn start_offset(&self) -> usize
pub fn start_offset(&self) -> usize
The start offset for views/slices.
Sourcepub fn num_elements(&self) -> usize
pub fn num_elements(&self) -> usize
Total number of elements.
Sourcepub fn is_contiguous(&self) -> bool
pub fn is_contiguous(&self) -> bool
Check if this layout is contiguous (row-major, positive strides).
Sourcepub fn contiguous_offsets(&self) -> Option<(usize, usize)>
pub fn contiguous_offsets(&self) -> Option<(usize, usize)>
If contiguous, return (start, end) offsets for direct slice access.
Sourcepub fn transpose(&self, dim1: usize, dim2: usize) -> Self
pub fn transpose(&self, dim1: usize, dim2: usize) -> Self
Transpose: swap two dimensions (zero-copy, metadata only).
Sourcepub fn permute(&self, axes: &[usize]) -> Self
pub fn permute(&self, axes: &[usize]) -> Self
Permute: reorder dimensions according to axes (zero-copy, metadata only).
axes must be a permutation of 0..ndim.
Sourcepub fn flip(&self, axes: &[usize]) -> Self
pub fn flip(&self, axes: &[usize]) -> Self
Flip: reverse elements along specified axes (zero-copy, metadata only).
For each flipped axis, negates the stride and adjusts start_offset to point to the last element along that dimension.
Sourcepub fn narrow(&self, dim: usize, start: usize, len: usize) -> Self
pub fn narrow(&self, dim: usize, start: usize, len: usize) -> Self
Narrow/slice along a dimension (zero-copy, metadata only).
Sourcepub fn slice(&self, slices: &[Slice]) -> (Self, bool)
pub fn slice(&self, slices: &[Slice]) -> (Self, bool)
Apply slices to create a new layout.
Returns (new_layout, needs_copy):
needs_copy = false: Can use zero-copy view with new layoutneeds_copy = true: Has negative steps requiring data reordering
Sourcepub fn reshape(&self, new_shape: Shape) -> Option<Self>
pub fn reshape(&self, new_shape: Shape) -> Option<Self>
Reshape to a new shape. Only works if contiguous with zero offset.
Returns None if not contiguous or has non-zero offset (would require data copy).
Sourcepub fn index(&self, indices: &[usize]) -> usize
pub fn index(&self, indices: &[usize]) -> usize
Compute linear index from multi-dimensional indices.
Sourcepub fn inner_stride(&self) -> usize
pub fn inner_stride(&self) -> usize
Get stride of the innermost (last) dimension. Returns 1 for contiguous tensors, larger values for transposed. Returns absolute value (ignores flip).
Sourcepub fn has_contiguous_inner(&self) -> bool
pub fn has_contiguous_inner(&self) -> bool
Check if innermost dimension is contiguous (|stride| == 1). This enables efficient vectorized inner loops.
Sourcepub fn as_2d_strides(&self) -> Option<(usize, usize, isize, isize)>
pub fn as_2d_strides(&self) -> Option<(usize, usize, isize, isize)>
For 2D layouts, get (outer_size, inner_size, outer_stride, inner_stride). Returns None if not 2D.
Sourcepub fn has_positive_strides(&self) -> bool
pub fn has_positive_strides(&self) -> bool
Check if all strides are non-negative.
Sourcepub fn strided_blocks(&self) -> StridedBlocks<'_>
pub fn strided_blocks(&self) -> StridedBlocks<'_>
Compute strided blocks for efficient iteration.
Returns (block_len, num_blocks, block_stride) where:
- block_len: number of contiguous elements in each block
- num_blocks: total number of blocks
- block_stride: stride between consecutive blocks (0 if single block)
For contiguous tensors: single block covering all elements. For transposed/strided: multiple blocks of contiguous data.
Trait Implementations§
impl Eq for Layout
impl StructuralPartialEq for Layout
Auto Trait Implementations§
impl Freeze for Layout
impl RefUnwindSafe for Layout
impl Send for Layout
impl Sync for Layout
impl Unpin for Layout
impl UnsafeUnpin for Layout
impl UnwindSafe for Layout
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<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.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