Mojo struct
Span
@register_passable(trivial)
struct Span[mut: Bool, //, T: Copyable & Movable, origin: Origin[mut], *, address_space: AddressSpace = AddressSpace(0)]
A non-owning view of contiguous data.
Parameters
- mut (Bool): Whether the span is mutable.
- T (Copyable&Movable): The type of the elements in the span.
- origin (Origin): The origin of the Span.
- address_space (AddressSpace): The address space associated with the allocated memory.
Implemented traits
AnyType,
Boolable,
Copyable,
Defaultable,
ImplicitlyCopyable,
Movable,
Sized,
UnknownDestructibility
Aliases
__copyinit__is_trivial
alias __copyinit__is_trivial = Int.__copyinit__is_trivial if UnsafePointer[T, address_space=address_space, mut=mut, origin=origin].__copyinit__is_trivial else UnsafePointer[T, address_space=address_space, mut=mut, origin=origin].__copyinit__is_trivial
__del__is_trivial
alias __del__is_trivial = Int.__del__is_trivial if UnsafePointer[T, address_space=address_space, mut=mut, origin=origin].__del__is_trivial else UnsafePointer[T, address_space=address_space, mut=mut, origin=origin].__del__is_trivial
__moveinit__is_trivial
alias __moveinit__is_trivial = Int.__moveinit__is_trivial if UnsafePointer[T, address_space=address_space, mut=mut, origin=origin].__moveinit__is_trivial else UnsafePointer[T, address_space=address_space, mut=mut, origin=origin].__moveinit__is_trivial
Immutable
alias Immutable = Span[T, __origin_of((muttoimm origin._mlir_origin))]
The immutable version of the Span.
Mutable
alias Mutable = Span[T, __origin_of((mutcast origin._mlir_origin))]
The mutable version of the Span.
UnsafePointerType
alias UnsafePointerType = UnsafePointer[T, address_space=address_space, mut=mut, origin=origin]
The UnsafePointer type that corresponds to this Span.
Methods
__init__
__init__() -> Self
Create an empty / zero-length span.
__init__(*, ptr: UnsafePointer[T, address_space=address_space, mut=mut, origin=origin], length: UInt) -> Self
Unsafe construction from a pointer and length.
Args:
- ptr (UnsafePointer): The underlying pointer of the span.
- length (UInt): The length of the view.
@implicit
__init__(ref [origin, address_space] list: List[T]) -> Self
Construct a Span from a List.
Args:
- list (List): The list to which the span refers.
@implicit
__init__[size: Int, //](ref [origin] array: InlineArray[T, size]) -> Self
Construct a Span from an InlineArray.
Parameters:
- size (Int): The size of theInlineArray.
Args:
- array (InlineArray): The array to which the span refers.
__bool__
__bool__(self) -> Bool
Check if a span is non-empty.
Returns:
Bool: True if a span is non-empty, False otherwise.
__getitem__
__getitem__[I: Indexer](self, idx: I) -> ref [origin, address_space] T
Get a reference to an element in the span.
Parameters:
- I (Indexer): A type that can be used as an index.
Args:
- idx (I): The index of the value to return.
Returns:
ref: 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:
Self: A new span that points to the same data as the current span.
__eq__
__eq__[T: EqualityComparable & Copyable & Movable, //](self: Span[T, origin], rhs: Span[T, origin]) -> Bool
Verify if span is equal to another span.
Parameters:
- T (EqualityComparable&Copyable&Movable): The type of the elements must implement the traitsEqualityComparable,CopyableandMovable.
Args:
- rhs (Span): The span to compare against.
Returns:
Bool: True if the spans are equal in length and contain the same elements, False otherwise.
__ne__
__ne__[T: EqualityComparable & Copyable & Movable, //](self: Span[T, origin], rhs: Span[T, origin]) -> Bool
Verify if span is not equal to another span.
Parameters:
- T (EqualityComparable&Copyable&Movable): The type of the elements in the span. Must implement the traitsEqualityComparable,CopyableandMovable.
Args:
- rhs (Span): The span to compare against.
Returns:
Bool: True if the spans are not equal in length or contents, False otherwise.
__contains__
__contains__[dtype: DType, //](self: Span[Scalar[dtype], origin, address_space=address_space], value: Scalar[dtype]) -> Bool
Verify if a given value is present in the Span.
Parameters:
- dtype (DType): The DType of the scalars stored in the Span.
Args:
- value (Scalar): The value to find.
Returns:
Bool: True if the value is contained in the list, False otherwise.
__iter__
__iter__(self) -> _SpanIter[T, origin, address_space=address_space]
Get an iterator over the elements of the Span.
Returns:
_SpanIter: An iterator over the elements of the Span.
__reversed__
__reversed__(self) -> _SpanIter[T, origin, False, address_space]
Iterate backwards over the Span.
Returns:
_SpanIter: A reversed iterator of the Span elements.
__len__
__len__(self) -> Int
Returns the length of the span. This is a known constant value.
Returns:
Int: The size of the span.
__str__
__str__[U: Representable & Copyable & Movable, //](self: Span[U, origin]) -> String
Returns a string representation of a Span.
Notes: Note that since we can't condition methods on a trait yet, the way to call this method is a bit special. Here is an example below:
var my_list = [1, 2, 3]
var my_span = Span(my_list)
print(my_span.__str__())When the compiler supports conditional methods, then a simple
String(my_span) will be enough.
Parameters:
- U (Representable&Copyable&Movable): The type of the elements in the span. Must implement the traitRepresentable.
Returns:
String: A string representation of the span.
write_to
write_to[U: Representable & Copyable & Movable, //](self: Span[U, origin], mut writer: T)
Write my_span.__str__() to a Writer.
Parameters:
- U (Representable&Copyable&Movable): The type of the Span elements. Must have the traitRepresentable.
Args:
- writer (T): The object to write to.
__repr__
__repr__[U: Representable & Copyable & Movable, //](self: Span[U, origin]) -> String
Returns a string representation of a Span.
Notes: Note that since we can't condition methods on a trait yet, the way to call this method is a bit special. Here is an example below:
var my_list = [1, 2, 3]
var my_span = Span(my_list)
print(my_span.__repr__())When the compiler supports conditional methods, then a simple
repr(my_span) will be enough.
Parameters:
- U (Representable&Copyable&Movable): The type of the elements in the span. Must implement the traitRepresentable.
Returns:
String: A string representation of the span.
get_immutable
get_immutable(self) -> Span[T, __origin_of((muttoimm origin._mlir_origin))]
Return an immutable version of this Span.
Returns:
Span: An immutable version of the same Span.
unsafe_ptr
unsafe_ptr(self) -> UnsafePointer[T, address_space=address_space, mut=mut, origin=origin]
Retrieves a pointer to the underlying memory.
Returns:
UnsafePointer: The pointer to the underlying memory.
as_ref
as_ref(self) -> Pointer[T, origin, address_space]
Gets a Pointer to the first element of this span.
Returns:
Pointer: 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): TheSpanto 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.
swap_elements
swap_elements(self: Span[T, origin], a: UInt, b: UInt)
Swap the values at indices a and b.
Args:
Raises:
If a or b are larger than the length of the span.
__merge_with__
__merge_with__[other_type: AnyStruct[Span[T, origin, address_space=address_space]]](self) -> Span[T, __origin_of((mutcast origin._mlir_origin), (mutcast origin._mlir_origin)), address_space=address_space]
Returns a pointer merged with the specified other_type.
Parameters:
- other_type (AnyStruct): The type of the pointer to merge with.
Returns:
Span: A pointer merged with the specified other_type.
reverse
reverse[dtype: DType, O: MutableOrigin, //](self: Span[Scalar[dtype], O])
Reverse the elements of the Span inplace.
Parameters:
- dtype (DType): The DType of the scalars theSpanstores.
- O (MutableOrigin): The origin of theSpan.
apply
apply[dtype: DType, O: MutableOrigin, //, func: fn[w: Int](SIMD[dtype, w]) capturing -> SIMD[dtype, w]](self: Span[Scalar[dtype], O])
Apply the function to the Span inplace.
Parameters:
- dtype (DType): The DType.
- O (MutableOrigin): The origin of theSpan.
- func (fn[w: Int](SIMD[dtype, w]) capturing -> SIMD[dtype, w]): The function to evaluate.
apply[dtype: DType, O: MutableOrigin, //, func: fn[w: Int](SIMD[dtype, w]) capturing -> SIMD[dtype, w], *, where: fn[w: Int](SIMD[dtype, w]) capturing -> SIMD[DType.bool, w]](self: Span[Scalar[dtype], O])
Apply the function to the Span inplace where the condition is True.
Parameters:
- dtype (DType): The DType.
- O (MutableOrigin): The origin of theSpan.
- func (fn[w: Int](SIMD[dtype, w]) capturing -> SIMD[dtype, w]): The function to evaluate.
- where (fn[w: Int](SIMD[dtype, w]) capturing -> SIMD[DType.bool, w]): The condition to apply the function.
count
count[dtype: DType, //, func: fn[w: Int](SIMD[dtype, w]) capturing -> SIMD[DType.bool, w]](self: Span[Scalar[dtype], origin]) -> UInt
Count the amount of times the function returns True.
Parameters:
- dtype (DType): The DType.
- func (fn[w: Int](SIMD[dtype, w]) capturing -> SIMD[DType.bool, w]): The function to evaluate.
Returns:
UInt: The amount of times the function returns True.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!
