forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ElementAt.cs
212 lines (185 loc) · 10.8 KB
/
ElementAt.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
// 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;
namespace System.Linq
{
public static partial class Enumerable
{
/// <summary>Returns the element at a specified index in a sequence.</summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <param name="source">An <see cref="IEnumerable{T}" /> to return an element from.</param>
/// <param name="index">The zero-based index of the element to retrieve.</param>
/// <returns>The element at the specified position in the source sequence.</returns>
/// <exception cref="System.ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
/// <exception cref="System.ArgumentOutOfRangeException"><paramref name="index" /> is less than 0 or greater than or equal to the number of elements in <paramref name="source" />.</exception>
/// <remarks>
/// <para>If the type of <paramref name="source" /> implements <see cref="IList{T}" />, that implementation is used to obtain the element at the specified index. Otherwise, this method obtains the specified element.</para>
/// <para>This method throws an exception if <paramref name="index" /> is out of range. To instead return a default value when the specified index is out of range, use the <see cref="O:Enumerable.ElementAtOrDefault" /> method.</para>
/// </remarks>
/// <example>The following code example demonstrates how to use <see cref="O:Enumerable.ElementAt" /> to return an element at a specific position.
/// :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Linq.Enumerable/CS/enumerable.cs" interactive="try-dotnet-method" id="Snippet28":::
/// :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Linq.Enumerable/VB/Enumerable.vb" id="Snippet28":::</example>
public static TSource ElementAt<TSource>(this IEnumerable<TSource> source, int index)
{
if (source == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source);
}
if (source is IPartition<TSource> partition)
{
TSource? element = partition.TryGetElementAt(index, out bool found);
if (found)
{
return element!;
}
}
else if (source is IList<TSource> list)
{
return list[index];
}
else if (TryGetElement(source, index, out TSource? element))
{
return element;
}
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index);
return default;
}
/// <summary>Returns the element at a specified index in a sequence.</summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <param name="source">An <see cref="IEnumerable{T}" /> to return an element from.</param>
/// <param name="index">The index of the element to retrieve, which is either from the start or the end.</param>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="index" /> is outside the bounds of the <paramref name="source" /> sequence.</exception>
/// <returns>The element at the specified position in the <paramref name="source" /> sequence.</returns>
/// <remarks>
/// <para>If the type of <paramref name="source" /> implements <see cref="IList{T}" />, that implementation is used to obtain the element at the specified index. Otherwise, this method obtains the specified element.</para>
/// <para>This method throws an exception if <paramref name="index" /> is out of range. To instead return a default value when the specified index is out of range, use the <see cref="O:Enumerable.ElementAtOrDefault" /> method.</para>
/// </remarks>
public static TSource ElementAt<TSource>(this IEnumerable<TSource> source, Index index)
{
if (source == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source);
}
if (!index.IsFromEnd)
{
return source.ElementAt(index.Value);
}
if (source.TryGetNonEnumeratedCount(out int count))
{
return source.ElementAt(count - index.Value);
}
if (!TryGetElementFromEnd(source, index.Value, out TSource? element))
{
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index);
}
return element;
}
/// <summary>Returns the element at a specified index in a sequence or a default value if the index is out of range.</summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <param name="source">An <see cref="IEnumerable{T}" /> to return an element from.</param>
/// <param name="index">The zero-based index of the element to retrieve.</param>
/// <returns><see langword="default" />(<typeparamref name="TSource" />) if the index is outside the bounds of the source sequence; otherwise, the element at the specified position in the source sequence.</returns>
/// <exception cref="System.ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
/// <remarks>
/// <para>If the type of <paramref name="source" /> implements <see cref="IList{T}" />, that implementation is used to obtain the element at the specified index. Otherwise, this method obtains the specified element.</para>
/// <para>The default value for reference and nullable types is <see langword="null" />.</para>
/// </remarks>
/// <example>The following code example demonstrates how to use <see cref="O:Enumerable.ElementAtOrDefault" />. This example uses an index that is outside the bounds of the array.
/// :::code language="csharp" source="~/samples/snippets/csharp/VS_Snippets_CLR_System/system.Linq.Enumerable/CS/enumerable.cs" interactive="try-dotnet-method" id="Snippet29":::
/// :::code language="vb" source="~/samples/snippets/visualbasic/VS_Snippets_CLR_System/system.Linq.Enumerable/VB/Enumerable.vb" id="Snippet29":::</example>
public static TSource? ElementAtOrDefault<TSource>(this IEnumerable<TSource> source, int index)
{
if (source == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source);
}
if (source is IPartition<TSource> partition)
{
return partition.TryGetElementAt(index, out bool _);
}
if (source is IList<TSource> list)
{
return index >= 0 && index < list.Count ? list[index] : default;
}
TryGetElement(source, index, out TSource? element);
return element;
}
/// <summary>Returns the element at a specified index in a sequence or a default value if the index is out of range.</summary>
/// <typeparam name="TSource">The type of the elements of <paramref name="source" />.</typeparam>
/// <param name="source">An <see cref="IEnumerable{T}" /> to return an element from.</param>
/// <param name="index">The index of the element to retrieve, which is either from the start or the end.</param>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
/// <returns><see langword="default" /> if <paramref name="index" /> is outside the bounds of the <paramref name="source" /> sequence; otherwise, the element at the specified position in the <paramref name="source" /> sequence.</returns>
/// <remarks>
/// <para>If the type of <paramref name="source" /> implements <see cref="IList{T}" />, that implementation is used to obtain the element at the specified index. Otherwise, this method obtains the specified element.</para>
/// <para>The default value for reference and nullable types is <see langword="null" />.</para>
/// </remarks>
public static TSource? ElementAtOrDefault<TSource>(this IEnumerable<TSource> source, Index index)
{
if (source == null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source);
}
if (!index.IsFromEnd)
{
return source.ElementAtOrDefault(index.Value);
}
if (source.TryGetNonEnumeratedCount(out int count))
{
return source.ElementAtOrDefault(count - index.Value);
}
TryGetElementFromEnd(source, index.Value, out TSource? element);
return element;
}
private static bool TryGetElement<TSource>(IEnumerable<TSource> source, int index, [MaybeNullWhen(false)] out TSource element)
{
Debug.Assert(source != null);
if (index >= 0)
{
using IEnumerator<TSource> e = source.GetEnumerator();
while (e.MoveNext())
{
if (index == 0)
{
element = e.Current;
return true;
}
index--;
}
}
element = default;
return false;
}
private static bool TryGetElementFromEnd<TSource>(IEnumerable<TSource> source, int indexFromEnd, [MaybeNullWhen(false)] out TSource element)
{
Debug.Assert(source != null);
if (indexFromEnd > 0)
{
using IEnumerator<TSource> e = source.GetEnumerator();
if (e.MoveNext())
{
Queue<TSource> queue = new();
queue.Enqueue(e.Current);
while (e.MoveNext())
{
if (queue.Count == indexFromEnd)
{
queue.Dequeue();
}
queue.Enqueue(e.Current);
}
if (queue.Count == indexFromEnd)
{
element = queue.Dequeue();
return true;
}
}
}
element = default;
return false;
}
}
}