-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathEquiZip.sbntxt
223 lines (203 loc) · 6.77 KB
/
EquiZip.sbntxt
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
{{~
$arity = arity
$ordinals = ordinals
$cardinals = cardinals
~}}
namespace SuperLinq;
#nullable enable
public static partial class SuperEnumerable
{
{{~ for $i in 2..4 ~}}
/// <summary>
/// <para>
/// Applies a specified function to the corresponding elements of second sequences, producing a sequence of the
/// results.
/// </para>
/// <para>
/// The resulting sequence has the same length as the input sequences. If the input sequences are of different
/// lengths, an exception is thrown.
/// </para>
/// </summary>
/// <typeparam name = "TResult">
/// The type of the elements of the result sequence.
/// </typeparam>
/// <param name = "resultSelector">
/// A projection function that combines elements from all of the sequences.
/// </param>
/// <returns>
/// A sequence of elements returned by <paramref name = "resultSelector"/>.
/// </returns>
/// <remarks>
/// This method uses deferred execution and stream its results.
/// </remarks>
/// <exception cref = "global::System.ArgumentNullException">
/// <paramref name = "resultSelector"/> or any of the input sequences is <see langword = "null"/>.
/// </exception>
/// <exception cref = "global::System.InvalidOperationException">
/// Any of the input sequences are shorter than the others.
/// </exception>
{{~ for $j in 1..$i ~}}
/// <typeparam name = "T{{ $cardinals[$j] }}">
/// The type of the elements of <paramref name = "{{ $ordinals[$j] }}" />.
/// </typeparam>
/// <param name = "{{ $ordinals[$j] }}">
/// The {{ $ordinals[$j] }} sequence of elements.
/// </param>
{{~ end ~}}
public static global::System.Collections.Generic.IEnumerable<TResult> EquiZip<{{ for $j in 1..$i }}T{{ $cardinals[$j] }}, {{ end }}TResult>(this
{{~ for $j in 1..$i ~}}
global::System.Collections.Generic.IEnumerable<T{{ $cardinals[$j] }}> {{ $ordinals[$j] }},
{{~ end ~}}
global::System.Func<{{ for $j in 1..$i }}T{{ $cardinals[$j] }}, {{ end }}TResult> resultSelector
)
{
{{~ for $j in 1..$i ~}}
ArgumentNullException.ThrowIfNull({{ $ordinals[$j] }});
{{~ end ~}}
ArgumentNullException.ThrowIfNull(resultSelector);
if (
{{~ for $j in 1..$i ~}}
{{ $ordinals[$j] }} is global::System.Collections.Generic.IList<T{{ $cardinals[$j] }}> list{{ $j }}{{ if !for.last }}&&{{ else }}){{ end }}
{{~ end ~}}
{
return new EquiZipIterator<{{ for $j in 1..$i }}T{{ $cardinals[$j] }}, {{ end }}TResult>(
{{~ for $j in 1..$i ~}}
list{{ $j }},
{{~ end ~}}
resultSelector
);
}
return Core(
{{~ for $j in 1..$i ~}}
{{ $ordinals[$j] }},
{{~ end ~}}
resultSelector
);
static global::System.Collections.Generic.IEnumerable<TResult> Core(
{{~ for $j in 1..$i ~}}
global::System.Collections.Generic.IEnumerable<T{{ $cardinals[$j] }}> {{ $ordinals[$j] }},
{{~ end ~}}
global::System.Func<{{ for $j in 1..$i }}T{{ $cardinals[$j] }}, {{ end }}TResult> resultSelector
)
{
{{~ for $j in 1..$i ~}}
using var e{{ $j }} = {{ $ordinals[$j] }}.GetEnumerator();
{{~ end ~}}
while (true)
{
if (!e1.MoveNext())
{
if (e2.MoveNext()
{{~
if $i >= 3
for $j in 3..$i ~}}
|| e{{$j}}.MoveNext()
{{~ end
end ~}}
)
{
ThrowHelper.ThrowInvalidOperationException("First sequence too short.");
}
yield break;
}
{{~ for $j in 2..$i ~}}
if (!e{{$j}}.MoveNext())
ThrowHelper.ThrowInvalidOperationException(
"{{ $cardinals[$j] }} sequence too short.");
{{~ end ~}}
yield return resultSelector(
{{~ for $j in 1..$i ~}}
e{{$j}}.Current{{ if !for.last }},{{ end }}
{{~ end ~}}
);
}
}
}
/// <summary>
/// Joins the corresponding elements of second sequences, producing a sequence of tuples containing them.
/// </summary>
/// <returns>
/// A sequence of <see cref="global::System.ValueTuple{ {{~ for $j in 1..$i ~}}T{{$j}}{{ if !for.last }},{{ end }}{{ end }} }" />
/// containing corresponding elements from each of the sequences.
/// </returns>
/// <remarks>
/// This method uses deferred execution and stream its results.
/// </remarks>
/// <exception cref = "global::System.ArgumentNullException">
/// Any of the input sequences is null.
/// </exception>
/// <exception cref = "global::System.InvalidOperationException">
/// Any of the input sequences are shorter than the others.
/// </exception>
{{~ for $j in 1..$i ~}}
/// <typeparam name="T{{ $cardinals[$j] }}">
/// The type of the elements of <paramref name="{{ $ordinals[$j] }}" />.
/// </typeparam>
/// <param name="{{ $ordinals[$j] }}">
/// The {{ $ordinals[$j] }} sequence of elements.
/// </param>
{{~ end ~}}
public static global::System.Collections.Generic.IEnumerable<({{~ for $j in 1..$i ~}}T{{ $cardinals[$j] }}{{ if !for.last }},{{ end }}{{ end }})>
EquiZip<{{~ for $j in 1..$i ~}}T{{ $cardinals[$j] }}{{ if !for.last }},{{ end }}{{ end }}>(this
{{~ for $j in 1..$i ~}}
global::System.Collections.Generic.IEnumerable<T{{ $cardinals[$j] }}> {{ $ordinals[$j] }}{{ if !for.last }},{{ end }}
{{~ end ~}}
) => EquiZip({{~ for $j in 1..$i ~}}{{ $ordinals[$j] }}, {{ end }}global::System.ValueTuple.Create);
private sealed class EquiZipIterator<{{ for $j in 1..$i }}T{{ $j }}, {{ end }}TResult> : ListIterator<TResult>
{
{{~ for $j in 1..$i ~}}
private readonly global::System.Collections.Generic.IList<T{{ $j }}> _list{{ $j }};
{{~ end ~}}
private readonly global::System.Func<{{ for $j in 1..$i }}T{{ $j }}, {{ end }}TResult> _resultSelector;
public EquiZipIterator(
{{~ for $j in 1..$i ~}}
global::System.Collections.Generic.IList<T{{ $j }}> {{ $ordinals[$j] }},
{{~ end ~}}
global::System.Func<{{ for $j in 1..$i }}T{{ $j }}, {{ end }}TResult> resultSelector
)
{
{{~ for $j in 1..$i ~}}
_list{{ $j }} = {{ $ordinals[$j] }};
{{~ end ~}}
_resultSelector = resultSelector;
}
public override int Count
{
get
{
var count = _list1.Count;
{{~ for $j in 2..$i ~}}
if (_list{{ $j }}.Count != count)
{
ThrowHelper.ThrowInvalidOperationException(
(count < _list{{ $j }}.Count ? "First" : "{{ $cardinals[$j] }}") + " sequence too short.");
}
{{~ end ~}}
return count;
}
}
protected override IEnumerable<TResult> GetEnumerable()
{
var cnt = (uint)Count;
for (var i = 0; i < cnt; i++)
{
yield return _resultSelector(
{{~ for $j in 1..$i ~}}
_list{{ $j }}[i]{{ if !for.last }}, {{ end }}
{{~ end ~}}
);
}
}
protected override TResult ElementAt(int index)
{
ArgumentOutOfRangeException.ThrowIfNegative(index);
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, Count);
return _resultSelector(
{{~ for $j in 1..$i ~}}
_list{{ $j }}[index]{{ if !for.last }}, {{ end }}
{{~ end ~}}
);
}
}
{{~ end ~}}
}