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__(inout self: Self)
This constructor creates an empty InlineList.
__init__(inout self: Self, *values: ElementType)
Constructs a list from the given values.
Args:
- β*values (
ElementType
): The values to populate the list with.
__del__
β
__del__(owned self: Self)
Destroy all the elements in the list and free the memory.
__bool__
β
__bool__(self: 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_is_lifetime] self: Self, owned 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.
__contains__
β
__contains__[C: EqualityComparableCollectionElement, //](self: 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: Self) -> Int
Returns the length of the list.
Returns:
The number of elements in the list.
__iter__
β
__iter__(ref [self_is_lifetime] self: Self) -> _InlineListIter[$0, ElementType, capacity, $1, 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: 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: 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!
π What went wrong?