Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
OrderedEnumerable.cs
Go to the documentation of this file.
3
4namespace System.Linq;
5
6internal abstract class OrderedEnumerable<TElement> : IPartition<TElement>, IIListProvider<TElement>, IEnumerable<TElement>, IEnumerable, IOrderedEnumerable<TElement>
7{
9
10 public TElement[] ToArray()
11 {
13 int count = buffer._count;
14 if (count == 0)
15 {
16 return buffer._items;
17 }
18 TElement[] array = new TElement[count];
19 int[] array2 = SortedMap(buffer);
20 for (int i = 0; i != array.Length; i++)
21 {
22 array[i] = buffer._items[array2[i]];
23 }
24 return array;
25 }
26
28 {
30 int count = buffer._count;
32 if (count > 0)
33 {
34 int[] array = SortedMap(buffer);
35 for (int i = 0; i != count; i++)
36 {
37 list.Add(buffer._items[array[i]]);
38 }
39 }
40 return list;
41 }
42
43 public int GetCount(bool onlyIfCheap)
44 {
46 {
47 return iIListProvider.GetCount(onlyIfCheap);
48 }
50 {
51 return -1;
52 }
53 return _source.Count();
54 }
55
56 internal TElement[] ToArray(int minIdx, int maxIdx)
57 {
59 int count = buffer._count;
60 if (count <= minIdx)
61 {
62 return Array.Empty<TElement>();
63 }
64 if (count <= maxIdx)
65 {
66 maxIdx = count - 1;
67 }
68 if (minIdx == maxIdx)
69 {
70 return new TElement[1] { GetEnumerableSorter().ElementAt(buffer._items, count, minIdx) };
71 }
73 TElement[] array2 = new TElement[maxIdx - minIdx + 1];
74 int num = 0;
75 while (minIdx <= maxIdx)
76 {
77 array2[num] = buffer._items[array[minIdx]];
78 num++;
79 minIdx++;
80 }
81 return array2;
82 }
83
85 {
87 int count = buffer._count;
88 if (count <= minIdx)
89 {
90 return new List<TElement>();
91 }
92 if (count <= maxIdx)
93 {
94 maxIdx = count - 1;
95 }
96 if (minIdx == maxIdx)
97 {
98 return new List<TElement>(1) { GetEnumerableSorter().ElementAt(buffer._items, count, minIdx) };
99 }
102 while (minIdx <= maxIdx)
103 {
104 list.Add(buffer._items[array[minIdx]]);
105 minIdx++;
106 }
107 return list;
108 }
109
110 internal int GetCount(int minIdx, int maxIdx, bool onlyIfCheap)
111 {
113 if (count <= 0)
114 {
115 return count;
116 }
117 if (count <= minIdx)
118 {
119 return 0;
120 }
121 return ((count <= maxIdx) ? count : (maxIdx + 1)) - minIdx;
122 }
123
125 {
126 return new OrderedPartition<TElement>(this, count, int.MaxValue);
127 }
128
130 {
131 return new OrderedPartition<TElement>(this, 0, count - 1);
132 }
133
134 public TElement TryGetElementAt(int index, out bool found)
135 {
136 if (index == 0)
137 {
138 return TryGetFirst(out found);
139 }
140 if (index > 0)
141 {
143 int count = buffer._count;
144 if (index < count)
145 {
146 found = true;
148 }
149 }
150 found = false;
151 return default(TElement);
152 }
153
154 public TElement TryGetFirst(out bool found)
155 {
157 using IEnumerator<TElement> enumerator = _source.GetEnumerator();
158 if (!enumerator.MoveNext())
159 {
160 found = false;
161 return default(TElement);
162 }
163 TElement val = enumerator.Current;
164 comparer.SetElement(val);
165 while (enumerator.MoveNext())
166 {
167 TElement current = enumerator.Current;
168 if (comparer.Compare(current, cacheLower: true) < 0)
169 {
170 val = current;
171 }
172 }
173 found = true;
174 return val;
175 }
176
177 public TElement TryGetLast(out bool found)
178 {
179 using IEnumerator<TElement> enumerator = _source.GetEnumerator();
180 if (!enumerator.MoveNext())
181 {
182 found = false;
183 return default(TElement);
184 }
186 TElement val = enumerator.Current;
187 comparer.SetElement(val);
188 while (enumerator.MoveNext())
189 {
190 TElement current = enumerator.Current;
191 if (comparer.Compare(current, cacheLower: false) >= 0)
192 {
193 val = current;
194 }
195 }
196 found = true;
197 return val;
198 }
199
200 public TElement TryGetLast(int minIdx, int maxIdx, out bool found)
201 {
203 int count = buffer._count;
204 if (minIdx >= count)
205 {
206 found = false;
207 return default(TElement);
208 }
209 found = true;
210 if (maxIdx >= count - 1)
211 {
212 return Last(buffer);
213 }
215 }
216
217 private TElement Last(Buffer<TElement> buffer)
218 {
220 TElement[] items = buffer._items;
221 int count = buffer._count;
222 TElement val = items[0];
223 comparer.SetElement(val);
224 for (int i = 1; i != count; i++)
225 {
226 TElement val2 = items[i];
227 if (comparer.Compare(val2, cacheLower: false) >= 0)
228 {
229 val = val2;
230 }
231 }
232 return val;
233 }
234
239
241 {
242 return GetEnumerableSorter().Sort(buffer._items, buffer._count);
243 }
244
246 {
247 return GetEnumerableSorter().Sort(buffer._items, buffer._count, minIdx, maxIdx);
248 }
249
251 {
253 if (buffer._count > 0)
254 {
255 int[] map = SortedMap(buffer);
256 for (int i = 0; i < buffer._count; i++)
257 {
258 yield return buffer._items[map[i]];
259 }
260 }
261 }
262
264 {
266 int count = buffer._count;
267 if (count <= minIdx)
268 {
269 yield break;
270 }
271 if (count <= maxIdx)
272 {
273 maxIdx = count - 1;
274 }
275 if (minIdx == maxIdx)
276 {
278 yield break;
279 }
280 int[] map = SortedMap(buffer, minIdx, maxIdx);
281 while (minIdx <= maxIdx)
282 {
283 yield return buffer._items[map[minIdx]];
284 int num = minIdx + 1;
285 minIdx = num;
286 }
287 }
288
293
295
297 {
298 return GetComparer(null);
299 }
300
302
312
314 {
316 using IEnumerator<TElement> enumerator = _source.GetEnumerator();
317 while (enumerator.MoveNext())
318 {
319 TElement val = enumerator.Current;
320 if (!predicate(val))
321 {
322 continue;
323 }
324 comparer.SetElement(val);
325 while (enumerator.MoveNext())
326 {
327 TElement current = enumerator.Current;
328 if (predicate(current) && comparer.Compare(current, cacheLower: false) >= 0)
329 {
330 val = current;
331 }
332 }
333 found = true;
334 return val;
335 }
336 found = false;
337 return default(TElement);
338 }
339}
340internal sealed class OrderedEnumerable<TElement, TKey> : OrderedEnumerable<TElement>
341{
343
345
346 private readonly IComparer<TKey> _comparer;
347
348 private readonly bool _descending;
349
366
381
391}
int[] Sort(TElement[] elements, int count)
TElement ElementAt(TElement[] elements, int count, int idx)
int[] SortedMap(Buffer< TElement > buffer, int minIdx, int maxIdx)
readonly OrderedEnumerable< TElement > _parent
List< TElement > ToList(int minIdx, int maxIdx)
int[] SortedMap(Buffer< TElement > buffer)
int GetCount(int minIdx, int maxIdx, bool onlyIfCheap)
CachingComparer< TElement > GetComparer()
IEnumerator< TElement > GetEnumerator(int minIdx, int maxIdx)
int GetCount(bool onlyIfCheap)
EnumerableSorter< TElement > GetEnumerableSorter()
CachingComparer< TElement > GetComparer(CachingComparer< TElement > childComparer)
OrderedEnumerable(IEnumerable< TElement > source, Func< TElement, TKey > keySelector, IComparer< TKey > comparer, bool descending, OrderedEnumerable< TElement > parent)
OrderedEnumerable(IEnumerable< TElement > source)
TElement TryGetLast(Func< TElement, bool > predicate, out bool found)
TElement[] ToArray(int minIdx, int maxIdx)
readonly Func< TElement, TKey > _keySelector
IEnumerator< TElement > GetEnumerator()
TElement TryGetLast(int minIdx, int maxIdx, out bool found)
TElement TryGetElementAt(int index, out bool found)
TElement TryGetFirst(out bool found)
override EnumerableSorter< TElement > GetEnumerableSorter(EnumerableSorter< TElement > next)
readonly IComparer< TKey > _comparer
IEnumerable< TElement > _source
EnumerableSorter< TElement > GetEnumerableSorter(EnumerableSorter< TElement > next)
IPartition< TElement > Skip(int count)
IPartition< TElement > Take(int count)
TElement Last(Buffer< TElement > buffer)
override CachingComparer< TElement > GetComparer(CachingComparer< TElement > childComparer)
TElement TryGetLast(out bool found)
static void ThrowArgumentNullException(ExceptionArgument argument)
Definition ThrowHelper.cs:8
static StringComparer CurrentCulture
new IEnumerator< T > GetEnumerator()