Slice

sealed class Slice<T : DType, V>(source)

Represents a slice descriptor for tensor slicing operations.

Slice is a sealed class hierarchy that describes different ways to select elements along a single tensor dimension. It provides the building blocks for complex tensor slicing operations while maintaining type safety and enabling compile-time optimization opportunities.

Slice Types

The sealed class hierarchy includes:

  • Range: Contiguous range of indices [start, end)

  • At: Single specific index selection

  • All: Full dimension selection (equivalent to :)

  • Step: Strided access with start, end, and step size

Design Principles

  • Type Safety: Compile-time guarantees about slice validity

  • Composability: Slices can be combined and normalized

  • Performance: Enable optimization based on slice patterns

  • Expressiveness: Support common slicing use cases

Parameters

T

the data type constraint extending DType

V

the actual value type

Inheritors

Types

Link copied to clipboard
class All<T : DType, V> : Slice<T, V>

Represents selection of the entire dimension.

Link copied to clipboard
data class At<T : DType, V>(val index: Int) : Slice<T, V>

Represents selection of a single specific index.

Link copied to clipboard
data class Range<T : DType, V>(val start: Int, val end: Int) : Slice<T, V>

Represents a contiguous range slice [start, end).

Link copied to clipboard
data class Step<T : DType, V>(val start: Int, val end: Int, val step: Int) : Slice<T, V>

Represents strided access with custom step size.

Functions

Link copied to clipboard
fun getResultSize(dimensionSize: Int): Int

Calculates the number of elements this slice will select.

Link copied to clipboard

Checks if this slice uses non-trivial stride patterns.

Link copied to clipboard

Determines if this slice represents contiguous memory access.

Link copied to clipboard

Determines if this slice selects zero elements.

Link copied to clipboard
fun isValid(dimensionSize: Int): Boolean

Validates that this slice is compatible with the given dimension size.

Link copied to clipboard
fun normalize(dimensionSize: Int): Slice<T, V>

Normalizes this slice against a specific dimension size.