Versioned enumerator for RecyclableList implemented. Benchmarks & tests added.
This commit is contained in:
1 parent
be746d65bf
commit
d830a53006
7 files changed
+177
-205
No files matched your search
@@ -0,0 +1,65 @@
|
||||
using System.Collections;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Recyclable.Collections
|
||||
{
|
||||
public partial class RecyclableList<T> : IVersionedList<T>
|
||||
{
|
||||
VersionedEnumerator IVersionedList<T>.GetEnumerator() => new(this);
|
||||
|
||||
[StructLayout(LayoutKind.Sequential, Pack = 1)]
|
||||
public struct VersionedEnumerator : IEnumerator<T>
|
||||
{
|
||||
private int _currentItemIndex;
|
||||
private readonly ulong _enumeratorVersion;
|
||||
private readonly RecyclableList<T> _list;
|
||||
|
||||
#nullable disable
|
||||
// [Pure]
|
||||
public readonly T Current => _enumeratorVersion == _list._version ? _list._memoryBlock[_currentItemIndex - 1] : throw new InvalidOperationException();
|
||||
// [Pure]
|
||||
readonly object IEnumerator.Current => _enumeratorVersion == _list._version ? _list._memoryBlock[_currentItemIndex - 1] : throw new InvalidOperationException();
|
||||
#nullable restore
|
||||
|
||||
public bool MoveNext()
|
||||
{
|
||||
if (_enumeratorVersion != _list._version)
|
||||
{
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
|
||||
Contract.EndContractBlock();
|
||||
|
||||
if (_currentItemIndex < _list._count)
|
||||
{
|
||||
_currentItemIndex++;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public VersionedEnumerator(RecyclableList<T> list)
|
||||
{
|
||||
_currentItemIndex = 0;
|
||||
_list = list;
|
||||
_enumeratorVersion = list._version;
|
||||
}
|
||||
|
||||
public readonly void Dispose()
|
||||
{
|
||||
}
|
||||
|
||||
void IEnumerator.Reset()
|
||||
{
|
||||
if (_enumeratorVersion != _list._version)
|
||||
{
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
|
||||
_currentItemIndex = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user