Table of Contents

Struct ArraySlice<T>

Namespace
NumSharp.Backends.Unmanaged
Assembly
NumSharp.dll

ArraySlice<T> is similar to Span<T> but it can be moved around without having to follow ref struct rules.

public readonly struct ArraySlice<T> : IArraySlice, ICloneable, IMemoryBlock<T>, IMemoryBlock, IEnumerable<T>, IEnumerable where T : unmanaged

Type Parameters

T

The type that the MemoryBlock implements.

Implements
Inherited Members
Extension Methods

Constructors

ArraySlice(UnmanagedMemoryBlock<T>)

public ArraySlice(UnmanagedMemoryBlock<T> memoryBlock)

Parameters

memoryBlock UnmanagedMemoryBlock<T>

ArraySlice(UnmanagedMemoryBlock<T>, UnmanagedSpan<T>)

Creates a sliced ArraySlice<T> from an UnmanagedSpan. Supports long indexing for arrays > 2B elements.

public ArraySlice(UnmanagedMemoryBlock<T> memoryBlock, UnmanagedSpan<T> slice)

Parameters

memoryBlock UnmanagedMemoryBlock<T>
slice UnmanagedSpan<T>

ArraySlice(UnmanagedMemoryBlock<T>, Span<T>)

public ArraySlice(UnmanagedMemoryBlock<T> memoryBlock, Span<T> slice)

Parameters

memoryBlock UnmanagedMemoryBlock<T>
slice Span<T>

ArraySlice(UnmanagedMemoryBlock<T>, T*, long)

Creates a sliced ArraySlice<T>.

public ArraySlice(UnmanagedMemoryBlock<T> memoryBlock, T* address, long count)

Parameters

memoryBlock UnmanagedMemoryBlock<T>
address T*

The address of the first element

count long

The number of T this slice should contain

Fields

Address

public readonly T* Address

Field Value

T*

Count

public readonly long Count

Field Value

long

IsSlice

Is this ArraySlice<T> a smaller part/slice of an unmanaged allocation?

public readonly bool IsSlice

Field Value

bool

MemoryBlock

The memory block this ArraySlice<T> is stored in.

public readonly UnmanagedMemoryBlock<T> MemoryBlock

Field Value

UnmanagedMemoryBlock<T>

Remarks

If IsSlice is false then this slice represents the entire MemoryBlock.

VoidAddress

public readonly void* VoidAddress

Field Value

void*

Properties

this[long]

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

Parameters

index long

Property Value

T

ItemLength

The size of a single item stored in Address.

public int ItemLength { get; }

Property Value

int

Remarks

Equivalent to NPTypeCode.SizeOf extension.

TypeCode

public static NPTypeCode TypeCode { get; }

Property Value

NPTypeCode

Methods

Allocate(long)

Allocate an array filled with noisy memory.

public static ArraySlice<T> Allocate(long count)

Parameters

count long

How many items this array will have (aka Count).

Returns

ArraySlice<T>

A newly allocated array.

Allocate(long, bool)

Allocate an array filled with default value of T.

public static ArraySlice<T> Allocate(long count, bool fillDefault)

Parameters

count long

How many items this array will have (aka Count).

fillDefault bool

Should the newly allocated memory be filled with the default of T

Returns

ArraySlice<T>

A newly allocated array.

Allocate(long, T)

Allocate an array filled filled with fill.

public static ArraySlice<T> Allocate(long count, T fill)

Parameters

count long

How many items this array will have (aka Count).

fill T

The item to fill the newly allocated memory with.

Returns

ArraySlice<T>

A newly allocated array.

Clone()

public ArraySlice<T> Clone()

Returns

ArraySlice<T>

Contains(T)

public bool Contains(T item)

Parameters

item T

Returns

bool

CopyTo(UnmanagedSpan<T>)

Copies this slice's contents to an UnmanagedSpan destination. Supports long indexing for arrays > 2B elements.

public void CopyTo(UnmanagedSpan<T> destination)

Parameters

destination UnmanagedSpan<T>

The destination span.

CopyTo(UnmanagedSpan<T>, long)

Copies a portion of this slice to an UnmanagedSpan destination. Supports long indexing for arrays > 2B elements.

public void CopyTo(UnmanagedSpan<T> destination, long sourceOffset)

Parameters

destination UnmanagedSpan<T>

The destination span.

sourceOffset long

Offset in source (element count, not bytes).

CopyTo(UnmanagedSpan<T>, long, long)

Copies a portion of this slice to an UnmanagedSpan destination. Supports long indexing for arrays > 2B elements.

public void CopyTo(UnmanagedSpan<T> destination, long sourceOffset, long sourceLength)

Parameters

destination UnmanagedSpan<T>

The destination span.

sourceOffset long

Offset in source (element count, not bytes).

sourceLength long

Number of elements to copy.

CopyTo(nint)

Copies the entire array to address.

public void CopyTo(nint dst)

Parameters

dst nint

The address to copy to

Remarks

The destiniton has to be atleast the size of this array, otherwise memory corruption is likely to occur.

CopyTo(nint, int, int)

Copies the entire array to address.

public void CopyTo(nint dst, int sourceOffset, int sourceCount)

Parameters

dst nint

The address to copy to

sourceOffset int
sourceCount int

Remarks

The destiniton has to be atleast the size of this array, otherwise memory corruption is likely to occur.

CopyTo(Span<T>)

public void CopyTo(Span<T> destination)

Parameters

destination Span<T>

CopyTo(Span<T>, long)

public void CopyTo(Span<T> destination, long sourceOffset)

Parameters

destination Span<T>
sourceOffset long

offset of source via count (not bytes)

CopyTo(Span<T>, long, long)

public void CopyTo(Span<T> destination, long sourceOffset, long sourceLength)

Parameters

destination Span<T>
sourceOffset long

offset of source via count (not bytes)

sourceLength long

How many items to copy

DangerousFree()

Performs dispose on the internal unmanaged memory block.

public void DangerousFree()

Remarks

Dangerous because this ArraySlice might be a IsSlice therefore there might be other slices that point to current MemoryBlock.
So releasing the MemoryBlock might cause memory corruption elsewhere.
It is best to leave MemoryBlock to GC.

Fill(T)

public void Fill(T value)

Parameters

value T

GetEnumerator()

Returns an enumerator that iterates through the collection.

public IEnumerator<T> GetEnumerator()

Returns

IEnumerator<T>

An enumerator that can be used to iterate through the collection.

GetIndex(long)

public T GetIndex(long index)

Parameters

index long

Returns

T

SetIndex(int, T)

Backwards-compatible overload accepting int index.

public void SetIndex(int index, T value)

Parameters

index int
value T

SetIndex(long, object)

public void SetIndex(long index, object value)

Parameters

index long
value object

Slice(long)

public ArraySlice<T> Slice(long start)

Parameters

start long

Returns

ArraySlice<T>

Slice(long, long)

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

Parameters

start long
length long

Returns

ArraySlice<T>

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[]

TryCopyTo(UnmanagedSpan<T>)

Tries to copy this slice's contents to an UnmanagedSpan destination. Supports long indexing for arrays > 2B elements.

public bool TryCopyTo(UnmanagedSpan<T> destination)

Parameters

destination UnmanagedSpan<T>

The destination span.

Returns

bool

True if the copy succeeded, false if destination is too short.

TryCopyTo(Span<T>)

public bool TryCopyTo(Span<T> destination)

Parameters

destination Span<T>

Returns

bool