Skip to main content
Log in

Mojo struct

InlinedFixedVector

struct InlinedFixedVector[type: AnyTrivialRegType, size: Int = _calculate_fixed_vector_default_size[AnyTrivialRegType]()]

A dynamically-allocated vector with small-vector optimization and a fixed maximum capacity.

The InlinedFixedVector does not resize or implement bounds checks. It is initialized with both a small-vector size (specified at compile time) and a maximum capacity (specified at runtime).

The first size elements are stored in the statically-allocated small vector storage. Any remaining elements are stored in dynamically-allocated storage.

When it is deallocated, it frees its memory.

TODO: It should call its element destructors once we have traits.

This data structure is useful for applications where the number of required elements is not known at compile time, but once known at runtime, is guaranteed to be equal to or less than a certain capacity.

Parameters

  • type (AnyTrivialRegType): The type of the elements.
  • size (Int): The statically-known small-vector size.

Aliases

  • static_size = size:
  • static_data_type = StaticTuple[type, size]:

Fields

  • static_data (StaticTuple[type, size]): The underlying static storage, used for small vectors.
  • dynamic_data (UnsafePointer[type]): The underlying dynamic storage, used to grow large vectors.
  • current_size (Int): The number of elements in the vector.
  • capacity (Int): The maximum number of elements that can fit in the vector.

Implemented traits

AnyType, ExplicitlyCopyable, Sized, UnknownDestructibility

Methods

__init__

@implicit __init__(out self, capacity: Int)

Constructs InlinedFixedVector with the given capacity.

The dynamically allocated portion is capacity - size.

Args:

  • capacity (Int): The requested maximum capacity of the vector.

@implicit __init__(out self, existing: Self)

Copy constructor.

Args:

  • existing (Self): The InlinedFixedVector to copy.

__moveinit__

__moveinit__(out self, owned existing: Self)

Move constructor.

Args:

  • existing (Self): The InlinedFixedVector to consume.

__del__

__del__(owned self)

Destructor.

__getitem__

__getitem__(self, idx: Int) -> type

Gets a vector element at the given index.

Args:

  • idx (Int): The index of the element.

Returns:

The element at the given index.

__setitem__

__setitem__(mut self, idx: Int, value: type)

Sets a vector element at the given index.

Args:

  • idx (Int): The index of the element.
  • value (type): The value to assign.

append

append(mut self, value: type)

Appends a value to this vector.

Args:

  • value (type): The value to append.

__len__

__len__(self) -> Int

Gets the number of elements in the vector.

Returns:

The number of elements in the vector.

clear

clear(mut self)

Clears the elements in the vector.

__iter__

__iter__(mut self) -> _VecIter[type, InlinedFixedVector[type, size], _deref_iter_impl[type, size]]

Iterate over the vector.

Returns:

An iterator to the start of the vector.