Mojo struct
InlineArray
A fixed-size sequence of size homogeneous elements where size is a constant expression.
Parameters
- ElementType (
CollectionElementNew
): The type of the elements in the array. - size (
Int
): The size of the array. - run_destructors (
Bool
): Whether to run destructors on the elements. Defaults to False for backwards compatibility reasons only. Eventually this will default toTrue
and/or the parameter will be removed to unconditionally run destructors on the elements.
Aliases
type = array<#lit.struct.extract<:@stdlib::@builtin::@int::@Int size, "value">, !kgen.paramref<:trait<@stdlib::@builtin::@value::@CollectionElementNew> ElementType>>
:
Implemented traits
AnyType
,
Copyable
,
ExplicitlyCopyable
,
Movable
,
Sized
Methods
__init__
__init__(inout self: Self)
This constructor will always cause a compile time error if used. It is used to steer users away from uninitialized memory.
__init__(inout self: Self, *, unsafe_uninitialized: Bool)
Create an InlineArray with uninitialized memory.
Note that this is highly unsafe and should be used with caution.
We recommend to use the InlineList
instead if all the objects
are not available when creating the array.
If despite those workarounds, one still needs an uninitialized array, it is possible with:
var uninitialized_array = InlineArray[Int, 10](unsafe_uninitialized=True)
var uninitialized_array = InlineArray[Int, 10](unsafe_uninitialized=True)
Args:
- unsafe_uninitialized (
Bool
): A boolean to indicate if the array should be initialized. Always set toTrue
(it's not actually used inside the constructor).
__init__(inout self: Self, *, owned unsafe_assume_initialized: InlineArray[UnsafeMaybeUninitialized[ElementType], size, 0])
Constructs an InlineArray
from an InlineArray
of UnsafeMaybeUninitialized
.
Calling this function assumes that all elements in the input array are initialized.
If the elements of the input array are not initialized, the behavior is undefined,
even if ElementType
is valid for every possible bit pattern (e.g. Int
or Float
).
Args:
- unsafe_assume_initialized (
InlineArray[UnsafeMaybeUninitialized[ElementType], size, 0]
): The array ofUnsafeMaybeUninitialized
elements.
__init__(inout self: Self, fill: ElementType)
Constructs an empty array where each element is the supplied fill
.
Args:
- fill (
ElementType
): The element to fill each index.
__init__(inout self: Self, owned *elems: ElementType)
Constructs an array given a set of arguments.
Args:
- *elems (
ElementType
): The element types.
__init__(inout self: Self, *, owned storage: VariadicListMem[elt_is_mutable, ElementType, lifetime])
Construct an array from a low-level internal representation.
Args:
- storage (
VariadicListMem[elt_is_mutable, ElementType, lifetime]
): The variadic list storage to construct from.
__init__(inout self: Self, *, other: Self)
Explicitly copy the provided value.
Args:
- other (
Self
): The value to copy.
__copyinit__
__copyinit__(inout self: Self, other: Self)
Copy construct the array.
Args:
- other (
Self
): The array to copy.
__del__
__del__(owned self: Self)
Deallocate the array.
__getitem__
__getitem__(ref [self_is_lifetime] self: Self, idx: Int) -> ref [_] ElementType
Get a Reference
to the element at the given index.
Args:
- idx (
Int
): The index of the item.
Returns:
A reference to the item at the given index.
__getitem__[idx: Int](ref [self_is_lifetime] self: Self) -> ref [_] ElementType
Get a Reference
to the element at the given index.
Parameters:
- idx (
Int
): The index of the item.
Returns:
A reference to the item at the given index.
__contains__
__contains__[T: EqualityComparableCollectionElement, //](self: Self, value: T) -> Bool
Verify if a given value is present in the array.
from collections import InlineArray
var x = InlineArray[Int, 3](1,2,3)
if 3 in x: print("x contains 3")
from collections import InlineArray
var x = InlineArray[Int, 3](1,2,3)
if 3 in x: print("x contains 3")
Parameters:
- T (
EqualityComparableCollectionElement
): The type of the elements in the array. Must implement the traitsEqualityComparable
andCollectionElement
.
Args:
- value (
T
): The value to find.
Returns:
True if the value is contained in the array, False otherwise.
__len__
__len__(self: Self) -> Int
Returns the length of the array. This is a known constant value.
Returns:
The size of the array.
unsafe_get
unsafe_get(ref [self_is_lifetime] self: Self, idx: Int) -> ref [_] ElementType
Get a reference to an element of self without checking index bounds.
Users should opt for __getitem__
instead of this method as it is
unsafe.
Note that there is no wraparound for negative indices. Using negative indices is considered undefined behavior.
Args:
- idx (
Int
): The index of the element to get.
Returns:
A reference to the element at the given index.
unsafe_ptr
unsafe_ptr(self: Self) -> UnsafePointer[ElementType, 0, 0, alignof[::AnyType,__mlir_type.!kgen.target]() if triple_is_nvidia_cuda() else 1]
Get an UnsafePointer
to the underlying array.
That pointer is unsafe but can be used to read or write to the array.
Be careful when using this. As opposed to a pointer to a List
,
this pointer becomes invalid when the InlineArray
is moved.
Make sure to refresh your pointer every time the InlineArray
is moved.
Returns:
An UnsafePointer
to the underlying array.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!
😔 What went wrong?