-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
WindowRight.cs
146 lines (124 loc) · 3.47 KB
/
WindowRight.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
namespace SuperLinq;
public static partial class SuperEnumerable
{
/// <summary>
/// Creates a right-aligned sliding window over the source sequence of a given size.
/// </summary>
/// <typeparam name="TSource">
/// The type of the elements of <paramref name="source"/>.
/// </typeparam>
/// <param name="source">
/// The sequence over which to create the sliding window.
/// </param>
/// <param name="size">
/// Size of the sliding window.
/// </param>
/// <returns>
/// A sequence representing each sliding window.
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="source"/> is <see langword="null" />.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="size"/> is below <c>1</c>.
/// </exception>
/// <remarks>
/// <para>
/// A window can contain fewer elements than <paramref name="size"/>, especially as it slides over the start of
/// the sequence.
/// </para>
/// <para>
/// This operator uses deferred execution and streams its results.
/// </para>
/// </remarks>
public static IEnumerable<IList<TSource>> WindowRight<TSource>(this IEnumerable<TSource> source, int size)
{
ArgumentNullException.ThrowIfNull(source);
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(size);
if (source is IList<TSource> list)
return new WindowRightIterator<TSource>(list, size);
return Core(source, size);
static IEnumerable<IList<TSource>> Core(IEnumerable<TSource> source, int size)
{
using var e = source.GetEnumerator();
if (!e.MoveNext())
yield break;
var window = new TSource[1] { e.Current };
for (var i = 1; i < size; i++)
{
if (!e.MoveNext())
{
yield return window;
yield break;
}
var newWindow = new TSource[i + 1];
window.AsSpan().CopyTo(newWindow);
newWindow[i] = e.Current;
yield return window;
window = newWindow;
}
while (e.MoveNext())
{
var newWindow = new TSource[size];
window.AsSpan()[1..].CopyTo(newWindow);
newWindow[^1] = e.Current;
yield return window;
window = newWindow;
}
yield return window;
}
}
private sealed class WindowRightIterator<T>(
IList<T> source,
int size
) : ListIterator<IList<T>>
{
public override int Count => source.Count;
protected override IEnumerable<IList<T>> GetEnumerable()
{
if (source.Count == 0)
yield break;
var window = new T[1] { source[0] };
var max = (uint)Math.Min(source.Count, size);
for (var i = 1; i < max; i++)
{
var newWindow = new T[i + 1];
window.AsSpan()[..].CopyTo(newWindow);
newWindow[^1] = source[i];
yield return window;
window = newWindow;
}
max = (uint)source.Count;
for (var i = window.Length; i < max; i++)
{
var newWindow = new T[size];
window.AsSpan()[1..].CopyTo(newWindow);
newWindow[^1] = source[i];
yield return window;
window = newWindow;
}
yield return window;
}
protected override IList<T> ElementAt(int index)
{
ArgumentOutOfRangeException.ThrowIfNegative(index);
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, Count);
if (index < size)
{
var arr = new T[index];
var max = (uint)index;
for (var i = 0; i < max; i++)
arr[i] = source[i];
return arr;
}
else
{
var arr = new T[size];
var max = (uint)index + 1;
for (int i = 0, j = index - size + 1; i < arr.Length && j < max; i++, j++)
arr[i] = source[j];
return arr;
}
}
}
}