namespace Vanara.PInvoke
{
/// Enumerator with zero copy access using ref.
/// The structure type.
public unsafe ref struct RefEnumerator where T : unmanaged
{
private T* _arrayPtr, basePtr;
private int _index;
/// Create RefEnumerator.
/// Pointer to unmanaged array
/// Number of elements in the
public RefEnumerator(T* arrayPtr, int count)
{
_arrayPtr = basePtr = arrayPtr;
Count = count;
_index = -1;
}
/// Gets the number of elements available.
/// The number of elements available.
public int Count { get; private set; }
/// Return current element.
public ref T Current => ref *_arrayPtr;
/// Gets the at the specified index.
/// The .
/// The index.
/// A reference to .
public ref T this[int index] => ref _arrayPtr[index];
/// Move to next element.
public bool MoveNext()
{
int index = _index + 1;
if (index < Count)
{
_index = index;
_arrayPtr++;
return true;
}
return false;
}
/// Resets this iterator.
public void Reset()
{
_arrayPtr = basePtr;
_index = -1;
}
}
}