Mojo struct
InlineArray
struct InlineArray[ElementType: CollectionElement, size: Int, *, run_destructors: Bool = False]
A fixed-size sequence of size homogeneous elements where size is a constant expression.
Parameters
- ElementType (
CollectionElement
): 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">, :trait<@stdlib::@builtin::@value::@CollectionElement> ElementType>
:
Implemented traits
AnyType
,
Copyable
,
ExplicitlyCopyable
,
Movable
,
Sized
,
UnknownDestructibility
Methods
__init__
__init__(out self)
This constructor will always cause a compile time error if used. It is used to steer users away from uninitialized memory.
__init__(out 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__(out self, *, owned unsafe_assume_initialized: InlineArray[UnsafeMaybeUninitialized[ElementType], size])
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]
): The array ofUnsafeMaybeUninitialized
elements.
__init__(out self, fill: ElementType)
Constructs an empty array where each element is the supplied fill
.
Args:
- fill (
ElementType
): The element to fill each index.
__init__(out self, owned *elems: ElementType)
Constructs an array given a set of arguments.
Args:
- *elems (
ElementType
): The element types.
__init__(out self, *, owned storage: VariadicListMem[ElementType, origin])
Construct an array from a low-level internal representation.
Args:
- storage (
VariadicListMem[ElementType, origin]
): The variadic list storage to construct from.
__init__(out self, *, other: Self)
Explicitly copy the provided value.
Args:
- other (
Self
): The value to copy.
__copyinit__
__copyinit__(out self, other: Self)
Copy construct the array.
Args:
- other (
Self
): The array to copy.
__del__
__del__(owned self)
Deallocate the array.
__getitem__
__getitem__(ref self, idx: Int) -> ref [self] ElementType
Get a Pointer
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) -> ref [self] ElementType
Get a Pointer
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: InlineArray[T, size], 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) -> 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, idx: Int) -> ref [self] 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) -> UnsafePointer[ElementType]
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!