Show / Hide Table of Contents

Class Span

Provides extension methods for type Span<T> and ReadOnlySpan<T>.

Inheritance
object
Span
Inherited Members
object.Equals(object)
object.Equals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
object.ReferenceEquals(object, object)
object.ToString()
Namespace: DotNext
Assembly: DotNext.dll
Syntax
public static class Span

Methods

| Edit this page View Source

Advance<T>(ref ReadOnlySpan<T>)

Takes the first element and adjusts the span.

Declaration
public static ref readonly T Advance<T>(this ref ReadOnlySpan<T> source)
Parameters
Type Name Description
ReadOnlySpan<T> source

The source span.

Returns
Type Description
T

The reference to the first element in the span.

Type Parameters
Name Description
T

The type of elements in the span.

Exceptions
Type Condition
ArgumentOutOfRangeException

source is empty.

| Edit this page View Source

Advance<T>(ref ReadOnlySpan<T>, int)

Takes the specified number of elements and adjusts the span.

Declaration
public static ReadOnlySpan<T> Advance<T>(this ref ReadOnlySpan<T> source, int count)
Parameters
Type Name Description
ReadOnlySpan<T> source

The source span.

int count

The number of elements to take.

Returns
Type Description
ReadOnlySpan<T>

The span containing count elements.

Type Parameters
Name Description
T

The type of elements in the span.

Exceptions
Type Condition
ArgumentOutOfRangeException

count is greater than the length of source.

| Edit this page View Source

Advance<T>(ref Span<T>)

Takes the first element and adjusts the span.

Declaration
public static ref T Advance<T>(this ref Span<T> source)
Parameters
Type Name Description
Span<T> source

The source span.

Returns
Type Description
T

The reference to the first element in the span.

Type Parameters
Name Description
T

The type of elements in the span.

Exceptions
Type Condition
ArgumentOutOfRangeException

source is empty.

| Edit this page View Source

Advance<T>(ref Span<T>, int)

Takes the specified number of elements and adjusts the span.

Declaration
public static Span<T> Advance<T>(this ref Span<T> source, int count)
Parameters
Type Name Description
Span<T> source

The source span.

int count

The number of elements to take.

Returns
Type Description
Span<T>

The span containing count elements.

Type Parameters
Name Description
T

The type of elements in the span.

Exceptions
Type Condition
ArgumentOutOfRangeException

count is greater than the length of source.

| Edit this page View Source

AsBytes<T>(T*)

Converts contiguous memory identified by the specified pointer into Span<T>.

Declaration
[CLSCompliant(false)]
public static Span<byte> AsBytes<T>(T* pointer) where T : unmanaged
Parameters
Type Name Description
T* pointer

The typed pointer.

Returns
Type Description
Span<byte>

The span of contiguous memory.

Type Parameters
Name Description
T

The type of the pointer.

| Edit this page View Source

AsBytes<T>(ref T)

Converts contiguous memory identified by the specified pointer into Span<T>.

Declaration
public static Span<byte> AsBytes<T>(ref T value) where T : unmanaged
Parameters
Type Name Description
T value

The managed pointer.

Returns
Type Description
Span<byte>

The span of contiguous memory.

Type Parameters
Name Description
T

The type of the pointer.

| Edit this page View Source

AsReadOnlyBytes<T>(ref readonly T)

Converts contiguous memory identified by the specified pointer into ReadOnlySpan<T>.

Declaration
public static ReadOnlySpan<byte> AsReadOnlyBytes<T>(ref readonly T value) where T : unmanaged
Parameters
Type Name Description
T value

The managed pointer.

Returns
Type Description
ReadOnlySpan<byte>

The span of contiguous memory.

Type Parameters
Name Description
T

The type of the pointer.

| Edit this page View Source

BitwiseCompare<T>(ReadOnlySpan<T>, ReadOnlySpan<T>)

Compares content of the two memory blocks identified by the given spans.

Declaration
public static int BitwiseCompare<T>(this ReadOnlySpan<T> x, ReadOnlySpan<T> y) where T : unmanaged
Parameters
Type Name Description
ReadOnlySpan<T> x

The first memory span to compare.

ReadOnlySpan<T> y

The second array to compare.

Returns
Type Description
int

Comparison result.

Type Parameters
Name Description
T

The type of elements in the span.

| Edit this page View Source

BitwiseEquals<T>(ReadOnlySpan<T>, ReadOnlySpan<T>)

Determines whether two memory blocks identified by the given spans contain the same set of elements.

Declaration
public static bool BitwiseEquals<T>(this ReadOnlySpan<T> x, ReadOnlySpan<T> y) where T : unmanaged
Parameters
Type Name Description
ReadOnlySpan<T> x

The first memory span to compare.

ReadOnlySpan<T> y

The second memory span to compare.

Returns
Type Description
bool

true, if both memory blocks are equal; otherwise, false.

Type Parameters
Name Description
T

The type of elements in the span.

Remarks

This method performs bitwise equality between each pair of elements.

| Edit this page View Source

CheckMask<T>(ReadOnlySpan<T>, ReadOnlySpan<T>)

Determines whether the specified value satisfies the given mask.

Declaration
public static bool CheckMask<T>(this ReadOnlySpan<T> value, ReadOnlySpan<T> mask) where T : unmanaged
Parameters
Type Name Description
ReadOnlySpan<T> value

The value to check.

ReadOnlySpan<T> mask

The mask.

Returns
Type Description
bool

true if value & mask == mask; otherwise, false.

Type Parameters
Name Description
T

The type of the values.

| Edit this page View Source

Concat(ReadOnlySpan<string?>, MemoryAllocator<char>?)

Concatenates multiple strings.

Declaration
public static MemoryOwner<char> Concat(ReadOnlySpan<string?> values, MemoryAllocator<char>? allocator = null)
Parameters
Type Name Description
ReadOnlySpan<string> values

An array of strings.

MemoryAllocator<char> allocator

The allocator of the concatenated string.

Returns
Type Description
MemoryOwner<char>

A buffer containing characters from the concatenated strings.

Remarks

You can use methods from TupleExtensions to emulate variadic arguments.

Exceptions
Type Condition
OutOfMemoryException

The concatenated string is too large.

| Edit this page View Source

Concat<T>(ReadOnlySpan<T>, ReadOnlySpan<T>, MemoryAllocator<T>?)

Concatenates memory blocks.

Declaration
public static MemoryOwner<T> Concat<T>(this ReadOnlySpan<T> first, ReadOnlySpan<T> second, MemoryAllocator<T>? allocator = null)
Parameters
Type Name Description
ReadOnlySpan<T> first

The first memory block.

ReadOnlySpan<T> second

The second memory block.

MemoryAllocator<T> allocator

The memory allocator used to allocate buffer for the result.

Returns
Type Description
MemoryOwner<T>

The memory block containing elements from the specified two memory blocks.

Type Parameters
Name Description
T

The type of the elements in the memory.

| Edit this page View Source

Concat<T>(ReadOnlySpan<T>, ReadOnlySpan<T>, ReadOnlySpan<T>, MemoryAllocator<T>?)

Concatenates memory blocks.

Declaration
public static MemoryOwner<T> Concat<T>(this ReadOnlySpan<T> first, ReadOnlySpan<T> second, ReadOnlySpan<T> third, MemoryAllocator<T>? allocator = null)
Parameters
Type Name Description
ReadOnlySpan<T> first

The first memory block.

ReadOnlySpan<T> second

The second memory block.

ReadOnlySpan<T> third

The third memory block.

MemoryAllocator<T> allocator

The memory allocator used to allocate buffer for the result.

Returns
Type Description
MemoryOwner<T>

The memory block containing elements from the specified two memory blocks.

Type Parameters
Name Description
T

The type of the elements in the memory.

| Edit this page View Source

Contravariance<T, TBase>(ReadOnlySpan<T>)

Upcasts the span.

Declaration
public static ReadOnlySpan<TBase> Contravariance<T, TBase>(this ReadOnlySpan<T> span) where T : class?, TBase where TBase : class?
Parameters
Type Name Description
ReadOnlySpan<T> span

The span over elements.

Returns
Type Description
ReadOnlySpan<TBase>

The span pointing to the same memory as span.

Type Parameters
Name Description
T

The source type.

TBase

The target type.

| Edit this page View Source

CopyTo<T>(ReadOnlySpan<T>, Span<T>, out int)

Copies the contents from the source span into a destination span.

Declaration
public static void CopyTo<T>(this ReadOnlySpan<T> source, Span<T> destination, out int writtenCount)
Parameters
Type Name Description
ReadOnlySpan<T> source

Source memory.

Span<T> destination

Destination memory.

int writtenCount

The number of copied elements.

Type Parameters
Name Description
T

The type of the elements in the span.

| Edit this page View Source

CopyTo<T>(Span<T>, Span<T>, out int)

Copies the contents from the source span into a destination span.

Declaration
public static void CopyTo<T>(this Span<T> source, Span<T> destination, out int writtenCount)
Parameters
Type Name Description
Span<T> source

Source memory.

Span<T> destination

Destination memory.

int writtenCount

The number of copied elements.

Type Parameters
Name Description
T

The type of the elements in the span.

| Edit this page View Source

Copy<T>(ReadOnlySpan<T>, MemoryAllocator<T>?)

Creates buffered copy of the memory block.

Declaration
public static MemoryOwner<T> Copy<T>(this ReadOnlySpan<T> span, MemoryAllocator<T>? allocator = null)
Parameters
Type Name Description
ReadOnlySpan<T> span

The span of elements to be copied to the buffer.

MemoryAllocator<T> allocator

Optional buffer allocator.

Returns
Type Description
MemoryOwner<T>

The copy of the elements from span.

Type Parameters
Name Description
T

The type of the elements in the memory.

| Edit this page View Source

FirstOrNone<T>(ReadOnlySpan<T>, Predicate<T>?)

Returns the first element in a span that satisfies a specified condition.

Declaration
public static Optional<T> FirstOrNone<T>(this ReadOnlySpan<T> span, Predicate<T>? filter = null)
Parameters
Type Name Description
ReadOnlySpan<T> span

The source span.

Predicate<T> filter

A function to test each element for a condition.

Returns
Type Description
Optional<T>

The first element in the span that matches to the specified filter; or None.

Type Parameters
Name Description
T

The type of the elements in the span.

| Edit this page View Source

ForEach<T>(Span<T>, RefAction<T, int>)

Iterates over elements of the span.

Declaration
public static void ForEach<T>(this Span<T> span, RefAction<T, int> action)
Parameters
Type Name Description
Span<T> span

The span to iterate.

RefAction<T, int> action

The action to be applied for each element of the span.

Type Parameters
Name Description
T

The type of the elements.

| Edit this page View Source

ForEach<T, TArg>(Span<T>, delegate*<ref T, TArg, void>, TArg)

Iterates over elements of the span.

Declaration
[CLSCompliant(false)]
public static void ForEach<T, TArg>(this Span<T> span, delegate*<ref T, TArg, void> action, TArg arg)
Parameters
Type Name Description
Span<T> span

The span to iterate.

delegate*<ref T, TArg, void> action

The action to be applied for each element of the span.

TArg arg

The argument to be passed to the action.

Type Parameters
Name Description
T

The type of the elements.

TArg

The type of the argument to be passed to the action.

Exceptions
Type Condition
ArgumentNullException

action is zero.

| Edit this page View Source

IndexOf<T>(ReadOnlySpan<T>, T, int, delegate*<T, T, bool>)

Returns the zero-based index of the first occurrence of the specified value in the Span<T>. The search starts at a specified position.

Declaration
[CLSCompliant(false)]
public static int IndexOf<T>(this ReadOnlySpan<T> span, T value, int startIndex, delegate*<T, T, bool> comparer)
Parameters
Type Name Description
ReadOnlySpan<T> span

The span to search.

T value

The value to search for.

int startIndex

The search starting position.

delegate*<T, T, bool> comparer

The comparer used to compare the expected value and the actual value from the span.

Returns
Type Description
int

The zero-based index position of value from the start of the given span if that value is found, or -1 if it is not.

Type Parameters
Name Description
T

The of the elements in the span.

| Edit this page View Source

IndexOf<T>(ReadOnlySpan<T>, T, int, Func<T, T, bool>)

Returns the zero-based index of the first occurrence of the specified value in the Span<T>. The search starts at a specified position.

Declaration
public static int IndexOf<T>(this ReadOnlySpan<T> span, T value, int startIndex, Func<T, T, bool> comparer)
Parameters
Type Name Description
ReadOnlySpan<T> span

The span to search.

T value

The value to search for.

int startIndex

The search starting position.

Func<T, T, bool> comparer

The comparer used to compare the expected value and the actual value from the span.

Returns
Type Description
int

The zero-based index position of value from the start of the given span if that value is found, or -1 if it is not.

Type Parameters
Name Description
T

The of the elements in the span.

| Edit this page View Source

Initialize<T>(Span<T>)

Initializes each element in the span.

Declaration
public static void Initialize<T>(this Span<T> span) where T : new()
Parameters
Type Name Description
Span<T> span

The span of elements.

Type Parameters
Name Description
T

The type of the element.

Remarks

This method has the same behavior as Initialize() and supports reference types.

| Edit this page View Source

IsBitwiseAndNonZero<T>(ReadOnlySpan<T>, ReadOnlySpan<T>)

Determines whether the specified value and the given mask produces non-zero bitwise AND.

Declaration
public static bool IsBitwiseAndNonZero<T>(this ReadOnlySpan<T> value, ReadOnlySpan<T> mask) where T : unmanaged
Parameters
Type Name Description
ReadOnlySpan<T> value

The value to check.

ReadOnlySpan<T> mask

The mask.

Returns
Type Description
bool

true if value & mask != 0; otherwise, false.

Type Parameters
Name Description
T

The type of the values.

| Edit this page View Source

Move<T>(Span<T>, Range, Index)

Moves the range within the span to the specified index.

Declaration
public static void Move<T>(this Span<T> span, Range range, Index destinationIndex)
Parameters
Type Name Description
Span<T> span

The span of elements to modify.

Range range

The range of elements within span to move.

Index destinationIndex

The index of the element before which range of elements will be placed.

Type Parameters
Name Description
T

The type of the elements in the span.

Exceptions
Type Condition
ArgumentOutOfRangeException

destinationIndex is not a valid index within span.

| Edit this page View Source

Sort<T>(Span<T>, delegate*<T?, T?, int>)

Sorts the elements.

Declaration
[CLSCompliant(false)]
public static void Sort<T>(this Span<T> span, delegate*<T?, T?, int> comparison)
Parameters
Type Name Description
Span<T> span

The contiguous region of arbitrary memory to sort.

delegate*<T, T, int> comparison

The comparer used for sorting.

Type Parameters
Name Description
T

The type of the elements.

| Edit this page View Source

Swap<T>(Span<T>, Range, Range)

Swaps two ranges within the same span.

Declaration
public static void Swap<T>(this Span<T> span, Range range1, Range range2)
Parameters
Type Name Description
Span<T> span

The source span.

Range range1

The first range.

Range range2

The second range.

Type Parameters
Name Description
T

The type of the elements in the span.

Exceptions
Type Condition
ArgumentOutOfRangeException

range1 or range2 is out of valid range.

ArgumentException

range2 is overlapped with range1.

| Edit this page View Source

Swap<T>(Span<T>, Span<T>)

Swaps contents of the two spans.

Declaration
public static void Swap<T>(this Span<T> x, Span<T> y)
Parameters
Type Name Description
Span<T> x

The first span.

Span<T> y

The second span.

Type Parameters
Name Description
T

The type of the elements in the span.

Exceptions
Type Condition
ArgumentOutOfRangeException

The length of y is not of the same length as x.

ArgumentException

x overlaps with y.

| Edit this page View Source

TrimLength<T>(ReadOnlySpan<T>, int)

Trims the span to specified length if it exceeds it. If length is less that maxLength then the original span returned.

Declaration
public static ReadOnlySpan<T> TrimLength<T>(this ReadOnlySpan<T> span, int maxLength)
Parameters
Type Name Description
ReadOnlySpan<T> span

A contiguous region of arbitrary memory.

int maxLength

Maximum length.

Returns
Type Description
ReadOnlySpan<T>

Trimmed span.

Type Parameters
Name Description
T

The type of items in the span.

Exceptions
Type Condition
ArgumentOutOfRangeException

maxLength is less than zero.

| Edit this page View Source

TrimLength<T>(Span<T>, int)

Trims the span to specified length if it exceeds it. If length is less that maxLength then the original span returned.

Declaration
public static Span<T> TrimLength<T>(this Span<T> span, int maxLength)
Parameters
Type Name Description
Span<T> span

A contiguous region of arbitrary memory.

int maxLength

Maximum length.

Returns
Type Description
Span<T>

Trimmed span.

Type Parameters
Name Description
T

The type of items in the span.

Exceptions
Type Condition
ArgumentOutOfRangeException

maxLength is less than zero.

| Edit this page View Source

TrimLength<T>(Span<T>, int, out Span<T>)

Trims the span to specified length if it exceeds it. If length is less that maxLength then the original span returned.

Declaration
public static Span<T> TrimLength<T>(this Span<T> span, int maxLength, out Span<T> rest)
Parameters
Type Name Description
Span<T> span

A contiguous region of arbitrary memory.

int maxLength

Maximum length.

Span<T> rest

The rest of span.

Returns
Type Description
Span<T>

Trimmed span.

Type Parameters
Name Description
T

The type of items in the span.

Exceptions
Type Condition
ArgumentOutOfRangeException

maxLength is less than zero.

  • Edit this page
  • View Source
☀
☾
In this article
Back to top
Supported by the .NET Foundation
☀
☾