Mojo struct
Span
@register_passable(trivial)
struct Span[mut: Bool, //, T: CollectionElement, origin: Origin[mut]]
A non-owning view of contiguous data.
Parameters
- mut (
Bool
): Whether the span is mutable. - T (
CollectionElement
): The type of the elements in the span. - origin (
Origin[mut]
): The origin of the Span.
Implemented traits
AnyType
,
CollectionElementNew
,
Copyable
,
ExplicitlyCopyable
,
Movable
,
Sized
,
UnknownDestructibility
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 copy of the provided Span
.
Args:
- other (
Self
): TheSpan
to copy.
@implicit
__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 theInlineArray
.
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 traitsEqualityComparable
andCollectionElement
.
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 traitsEqualityComparable
andCollectionElement
.
Args:
- rhs (
Span[T, origin]
): The span to compare against.
Returns:
True if the spans are not equal in length or contents, False otherwise.
__contains__
__contains__[type: DType, //](self: Span[SIMD[type, 1], origin], value: SIMD[type, 1]) -> Bool
Verify if a given value is present in the Span.
Parameters:
- type (
DType
): The DType of the scalars stored in the Span.
Args:
- value (
SIMD[type, 1]
): The value to find.
Returns:
True if the value is contained in the list, 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
.
__reversed__
__reversed__(self) -> _SpanIter[T, origin, False]
Iterate backwards over the Span
.
Returns:
A reversed iterator of the Span
elements.
__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, mut=mut, origin=origin]
Retrieves a pointer to the underlying memory.
Returns:
The pointer to the underlying memory.
as_ref
as_ref(self) -> Pointer[T, origin]
Gets a Pointer
to the first element of this span.
Returns:
A Pointer
pointing at the first element of this span.
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]
): TheSpan
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._mlir_origin)]
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!