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

Return isolated windows from Window* methods #655

Merged
merged 15 commits into from
Nov 20, 2019
Merged
Show file tree
Hide file tree
Changes from 10 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
67 changes: 67 additions & 0 deletions MoreLinq.Test/WindowLeftTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using NUnit.Framework.Interfaces;
Orace marked this conversation as resolved.
Show resolved Hide resolved

namespace MoreLinq.Test
{
using System.Collections.Generic;
Expand All @@ -12,6 +14,51 @@ public void WindowLeftIsLazy()
new BreakingSequence<int>().WindowLeft(1);
}

[Test]
public void ModifyWindowBeforeMoveNextDoNotAffectNextWindow()
{
var sequence = Enumerable.Range(0, 3);
using var e = sequence.WindowLeft(2).GetEnumerator();

e.MoveNext();
var window1 = e.Current;
window1[1] = -1;
e.MoveNext();
var window2 = e.Current;

Assert.That(window2[0], Is.EqualTo(1));
}

[Test]
public void ModifyWindowAfterMoveNextDoNotAffectNextWindow()
{
var sequence = Enumerable.Range(0, 3);
using var e = sequence.WindowLeft(2).GetEnumerator();

e.MoveNext();
var window1 = e.Current;
e.MoveNext();
window1[1] = -1;
var window2 = e.Current;

Assert.That(window2[0], Is.EqualTo(1));
}

[Test]
public void ModifyWindowDoNotAffectPrevWindow()
{
var sequence = Enumerable.Range(0, 3);
using var e = sequence.WindowLeft(2).GetEnumerator();

e.MoveNext();
var window1 = e.Current;
e.MoveNext();
var window2 = e.Current;
window2[0] = -1;

Assert.That(window1[1], Is.EqualTo(1));
}

[Test]
public void WindowLeftWithNegativeWindowSize()
{
Expand Down Expand Up @@ -76,5 +123,25 @@ public void WindowLeftWithWindowSizeSmallerThanSequence()
reader.ReadEnd();
}
}

static IEnumerable<T> Seq<T>(params T[] values) => values;

public static readonly IEnumerable<ITestCaseData> TestData =
from e in new[]
{
new {Source = Enumerable.Range(0, 4), Size = 1, Result = new[] {Seq(0), Seq(1), Seq(2), Seq(3)}},
new {Source = Enumerable.Range(0, 4), Size = 2, Result = new[] {Seq(0, 1), Seq(1, 2), Seq(2, 3), Seq(3)}},
new {Source = Enumerable.Range(0, 4), Size = 3, Result = new[] {Seq(0, 1, 2), Seq(1, 2, 3), Seq(2, 3), Seq(3)}},
new {Source = Enumerable.Range(0, 4), Size = 4, Result = new[] {Seq(0, 1, 2, 3), Seq(1, 2, 3), Seq(2, 3), Seq(3)}}
}
select new TestCaseData(e.Source, e.Size).Returns(e.Result);

[Test, TestCaseSource(nameof(TestData))]
public IEnumerable<IEnumerable<int>> TestWindowLeftOnKnownResults(IEnumerable<int> sequence, int sizes)
Orace marked this conversation as resolved.
Show resolved Hide resolved
{
using var testingSequence = sequence.AsTestingSequence();
var r = testingSequence.WindowLeft(sizes).ToList();
return r;
}
}
}
67 changes: 67 additions & 0 deletions MoreLinq.Test/WindowRightTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using NUnit.Framework.Interfaces;
Orace marked this conversation as resolved.
Show resolved Hide resolved

namespace MoreLinq.Test
{
using System.Collections.Generic;
Expand All @@ -12,6 +14,51 @@ public void WindowRightIsLazy()
new BreakingSequence<int>().WindowRight(1);
}

[Test]
public void ModifyWindowBeforeMoveNextDoNotAffectNextWindow()
{
var sequence = Enumerable.Range(0, 3);
using var e = sequence.WindowRight(2).GetEnumerator();

e.MoveNext();
var window1 = e.Current;
window1[0] = -1;
e.MoveNext();
var window2 = e.Current;

Assert.That(window2[0], Is.EqualTo(0));
}

[Test]
public void ModifyWindowAfterMoveNextDoNotAffectNextWindow()
{
var sequence = Enumerable.Range(0, 3);
using var e = sequence.WindowRight(2).GetEnumerator();

e.MoveNext();
var window1 = e.Current;
e.MoveNext();
window1[0] = -1;
var window2 = e.Current;

Assert.That(window2[0], Is.EqualTo(0));
}

[Test]
public void ModifyWindowDoNotAffectPrevWindow()
{
var sequence = Enumerable.Range(0, 3);
using var e = sequence.WindowRight(2).GetEnumerator();

e.MoveNext();
var window1 = e.Current;
e.MoveNext();
var window2 = e.Current;
window2[0] = -1;

Assert.That(window1[0], Is.EqualTo(0));
}

[Test]
public void WindowRightWithNegativeWindowSize()
{
Expand Down Expand Up @@ -76,5 +123,25 @@ public void WindowRightWithWindowSizeSmallerThanSequence()
reader.ReadEnd();
}
}

static IEnumerable<T> Seq<T>(params T[] values) => values;

public static readonly IEnumerable<ITestCaseData> TestData =
from e in new[]
{
new {Source = Enumerable.Range(0, 4), Size = 1, Result = new[] {Seq(0), Seq(1), Seq(2), Seq(3)}},
new {Source = Enumerable.Range(0, 4), Size = 2, Result = new[] {Seq(0), Seq(0, 1), Seq(1, 2), Seq(2, 3)}},
new {Source = Enumerable.Range(0, 4), Size = 3, Result = new[] {Seq(0), Seq(0, 1), Seq(0, 1, 2), Seq(1, 2, 3)}},
new {Source = Enumerable.Range(0, 4), Size = 4, Result = new[] {Seq(0), Seq(0, 1), Seq(0, 1, 2), Seq(0, 1, 2, 3)}}
}
select new TestCaseData(e.Source, e.Size).Returns(e.Result);

[Test, TestCaseSource(nameof(TestData))]
public IEnumerable<IEnumerable<int>> TestWindowRightOnKnownResults(IEnumerable<int> sequence, int sizes)
Orace marked this conversation as resolved.
Show resolved Hide resolved
{
using var testingSequence = sequence.AsTestingSequence();
var r = testingSequence.WindowRight(sizes).ToList();
return r;
}
}
}
69 changes: 68 additions & 1 deletion MoreLinq.Test/WindowTest.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
using System.Collections.Generic;
Orace marked this conversation as resolved.
Show resolved Hide resolved
using NUnit.Framework.Interfaces;

namespace MoreLinq.Test
{
using NUnit.Framework;
Expand All @@ -17,6 +20,51 @@ public void TestWindowIsLazy()
new BreakingSequence<int>().Window(1);
}

[Test]
public void ModifyWindowBeforeMoveNextDoNotAffectNextWindow()
{
var sequence = Enumerable.Range(0, 3);
using var e = sequence.Window(2).GetEnumerator();

e.MoveNext();
var window1 = e.Current;
window1[1] = -1;
e.MoveNext();
var window2 = e.Current;

Assert.That(window2[0], Is.EqualTo(1));
}

[Test]
public void ModifyWindowAfterMoveNextDoNotAffectNextWindow()
{
var sequence = Enumerable.Range(0, 3);
using var e = sequence.Window(2).GetEnumerator();

e.MoveNext();
var window1 = e.Current;
e.MoveNext();
window1[1] = -1;
var window2 = e.Current;

Assert.That(window2[0], Is.EqualTo(1));
}

[Test]
public void ModifyWindowDoNotAffectPrevWindow()
{
var sequence = Enumerable.Range(0, 3);
using var e = sequence.Window(2).GetEnumerator();

e.MoveNext();
var window1 = e.Current;
e.MoveNext();
var window2 = e.Current;
window2[0] = -1;

Assert.That(window1[1], Is.EqualTo(1));
}

/// <summary>
/// Verify that a negative window size results in an exception
/// </summary>
Expand All @@ -25,7 +73,7 @@ public void TestWindowNegativeWindowSizeException()
{
var sequence = Enumerable.Repeat(1, 10);

AssertThrowsArgument.OutOfRangeException("size",() =>
AssertThrowsArgument.OutOfRangeException("size", () =>
sequence.Window(-5));
}

Expand Down Expand Up @@ -114,5 +162,24 @@ public void TestWindowWindowsImmutability()
reader.ReadEnd();
}
}

static IEnumerable<T> Seq<T>(params T[] values) => values;

public static readonly IEnumerable<ITestCaseData> TestData =
from e in new[]
{
new {Source = Enumerable.Range(0, 4), Size = 1, Result = new[] {Seq(0), Seq(1), Seq(2), Seq(3)}},
new {Source = Enumerable.Range(0, 4), Size = 2, Result = new[] {Seq(0, 1), Seq(1, 2), Seq(2, 3)}},
new {Source = Enumerable.Range(0, 4), Size = 3, Result = new[] {Seq(0, 1, 2), Seq(1, 2, 3)}},
new {Source = Enumerable.Range(0, 4), Size = 4, Result = new[] {Seq(0, 1, 2, 3)}}
}
select new TestCaseData(e.Source, e.Size).Returns(e.Result);

[Test, TestCaseSource(nameof(TestData))]
public IEnumerable<IEnumerable<int>> TestWindowOnKnownResults(IEnumerable<int> sequence, int sizes)
Orace marked this conversation as resolved.
Show resolved Hide resolved
{
using var testingSequence = sequence.AsTestingSequence();
return testingSequence.Window(sizes).ToList();
}
}
}
17 changes: 8 additions & 9 deletions MoreLinq/Window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,20 @@ public static IEnumerable<IList<TSource>> Window<TSource>(this IEnumerable<TSour
if (i < size)
yield break;

// return the first window (whatever size it may be)
yield return window;

// generate the next window by shifting forward by one item
while (iter.MoveNext())
while(iter.MoveNext())
Orace marked this conversation as resolved.
Show resolved Hide resolved
{
// NOTE: If we used a circular queue rather than a list,
// we could make this quite a bit more efficient.
// Sadly the BCL does not offer such a collection.
// generate the next window by shifting forward by one item
// and do that before exposing the data
var newWindow = new TSource[size];
Array.Copy(window, 1, newWindow, 0, size - 1);
newWindow[size - 1] = iter.Current;
yield return newWindow;

yield return window;
window = newWindow;
}

// return the last window.
yield return window;
}
}

Expand Down
9 changes: 7 additions & 2 deletions MoreLinq/WindowLeft.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,18 @@ public static IEnumerable<IList<TSource>> WindowLeft<TSource>(this IEnumerable<T
window.Add(item);
if (window.Count < size)
continue;

// prepare next window before exposing data
var nextWindow = new List<TSource>(window.Skip(1));
yield return window;
window = new List<TSource>(window.Skip(1));
window = nextWindow;
}
while (window.Count > 0)
{
// prepare next window before exposing data
var nextWindow = new List<TSource>(window.Skip(1));
yield return window;
window = new List<TSource>(window.Skip(1));
window = nextWindow;
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion MoreLinq/WindowRight.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,11 @@ static IEnumerable<IList<TSource>> WindowRightWhile<TSource>(
foreach (var item in source)
{
window.Add(item);

// prepare next window before exposing data
var nextWindow = new List<TSource>(predicate(item, window.Count) ? window : window.Skip(1));
yield return window;
window = new List<TSource>(predicate(item, window.Count) ? window : window.Skip(1));
window = nextWindow;
}
}
}
Expand Down