Skip to content
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

Simplify EquiZip implementation #902

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions MoreLinq.Test/EquiZipTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@

namespace MoreLinq.Test
{
using System;
using System.Collections.Generic;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using Tuple = System.ValueTuple;

[TestFixture]
Expand Down Expand Up @@ -76,6 +79,29 @@ public void ZipIsLazy()
bs.EquiZip(bs, BreakingFunc.Of<int, int, int>());
}

public static readonly IEnumerable<ITestCaseData> TestData =
from e in new[]
{
new { A = 1, B = "First", },
new { A = 2, B = "Second", },
new { A = 3, B = "Third", },
new { A = 4, B = "Fourth", },
}
select new TestCaseData(e.A, e.B);

[Test, TestCaseSource(nameof(TestData))]
public void ShortestSequenceIsIdentifiedProperly(int shortSequence, string sequenceName)
{
using var seq1 = Enumerable.Range(1, shortSequence == 1 ? 2 : 3).AsTestingSequence();
using var seq2 = Enumerable.Range(1, shortSequence == 2 ? 2 : 3).AsTestingSequence();
using var seq3 = Enumerable.Range(1, shortSequence == 3 ? 2 : 3).AsTestingSequence();
using var seq4 = Enumerable.Range(1, shortSequence == 4 ? 2 : 3).AsTestingSequence();

var ex = Assert.Throws<InvalidOperationException>(() =>
seq1.EquiZip(seq2, seq3, seq4, (_, _, _, _) => 0).Consume());
Assert.That(ex.Message, Is.EqualTo(sequenceName + " sequence too short."));
}

[Test]
public void MoveNextIsNotCalledUnnecessarily()
{
Expand Down
75 changes: 39 additions & 36 deletions MoreLinq/EquiZip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static IEnumerable<TResult> EquiZip<TFirst, TSecond, TResult>(
if (second == null) throw new ArgumentNullException(nameof(second));
if (resultSelector == null) throw new ArgumentNullException(nameof(resultSelector));

return EquiZipImpl<TFirst, TSecond, object, object, TResult>(first, second, null, null, (a, b, _, _) => resultSelector(a, b));
return EquiZipImpl(first, second, Enumerable.Repeat(default(object?), int.MaxValue), Enumerable.Repeat(default(object?), int.MaxValue), (a, b, _, _) => resultSelector(a, b), 2);
}

/// <summary>
Expand Down Expand Up @@ -121,7 +121,7 @@ public static IEnumerable<TResult> EquiZip<T1, T2, T3, TResult>(
if (third == null) throw new ArgumentNullException(nameof(third));
if (resultSelector == null) throw new ArgumentNullException(nameof(resultSelector));

return EquiZipImpl<T1, T2, T3, object, TResult>(first, second, third, null, (a, b, c, _) => resultSelector(a, b, c));
return EquiZipImpl(first, second, third, Enumerable.Repeat(default(object?), int.MaxValue), (a, b, c, _) => resultSelector(a, b, c), 3);
}

/// <summary>
Expand Down Expand Up @@ -178,46 +178,49 @@ public static IEnumerable<TResult> EquiZip<T1, T2, T3, T4, TResult>(
if (fourth == null) throw new ArgumentNullException(nameof(fourth));
if (resultSelector == null) throw new ArgumentNullException(nameof(resultSelector));

return EquiZipImpl(first, second, third, fourth, resultSelector);
return EquiZipImpl(first, second, third, fourth, resultSelector, 4);
}

static IEnumerable<TResult> EquiZipImpl<T1, T2, T3, T4, TResult>(
IEnumerable<T1> s1,
IEnumerable<T2> s2,
IEnumerable<T3>? s3,
IEnumerable<T4>? s4,
Func<T1, T2, T3, T4, TResult> resultSelector)
IEnumerable<T1> s1,
IEnumerable<T2> s2,
IEnumerable<T3> s3,
IEnumerable<T4> s4,
Func<T1, T2, T3, T4, TResult> resultSelector,
int expectedTerminations)
{
const int zero = 0, one = 1;

var limit = 1 + (s3 != null ? one : zero)
+ (s4 != null ? one : zero);
using var e1 = s1.GetEnumerator();
using var e2 = s2.GetEnumerator();
using var e3 = s3.GetEnumerator();
using var e4 = s4.GetEnumerator();

return ZipImpl(s1, s2, s3, s4, resultSelector, limit, enumerators =>
while (true)
{
var i = enumerators.Index().First(x => x.Value == null).Key;
return new InvalidOperationException(OrdinalNumbers[i] + " sequence too short.");
});
}
if (!e1.MoveNext())
{
if (e2.MoveNext()
|| expectedTerminations >= 3 && e3.MoveNext()
|| expectedTerminations >= 4 && e4.MoveNext())
{
throw new InvalidOperationException("First sequence too short.");
}

static readonly string[] OrdinalNumbers =
{
"First",
"Second",
"Third",
"Fourth",
// "Fifth",
// "Sixth",
// "Seventh",
// "Eighth",
// "Ninth",
// "Tenth",
// "Eleventh",
// "Twelfth",
// "Thirteenth",
// "Fourteenth",
// "Fifteenth",
// "Sixteenth",
};
yield break;
}

if (!e2.MoveNext())
throw new InvalidOperationException("Second sequence too short.");
if (!e3.MoveNext())
throw new InvalidOperationException("Third sequence too short.");
if (!e4.MoveNext())
throw new InvalidOperationException("Fourth sequence too short.");

yield return resultSelector(
e1.Current,
e2.Current,
e3.Current,
e4.Current);
}
}
}
}