Table of Contents

Struct UnmanagedSpan<T>

Namespace
NumSharp.Utilities
Assembly
NumSharp.dll

UnmanagedSpan represents a contiguous region of arbitrary memory. Unlike arrays, it can point to either managed or native memory, or to memory allocated on the stack. It is type-safe and memory-safe.

public readonly ref struct UnmanagedSpan<T> where T : unmanaged

Type Parameters

T
Inherited Members
Extension Methods

Constructors

UnmanagedSpan(void*, long)

Creates a new span over the target unmanaged buffer. Clearly this is quite dangerous, because we are creating arbitrarily typed T's out of a void*-typed block of memory. And the length is not checked. But if this creation is correct, then all subsequent uses are correct.

[CLSCompliant(false)]
public UnmanagedSpan(void* pointer, long length)

Parameters

pointer void*

An unmanaged pointer to memory.

length long

The number of T elements the memory contains.

Exceptions

ArgumentException

Thrown when T is reference type or contains pointers and hence cannot be stored in unmanaged memory.

ArgumentOutOfRangeException

Thrown when the specified length is negative.

UnmanagedSpan(ref T)

Creates a new UnmanagedSpan<T> of length 1 around the specified reference.

public UnmanagedSpan(ref T reference)

Parameters

reference T

A reference to data.

UnmanagedSpan(T[]?)

Creates a new span over the entirety of the target array.

public UnmanagedSpan(T[]? array)

Parameters

array T[]

The target array.

Remarks

Returns default when array is null.

Exceptions

ArrayTypeMismatchException

Thrown when array is covariant and array's type is not exactly T[].

UnmanagedSpan(T[]?, int, int)

Creates a new span over the portion of the target array beginning at 'start' index and ending at 'end' index (exclusive).

public UnmanagedSpan(T[]? array, int start, int length)

Parameters

array T[]

The target array.

start int

The zero-based index at which to begin the span.

length int

The number of items in the span.

Remarks

Returns default when array is null.

Exceptions

ArrayTypeMismatchException

Thrown when array is covariant and array's type is not exactly T[].

ArgumentOutOfRangeException

Thrown when the specified start or end index is not in the range (<0 or >Length).

Properties

Empty

Returns an empty UnmanagedSpan<T>

public static UnmanagedSpan<T> Empty { get; }

Property Value

UnmanagedSpan<T>

IsEmpty

Gets a value indicating whether this UnmanagedSpan<T> is empty.

public bool IsEmpty { get; }

Property Value

bool

true if this span is empty; otherwise, false.

this[long]

Returns a reference to specified element of the UnmanagedSpan.

public ref T this[long index] { get; }

Parameters

index long

The zero-based index.

Property Value

T

Exceptions

IndexOutOfRangeException

Thrown when index less than 0 or index greater than or equal to Length

Length

The number of items in the span.

public long Length { get; }

Property Value

long

Methods

Clear()

Clears the contents of this span.

public void Clear()

CopyTo(UnmanagedSpan<T>)

Copies the contents of this span into destination span. If the source and destinations overlap, this method behaves as if the original values in a temporary location before the destination is overwritten.

public void CopyTo(UnmanagedSpan<T> destination)

Parameters

destination UnmanagedSpan<T>

The span to copy items into.

Exceptions

ArgumentException

Thrown when the destination UnmanagedSpan is shorter than the source UnmanagedSpan.

Fill(T)

Fills the contents of this span with the given value.

public void Fill(T value)

Parameters

value T

GetEnumerator()

Gets an enumerator for this span.

public UnmanagedSpan<T>.Enumerator GetEnumerator()

Returns

UnmanagedSpan<T>.Enumerator

Slice(long)

Forms a slice out of the given span, beginning at 'start'.

public UnmanagedSpan<T> Slice(long start)

Parameters

start long

The zero-based index at which to begin this slice.

Returns

UnmanagedSpan<T>

Exceptions

ArgumentOutOfRangeException

Thrown when the specified start index is not in range (<0 or >Length).

Slice(long, long)

Forms a slice out of the given span, beginning at 'start', of given length

public UnmanagedSpan<T> Slice(long start, long length)

Parameters

start long

The zero-based index at which to begin this slice.

length long

The desired length for the slice (exclusive).

Returns

UnmanagedSpan<T>

Exceptions

ArgumentOutOfRangeException

Thrown when the specified start or end index is not in range (<0 or >Length).

ToArray()

Copies the contents of this span into a new array. This heap allocates, so should generally be avoided, however it is sometimes necessary to bridge the gap with APIs written in terms of arrays.

public T[] ToArray()

Returns

T[]

ToString()

For UnmanagedSpan<T>, returns a new instance of string that represents the characters pointed to by the span. Otherwise, returns a string with the name of the type and the number of elements.

public override string ToString()

Returns

string

TryCopyTo(UnmanagedSpan<T>)

Copies the contents of this span into destination span. If the source and destinations overlap, this method behaves as if the original values in a temporary location before the destination is overwritten.

public bool TryCopyTo(UnmanagedSpan<T> destination)

Parameters

destination UnmanagedSpan<T>

The span to copy items into.

Returns

bool

If the destination span is shorter than the source span, this method return false and no data is written to the destination.

Operators

operator ==(UnmanagedSpan<T>, UnmanagedSpan<T>)

Returns true if left and right point at the same memory and have the same length. Note that this does not check to see if the contents are equal.

public static bool operator ==(UnmanagedSpan<T> left, UnmanagedSpan<T> right)

Parameters

left UnmanagedSpan<T>
right UnmanagedSpan<T>

Returns

bool

implicit operator ReadOnlyUnmanagedSpan<T>(UnmanagedSpan<T>)

Defines an implicit conversion of a UnmanagedSpan<T> to a ReadOnlyUnmanagedSpan<T>

public static implicit operator ReadOnlyUnmanagedSpan<T>(UnmanagedSpan<T> span)

Parameters

span UnmanagedSpan<T>

Returns

ReadOnlyUnmanagedSpan<T>

implicit operator UnmanagedSpan<T>(ArraySegment<T>)

Defines an implicit conversion of a ArraySegment<T> to a UnmanagedSpan<T>

public static implicit operator UnmanagedSpan<T>(ArraySegment<T> segment)

Parameters

segment ArraySegment<T>

Returns

UnmanagedSpan<T>

implicit operator UnmanagedSpan<T>(T[]?)

Defines an implicit conversion of an array to a UnmanagedSpan<T>

public static implicit operator UnmanagedSpan<T>(T[]? array)

Parameters

array T[]

Returns

UnmanagedSpan<T>

operator !=(UnmanagedSpan<T>, UnmanagedSpan<T>)

Returns false if left and right point at the same memory and have the same length. Note that this does not check to see if the contents are equal.

public static bool operator !=(UnmanagedSpan<T> left, UnmanagedSpan<T> right)

Parameters

left UnmanagedSpan<T>
right UnmanagedSpan<T>

Returns

bool