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!
If you'd like to share more information, please report an issue on GitHub
π What went wrong?