Skip to main content
Log in

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 traits EqualityComparable and CollectionElementNew.

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 traits EqualityComparable and CollectionElementNew.

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?