Skip to main content

Mojo struct

Span

A non owning view of contiguous data.

Parameters

  • is_mutable (Bool): Whether the span is mutable.
  • T (CollectionElement): The type of the elements in the span.
  • origin (Origin[is_mutable.value]): The origin of the Span.

Implemented traits

AnyType, CollectionElement, CollectionElementNew, Copyable, ExplicitlyCopyable, Movable, Sized

Methods

__init__

__init__(out self, *, ptr: UnsafePointer[T], length: Int)

Unsafe construction from a pointer and length.

Args:

  • ptr (UnsafePointer[T]): The underlying pointer of the span.
  • length (Int): The length of the view.

__init__(out self, *, other: Self)

Explicitly construct a deep copy of the provided Span.

Args:

  • other (Self): The Span to copy.

__init__(out self, ref [origin] list: List[T, hint_trivial_type])

Construct a Span from a List.

Args:

  • list (List[T, hint_trivial_type]): The list to which the span refers.

__init__[size: Int, //](out self, ref [origin] array: InlineArray[T, size])

Construct a Span from an InlineArray.

Parameters:

  • size (Int): The size of the InlineArray.

Args:

  • array (InlineArray[T, size]): The array to which the span refers.

__bool__

__bool__(self) -> Bool

Check if a span is non-empty.

Returns:

True if a span is non-empty, False otherwise.

__getitem__

__getitem__(self, idx: Int) -> ref [origin] T

Get a reference to an element in the span.

Args:

  • idx (Int): The index of the value to return.

Returns:

An element reference.

__getitem__(self, slc: Slice) -> Self

Get a new span from a slice of the current span.

Allocation: This function allocates when the step is negative, to avoid a memory leak, take ownership of the value.

Args:

  • slc (Slice): The slice specifying the range of the new subslice.

Returns:

A new span that points to the same data as the current span.

__eq__

__eq__[T: EqualityComparableCollectionElement, //](self: Span[T, origin], rhs: Span[T, origin]) -> Bool

Verify if span is equal to another span.

Parameters:

  • T (EqualityComparableCollectionElement): The type of the elements in the span. Must implement the traits EqualityComparable and CollectionElement.

Args:

  • rhs (Span[T, origin]): The span to compare against.

Returns:

True if the spans are equal in length and contain the same elements, False otherwise.

__ne__

__ne__[T: EqualityComparableCollectionElement, //](self: Span[T, origin], rhs: Span[T, origin]) -> Bool

Verify if span is not equal to another span.

Parameters:

  • T (EqualityComparableCollectionElement): The type of the elements in the span. Must implement the traits EqualityComparable and CollectionElement.

Args:

  • rhs (Span[T, origin]): The span to compare against.

Returns:

True if the spans are not equal in length or contents, False otherwise.

__iter__

__iter__(self) -> _SpanIter[T, origin]

Get an iterator over the elements of the span.

Returns:

An iterator over the elements of the span.

__len__

__len__(self) -> Int

Returns the length of the span. This is a known constant value.

Returns:

The size of the span.

unsafe_ptr

unsafe_ptr(self) -> UnsafePointer[T]

Gets a pointer to the first element of this slice.

Returns:

A pointer pointing at the first element of this slice.

as_ref

as_ref(self) -> Pointer[T, origin]

Gets a Pointer to the first element of this slice.

Returns:

A Pointer pointing at the first element of this slice.

copy_from

copy_from[origin: MutableOrigin, //](self: Span[T, origin], other: Span[T, origin])

Performs an element wise copy from all elements of other into all elements of self.

Parameters:

  • origin (MutableOrigin): The inferred mutable origin of the data within the Span.

Args:

  • other (Span[T, origin]): The Span to copy all elements from.

fill

fill[origin: MutableOrigin, //](self: Span[T, origin], value: T)

Fill the memory that a span references with a given value.

Parameters:

  • origin (MutableOrigin): The inferred mutable origin of the data within the Span.

Args:

  • value (T): The value to assign to each element.

get_immutable

get_immutable(self) -> Span[T, (muttoimm origin)]

Return an immutable version of this span.

Returns:

A span covering the same elements, but without mutability.