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>, Span<T>)
public ArraySlice(UnmanagedMemoryBlock<T> memoryBlock, Span<T> slice)
Parameters
memoryBlockUnmanagedMemoryBlock<T>sliceSpan<T>
ArraySlice(UnmanagedMemoryBlock<T>, T*, int)
Creates a sliced ArraySlice<T>.
public ArraySlice(UnmanagedMemoryBlock<T> memoryBlock, T* address, int count)
Parameters
memoryBlockUnmanagedMemoryBlock<T>addressT*countintThe number of
Tthis slice should contain - relative to thememoryBlock
ArraySlice(UnmanagedMemoryBlock<T>, T*, long)
Creates a sliced ArraySlice<T>.
public ArraySlice(UnmanagedMemoryBlock<T> memoryBlock, T* address, long count)
Parameters
memoryBlockUnmanagedMemoryBlock<T>addressT*countlongThe number of
Tthis slice should contain - relative to thememoryBlock
Fields
Address
public readonly T* Address
Field Value
- T*
Count
public readonly int 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[int]
public T this[int index] { get; set; }
Parameters
indexint
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(int)
Allocate an array filled with noisy memory.
public static ArraySlice<T> Allocate(int count)
Parameters
countintHow 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
countintHow 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(int, T)
Allocate an array filled filled with fill.
public static ArraySlice<T> Allocate(int count, T fill)
Parameters
countintHow 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(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>, int)
public void CopyTo(Span<T> destination, int sourceOffset)
Parameters
CopyTo(Span<T>, int, int)
public void CopyTo(Span<T> destination, int sourceOffset, int sourceLength)
Parameters
destinationSpan<T>sourceOffsetintoffset of source via count (not bytes)
sourceLengthintHow 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(int)
public T GetIndex(int index)
Parameters
indexint
Returns
- T
SetIndex(int, object)
public void SetIndex(int index, object value)
Parameters
SetIndex(int, T)
public void SetIndex(int index, T value)
Parameters
indexintvalueT
Slice(int)
public ArraySlice<T> Slice(int start)
Parameters
startint
Returns
- ArraySlice<T>
Slice(int, int)
public ArraySlice<T> Slice(int start, int 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(Span<T>)
public bool TryCopyTo(Span<T> destination)
Parameters
destinationSpan<T>