-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Lag.cs
187 lines (177 loc) · 6.21 KB
/
Lag.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
namespace SuperLinq;
public static partial class SuperEnumerable
{
/// <summary>
/// Produces a projection of a sequence by evaluating pairs of elements separated by a negative offset.
/// </summary>
/// <typeparam name="TSource">
/// The type of the elements of the source sequence
/// </typeparam>
/// <param name="source">
/// The sequence over which to evaluate lag
/// </param>
/// <param name="offset">
/// The offset (expressed as a positive number) by which to lag each value of the sequence
/// </param>
/// <returns>
/// A sequence of tuples with the current and lagged elements
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="source"/> is <see langword="null"/>.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="offset"/> is below <c>1</c>.
/// </exception>
/// <remarks>
/// <para>
/// For elements prior to the lag offset, <c><see langword="default"/>(<typeparamref name="TSource"/>?)</c> is
/// used as the lagged value.<br/>
/// </para>
/// <para>
/// This operator evaluates in a deferred and streaming manner.
/// </para>
/// </remarks>
public static IEnumerable<(TSource current, TSource? lag)> Lag<TSource>(this IEnumerable<TSource> source, int offset)
{
return source.Lag(offset, ValueTuple.Create);
}
/// <summary>
/// Produces a projection of a sequence by evaluating pairs of elements separated by a negative offset.
/// </summary>
/// <typeparam name="TSource">
/// The type of the elements of the source sequence
/// </typeparam>
/// <typeparam name="TResult">
/// The type of the elements of the result sequence
/// </typeparam>
/// <param name="source">
/// The sequence over which to evaluate lag
/// </param>
/// <param name="offset">
/// The offset (expressed as a positive number) by which to lag each value of the sequence
/// </param>
/// <param name="resultSelector">
/// A projection function which accepts the current and lagged items (in that order) and returns a result
/// </param>
/// <returns>
/// A sequence produced by projecting each element of the sequence with its lagged pairing
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="source"/> or <paramref name="resultSelector"/> is <see langword="null"/>.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="offset"/> is below <c>1</c>.
/// </exception>
/// <remarks>
/// <para>
/// For elements prior to the lag offset, <c><see langword="default"/>(<typeparamref name="TSource"/>?)</c> is
/// used as the lagged value.<br/>
/// </para>
/// <para>
/// This operator evaluates in a deferred and streaming manner.
/// </para>
/// </remarks>
public static IEnumerable<TResult> Lag<TSource, TResult>(
this IEnumerable<TSource> source,
int offset,
Func<TSource, TSource?, TResult> resultSelector
)
{
return source.Lag(offset, default!, resultSelector);
}
/// <summary>
/// Produces a projection of a sequence by evaluating pairs of elements separated by a negative offset.
/// </summary>
/// <typeparam name="TSource">
/// The type of the elements of the source sequence
/// </typeparam>
/// <typeparam name="TResult">
/// The type of the elements of the result sequence
/// </typeparam>
/// <param name="source">
/// The sequence over which to evaluate lag
/// </param>
/// <param name="offset">
/// The offset (expressed as a positive number) by which to lag each value of the sequence
/// </param>
/// <param name="defaultLagValue">
/// A default value supplied for the lagged value prior to the lag offset
/// </param>
/// <param name="resultSelector">
/// A projection function which accepts the current and lagged items (in that order) and returns a result
/// </param>
/// <returns>
/// A sequence produced by projecting each element of the sequence with its lagged pairing
/// </returns>
/// <exception cref="ArgumentNullException">
/// <paramref name="source"/> or <paramref name="resultSelector"/> is <see langword="null"/>.
/// </exception>
/// <exception cref="ArgumentOutOfRangeException">
/// <paramref name="offset"/> is below <c>1</c>.
/// </exception>
/// <remarks>
/// <para>
/// For elements prior to the lag offset, <paramref name="defaultLagValue"/> is used as the lagged value.<br/>
/// </para>
/// <para>
/// This operator evaluates in a deferred and streaming manner.
/// </para>
/// </remarks>
public static IEnumerable<TResult> Lag<TSource, TResult>(
this IEnumerable<TSource> source,
int offset,
TSource defaultLagValue,
Func<TSource, TSource, TResult> resultSelector
)
{
ArgumentNullException.ThrowIfNull(source);
ArgumentNullException.ThrowIfNull(resultSelector);
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(offset);
if (source is IList<TSource> list)
return new LagIterator<TSource, TResult>(list, offset, defaultLagValue, resultSelector);
return Core(source, offset, defaultLagValue, resultSelector);
static IEnumerable<TResult> Core(
IEnumerable<TSource> source,
int offset,
TSource defaultLagValue,
Func<TSource, TSource, TResult> resultSelector
)
{
var lagQueue = new Queue<TSource>(offset + 1);
foreach (var item in source)
{
lagQueue.Enqueue(item);
yield return resultSelector(
item,
lagQueue.Count > offset ? lagQueue.Dequeue() : defaultLagValue);
}
}
}
private sealed class LagIterator<TSource, TResult>(
IList<TSource> source,
int offset,
TSource defaultLagValue,
Func<TSource, TSource, TResult> resultSelector
) : ListIterator<TResult>
{
public override int Count => source.Count;
protected override IEnumerable<TResult> GetEnumerable()
{
var cnt = (uint)source.Count;
for (var i = 0; i < cnt; i++)
{
yield return resultSelector(
source[i],
i < offset ? defaultLagValue : source[i - offset]);
}
}
protected override TResult ElementAt(int index)
{
ArgumentOutOfRangeException.ThrowIfNegative(index);
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, Count);
return resultSelector(
source[index],
index < offset ? defaultLagValue : source[index - offset]);
}
}
}