-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
Select.SpeedOpt.cs
852 lines (711 loc) · 27.4 KB
/
Select.SpeedOpt.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using static System.Linq.Utilities;
namespace System.Linq
{
public static partial class Enumerable
{
static partial void CreateSelectIPartitionIterator<TResult, TSource>(
Func<TSource, TResult> selector, IPartition<TSource> partition, ref IEnumerable<TResult>? result) // TODO-NULLABLE: https://github.com/dotnet/roslyn/issues/38327
{
result = partition is EmptyPartition<TSource> ?
EmptyPartition<TResult>.Instance :
new SelectIPartitionIterator<TSource, TResult>(partition, selector);
}
private sealed partial class SelectEnumerableIterator<TSource, TResult> : IIListProvider<TResult>
{
public TResult[] ToArray()
{
var builder = new LargeArrayBuilder<TResult>(initialize: true);
foreach (TSource item in _source)
{
builder.Add(_selector(item));
}
return builder.ToArray();
}
public List<TResult> ToList()
{
var list = new List<TResult>();
foreach (TSource item in _source)
{
list.Add(_selector(item));
}
return list;
}
public int GetCount(bool onlyIfCheap)
{
// In case someone uses Count() to force evaluation of
// the selector, run it provided `onlyIfCheap` is false.
if (onlyIfCheap)
{
return -1;
}
int count = 0;
foreach (TSource item in _source)
{
_selector(item);
checked
{
count++;
}
}
return count;
}
}
private sealed partial class SelectArrayIterator<TSource, TResult> : IPartition<TResult>
{
public TResult[] ToArray()
{
// See assert in constructor.
// Since _source should never be empty, we don't check for 0/return Array.Empty.
Debug.Assert(_source.Length > 0);
var results = new TResult[_source.Length];
for (int i = 0; i < results.Length; i++)
{
results[i] = _selector(_source[i]);
}
return results;
}
public List<TResult> ToList()
{
TSource[] source = _source;
var results = new List<TResult>(source.Length);
for (int i = 0; i < source.Length; i++)
{
results.Add(_selector(source[i]));
}
return results;
}
public int GetCount(bool onlyIfCheap)
{
// In case someone uses Count() to force evaluation of
// the selector, run it provided `onlyIfCheap` is false.
if (!onlyIfCheap)
{
foreach (TSource item in _source)
{
_selector(item);
}
}
return _source.Length;
}
public IPartition<TResult> Skip(int count)
{
Debug.Assert(count > 0);
if (count >= _source.Length)
{
return EmptyPartition<TResult>.Instance;
}
return new SelectListPartitionIterator<TSource, TResult>(_source, _selector, count, int.MaxValue);
}
public IPartition<TResult> Take(int count)
{
Debug.Assert(count > 0);
return count >= _source.Length ?
(IPartition<TResult>)this :
new SelectListPartitionIterator<TSource, TResult>(_source, _selector, 0, count - 1);
}
[return: MaybeNull]
public TResult TryGetElementAt(int index, out bool found)
{
if (unchecked((uint)index < (uint)_source.Length))
{
found = true;
return _selector(_source[index]);
}
found = false;
return default!;
}
public TResult TryGetFirst(out bool found)
{
Debug.Assert(_source.Length > 0); // See assert in constructor
found = true;
return _selector(_source[0]);
}
public TResult TryGetLast(out bool found)
{
Debug.Assert(_source.Length > 0); // See assert in constructor
found = true;
return _selector(_source[_source.Length - 1]);
}
}
private sealed partial class SelectRangeIterator<TResult> : Iterator<TResult>, IPartition<TResult>
{
private readonly int _start;
private readonly int _end;
private readonly Func<int, TResult> _selector;
public SelectRangeIterator(int start, int end, Func<int, TResult> selector)
{
Debug.Assert(start < end);
Debug.Assert((uint)(end - start) <= (uint)int.MaxValue);
Debug.Assert(selector != null);
_start = start;
_end = end;
_selector = selector;
}
public override Iterator<TResult> Clone() =>
new SelectRangeIterator<TResult>(_start, _end, _selector);
public override bool MoveNext()
{
if (_state < 1 || _state == (_end - _start + 1))
{
Dispose();
return false;
}
int index = _state++ - 1;
Debug.Assert(_start < _end - index);
_current = _selector(_start + index);
return true;
}
public override IEnumerable<TResult2> Select<TResult2>(Func<TResult, TResult2> selector) =>
new SelectRangeIterator<TResult2>(_start, _end, CombineSelectors(_selector, selector));
public TResult[] ToArray()
{
var results = new TResult[_end - _start];
int srcIndex = _start;
for (int i = 0; i < results.Length; i++)
{
results[i] = _selector(srcIndex++);
}
return results;
}
public List<TResult> ToList()
{
var results = new List<TResult>(_end - _start);
for (int i = _start; i != _end; i++)
{
results.Add(_selector(i));
}
return results;
}
public int GetCount(bool onlyIfCheap)
{
// In case someone uses Count() to force evaluation of the selector,
// run it provided `onlyIfCheap` is false.
if (!onlyIfCheap)
{
for (int i = _start; i != _end; i++)
{
_selector(i);
}
}
return _end - _start;
}
public IPartition<TResult> Skip(int count)
{
Debug.Assert(count > 0);
if (count >= (_end - _start))
{
return EmptyPartition<TResult>.Instance;
}
return new SelectRangeIterator<TResult>(_start + count, _end, _selector);
}
public IPartition<TResult> Take(int count)
{
Debug.Assert(count > 0);
if (count >= (_end - _start))
{
return this;
}
return new SelectRangeIterator<TResult>(_start, _start + count, _selector);
}
[return: MaybeNull]
public TResult TryGetElementAt(int index, out bool found)
{
if ((uint)index < (uint)(_end - _start))
{
found = true;
return _selector(_start + index);
}
found = false;
return default!;
}
public TResult TryGetFirst(out bool found)
{
Debug.Assert(_end > _start);
found = true;
return _selector(_start);
}
public TResult TryGetLast(out bool found)
{
Debug.Assert(_end > _start);
found = true;
return _selector(_end - 1);
}
}
private sealed partial class SelectListIterator<TSource, TResult> : IPartition<TResult>
{
public TResult[] ToArray()
{
int count = _source.Count;
if (count == 0)
{
return Array.Empty<TResult>();
}
var results = new TResult[count];
for (int i = 0; i < results.Length; i++)
{
results[i] = _selector(_source[i]);
}
return results;
}
public List<TResult> ToList()
{
int count = _source.Count;
var results = new List<TResult>(count);
for (int i = 0; i < count; i++)
{
results.Add(_selector(_source[i]));
}
return results;
}
public int GetCount(bool onlyIfCheap)
{
// In case someone uses Count() to force evaluation of
// the selector, run it provided `onlyIfCheap` is false.
int count = _source.Count;
if (!onlyIfCheap)
{
for (int i = 0; i < count; i++)
{
_selector(_source[i]);
}
}
return count;
}
public IPartition<TResult> Skip(int count)
{
Debug.Assert(count > 0);
return new SelectListPartitionIterator<TSource, TResult>(_source, _selector, count, int.MaxValue);
}
public IPartition<TResult> Take(int count)
{
Debug.Assert(count > 0);
return new SelectListPartitionIterator<TSource, TResult>(_source, _selector, 0, count - 1);
}
[return: MaybeNull]
public TResult TryGetElementAt(int index, out bool found)
{
if (unchecked((uint)index < (uint)_source.Count))
{
found = true;
return _selector(_source[index]);
}
found = false;
return default!;
}
[return: MaybeNull]
public TResult TryGetFirst(out bool found)
{
if (_source.Count != 0)
{
found = true;
return _selector(_source[0]);
}
found = false;
return default!;
}
[return: MaybeNull]
public TResult TryGetLast(out bool found)
{
int len = _source.Count;
if (len != 0)
{
found = true;
return _selector(_source[len - 1]);
}
found = false;
return default!;
}
}
private sealed partial class SelectIListIterator<TSource, TResult> : IPartition<TResult>
{
public TResult[] ToArray()
{
int count = _source.Count;
if (count == 0)
{
return Array.Empty<TResult>();
}
var results = new TResult[count];
for (int i = 0; i < results.Length; i++)
{
results[i] = _selector(_source[i]);
}
return results;
}
public List<TResult> ToList()
{
int count = _source.Count;
var results = new List<TResult>(count);
for (int i = 0; i < count; i++)
{
results.Add(_selector(_source[i]));
}
return results;
}
public int GetCount(bool onlyIfCheap)
{
// In case someone uses Count() to force evaluation of
// the selector, run it provided `onlyIfCheap` is false.
int count = _source.Count;
if (!onlyIfCheap)
{
for (int i = 0; i < count; i++)
{
_selector(_source[i]);
}
}
return count;
}
public IPartition<TResult> Skip(int count)
{
Debug.Assert(count > 0);
return new SelectListPartitionIterator<TSource, TResult>(_source, _selector, count, int.MaxValue);
}
public IPartition<TResult> Take(int count)
{
Debug.Assert(count > 0);
return new SelectListPartitionIterator<TSource, TResult>(_source, _selector, 0, count - 1);
}
[return: MaybeNull]
public TResult TryGetElementAt(int index, out bool found)
{
if (unchecked((uint)index < (uint)_source.Count))
{
found = true;
return _selector(_source[index]);
}
found = false;
return default!;
}
[return: MaybeNull]
public TResult TryGetFirst(out bool found)
{
if (_source.Count != 0)
{
found = true;
return _selector(_source[0]);
}
found = false;
return default!;
}
[return: MaybeNull]
public TResult TryGetLast(out bool found)
{
int len = _source.Count;
if (len != 0)
{
found = true;
return _selector(_source[len - 1]);
}
found = false;
return default!;
}
}
/// <summary>
/// An iterator that maps each item of an <see cref="IPartition{TSource}"/>.
/// </summary>
/// <typeparam name="TSource">The type of the source partition.</typeparam>
/// <typeparam name="TResult">The type of the mapped items.</typeparam>
private sealed class SelectIPartitionIterator<TSource, TResult> : Iterator<TResult>, IPartition<TResult>
{
private readonly IPartition<TSource> _source;
private readonly Func<TSource, TResult> _selector;
private IEnumerator<TSource>? _enumerator;
public SelectIPartitionIterator(IPartition<TSource> source, Func<TSource, TResult> selector)
{
Debug.Assert(source != null);
Debug.Assert(selector != null);
_source = source;
_selector = selector;
}
public override Iterator<TResult> Clone() =>
new SelectIPartitionIterator<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 != null);
if (_enumerator.MoveNext())
{
_current = _selector(_enumerator.Current);
return true;
}
Dispose();
break;
}
return false;
}
public override void Dispose()
{
if (_enumerator != null)
{
_enumerator.Dispose();
_enumerator = null;
}
base.Dispose();
}
public override IEnumerable<TResult2> Select<TResult2>(Func<TResult, TResult2> selector) =>
new SelectIPartitionIterator<TSource, TResult2>(_source, CombineSelectors(_selector, selector));
public IPartition<TResult> Skip(int count)
{
Debug.Assert(count > 0);
return new SelectIPartitionIterator<TSource, TResult>(_source.Skip(count), _selector);
}
public IPartition<TResult> Take(int count)
{
Debug.Assert(count > 0);
return new SelectIPartitionIterator<TSource, TResult>(_source.Take(count), _selector);
}
[return: MaybeNull]
public TResult TryGetElementAt(int index, out bool found)
{
bool sourceFound;
TSource input = _source.TryGetElementAt(index, out sourceFound);
found = sourceFound;
return sourceFound ? _selector(input!) : default!;
}
[return: MaybeNull]
public TResult TryGetFirst(out bool found)
{
bool sourceFound;
TSource input = _source.TryGetFirst(out sourceFound);
found = sourceFound;
return sourceFound ? _selector(input!) : default!;
}
[return: MaybeNull]
public TResult TryGetLast(out bool found)
{
bool sourceFound;
TSource input = _source.TryGetLast(out sourceFound);
found = sourceFound;
return sourceFound ? _selector(input!) : default!;
}
private TResult[] LazyToArray()
{
Debug.Assert(_source.GetCount(onlyIfCheap: true) == -1);
var builder = new LargeArrayBuilder<TResult>(initialize: true);
foreach (TSource input in _source)
{
builder.Add(_selector(input));
}
return builder.ToArray();
}
private TResult[] PreallocatingToArray(int count)
{
Debug.Assert(count > 0);
Debug.Assert(count == _source.GetCount(onlyIfCheap: true));
TResult[] array = new TResult[count];
int index = 0;
foreach (TSource input in _source)
{
array[index] = _selector(input);
++index;
}
return array;
}
public TResult[] ToArray()
{
int count = _source.GetCount(onlyIfCheap: true);
return count switch
{
-1 => LazyToArray(),
0 => Array.Empty<TResult>(),
_ => PreallocatingToArray(count),
};
}
public List<TResult> ToList()
{
int count = _source.GetCount(onlyIfCheap: true);
List<TResult> list;
switch (count)
{
case -1:
list = new List<TResult>();
break;
case 0:
return new List<TResult>();
default:
list = new List<TResult>(count);
break;
}
foreach (TSource input in _source)
{
list.Add(_selector(input));
}
return list;
}
public int GetCount(bool onlyIfCheap)
{
if (!onlyIfCheap)
{
// In case someone uses Count() to force evaluation of
// the selector, run it provided `onlyIfCheap` is false.
int count = 0;
foreach (TSource item in _source)
{
_selector(item);
checked { count++; }
}
return count;
}
return _source.GetCount(onlyIfCheap);
}
}
/// <summary>
/// An iterator that maps each item of part 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 = {Count}")]
private sealed class SelectListPartitionIterator<TSource, TResult> : Iterator<TResult>, IPartition<TResult>
{
private readonly IList<TSource> _source;
private readonly Func<TSource, TResult> _selector;
private readonly int _minIndexInclusive;
private readonly int _maxIndexInclusive;
public SelectListPartitionIterator(IList<TSource> source, Func<TSource, TResult> selector, int minIndexInclusive, int maxIndexInclusive)
{
Debug.Assert(source != null);
Debug.Assert(selector != null);
Debug.Assert(minIndexInclusive >= 0);
Debug.Assert(minIndexInclusive <= maxIndexInclusive);
_source = source;
_selector = selector;
_minIndexInclusive = minIndexInclusive;
_maxIndexInclusive = maxIndexInclusive;
}
public override Iterator<TResult> Clone() =>
new SelectListPartitionIterator<TSource, TResult>(_source, _selector, _minIndexInclusive, _maxIndexInclusive);
public override bool MoveNext()
{
// _state - 1 represents the zero-based index into the list.
// Having a separate field for the index would be more readable. However, we save it
// into _state with a bias to minimize field size of the iterator.
int index = _state - 1;
if (unchecked((uint)index <= (uint)(_maxIndexInclusive - _minIndexInclusive) && index < _source.Count - _minIndexInclusive))
{
_current = _selector(_source[_minIndexInclusive + index]);
++_state;
return true;
}
Dispose();
return false;
}
public override IEnumerable<TResult2> Select<TResult2>(Func<TResult, TResult2> selector) =>
new SelectListPartitionIterator<TSource, TResult2>(_source, CombineSelectors(_selector, selector), _minIndexInclusive, _maxIndexInclusive);
public IPartition<TResult> Skip(int count)
{
Debug.Assert(count > 0);
int minIndex = _minIndexInclusive + count;
return (uint)minIndex > (uint)_maxIndexInclusive ? EmptyPartition<TResult>.Instance : new SelectListPartitionIterator<TSource, TResult>(_source, _selector, minIndex, _maxIndexInclusive);
}
public IPartition<TResult> Take(int count)
{
Debug.Assert(count > 0);
int maxIndex = _minIndexInclusive + count - 1;
return (uint)maxIndex >= (uint)_maxIndexInclusive ? this : new SelectListPartitionIterator<TSource, TResult>(_source, _selector, _minIndexInclusive, maxIndex);
}
[return: MaybeNull]
public TResult TryGetElementAt(int index, out bool found)
{
if ((uint)index <= (uint)(_maxIndexInclusive - _minIndexInclusive) && index < _source.Count - _minIndexInclusive)
{
found = true;
return _selector(_source[_minIndexInclusive + index]);
}
found = false;
return default!;
}
[return: MaybeNull]
public TResult TryGetFirst(out bool found)
{
if (_source.Count > _minIndexInclusive)
{
found = true;
return _selector(_source[_minIndexInclusive]);
}
found = false;
return default!;
}
[return: MaybeNull]
public TResult TryGetLast(out bool found)
{
int lastIndex = _source.Count - 1;
if (lastIndex >= _minIndexInclusive)
{
found = true;
return _selector(_source[Math.Min(lastIndex, _maxIndexInclusive)]);
}
found = false;
return default!;
}
private int Count
{
get
{
int count = _source.Count;
if (count <= _minIndexInclusive)
{
return 0;
}
return Math.Min(count - 1, _maxIndexInclusive) - _minIndexInclusive + 1;
}
}
public TResult[] ToArray()
{
int count = Count;
if (count == 0)
{
return Array.Empty<TResult>();
}
TResult[] array = new TResult[count];
for (int i = 0, curIdx = _minIndexInclusive; i != array.Length; ++i, ++curIdx)
{
array[i] = _selector(_source[curIdx]);
}
return array;
}
public List<TResult> ToList()
{
int count = Count;
if (count == 0)
{
return new List<TResult>();
}
List<TResult> list = new List<TResult>(count);
int end = _minIndexInclusive + count;
for (int i = _minIndexInclusive; i != end; ++i)
{
list.Add(_selector(_source[i]));
}
return list;
}
public int GetCount(bool onlyIfCheap)
{
// In case someone uses Count() to force evaluation of
// the selector, run it provided `onlyIfCheap` is false.
int count = Count;
if (!onlyIfCheap)
{
int end = _minIndexInclusive + count;
for (int i = _minIndexInclusive; i != end; ++i)
{
_selector(_source[i]);
}
}
return count;
}
}
}
}