Skip to main content

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 (CollectionElement): 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__(self: Reference[InlineList[ElementType, capacity], is_mutable, lifetime, 0], owned idx: Int) -> ref [$1] 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: ComparableCollectionElement](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")

Parameters:

  • C (ComparableCollectionElement): The type of the elements in the list. Must implement the traits EqualityComparable and CollectionElement.

Args:

  • value (C): The value to find.

Returns:

True if the value is contained in the list, False otherwise.

append

append(inout self: Self, owned value: ElementType)

Appends a value to the list.

Args:

  • value (ElementType): The value to append.

count

count[C: ComparableCollectionElement](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))

Parameters:

  • C (ComparableCollectionElement): The type of the elements in the list. Must implement the traits EqualityComparable and CollectionElement.

Args:

  • value (C): The value to count.

Returns:

The number of occurrences of the value in the list.