IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /docs/manual/basics.md). For the complete Mojo documentation index, see llms.txt.
Skip to main content
Version: Nightly
For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).

IndexList

struct IndexList[size: Int, *, element_type: DType = .int64]

A base struct that implements size agnostic index functions.

Parameters​

  • ​size (Int): The size of the tuple.
  • ​element_type (DType): The underlying dtype of the integer element value.

Fields​

  • ​data (StaticTuple[Scalar[element_type], size]): The underlying storage of the tuple value.

Implemented traits​

AnyType, Comparable, Copyable, Defaultable, Deinitable, DevicePassable, Equatable, Hashable, ImplicitlyCopyable, Movable, RegisterPassable, Sized, TrivialRegisterPassable, Writable

comptime members​

device_type​

comptime device_type = IndexList[size, element_type=element_type]

Indicate the type being used on accelerator devices.

Methods​

__init__​

def __init__() -> Self

Constructs a static int tuple of the given size.

@implicit def __init__(data: StaticTuple[Scalar[element_type], size]) -> Self

Constructs a static int tuple of the given size.

Args:

@implicit def __init__[*Ts: Intable & Movable](elems: Tuple[*Ts.values]) -> Self

Constructs a static int tuple given a tuple of integers.

Parameters:

  • ​*Ts (Intable & Movable): The element types of the input tuple (must be Intable).

Args:

def __init__(*elems: Int, *, __list_literal__: NoneType = None) -> Self

Constructs a static int tuple given a set of arguments.

Args:

  • ​*elems (Int): The elements to construct the tuple.
  • ​list_literal (NoneType): Specifies that this constructor can be used for list literals.

def __init__(fill: Int) -> Self

Constructs a static int tuple given a set of arguments.

Args:

  • ​fill (Int): The elem to splat into the tuple.

__getitem__​

def __getitem__[I: Indexer](self, idx: I) -> Int

Gets an element from the tuple by index.

Parameters:

  • ​I (Indexer): A type that can be used as an index.

Args:

  • ​idx (I): The element index.

Returns:

Int: The tuple element value.

__setitem__​

def __setitem__(mut self, idx: Int, val: Int)

Sets an element in the tuple at the given index.

Args:

  • ​idx (Int): The element index.
  • ​val (Int): The value to store.

__lt__​

def __lt__(self, rhs: Self) -> Bool

Compares this tuple to another tuple using LT comparison.

A tuple is less-than another tuple if all corresponding elements of lhs is less than rhs.

Note: This is not a lexical comparison.

Args:

  • ​rhs (Self): Right hand side tuple.

Returns:

Bool: The comparison result.

__le__​

def __le__(self, rhs: Self) -> Bool

Compares this tuple to another tuple using LE comparison.

A tuple is less-or-equal than another tuple if all corresponding elements of lhs is less-or-equal than rhs.

Note: This is not a lexical comparison.

Args:

  • ​rhs (Self): Right hand side tuple.

Returns:

Bool: The comparison result.

__eq__​

def __eq__(self, rhs: Self) -> Bool

Compares this tuple to another tuple for equality.

The tuples are equal if all corresponding elements are equal.

Args:

  • ​rhs (Self): The other tuple.

Returns:

Bool: The comparison result.

__gt__​

def __gt__(self, rhs: Self) -> Bool

Compares this tuple to another tuple using GT comparison.

A tuple is greater-than than another tuple if all corresponding elements of lhs is greater-than than rhs.

Note: This is not a lexical comparison.

Args:

  • ​rhs (Self): Right hand side tuple.

Returns:

Bool: The comparison result.

__ge__​

def __ge__(self, rhs: Self) -> Bool

Compares this tuple to another tuple using GE comparison.

A tuple is greater-or-equal than another tuple if all corresponding elements of lhs is greater-or-equal than rhs.

Note: This is not a lexical comparison.

Args:

  • ​rhs (Self): Right hand side tuple.

Returns:

Bool: The comparison result.

__add__​

def __add__(self, rhs: Self) -> Self

Performs element-wise integer add.

Args:

  • ​rhs (Self): Right hand side operand.

Returns:

Self: The resulting index tuple.

__sub__​

def __sub__(self, rhs: Self) -> Self

Performs element-wise integer subtract.

Args:

  • ​rhs (Self): Right hand side operand.

Returns:

Self: The resulting index tuple.

__mul__​

def __mul__(self, rhs: Self) -> Self

Performs element-wise integer multiply.

Args:

  • ​rhs (Self): Right hand side operand.

Returns:

Self: The resulting index tuple.

__floordiv__​

def __floordiv__(self, rhs: Self) -> Self

Performs element-wise integer floor division.

Args:

  • ​rhs (Self): The elementwise divisor.

Returns:

Self: The resulting index tuple.

__rfloordiv__​

def __rfloordiv__(self, rhs: Self) -> Self

Floor divides rhs by this object.

Args:

  • ​rhs (Self): The value to elementwise divide by self.

Returns:

Self: The resulting index tuple.

__len__​

def __len__(self) -> Int

Returns the size of the tuple.

Returns:

Int: The tuple size.

get​

def get[idx: Int](self) -> Int

Gets an element from the tuple by index parameter.

Parameters:

  • ​idx (Int): The element index.

Returns:

Int: The tuple element value.

as_tuple​

def as_tuple(self) -> StaticTuple[Int, size]

Converts this IndexList to StaticTuple.

Returns:

StaticTuple[Int, size]: The corresponding StaticTuple object.

as_index_tuple​

def as_index_tuple(self) -> StaticTuple[SIMDLength, size]

Converts this IndexList to a static tuple of mlir indexes.

Returns:

StaticTuple[SIMDLength, size]: The corresponding StaticTuple object.

canonicalize​

def canonicalize(self) -> IndexList[size]

Canonicalizes the IndexList.

Returns:

IndexList[size]: Canonicalizes the object.

reverse​

def reverse(self) -> Self

Reverses the IndexList.

Returns:

Self: A new IndexList with the elements in reverse order.

flattened_length​

def flattened_length(self) -> Int

Returns the flattened length of the tuple.

Returns:

Int: The flattened length of the tuple.

get_row_major_strides​

def get_row_major_strides(self) -> Self

Interpret the current index list as a shape, and return the strides to traverse such a shape in row-major order.

Returns:

Self: The strides to traverse the index list in row-major order.

remu​

def remu(self, rhs: Self) -> Self

Performs element-wise integer unsigned modulo.

Args:

  • ​rhs (Self): Right hand side operand.

Returns:

Self: The resulting index tuple.

write_to​

def write_to(self, mut writer: T)

Formats this IndexList value to the provided Writer.

Args:

  • ​writer (T): The object to write to.

write_repr_to​

def write_repr_to(self, mut writer: T)

Write the repr of this IndexList to a writer.

Args:

  • ​writer (T): The object to write to.

cast​

def cast[dtype: DType](self) -> IndexList[size, element_type=dtype]

Casts to the target DType.

Parameters:

  • ​dtype (DType): The dtype to cast towards.

Returns:

IndexList[size, element_type=dtype]: The list casted to the target type.

__hash__​

def __hash__[H: Hasher](self, mut hasher: H)

Updates hasher with the underlying bytes.

Parameters:

  • ​H (Hasher): The hasher type.

Args:

  • ​hasher (H): The hasher instance.

get_type_name​

static def get_type_name() -> String

Gets the name of the host type (the one implementing this trait). For example, Int would return "Int", DeviceBuffer[.float32] would return "DeviceBuffer[DType.float32]". This is used for error messages when passing types to the device. TODO: This method will be retired soon when better kernel call error messages arrive.

Returns:

String: The host type's name.