IVersionedLongList<T> interface & RecyclableLongList.GetVersionedEnumerator added for use with foreach.

This commit is contained in:
mlemanczyk committed 2023-06-25 00:04:05 +02:00
1 parent 49c0615a62
commit 448656fd29
4 files changed
+32 -4

No files matched your search

@@ -0,0 +1,7 @@
namespace Recyclable.Collections
{
public interface IVersionedLongList<T> : IEnumerable<T>
{
new RecyclableLongList<T>.VersionedEnumerator GetEnumerator();
}
}
@@ -3,8 +3,12 @@ using System.Diagnostics.Contracts;
namespace Recyclable.Collections
{
public partial class RecyclableLongList<T>
public partial class RecyclableLongList<T> : IVersionedLongList<T>
{
public VersionedEnumerator GetVersionedEnumerator() => new(this);
VersionedEnumerator IVersionedLongList<T>.GetEnumerator() => new(this);
public struct VersionedEnumerator : IEnumerator<T>
{
#nullable disable
@@ -26,13 +30,13 @@ namespace Recyclable.Collections
public bool MoveNext()
{
if (_enumeratorVersion != _list._version)
var list = _list;
if (_enumeratorVersion != list._version)
{
throw new InvalidOperationException();
}
Contract.EndContractBlock();
var list = _list;
if (_currentBlockIndex < list._lastBlockWithData)
{
@@ -1109,7 +1109,6 @@ namespace Recyclable.Collections
public Enumerator GetEnumerator() => new(this);
IEnumerator IEnumerable.GetEnumerator() => new Enumerator(this);
IEnumerator<T> IEnumerable<T>.GetEnumerator() => new Enumerator(this);
public VersionedEnumerator GetVersionedEnumerator() => new(this);
[MethodImpl(MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization)]
public int IndexOf(T item)
@@ -398,6 +398,24 @@ namespace Recyclable.CollectionsTests
_ = actual.Should().ContainInConsecutiveOrder(testData);
}
[Theory]
[MemberData(nameof(RecyclableLongListTestData.SourceTargetDataVariants), MemberType = typeof(RecyclableLongListTestData))]
public void VersionedEnumerateShouldYieldAllItemsInCorrectOrder(string testCase, IEnumerable<long> testData, long itemsCount, int targetBlockSize)
{
// Prepare
using var list = new RecyclableLongList<long>(testData, minBlockSize: targetBlockSize);
// Act
var actual = new List<long>();
foreach(var item in (IVersionedLongList<long>)list)
{
actual.Add(item);
}
// Validate
_ = actual.Should().ContainInConsecutiveOrder(testData);
}
[Fact]
public void IndexOfShouldNotFindAnythingWhenListEmpty()
{