Struct ArraySlice<T>
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
TThe type that the MemoryBlock implements.
- Implements
-
IMemoryBlock<T>IEnumerable<T>
- Inherited Members
- Extension Methods
Constructors
ArraySlice(UnmanagedMemoryBlock<T>)
public ArraySlice(UnmanagedMemoryBlock<T> memoryBlock)
Parameters
memoryBlockUnmanagedMemoryBlock<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
memoryBlockUnmanagedMemoryBlock<T>sliceUnmanagedSpan<T>
ArraySlice(UnmanagedMemoryBlock<T>, Span<T>)
public ArraySlice(UnmanagedMemoryBlock<T> memoryBlock, Span<T> slice)
Parameters
memoryBlockUnmanagedMemoryBlock<T>sliceSpan<T>
ArraySlice(UnmanagedMemoryBlock<T>, T*, long)
Creates a sliced ArraySlice<T>.
public ArraySlice(UnmanagedMemoryBlock<T> memoryBlock, T* address, long count)
Parameters
memoryBlockUnmanagedMemoryBlock<T>addressT*The address of the first element
countlongThe number of
Tthis slice should contain
Fields
Address
public readonly T* Address
Field Value
- T*
Count
public readonly long Count
Field Value
IsSlice
Is this ArraySlice<T> a smaller part/slice of an unmanaged allocation?
public readonly bool IsSlice
Field Value
MemoryBlock
The memory block this ArraySlice<T> is stored in.
public readonly UnmanagedMemoryBlock<T> MemoryBlock
Field Value
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
indexlong
Property Value
- T
ItemLength
The size of a single item stored in Address.
public int ItemLength { get; }
Property Value
Remarks
Equivalent to NPTypeCode.SizeOf extension.
TypeCode
public static NPTypeCode TypeCode { get; }
Property Value
Methods
Allocate(long)
Allocate an array filled with noisy memory.
public static ArraySlice<T> Allocate(long count)
Parameters
countlongHow 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
countlongHow many items this array will have (aka Count).
fillDefaultboolShould 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
countlongHow many items this array will have (aka Count).
fillTThe 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
itemT
Returns
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
destinationUnmanagedSpan<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
destinationUnmanagedSpan<T>The destination span.
sourceOffsetlongOffset 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
destinationUnmanagedSpan<T>The destination span.
sourceOffsetlongOffset in source (element count, not bytes).
sourceLengthlongNumber of elements to copy.
CopyTo(nint)
Copies the entire array to address.
public void CopyTo(nint dst)
Parameters
dstnintThe 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
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
destinationSpan<T>
CopyTo(Span<T>, long)
public void CopyTo(Span<T> destination, long sourceOffset)
Parameters
CopyTo(Span<T>, long, long)
public void CopyTo(Span<T> destination, long sourceOffset, long sourceLength)
Parameters
destinationSpan<T>sourceOffsetlongoffset of source via count (not bytes)
sourceLengthlongHow 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
valueT
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
indexlong
Returns
- T
SetIndex(int, T)
Backwards-compatible overload accepting int index.
public void SetIndex(int index, T value)
Parameters
indexintvalueT
SetIndex(long, object)
public void SetIndex(long index, object value)
Parameters
Slice(long)
public ArraySlice<T> Slice(long start)
Parameters
startlong
Returns
- ArraySlice<T>
Slice(long, long)
public ArraySlice<T> Slice(long start, long length)
Parameters
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
destinationUnmanagedSpan<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
destinationSpan<T>