This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/
Range.SpeedOpt.cs
90 lines (75 loc) · 2.47 KB
/
Range.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
// 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;
namespace System.Linq
{
public static partial class Enumerable
{
private sealed partial class RangeIterator : IPartition<int>
{
public override IEnumerable<TResult> Select<TResult>(Func<int, TResult> selector)
{
return new SelectRangeIterator<TResult>(_start, _end, selector);
}
public int[] ToArray()
{
int[] array = new int[_end - _start];
int cur = _start;
for (int i = 0; i != array.Length; ++i)
{
array[i] = cur;
++cur;
}
return array;
}
public List<int> ToList()
{
List<int> list = new List<int>(_end - _start);
for (int cur = _start; cur != _end; cur++)
{
list.Add(cur);
}
return list;
}
public int GetCount(bool onlyIfCheap) => unchecked(_end - _start);
public IPartition<int> Skip(int count)
{
if (count >= _end - _start)
{
return EmptyPartition<int>.Instance;
}
return new RangeIterator(_start + count, _end - _start - count);
}
public IPartition<int> Take(int count)
{
int curCount = _end - _start;
if (count >= curCount)
{
return this;
}
return new RangeIterator(_start, count);
}
public int TryGetElementAt(int index, out bool found)
{
if (unchecked((uint)index < (uint)(_end - _start)))
{
found = true;
return _start + index;
}
found = false;
return 0;
}
public int TryGetFirst(out bool found)
{
found = true;
return _start;
}
public int TryGetLast(out bool found)
{
found = true;
return _end - 1;
}
}
}
}