Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
ArraySortHelper.cs
Go to the documentation of this file.
4
6
7[TypeDependency("System.Collections.Generic.GenericArraySortHelper`1")]
8internal sealed class ArraySortHelper<T> : IArraySortHelper<T>
9{
11
13
23
25 {
26 try
27 {
28 if (comparer == null)
29 {
30 comparer = Comparer<T>.Default;
31 }
33 }
35 {
37 }
38 catch (Exception e)
39 {
40 ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_IComparerFailed, e);
41 }
42 }
43
45 {
46 try
47 {
48 if (comparer == null)
49 {
50 comparer = Comparer<T>.Default;
51 }
53 }
54 catch (Exception e)
55 {
56 ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_IComparerFailed, e);
57 return 0;
58 }
59 }
60
61 internal static void Sort(Span<T> keys, Comparison<T> comparer)
62 {
63 try
64 {
66 }
68 {
70 }
71 catch (Exception e)
72 {
73 ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_IComparerFailed, e);
74 }
75 }
76
77 internal static int InternalBinarySearch(T[] array, int index, int length, T value, IComparer<T> comparer)
78 {
79 int num = index;
80 int num2 = index + length - 1;
81 while (num <= num2)
82 {
83 int num3 = num + (num2 - num >> 1);
84 int num4 = comparer.Compare(array[num3], value);
85 if (num4 == 0)
86 {
87 return num3;
88 }
89 if (num4 < 0)
90 {
91 num = num3 + 1;
92 }
93 else
94 {
95 num2 = num3 - 1;
96 }
97 }
98 return ~num;
99 }
100
101 private static void SwapIfGreater(Span<T> keys, Comparison<T> comparer, int i, int j)
102 {
103 if (comparer(keys[i], keys[j]) > 0)
104 {
105 T val = keys[i];
106 keys[i] = keys[j];
107 keys[j] = val;
108 }
109 }
110
111 [MethodImpl(MethodImplOptions.AggressiveInlining)]
112 private static void Swap(Span<T> a, int i, int j)
113 {
114 T val = a[i];
115 a[i] = a[j];
116 a[j] = val;
117 }
118
120 {
121 if (keys.Length > 1)
122 {
123 IntroSort(keys, 2 * (BitOperations.Log2((uint)keys.Length) + 1), comparer);
124 }
125 }
126
128 {
129 int num = keys.Length;
130 while (num > 1)
131 {
132 if (num <= 16)
133 {
134 switch (num)
135 {
136 case 2:
138 break;
139 case 3:
143 break;
144 default:
145 InsertionSort(keys.Slice(0, num), comparer);
146 break;
147 }
148 break;
149 }
150 if (depthLimit == 0)
151 {
152 HeapSort(keys.Slice(0, num), comparer);
153 break;
154 }
155 depthLimit--;
156 int num2 = PickPivotAndPartition(keys.Slice(0, num), comparer);
157 Span<T> span = keys;
158 IntroSort(span[(num2 + 1)..num], depthLimit, comparer);
159 num = num2;
160 }
161 }
162
164 {
165 int num = keys.Length - 1;
166 int num2 = num >> 1;
168 SwapIfGreater(keys, comparer, 0, num);
170 T val = keys[num2];
171 Swap(keys, num2, num - 1);
172 int num3 = 0;
173 int num4 = num - 1;
174 while (num3 < num4)
175 {
176 while (comparer(keys[++num3], val) < 0)
177 {
178 }
179 while (comparer(val, keys[--num4]) < 0)
180 {
181 }
182 if (num3 >= num4)
183 {
184 break;
185 }
186 Swap(keys, num3, num4);
187 }
188 if (num3 != num - 1)
189 {
190 Swap(keys, num3, num - 1);
191 }
192 return num3;
193 }
194
196 {
197 int length = keys.Length;
198 for (int num = length >> 1; num >= 1; num--)
199 {
201 }
202 for (int num2 = length; num2 > 1; num2--)
203 {
204 Swap(keys, 0, num2 - 1);
205 DownHeap(keys, 1, num2 - 1, comparer);
206 }
207 }
208
209 private static void DownHeap(Span<T> keys, int i, int n, Comparison<T> comparer)
210 {
211 T val = keys[i - 1];
212 while (i <= n >> 1)
213 {
214 int num = 2 * i;
215 if (num < n && comparer(keys[num - 1], keys[num]) < 0)
216 {
217 num++;
218 }
219 if (comparer(val, keys[num - 1]) >= 0)
220 {
221 break;
222 }
223 keys[i - 1] = keys[num - 1];
224 i = num;
225 }
226 keys[i - 1] = val;
227 }
228
230 {
231 for (int i = 0; i < keys.Length - 1; i++)
232 {
233 T val = keys[i + 1];
234 int num = i;
235 while (num >= 0 && comparer(val, keys[num]) < 0)
236 {
237 keys[num + 1] = keys[num];
238 num--;
239 }
240 keys[num + 1] = val;
241 }
242 }
243}
244[TypeDependency("System.Collections.Generic.GenericArraySortHelper`2")]
245internal sealed class ArraySortHelper<TKey, TValue> : IArraySortHelper<TKey, TValue>
246{
248
250
260
262 {
263 try
264 {
266 }
268 {
270 }
271 catch (Exception e)
272 {
273 ThrowHelper.ThrowInvalidOperationException(ExceptionResource.InvalidOperation_IComparerFailed, e);
274 }
275 }
276
278 {
279 if (comparer.Compare(keys[i], keys[j]) > 0)
280 {
281 TKey val = keys[i];
282 keys[i] = keys[j];
283 keys[j] = val;
284 TValue val2 = values[i];
285 values[i] = values[j];
286 values[j] = val2;
287 }
288 }
289
290 [MethodImpl(MethodImplOptions.AggressiveInlining)]
291 private static void Swap(Span<TKey> keys, Span<TValue> values, int i, int j)
292 {
293 TKey val = keys[i];
294 keys[i] = keys[j];
295 keys[j] = val;
296 TValue val2 = values[i];
297 values[i] = values[j];
298 values[j] = val2;
299 }
300
302 {
303 if (keys.Length > 1)
304 {
305 IntroSort(keys, values, 2 * (BitOperations.Log2((uint)keys.Length) + 1), comparer);
306 }
307 }
308
310 {
311 int num = keys.Length;
312 while (num > 1)
313 {
314 if (num <= 16)
315 {
316 switch (num)
317 {
318 case 2:
320 break;
321 case 3:
325 break;
326 default:
327 InsertionSort(keys.Slice(0, num), values.Slice(0, num), comparer);
328 break;
329 }
330 break;
331 }
332 if (depthLimit == 0)
333 {
334 HeapSort(keys.Slice(0, num), values.Slice(0, num), comparer);
335 break;
336 }
337 depthLimit--;
338 int num2 = PickPivotAndPartition(keys.Slice(0, num), values.Slice(0, num), comparer);
340 Span<TKey> keys2 = span[(num2 + 1)..num];
343 num = num2;
344 }
345 }
346
348 {
349 int num = keys.Length - 1;
350 int num2 = num >> 1;
354 TKey val = keys[num2];
355 Swap(keys, values, num2, num - 1);
356 int num3 = 0;
357 int num4 = num - 1;
358 while (num3 < num4)
359 {
360 while (comparer.Compare(keys[++num3], val) < 0)
361 {
362 }
363 while (comparer.Compare(val, keys[--num4]) < 0)
364 {
365 }
366 if (num3 >= num4)
367 {
368 break;
369 }
371 }
372 if (num3 != num - 1)
373 {
374 Swap(keys, values, num3, num - 1);
375 }
376 return num3;
377 }
378
380 {
381 int length = keys.Length;
382 for (int num = length >> 1; num >= 1; num--)
383 {
385 }
386 for (int num2 = length; num2 > 1; num2--)
387 {
388 Swap(keys, values, 0, num2 - 1);
389 DownHeap(keys, values, 1, num2 - 1, comparer);
390 }
391 }
392
394 {
395 TKey val = keys[i - 1];
396 TValue val2 = values[i - 1];
397 while (i <= n >> 1)
398 {
399 int num = 2 * i;
400 if (num < n && comparer.Compare(keys[num - 1], keys[num]) < 0)
401 {
402 num++;
403 }
404 if (comparer.Compare(val, keys[num - 1]) >= 0)
405 {
406 break;
407 }
408 keys[i - 1] = keys[num - 1];
409 values[i - 1] = values[num - 1];
410 i = num;
411 }
412 keys[i - 1] = val;
413 values[i - 1] = val2;
414 }
415
417 {
418 for (int i = 0; i < keys.Length - 1; i++)
419 {
420 TKey val = keys[i + 1];
421 TValue val2 = values[i + 1];
422 int num = i;
423 while (num >= 0 && comparer.Compare(val, keys[num]) < 0)
424 {
425 keys[num + 1] = keys[num];
426 values[num + 1] = values[num];
427 num--;
428 }
429 keys[num + 1] = val;
430 values[num + 1] = val2;
431 }
432 }
433}
void Sort(Span< T > keys, IComparer< T > comparer)
static void IntroSort(Span< T > keys, int depthLimit, Comparison< T > comparer)
static void SwapIfGreaterWithValues(Span< TKey > keys, Span< TValue > values, IComparer< TKey > comparer, int i, int j)
static void HeapSort(Span< TKey > keys, Span< TValue > values, IComparer< TKey > comparer)
void Sort(Span< TKey > keys, Span< TValue > values, IComparer< TKey > comparer)
static void HeapSort(Span< T > keys, Comparison< T > comparer)
int BinarySearch(T[] array, int index, int length, T value, IComparer< T > comparer)
static void Swap(Span< TKey > keys, Span< TValue > values, int i, int j)
static void IntrospectiveSort(Span< TKey > keys, Span< TValue > values, IComparer< TKey > comparer)
static readonly IArraySortHelper< T > s_defaultArraySortHelper
static void IntrospectiveSort(Span< T > keys, Comparison< T > comparer)
static IArraySortHelper< T > CreateArraySortHelper()
static int PickPivotAndPartition(Span< TKey > keys, Span< TValue > values, IComparer< TKey > comparer)
static void Sort(Span< T > keys, Comparison< T > comparer)
static void Swap(Span< T > a, int i, int j)
static void IntroSort(Span< TKey > keys, Span< TValue > values, int depthLimit, IComparer< TKey > comparer)
static int PickPivotAndPartition(Span< T > keys, Comparison< T > comparer)
static void InsertionSort(Span< TKey > keys, Span< TValue > values, IComparer< TKey > comparer)
static void DownHeap(Span< TKey > keys, Span< TValue > values, int i, int n, IComparer< TKey > comparer)
static void SwapIfGreater(Span< T > keys, Comparison< T > comparer, int i, int j)
static IArraySortHelper< TKey, TValue > CreateArraySortHelper()
static void InsertionSort(Span< T > keys, Comparison< T > comparer)
static int InternalBinarySearch(T[] array, int index, int length, T value, IComparer< T > comparer)
static void DownHeap(Span< T > keys, int i, int n, Comparison< T > comparer)
static int Log2(uint value)
static void ThrowArgumentException_BadComparer(object comparer)
static void ThrowInvalidOperationException()
static unsafe object CreateInstanceForAnotherGenericParameter([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] RuntimeType type, RuntimeType genericParameter)