Skip to content

Commit

Permalink
Line-up Windowed with WindowLeft/Right
Browse files Browse the repository at this point in the history
- Renamed Windowed to Window
- Changed return type to return windows as lists
- Windowed marked obsolete and redirected to Window

Merge of PR morelinq#521 the closes morelinq#520
  • Loading branch information
atifaziz authored Jun 29, 2018
1 parent a112cea commit eacbd4e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 21 deletions.
34 changes: 17 additions & 17 deletions MoreLinq.Test/WindowedTest.cs → MoreLinq.Test/WindowTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,41 @@ namespace MoreLinq.Test
using NUnit.Framework;

/// <summary>
/// Verify the behavior of the Windowed operator
/// Verify the behavior of the Window operator
/// </summary>
[TestFixture]
public class WindowedTests
public class WindowTests
{
/// <summary>
/// Verify that Windowed behaves in a lazy manner
/// Verify that Window behaves in a lazy manner
/// </summary>
[Test]
public void TestWindowedIsLazy()
public void TestWindowIsLazy()
{
new BreakingSequence<int>().Windowed(1);
new BreakingSequence<int>().Window(1);
}

/// <summary>
/// Verify that a negative window size results in an exception
/// </summary>
[Test]
public void TestWindowedNegativeWindowSizeException()
public void TestWindowNegativeWindowSizeException()
{
var sequence = Enumerable.Repeat(1, 10);

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

/// <summary>
/// Verify that a sliding window of an any size over an empty sequence
/// is an empty sequence
/// </summary>
[Test]
public void TestWindowedEmptySequence()
public void TestWindowEmptySequence()
{
var sequence = Enumerable.Empty<int>();
var result = sequence.Windowed(5);
var result = sequence.Window(5);

Assert.That(result, Is.Empty);
}
Expand All @@ -47,11 +47,11 @@ public void TestWindowedEmptySequence()
/// degenerates to the original sequence.
/// </summary>
[Test]
public void TestWindowedOfSingleElement()
public void TestWindowOfSingleElement()
{
const int count = 100;
var sequence = Enumerable.Range(1, count);
var result = sequence.Windowed(1);
var result = sequence.Window(1);

// number of windows should be equal to the source sequence length
Assert.AreEqual(count, result.Count());
Expand All @@ -66,11 +66,11 @@ public void TestWindowedOfSingleElement()
/// in a empty sequence.
/// </summary>
[Test]
public void TestWindowedLargerThanSequence()
public void TestWindowLargerThanSequence()
{
const int count = 100;
var sequence = Enumerable.Range(1, count);
var result = sequence.Windowed(count + 1);
var result = sequence.Window(count + 1);

// there should only be one window whose contents is the same
// as the source sequence
Expand All @@ -82,12 +82,12 @@ public void TestWindowedLargerThanSequence()
/// in N sequences, where N = (source.Count() - windowSize) + 1.
/// </summary>
[Test]
public void TestWindowedSmallerThanSequence()
public void TestWindowSmallerThanSequence()
{
const int count = 100;
const int windowSize = count / 3;
var sequence = Enumerable.Range(1, count);
var result = sequence.Windowed(windowSize);
var result = sequence.Window(windowSize);

// ensure that the number of windows is correct
Assert.AreEqual(count - windowSize + 1, result.Count());
Expand All @@ -102,9 +102,9 @@ public void TestWindowedSmallerThanSequence()
/// </summary>

[Test]
public void TestWindowedWindowsImmutability()
public void TestWindowWindowsImmutability()
{
using (var windows = Enumerable.Range(1, 5).Windowed(2).AsTestingSequence())
using (var windows = Enumerable.Range(1, 5).Window(2).AsTestingSequence())
using (var reader = windows.ToArray().Read())
{
reader.Read().AssertSequenceEqual(1, 2);
Expand Down
2 changes: 1 addition & 1 deletion MoreLinq/MoreLinq.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
- TraverseBreadthFirst
- TraverseDepthFirst
- Unfold
- Windowed
- Window
- WindowLeft
- WindowRight
- ZipLongest
Expand Down
20 changes: 18 additions & 2 deletions MoreLinq/Windowed.cs → MoreLinq/Window.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ public static partial class MoreEnumerable
/// <param name="size">The size (number of elements) in each window</param>
/// <returns>A series of sequences representing each sliding window subsequence</returns>

public static IEnumerable<IEnumerable<TSource>> Windowed<TSource>(this IEnumerable<TSource> source, int size)
public static IEnumerable<IList<TSource>> Window<TSource>(this IEnumerable<TSource> source, int size)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (size <= 0) throw new ArgumentOutOfRangeException(nameof(size));

return _(); IEnumerable<IEnumerable<TSource>> _()
return _(); IEnumerable<IList<TSource>> _()
{
using (var iter = source.GetEnumerator())
{
Expand Down Expand Up @@ -70,5 +70,21 @@ public static IEnumerable<IEnumerable<TSource>> Windowed<TSource>(this IEnumerab
}
}
}

/// <summary>
/// Processes a sequence into a series of subsequences representing a windowed subset of the original
/// </summary>
/// <remarks>
/// The number of sequences returned is: <c>Max(0, sequence.Count() - windowSize) + 1</c><br/>
/// Returned subsequences are buffered, but the overall operation is streamed.<br/>
/// </remarks>
/// <typeparam name="TSource">The type of the elements of the source sequence</typeparam>
/// <param name="source">The sequence to evaluate a sliding window over</param>
/// <param name="size">The size (number of elements) in each window</param>
/// <returns>A series of sequences representing each sliding window subsequence</returns>

[Obsolete("Use " + nameof(Window) + " instead.")]
public static IEnumerable<IEnumerable<TSource>> Windowed<TSource>(this IEnumerable<TSource> source, int size) =>
source.Window(size);
}
}
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -607,11 +607,19 @@ its value, and the next state in the recursive call.

This method has 2 overloads.

### Windowed
### Window

Processes a sequence into a series of subsequences representing a windowed
subset of the original

### ~~Windowed~~

Processes a sequence into a series of subsequences representing a windowed
subset of the original

This method is obsolete and will be removed in a future version. Use `Window`
instead.

### WindowLeft

Creates a left-aligned sliding window over the source sequence of a given size.
Expand Down

0 comments on commit eacbd4e

Please sign in to comment.