Mojo struct
InlineList
A list allocated on the stack with a maximum size known at compile time.
It is backed by an InlineArray
and an Int
to represent the size.
This struct has the same API as a regular List
, but it is not possible to change the
capacity. In other words, it has a fixed maximum size.
This is typically faster than a List
as it is only stack-allocated and does not require
any dynamic memory allocation.
Parameters
- ElementType (
CollectionElementNew
): The type of the elements in the list. - capacity (
Int
): The maximum number of elements that the list can hold.
Implemented traits
AnyType
,
Sized
Methods
__init__
__init__(out self)
This constructor creates an empty InlineList.
__init__(out self, *values: ElementType)
Constructs a list from the given values.
Args:
- *values (
ElementType
): The values to populate the list with.
__del__
__del__(owned self)
Destroy all the elements in the list and free the memory.
__bool__
__bool__(self) -> Bool
Checks whether the list has any elements or not.
Returns:
False
if the list is empty, True
if there is at least one element.
__getitem__
__getitem__(ref self, owned idx: Int) -> ref [$1._array] 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.
__contains__
__contains__[C: EqualityComparableCollectionElement, //](self, value: C) -> Bool
Verify if a given value is present in the list.
var x = InlineList[Int](1,2,3)
if 3 in x: print("x contains 3")
var x = InlineList[Int](1,2,3)
if 3 in x: print("x contains 3")
Parameters:
- C (
EqualityComparableCollectionElement
): The type of the elements in the list. Must implement the traitsEqualityComparable
andCollectionElementNew
.
Args:
- value (
C
): The value to find.
Returns:
True if the value is contained in the list, False otherwise.
__len__
__len__(self) -> Int
Returns the length of the list.
Returns:
The number of elements in the list.
__iter__
__iter__(ref self) -> _InlineListIter[ElementType, capacity, $1]
Iterate over elements of the list, returning immutable references.
Returns:
An iterator of immutable references to the list elements.
count
count[C: EqualityComparableCollectionElement, //](self, value: C) -> Int
Counts the number of occurrences of a value in the list.
var my_list = InlineList[Int](1, 2, 3)
print(my_list.count(1))
var my_list = InlineList[Int](1, 2, 3)
print(my_list.count(1))
Parameters:
- C (
EqualityComparableCollectionElement
): The type of the elements in the list. Must implement the traitsEqualityComparable
andCollectionElementNew
.
Args:
- value (
C
): The value to count.
Returns:
The number of occurrences of the value in the list.
append
append(inout self, owned value: ElementType)
Appends a value to the list.
Args:
- value (
ElementType
): The value to append.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!