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. - lifetime (
AnyLifetime[is_mutable.value]
): The lifetime of the Span.
Implemented traits
AnyType
,
CollectionElementNew
,
Copyable
,
ExplicitlyCopyable
,
Movable
,
Sized
Methods
__init__
__init__(inout self: Self, *, unsafe_ptr: UnsafePointer[T, 0, 0, alignof[::AnyType,__mlir_type.!kgen.target]() if triple_is_nvidia_cuda() else 1], len: Int)
Unsafe construction from a pointer and length.
Args:
- unsafe_ptr (
UnsafePointer[T, 0, 0, alignof[::AnyType,__mlir_type.!kgen.target]() if triple_is_nvidia_cuda() else 1]
): The underlying pointer of the span. - len (
Int
): The length of the view.
__init__(inout self: Self, *, other: Self)
Explicitly construct a deep copy of the provided Span.
Args:
- other (
Self
): The Span to copy.
__init__(inout self: Self, ref [lifetime] 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__[T2: CollectionElementNew, size: Int, //](inout self: Self, ref [lifetime] array: InlineArray[T2, size, 0])
Construct a Span from an InlineArray.
Parameters:
- T2 (
CollectionElementNew
): The type of the elements in the span. - size (
Int
): The size of the InlineArray.
Args:
- array (
InlineArray[T2, size, 0]
): The array to which the span refers.
__bool__
__bool__(self: Self) -> Bool
Check if a span is non-empty.
Returns:
True if a span is non-empty, False otherwise.
__getitem__
__getitem__(self: Self, idx: Int) -> ref [lifetime] 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: Self, slc: Slice) -> Self
Get a new span from a slice of the current span.
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, //](ref [self_is_lifetime] self: Span[is_mutable, T, lifetime], ref [rhs_is_lifetime] rhs: Span[is_mutable, T, lifetime]) -> Bool
Verify if span is equal to another span.
Parameters:
- T (
EqualityComparableCollectionElement
): The type of the elements in the span. Must implement the traitsEqualityComparable
andCollectionElement
.
Args:
- rhs (
Span[is_mutable, T, lifetime]
): 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, //](ref [self_is_lifetime] self: Span[is_mutable, T, lifetime], ref [rhs_is_lifetime] rhs: Span[is_mutable, T, lifetime]) -> Bool
Verify if span is not equal to another span.
Parameters:
- T (
EqualityComparableCollectionElement
): The type of the elements in the span. Must implement the traitsEqualityComparable
andCollectionElement
.
Args:
- rhs (
Span[is_mutable, T, lifetime]
): The span to compare against.
Returns:
True if the spans are not equal in length or contents, False otherwise.
__iter__
__iter__(self: Self) -> _SpanIter[is_mutable, T, lifetime, 1]
Get an iterator over the elements of the span.
Returns:
An iterator over the elements of the span.
__len__
__len__(self: 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: Self) -> UnsafePointer[T, 0, 0, alignof[::AnyType,__mlir_type.!kgen.target]() if triple_is_nvidia_cuda() else 1]
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: Self) -> Reference[is_mutable, T, lifetime, 0]
Gets a Reference to the first element of this slice.
Returns:
A Reference pointing at the first element of this slice.
copy_from
copy_from[lifetime: MutableLifetime, //](self: Span[1, T, lifetime], other: Span[is_mutable, T, lifetime])
Performs an element wise copy from all elements of other
into all elements of self
.
Parameters:
- lifetime (
MutableLifetime
): The inferred mutable lifetime of the data within the Span.
Args:
- other (
Span[is_mutable, T, lifetime]
): The Span to copy all elements from.
fill
fill[lifetime: MutableLifetime, //](self: Span[1, T, lifetime], value: T)
Fill the memory that a span references with a given value.
Parameters:
- lifetime (
MutableLifetime
): The inferred mutable lifetime of the data within the Span.
Args:
- value (
T
): The value to assign to each element.
get_immutable
get_immutable(self: Self) -> Span[0, T, (mutcast mut=#lit.struct.extract<:_stdlib::_builtin::_bool::_Bool is_mutable, "value">, lifetime)]
Return an immutable version of this span.
Returns:
A span covering the same elements, but without mutability.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!
😔 What went wrong?