pub struct Layout { /* private fields */ }Expand description
Layout describes how a tensor’s logical shape maps to flat storage.
Implementations§
Source§impl Layout
impl Layout
Sourcepub fn contiguous(shape: Shape) -> Self
pub fn contiguous(shape: Shape) -> Self
Create a new contiguous layout for the given shape. Strides are computed as row-major (C-order).
Sourcepub fn new(shape: Shape, strides: Vec<usize>, offset: usize) -> Self
pub fn new(shape: Shape, strides: Vec<usize>, offset: usize) -> Self
Create a layout with explicit strides and offset (for views).
pub fn shape(&self) -> &Shape
pub fn strides(&self) -> &[usize]
pub fn offset(&self) -> usize
pub fn rank(&self) -> usize
pub fn dims(&self) -> &[usize]
pub fn elem_count(&self) -> usize
Sourcepub fn is_contiguous(&self) -> bool
pub fn is_contiguous(&self) -> bool
Check if this layout is contiguous (row-major, no gaps). A tensor is contiguous if its strides equal the default strides for its shape AND offset is 0.
Sourcepub fn transpose(&self, dim0: usize, dim1: usize) -> Result<Layout>
pub fn transpose(&self, dim0: usize, dim1: usize) -> Result<Layout>
Transpose two dimensions. Returns a new layout with swapped shape/strides. This is a “free” operation — no data is copied.
Example: [2, 3, 4] transpose(0, 2) → [4, 3, 2] strides [12, 4, 1] → [1, 4, 12]
Sourcepub fn narrow(&self, dim: usize, start: usize, len: usize) -> Result<Layout>
pub fn narrow(&self, dim: usize, start: usize, len: usize) -> Result<Layout>
Narrow (slice) along a dimension. Returns a new layout that is a view into the same storage with adjusted shape and offset.
Example: tensor of shape [4, 6], narrow(dim=1, start=2, len=3) → shape [4, 3], offset += 2 * stride[1]
Sourcepub fn flat_index(&self, index: &[usize]) -> usize
pub fn flat_index(&self, index: &[usize]) -> usize
Compute the flat index into storage for a given multi-dimensional index. This is the core formula: flat_index = offset + sum(index[i] * stride[i])
Sourcepub fn strided_indices(&self) -> StridedIter ⓘ
pub fn strided_indices(&self) -> StridedIter ⓘ
Iterator over all flat indices of this layout, in logical order. This handles non-contiguous layouts correctly by walking through multi-dimensional indices and converting via strides.