Table of Contents

Struct ReadOnlyUnmanagedSpan<T>

Namespace
NumSharp.Utilities
Assembly
NumSharp.dll

ReadOnlyUnmanagedSpan 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 ReadOnlyUnmanagedSpan<T> where T : unmanaged

Type Parameters

T
Inherited Members
Extension Methods

Constructors

ReadOnlyUnmanagedSpan(void*, long)

Creates a new read-only 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 ReadOnlyUnmanagedSpan(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.

ReadOnlyUnmanagedSpan(ref readonly T)

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

public ReadOnlyUnmanagedSpan(ref readonly T reference)

Parameters

reference T

A reference to data.

ReadOnlyUnmanagedSpan(T[]?)

Creates a new read-only span over the entirety of the target array.

public ReadOnlyUnmanagedSpan(T[]? array)

Parameters

array T[]

The target array.

Remarks

Returns default when array is null.

ReadOnlyUnmanagedSpan(T[]?, int, int)

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

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

Parameters

array T[]

The target array.

start int

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

length int

The number of items in the read-only span.

Remarks

Returns default when array is null.

Exceptions

ArgumentOutOfRangeException

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

Properties

Empty

Returns a 0-length read-only span whose base is the null pointer.

public static ReadOnlyUnmanagedSpan<T> Empty { get; }

Property Value

ReadOnlyUnmanagedSpan<T>

IsEmpty

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

public bool IsEmpty { get; }

Property Value

bool

true if this span is empty; otherwise, false.

this[long]

Returns the specified element of the read-only span.

public ref readonly 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 read-only span.

public long Length { get; }

Property Value

long

Methods

CopyTo(UnmanagedSpan<T>)

Copies the contents of this read-only 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.

GetEnumerator()

Gets an enumerator for this span.

public ReadOnlyUnmanagedSpan<T>.Enumerator GetEnumerator()

Returns

ReadOnlyUnmanagedSpan<T>.Enumerator

Slice(long)

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

public ReadOnlyUnmanagedSpan<T> Slice(long start)

Parameters

start long

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

Returns

ReadOnlyUnmanagedSpan<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 read-only span, beginning at 'start', of given length

public ReadOnlyUnmanagedSpan<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

ReadOnlyUnmanagedSpan<T>

Exceptions

ArgumentOutOfRangeException

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

ToArray()

Copies the contents of this read-only 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 ReadOnlyUnmanagedSpan<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 read-only 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 ==(ReadOnlyUnmanagedSpan<T>, ReadOnlyUnmanagedSpan<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 ==(ReadOnlyUnmanagedSpan<T> left, ReadOnlyUnmanagedSpan<T> right)

Parameters

left ReadOnlyUnmanagedSpan<T>
right ReadOnlyUnmanagedSpan<T>

Returns

bool

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

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

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

Parameters

segment ArraySegment<T>

Returns

ReadOnlyUnmanagedSpan<T>

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

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

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

Parameters

array T[]

Returns

ReadOnlyUnmanagedSpan<T>

operator !=(ReadOnlyUnmanagedSpan<T>, ReadOnlyUnmanagedSpan<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 !=(ReadOnlyUnmanagedSpan<T> left, ReadOnlyUnmanagedSpan<T> right)

Parameters

left ReadOnlyUnmanagedSpan<T>
right ReadOnlyUnmanagedSpan<T>

Returns

bool