-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathIEnumeratorTExtensions.cs
66 lines (51 loc) · 1.98 KB
/
IEnumeratorTExtensions.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
namespace System.Collections.Generic
{
public static class IEnumeratorTExtensions
{
public static void GetRange<T>(this IEnumerator<T> self, in IntRange range, ICollection<T> output, bool allowDuplicate = true, bool allowNull = false)
{
var start = Math.Min(range.Start, range.End);
var end = Math.Max(range.Start, range.End);
self.GetRange(start, end - start + 1, output, allowDuplicate, allowNull);
}
public static void GetRange<T>(this IEnumerator<T> self, int offset, ICollection<T> output, bool allowDuplicate = true, bool allowNull = false)
=> self.GetRange(offset, -1, output, allowDuplicate, allowNull);
public static void GetRange<T>(this IEnumerator<T> self, int offset, int count, ICollection<T> output, bool allowDuplicate = true, bool allowNull = false)
{
if (self == null || output == null || count == 0)
return;
offset = Math.Max(offset, 0);
var o = 0;
if (count < 0)
{
while (self.MoveNext())
{
if (o < offset)
{
o += 1;
continue;
}
var item = self.Current;
if ((allowNull || item != null) && (allowDuplicate || !output.Contains(item)))
output.Add(item);
}
return;
}
var c = 0;
while (self.MoveNext())
{
if (o < offset)
{
o += 1;
continue;
}
if (c >= count)
break;
var item = self.Current;
if ((allowNull || item != null) && (allowDuplicate || !output.Contains(item)))
output.Add(item);
c += 1;
}
}
}
}