-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
Select.cs
303 lines (254 loc) · 10.7 KB
/
Select.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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using static System.Linq.Utilities;
namespace System.Linq
{
public static partial class Enumerable
{
public static IEnumerable<TResult> Select<TSource, TResult>(
this IEnumerable<TSource> source, Func<TSource, TResult> selector)
{
if (source is null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source);
}
if (selector is null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector);
}
if (source is Iterator<TSource> iterator)
{
return iterator.Select(selector);
}
if (source is IList<TSource> ilist)
{
if (source is TSource[] array)
{
if (array.Length == 0)
{
return [];
}
return new ArraySelectIterator<TSource, TResult>(array, selector);
}
if (source is List<TSource> list)
{
return new ListSelectIterator<TSource, TResult>(list, selector);
}
return new IListSelectIterator<TSource, TResult>(ilist, selector);
}
return new IEnumerableSelectIterator<TSource, TResult>(source, selector);
}
public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, int, TResult> selector)
{
if (source is null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source);
}
if (selector is null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.selector);
}
if (IsEmptyArray(source))
{
return [];
}
return SelectIterator(source, selector);
}
private static IEnumerable<TResult> SelectIterator<TSource, TResult>(IEnumerable<TSource> source, Func<TSource, int, TResult> selector)
{
int index = -1;
foreach (TSource element in source)
{
checked
{
index++;
}
yield return selector(element, index);
}
}
/// <summary>
/// An iterator that maps each item of an <see cref="IEnumerable{TSource}"/>.
/// </summary>
/// <typeparam name="TSource">The type of the source enumerable.</typeparam>
/// <typeparam name="TResult">The type of the mapped items.</typeparam>
private sealed partial class IEnumerableSelectIterator<TSource, TResult> : Iterator<TResult>
{
private readonly IEnumerable<TSource> _source;
private readonly Func<TSource, TResult> _selector;
private IEnumerator<TSource>? _enumerator;
public IEnumerableSelectIterator(IEnumerable<TSource> source, Func<TSource, TResult> selector)
{
Debug.Assert(source is not null);
Debug.Assert(selector is not null);
_source = source;
_selector = selector;
}
private protected override Iterator<TResult> Clone() =>
new IEnumerableSelectIterator<TSource, TResult>(_source, _selector);
public override void Dispose()
{
if (_enumerator is not null)
{
_enumerator.Dispose();
_enumerator = null;
}
base.Dispose();
}
public override bool MoveNext()
{
switch (_state)
{
case 1:
_enumerator = _source.GetEnumerator();
_state = 2;
goto case 2;
case 2:
Debug.Assert(_enumerator is not null);
if (_enumerator.MoveNext())
{
_current = _selector(_enumerator.Current);
return true;
}
Dispose();
break;
}
return false;
}
public override IEnumerable<TResult2> Select<TResult2>(Func<TResult, TResult2> selector) =>
new IEnumerableSelectIterator<TSource, TResult2>(_source, CombineSelectors(_selector, selector));
}
/// <summary>
/// An iterator that maps each item of an array.
/// </summary>
/// <typeparam name="TSource">The type of the source array.</typeparam>
/// <typeparam name="TResult">The type of the mapped items.</typeparam>
[DebuggerDisplay("Count = {CountForDebugger}")]
private sealed partial class ArraySelectIterator<TSource, TResult> : Iterator<TResult>
{
private readonly TSource[] _source;
private readonly Func<TSource, TResult> _selector;
public ArraySelectIterator(TSource[] source, Func<TSource, TResult> selector)
{
Debug.Assert(source is not null);
Debug.Assert(selector is not null);
Debug.Assert(source.Length > 0); // Caller should check this beforehand and return a cached result
_source = source;
_selector = selector;
}
private int CountForDebugger => _source.Length;
private protected override Iterator<TResult> Clone() => new ArraySelectIterator<TSource, TResult>(_source, _selector);
public override bool MoveNext()
{
TSource[] source = _source;
int index = _state - 1;
if ((uint)index < (uint)source.Length)
{
_state++;
_current = _selector(source[index]);
return true;
}
Dispose();
return false;
}
public override IEnumerable<TResult2> Select<TResult2>(Func<TResult, TResult2> selector) =>
new ArraySelectIterator<TSource, TResult2>(_source, CombineSelectors(_selector, selector));
}
/// <summary>
/// An iterator that maps each item of a <see cref="List{TSource}"/>.
/// </summary>
/// <typeparam name="TSource">The type of the source list.</typeparam>
/// <typeparam name="TResult">The type of the mapped items.</typeparam>
[DebuggerDisplay("Count = {CountForDebugger}")]
private sealed partial class ListSelectIterator<TSource, TResult> : Iterator<TResult>
{
private readonly List<TSource> _source;
private readonly Func<TSource, TResult> _selector;
private List<TSource>.Enumerator _enumerator;
public ListSelectIterator(List<TSource> source, Func<TSource, TResult> selector)
{
Debug.Assert(source is not null);
Debug.Assert(selector is not null);
_source = source;
_selector = selector;
}
private int CountForDebugger => _source.Count;
private protected override Iterator<TResult> Clone() => new ListSelectIterator<TSource, TResult>(_source, _selector);
public override bool MoveNext()
{
switch (_state)
{
case 1:
_enumerator = _source.GetEnumerator();
_state = 2;
goto case 2;
case 2:
if (_enumerator.MoveNext())
{
_current = _selector(_enumerator.Current);
return true;
}
Dispose();
break;
}
return false;
}
public override IEnumerable<TResult2> Select<TResult2>(Func<TResult, TResult2> selector) =>
new ListSelectIterator<TSource, TResult2>(_source, CombineSelectors(_selector, selector));
}
/// <summary>
/// An iterator that maps each item of an <see cref="IList{TSource}"/>.
/// </summary>
/// <typeparam name="TSource">The type of the source list.</typeparam>
/// <typeparam name="TResult">The type of the mapped items.</typeparam>
[DebuggerDisplay("Count = {CountForDebugger}")]
private sealed partial class IListSelectIterator<TSource, TResult> : Iterator<TResult>
{
private readonly IList<TSource> _source;
private readonly Func<TSource, TResult> _selector;
private IEnumerator<TSource>? _enumerator;
public IListSelectIterator(IList<TSource> source, Func<TSource, TResult> selector)
{
Debug.Assert(source is not null);
Debug.Assert(selector is not null);
_source = source;
_selector = selector;
}
private int CountForDebugger => _source.Count;
private protected override Iterator<TResult> Clone() => new IListSelectIterator<TSource, TResult>(_source, _selector);
public override bool MoveNext()
{
switch (_state)
{
case 1:
_enumerator = _source.GetEnumerator();
_state = 2;
goto case 2;
case 2:
Debug.Assert(_enumerator is not null);
if (_enumerator.MoveNext())
{
_current = _selector(_enumerator.Current);
return true;
}
Dispose();
break;
}
return false;
}
public override void Dispose()
{
if (_enumerator is not null)
{
_enumerator.Dispose();
_enumerator = null;
}
base.Dispose();
}
public override IEnumerable<TResult2> Select<TResult2>(Func<TResult, TResult2> selector) =>
new IListSelectIterator<TSource, TResult2>(_source, CombineSelectors(_selector, selector));
}
}
}