Repository navigation
Expand file tree
/
Copy pathElementAt.cs
More file actions
188 lines (160 loc) · 7.47 KB
/
Copy pathElementAt.cs
File metadata and controls
188 lines (160 loc) · 7.47 KB
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
// 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
{
public static TSource ElementAt<TSource>(this IEnumerable<TSource> source, int index)
{
if (source is null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source);
}
if (source is IList<TSource> list)
{
return list[index];
}
bool found;
TSource? element =
#if !OPTIMIZE_FOR_SIZE
source is Iterator<TSource> iterator ? iterator.TryGetElementAt(index, out found) :
#endif
TryGetElementAtNonIterator(source, index, out found);
if (!found)
{
ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.index);
}
return element!;
}
/// <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 is 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;
}
public static TSource? ElementAtOrDefault<TSource>(this IEnumerable<TSource> source, int index)
{
if (source is null)
{
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.source);
}
return TryGetElementAt(source, index, out _);
}
/// <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 is 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 TSource? TryGetElementAt<TSource>(this IEnumerable<TSource> source, int index, out bool found)
{
if (source is IList<TSource> list)
{
return (found = (uint)index < (uint)list.Count) ?
list[index] :
default;
}
return
#if !OPTIMIZE_FOR_SIZE
source is Iterator<TSource> iterator ? iterator.TryGetElementAt(index, out found) :
#endif
TryGetElementAtNonIterator(source, index, out found);
}
private static TSource? TryGetElementAtNonIterator<TSource>(IEnumerable<TSource> source, int index, out bool found)
{
Debug.Assert(source is not null);
if (index >= 0)
{
using IEnumerator<TSource> e = source.GetEnumerator();
while (e.MoveNext())
{
if (index == 0)
{
found = true;
return e.Current;
}
index--;
}
}
found = false;
return default;
}
private static bool TryGetElementFromEnd<TSource>(IEnumerable<TSource> source, int indexFromEnd, [MaybeNullWhen(false)] out TSource element)
{
Debug.Assert(source is not 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;
}
}
}