/
Take.cs
261 lines (224 loc) · 10 KB
/
Take.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
// 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;
namespace System.Linq
{
public static partial class Enumerable
{
public static IEnumerable<TSource> Take<TSource>(this IEnumerable<TSource> source, int count)
{
if (source == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source);
}
return count <= 0 ?
Empty<TSource>() :
TakeIterator<TSource>(source, count);
}
/// <summary>Returns a specified range of contiguous elements from a sequence.</summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <param name="source">The sequence to return elements from.</param>
/// <param name="range">The range of elements to return, which has start and end indexes either from the start or the end.</param>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
/// <returns>An <see cref="IEnumerable{T}" /> that contains the specified <paramref name="range" /> of elements from the <paramref name="source" /> sequence.</returns>
/// <remarks>
/// <para>This method is implemented by using deferred execution. The immediate return value is an object that stores all the information that is required to perform the action. The query represented by this method is not executed until the object is enumerated either by calling its `GetEnumerator` method directly or by using `foreach` in Visual C# or `For Each` in Visual Basic.</para>
/// <para><see cref="O:Enumerable.Take" /> enumerates <paramref name="source" /> and yields elements whose indices belong to the specified <paramref name="range"/>.</para>
/// </remarks>
public static IEnumerable<TSource> Take<TSource>(this IEnumerable<TSource> source, Range range)
{
if (source == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source);
}
Index start = range.Start;
Index end = range.End;
bool isStartIndexFromEnd = start.IsFromEnd;
bool isEndIndexFromEnd = end.IsFromEnd;
int startIndex = start.Value;
int endIndex = end.Value;
Debug.Assert(startIndex >= 0);
Debug.Assert(endIndex >= 0);
if (isStartIndexFromEnd)
{
if (startIndex == 0 || (isEndIndexFromEnd && endIndex >= startIndex))
{
return Empty<TSource>();
}
}
else if (!isEndIndexFromEnd)
{
return startIndex >= endIndex
? Empty<TSource>()
: TakeRangeIterator(source, startIndex, endIndex);
}
return TakeRangeFromEndIterator(source, isStartIndexFromEnd, startIndex, isEndIndexFromEnd, endIndex);
}
private static IEnumerable<TSource> TakeRangeFromEndIterator<TSource>(IEnumerable<TSource> source, bool isStartIndexFromEnd, int startIndex, bool isEndIndexFromEnd, int endIndex)
{
Debug.Assert(source != null);
Debug.Assert(isStartIndexFromEnd || isEndIndexFromEnd);
Debug.Assert(isStartIndexFromEnd
? startIndex > 0 && (!isEndIndexFromEnd || startIndex > endIndex)
: startIndex >= 0 && (isEndIndexFromEnd || startIndex < endIndex));
// Attempt to extract the count of the source enumerator,
// in order to convert fromEnd indices to regular indices.
// Enumerable counts can change over time, so it is very
// important that this check happens at enumeration time;
// do not move it outside of the iterator method.
if (source.TryGetNonEnumeratedCount(out int count))
{
startIndex = CalculateStartIndex(isStartIndexFromEnd, startIndex, count);
endIndex = CalculateEndIndex(isEndIndexFromEnd, endIndex, count);
if (startIndex < endIndex)
{
foreach (TSource element in TakeRangeIterator(source, startIndex, endIndex))
{
yield return element;
}
}
yield break;
}
Queue<TSource> queue;
if (isStartIndexFromEnd)
{
// TakeLast compat: enumerator should be disposed before yielding the first element.
using (IEnumerator<TSource> e = source.GetEnumerator())
{
if (!e.MoveNext())
{
yield break;
}
queue = new Queue<TSource>();
queue.Enqueue(e.Current);
count = 1;
while (e.MoveNext())
{
if (count < startIndex)
{
queue.Enqueue(e.Current);
++count;
}
else
{
do
{
queue.Dequeue();
queue.Enqueue(e.Current);
checked { ++count; }
} while (e.MoveNext());
break;
}
}
Debug.Assert(queue.Count == Math.Min(count, startIndex));
}
startIndex = CalculateStartIndex(isStartIndexFromEnd: true, startIndex, count);
endIndex = CalculateEndIndex(isEndIndexFromEnd, endIndex, count);
Debug.Assert(endIndex - startIndex <= queue.Count);
for (int rangeIndex = startIndex; rangeIndex < endIndex; rangeIndex++)
{
yield return queue.Dequeue();
}
}
else
{
Debug.Assert(!isStartIndexFromEnd && isEndIndexFromEnd);
// SkipLast compat: the enumerator should be disposed at the end of the enumeration.
using IEnumerator<TSource> e = source.GetEnumerator();
count = 0;
while (count < startIndex && e.MoveNext())
{
++count;
}
if (count == startIndex)
{
queue = new Queue<TSource>();
while (e.MoveNext())
{
if (queue.Count == endIndex)
{
do
{
queue.Enqueue(e.Current);
yield return queue.Dequeue();
} while (e.MoveNext());
break;
}
else
{
queue.Enqueue(e.Current);
}
}
}
}
static int CalculateStartIndex(bool isStartIndexFromEnd, int startIndex, int count) =>
Math.Max(0, isStartIndexFromEnd ? count - startIndex : startIndex);
static int CalculateEndIndex(bool isEndIndexFromEnd, int endIndex, int count) =>
Math.Min(count, isEndIndexFromEnd ? count - endIndex : endIndex);
}
public static IEnumerable<TSource> TakeWhile<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
if (source == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source);
}
if (predicate == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.predicate);
}
return TakeWhileIterator(source, predicate);
}
private static IEnumerable<TSource> TakeWhileIterator<TSource>(IEnumerable<TSource> source, Func<TSource, bool> predicate)
{
foreach (TSource element in source)
{
if (!predicate(element))
{
break;
}
yield return element;
}
}
public static IEnumerable<TSource> TakeWhile<TSource>(this IEnumerable<TSource> source, Func<TSource, int, bool> predicate)
{
if (source == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source);
}
if (predicate == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.predicate);
}
return TakeWhileIterator(source, predicate);
}
private static IEnumerable<TSource> TakeWhileIterator<TSource>(IEnumerable<TSource> source, Func<TSource, int, bool> predicate)
{
int index = -1;
foreach (TSource element in source)
{
checked
{
index++;
}
if (!predicate(element, index))
{
break;
}
yield return element;
}
}
public static IEnumerable<TSource> TakeLast<TSource>(this IEnumerable<TSource> source, int count)
{
if (source == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source);
}
return count <= 0 ?
Empty<TSource>() :
TakeRangeFromEndIterator(source,
isStartIndexFromEnd: true, startIndex: count,
isEndIndexFromEnd: true, endIndex: 0);
}
}
}