Mojo struct
IntArray
@register_passable
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. It supports both owned and non-owned (view) modes
for efficient memory sharing without copying.
This struct serves as the underlying storage mechanism for IntTuple
and related
data structures, optimized for high-performance tensor operations.
Implemented traits
AnyType
,
UnknownDestructibility
Methods
__init__
__init__(size: 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.
__init__(*, non_owned: Self, offset: Int = 0) -> Self
Create a non-owned view into another IntArray
.
Creates a view starting at the specified offset in the source array. The resulting array doesn't own the memory and won't free it when destroyed.
Args:
- non_owned (
Self
): The source array to create a view into. - offset (
Int
): Starting position in the source array. Defaults to 0.
__copyinit__
__copyinit__(existing: Self) -> Self
Initialize by copying an existing IntArray
.
For owned arrays, this performs a deep copy of the data. For non-owned arrays, this creates another view of the same data (zero-copy operation).
Args:
- existing (
Self
): The source array to copy from.
__del__
__del__(owned 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__
__getitem__(self, idx: Int) -> Int
Access an element at the specified index.
Note:
Bounds checking is only performed when INT_TUPLE_VALIDATION
is enabled.
Args:
- idx (
Int
): Zero-based index of the element to access.
Returns:
The integer value at the specified index.
__setitem__
__setitem__(mut self, idx: Int, value: Int)
Set the value at the specified index.
Note:
Bounds checking is only performed when INT_TUPLE_VALIDATION
is enabled.
Args:
- idx (
Int
): Zero-based index of the element to modify. - value (
Int
): The integer value to store at the specified index.
owning
owning(self) -> Bool
Check if this IntArray
owns its memory.
Returns:
True if this array owns its memory (positive _size), False if it's a view (negative _size).
size
size(self) -> Int
Get the number of elements in the array.
Returns:
The number of elements in the array, regardless of ownership status.
copy_from
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.
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?
Thank you! We'll create more content like this.
Thank you for helping us improve!