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>, Span<T>)

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

Parameters

memoryBlock UnmanagedMemoryBlock<T>
slice Span<T>

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

Creates a sliced ArraySlice<T>.

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

Parameters

memoryBlock UnmanagedMemoryBlock<T>
address T*
count int

The number of T this slice should contain - relative to the memoryBlock

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*
count long

The number of T this slice should contain - relative to the memoryBlock

Fields

Address

public readonly T* Address

Field Value

T*

Count

public readonly int Count

Field Value

int

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

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

Parameters

index int

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(int)

Allocate an array filled with noisy memory.

public static ArraySlice<T> Allocate(int count)

Parameters

count int

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

Returns

ArraySlice<T>

A newly allocated array.

Allocate(int, bool)

Allocate an array filled with default value of T.

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

Parameters

count int

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(int, T)

Allocate an array filled filled with fill.

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

Parameters

count int

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(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>, int)

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

Parameters

destination Span<T>
sourceOffset int

offset of source via count (not bytes)

CopyTo(Span<T>, int, int)

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

Parameters

destination Span<T>
sourceOffset int

offset of source via count (not bytes)

sourceLength int

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(int)

public T GetIndex(int index)

Parameters

index int

Returns

T

SetIndex(int, object)

public void SetIndex(int index, object value)

Parameters

index int
value object

SetIndex(int, T)

public void SetIndex(int index, T value)

Parameters

index int
value T

Slice(int)

public ArraySlice<T> Slice(int start)

Parameters

start int

Returns

ArraySlice<T>

Slice(int, int)

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

Parameters

start int
length int

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(Span<T>)

public bool TryCopyTo(Span<T> destination)

Parameters

destination Span<T>

Returns

bool