8000 Interleave: Do not call GetEnumerator eagerly by Orace · Pull Request #730 · morelinq/MoreLINQ · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Interleave: Do not call GetEnumerator eagerly #730

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations< 8000 /strong>
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 37 additions & 11 deletions MoreLinq.Test/InterleaveTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,55 @@ public void TestInterleaveIsLazy()
}

/// <summary>
/// Verify that interleaving do not call enumerators MoveNext method eagerly
/// Verify that interleaving disposes those enumerators that it managed
/// to open successfully
/// </summary>
[Test]
public void TestInterleaveDoNoCallMoveNextEagerly()
public void TestInterleaveDisposesOnErrorAtGetEnumerator()
{
var sequenceA = Enumerable.Range(1, 1);
var sequenceB = MoreEnumerable.From<int>(() => throw new TestException());
using var sequenceA = TestingSequence.Of<int>();
var sequenceB = new BreakingSequence<int>();

sequenceA.Interleave(sequenceB).Take(1).Consume();
// Expected and thrown by BreakingSequence
Assert.Throws<InvalidOperationException>(() => sequenceA.Interleave(sequenceB).Consume());
}

/// <summary>
/// Verify that interleaving disposes those enumerators that it managed
/// to open successfully
/// </summary>
[Test]
public void TestInterleaveDisposesOnError()
public void TestInterleaveDisposesOnErrorAtMoveNext()
{
using (var sequenceA = TestingSequence.Of<int>())
{
Assert.Throws<InvalidOperationException>(() => // Expected and thrown by BreakingSequence
sequenceA.Interleave(new BreakingSequence<int>()).Consume());
}
using var sequenceA = TestingSequence.Of<int>();
using var sequenceB = MoreEnumerable.From<int>(() => throw new TestException()).AsTestingSequence();

// Expected and thrown by sequenceB
Assert.Throws<TestException>(() => sequenceA.Interleave(sequenceB).Consume());
}

/// <summary>
/// Verify that interleaving do not call enumerable GetEnumerator method eagerly
/// </summary>
[Test]
public void TestInterleaveDoNotCallGetEnumeratorEagerly()
{
var sequenceA = TestingSequence.Of(1);
var sequenceB = new BreakingSequence<int>();

sequenceA.Interleave(sequenceB).Take(1).Consume();
}

/// <summary>
/// Verify that interleaving do not call enumerators MoveNext method eagerly
/// </summary>
[Test]
public void TestInterleaveDoNoCallMoveNextEagerly()
{
var sequenceA = Enumerable.Range(1, 1);
var sequenceB = MoreEnumerable.From<int>(() => throw new TestException());

sequenceA.Interleave(sequenceB).Take(1).Consume();
}

/// <summary>
Expand Down
20 changes: 16 additions & 4 deletions MoreLinq/Interleave.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,29 @@ public static IEnumerable<T> Interleave<T>(this IEnumerable<T> sequence, params
return _(); IEnumerable<T> _()
{
var sequences = new[] { sequence }.Concat(otherSequences);

// produce an enumerators collection for all IEnumerable<T> instances passed to us
var enumerators = sequences.Select(e => e.GetEnumerator()).Acquire();
var enumerators = new List<IEnumerator<T>>();

try
{
foreach (var enumerator in sequences.Select(s => s.GetEnumerator()))
{
enumerators.Add(enumerator);
if (enumerator.MoveNext())
{
yield return enumerator.Current;
}
else
{
enumerators.Remove(enumerator);
enumerator.Dispose();
}
}

var hasNext = true;
while (hasNext)
{
hasNext = false;
for (var i = 0; i < enumerators.Length; i++)
for (var i = 0; i < enumerators.Count; i++)
{
var enumerator = enumerators[i];
if (enumerator == null)
Expand Down
0