Skip to content

tenzr::StridedLayout

More...

#include <layout.h>

Public Functions

Name
StridedLayout() =default
StridedLayout(Shape shape)
StridedLayout(Shape shape, std::vector< int64_t > strides, int64_t offset =0)
const Shape & shape() const
The layout's shape.
const std::vector< int64_t > & Strides() const
Per-dimension strides, in elements.
int64_t Offset() const
Base element offset of the view.
size_t Rank() const
Number of dimensions.
int64_t Stride(size_t index) const
Stride along dimension index.
int64_t NumElements() const
Total number of elements in the shaped view.
bool IsContiguous() const
Whether the view is densely packed.
bool IsCContiguous() const
Whether the view is C (row-major) contiguous.
bool IsFContiguous() const
Whether the view is Fortran (column-major) contiguous.
int64_t LinearOffset(const std::vector< int64_t > & indices) const
Flat element offset for multidimensional indices.
StridedLayout Transpose(int64_t dim0 =-1, int64_t dim1 =-2) const
View with dimensions dim0 and dim1 swapped.
StridedLayout Permute(const std::vector< int64_t > & axes) const
View with dimensions reordered by axes.
StridedLayout Squeeze(std::optional< int64_t > dim =std::nullopt) const
View with a length-1 dimension removed; dim selects one, nullopt removes all.
StridedLayout Unsqueeze(int64_t dim) const
View with a length-1 dimension inserted at dim.
StridedLayout slice(const std::vector< Slice > & slices) const
View restricted by per-axis slices.
StridedLayout BroadcastTo(const Shape & target_shape) const
View broadcast to target_shape.
std::string ToString() const
Human-readable layout description.

Detailed Description

class tenzr::StridedLayout;

Strided view of a buffer: a Shape together with per-dimension element strides and a base element offset.

Public Functions Documentation

function StridedLayout

StridedLayout() =default

function StridedLayout

explicit StridedLayout(
    Shape shape
)

Parameters:

  • shape Logical shape.

Build a C-contiguous layout for shape.

function StridedLayout

StridedLayout(
    Shape shape,
    std::vector< int64_t > strides,
    int64_t offset =0
)

Parameters:

  • shape Logical shape.
  • strides Stride per dimension, which must match the rank.
  • offset Element offset of the first logical element.

Build a layout with explicit strides and base offset.

function shape

inline const Shape & shape() const

The layout's shape.

function Strides

inline const std::vector< int64_t > & Strides() const

Per-dimension strides, in elements.

function Offset

inline int64_t Offset() const

Base element offset of the view.

function Rank

inline size_t Rank() const

Number of dimensions.

function Stride

int64_t Stride(
    size_t index
) const

Stride along dimension index.

Parameters:

  • index Zero-based dimension index.

Return: Stride in elements.

Stride of the given dimension.

function NumElements

inline int64_t NumElements() const

Total number of elements in the shaped view.

function IsContiguous

bool IsContiguous() const

Whether the view is densely packed.

Return: true for a contiguous layout.

Whether the layout is contiguous in any supported order (currently C-order).

function IsCContiguous

bool IsCContiguous() const

Whether the view is C (row-major) contiguous.

Return: true for a C-contiguous layout.

Whether the layout is C-contiguous (row-major, zero offset).

function IsFContiguous

bool IsFContiguous() const

Whether the view is Fortran (column-major) contiguous.

Return: true for an F-contiguous layout.

Whether the layout is Fortran-contiguous (column-major, zero offset).

function LinearOffset

int64_t LinearOffset(
    const std::vector< int64_t > & indices
) const

Flat element offset for multidimensional indices.

Parameters:

  • indices Per-dimension indices, which must match the rank.

Return: Flat element offset including the base offset.

Map multi-dimensional indices to a flat element offset.

function Transpose

StridedLayout Transpose(
    int64_t dim0 =-1,
    int64_t dim1 =-2
) const

View with dimensions dim0 and dim1 swapped.

Parameters:

  • dim0 First dimension; negative values count from the end.
  • dim1 Second dimension.

Return: Transposed layout sharing the same storage.

Swap two dimensions, producing a view.

function Permute

StridedLayout Permute(
    const std::vector< int64_t > & axes
) const

View with dimensions reordered by axes.

Parameters:

  • axes Target position of each source dimension.

Return: Permuted layout sharing the same storage.

Reorder dimensions according to axes, producing a view.

function Squeeze

StridedLayout Squeeze(
    std::optional< int64_t > dim =std::nullopt
) const

View with a length-1 dimension removed; dim selects one, nullopt removes all.

Parameters:

  • dim Optional dimension to squeeze, which must have size 1.

Return: Squeezed view.

Remove a unit dimension (or all unit dimensions when dim is empty).

function Unsqueeze

StridedLayout Unsqueeze(
    int64_t dim
) const

View with a length-1 dimension inserted at dim.

Parameters:

  • dim Position for the new dimension; negative values count from the end.

Return: Unsqueezed view.

Insert a unit dimension at dim.

function slice

StridedLayout slice(
    const std::vector< Slice > & slices
) const

View restricted by per-axis slices.

Parameters:

  • slices Slice specification per dimension; trailing dimensions are kept whole.

Return: Sliced view.

Build a strided view for the given per-dimension slices.

function BroadcastTo

StridedLayout BroadcastTo(
    const Shape & target_shape
) const

View broadcast to target_shape.

Parameters:

  • target_shape Shape to broadcast to; rank must be >= the source rank.

Return: Broadcast view.

Broadcast to target_shape, setting stride 0 on expanded dimensions.

function ToString

std::string ToString() const

Human-readable layout description.

Return: Human-readable layout description.

Render the shape, strides, and offset as a string.