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

Mojo struct

IntArray

struct IntArray

A memory-efficient, register-passable array of integers.

IntArray provides a low-level implementation of a dynamically-sized integer array with direct memory management.

This struct serves as the underlying storage mechanism for IntTuple and related data structures, optimized for high-performance tensor operations.

Implemented traits​

AnyType, Copyable, ImplicitlyCopyable, ImplicitlyDeletable, Movable, RegisterPassable

Methods​

__init__​

def __init__(size: Int = Int(0)) -> Self

Initialize a new owned IntArray with the specified size.

Args:

  • ​size (Int): Number of integers to allocate space for. Defaults to 0.

def __init__(*, copy: Self) -> Self

Initialize by copying an existing IntArray.

For owned arrays, this performs a deep copy of the data.

Args:

  • ​copy (Self): The source array to copy from.

__deinit__​

def __deinit__(deinit self)

Destroy the IntArray and free its memory if owned.

Only frees memory for owned arrays (positive _size) to prevent double-free errors with views.

__getitem__​

def __getitem__(self, idx: Int) -> Int

Access an element at the specified index.

Args:

  • ​idx (Int): Zero-based index of the element to access.

Returns:

Int: The integer value at the specified index.

__setitem__​

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

Set the value at the specified index.

Note: Bounds checking is performed when assertions are enabled (e.g., -D ASSERT=all).

Args:

  • ​idx (Int): Zero-based index of the element to modify.
  • ​value (Int): The integer value to store at the specified index.

owning​

def owning(self) -> Bool

Check if this IntArray owns its memory.

Returns:

Bool: True if this array owns its memory (positive _size), False if it's a view (negative _size).

size​

def size(self) -> Int

Get the number of elements in the array.

Returns:

Int: The number of elements in the array, regardless of ownership status.

copy_from​

def copy_from(mut self, offset: Int, source: Self, size: Int)

Copy elements from another IntArray.

Args:

  • ​offset (Int): Destination offset in this array.
  • ​source (Self): Source array to copy from.
  • ​size (Int): Number of elements to copy.

def copy_from(mut self, dst_offset: Int, source: Self, src_offset: Int, size: Int)

Copy elements from another IntArray with source offset.

Args:

  • ​dst_offset (Int): Destination offset in this array.
  • ​source (Self): Source array to copy from.
  • ​src_offset (Int): Source offset in the source array.
  • ​size (Int): Number of elements to copy.

Was this page helpful?