Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
SortHelper.cs
Go to the documentation of this file.
3
5
6internal abstract class SortHelper<TInputOutput>
7{
8 internal abstract TInputOutput[] Sort();
9}
10internal sealed class SortHelper<TInputOutput, TKey> : SortHelper<TInputOutput>, IDisposable
11{
13
14 private readonly int _partitionCount;
15
16 private readonly int _partitionIndex;
17
19
20 private readonly int[][] _sharedIndices;
21
22 private readonly GrowingArray<TKey>[] _sharedKeys;
23
24 private readonly TInputOutput[][] _sharedValues;
25
26 private readonly Barrier[][] _sharedBarriers;
27
29
30 private readonly IComparer<TKey> _keyComparer;
31
45
47 {
48 int partitionCount = partitions.PartitionCount;
50 int num = 1;
51 int num2 = 0;
52 while (num < partitionCount)
53 {
54 num2++;
55 num <<= 1;
56 }
57 int[][] sharedIndices = new int[partitionCount][];
61 if (partitionCount > 1)
62 {
63 int num3 = 1;
64 for (int i = 0; i < array2.Length; i++)
65 {
66 for (int j = 0; j < array2[i].Length; j++)
67 {
68 if (j % num3 == 0)
69 {
70 array2[i][j] = new Barrier(2);
71 }
72 }
73 num3 *= 2;
74 }
75 }
76 for (int k = 0; k < partitionCount; k++)
77 {
79 }
80 return array;
81 }
82
83 public void Dispose()
84 {
85 if (_partitionIndex != 0)
86 {
87 return;
88 }
89 for (int i = 0; i < _sharedBarriers.Length; i++)
90 {
91 for (int j = 0; j < _sharedBarriers[i].Length; j++)
92 {
94 }
95 }
96 }
97
110
112 {
115 try
116 {
118 TKey currentKey = default(TKey);
119 bool flag = _source.MoveNext(ref currentElement, ref currentKey);
120 if (keys == null)
121 {
122 keys = new GrowingArray<TKey>();
123 }
124 if (!flag)
125 {
126 return;
127 }
128 int num = 0;
129 do
130 {
131 if ((num++ & 0x3F) == 0)
132 {
133 mergedCancellationToken.ThrowIfCancellationRequested();
134 }
135 keys.Add(currentKey);
137 }
138 while (_source.MoveNext(ref currentElement, ref currentKey));
139 }
140 finally
141 {
143 }
144 }
145
147 {
148 int[] array = new int[values.Count];
149 for (int i = 0; i < array.Length; i++)
150 {
151 array[i] = i;
152 }
153 if (array.Length > 1 && ordinalIndexState.IsWorseThan(OrdinalIndexState.Increasing))
154 {
156 }
157 if (_partitionCount == 1)
158 {
159 TInputOutput[] array2 = new TInputOutput[values.Count];
160 for (int j = 0; j < array.Length; j++)
161 {
162 array2[j] = values[array[j]];
163 }
165 }
166 else
167 {
172 }
173 }
174
176 {
178 int num = _sharedBarriers.Length;
179 for (int i = 0; i < num; i++)
180 {
181 bool flag = i == num - 1;
182 int num2 = ComputePartnerIndex(i);
183 if (num2 >= _partitionCount)
184 {
185 continue;
186 }
189 TKey[] internalArray = growingArray.InternalArray;
192 if (_partitionIndex < num2)
193 {
194 int[] array3 = _sharedIndices[num2];
200 int num3 = array2.Length;
201 int num4 = array4.Length;
202 int num5 = num3 + num4;
203 int[] array5 = null;
205 if (!flag)
206 {
207 array5 = new int[num5];
208 }
213 int num6 = (num5 + 1) / 2;
214 int j = 0;
215 int num7 = 0;
216 int num8 = 0;
217 for (; j < num6; j++)
218 {
219 if ((j & 0x3F) == 0)
220 {
221 mergedCancellationToken.ThrowIfCancellationRequested();
222 }
224 {
225 if (flag)
226 {
227 array6[j] = array2[array[num7]];
228 }
229 else
230 {
231 array5[j] = array[num7];
232 }
233 num7++;
234 }
235 else
236 {
237 if (flag)
238 {
240 }
241 else
242 {
243 array5[j] = num3 + array3[num8];
244 }
245 num8++;
246 }
247 }
248 if (!flag && num3 > 0)
249 {
251 }
253 continue;
254 }
259 int[] array9 = _sharedIndices[num2];
262 int num9 = array8.Length;
263 int num10 = array2.Length;
264 int num11 = num9 + num10;
265 int num12 = (num11 + 1) / 2;
266 int num13 = num11 - 1;
267 int num14 = num9 - 1;
268 int num15 = num10 - 1;
269 while (num13 >= num12)
270 {
271 if ((num13 & 0x3F) == 0)
272 {
273 mergedCancellationToken.ThrowIfCancellationRequested();
274 }
276 {
277 if (flag)
278 {
280 }
281 else
282 {
284 }
285 num14--;
286 }
287 else
288 {
289 if (flag)
290 {
292 }
293 else
294 {
296 }
297 num15--;
298 }
299 num13--;
300 }
301 if (!flag && array2.Length != 0)
302 {
303 growingArray2.CopyFrom(internalArray, array2.Length);
304 Array.Copy(array2, 0, array10, num9, array2.Length);
305 }
307 break;
308 }
309 }
310
311 private int ComputePartnerIndex(int phase)
312 {
313 int num = 1 << phase;
314 return _partitionIndex + ((_partitionIndex % (num * 2) == 0) ? num : (-num));
315 }
316
317 private void QuickSort(int left, int right, TKey[] keys, int[] indices, CancellationToken cancelToken)
318 {
319 if (right - left > 63)
320 {
321 cancelToken.ThrowIfCancellationRequested();
322 }
323 do
324 {
325 int num = left;
326 int num2 = right;
327 int num3 = indices[num + (num2 - num >> 1)];
328 TKey y = keys[num3];
329 while (true)
330 {
331 if (_keyComparer.Compare(keys[indices[num]], y) < 0)
332 {
333 num++;
334 continue;
335 }
336 while (_keyComparer.Compare(keys[indices[num2]], y) > 0)
337 {
338 num2--;
339 }
340 if (num > num2)
341 {
342 break;
343 }
344 if (num < num2)
345 {
346 int num4 = indices[num];
347 indices[num] = indices[num2];
348 indices[num2] = num4;
349 }
350 num++;
351 num2--;
352 if (num > num2)
353 {
354 break;
355 }
356 }
357 if (num2 - left <= right - num)
358 {
359 if (left < num2)
360 {
362 }
363 left = num;
364 }
365 else
366 {
367 if (num < right)
368 {
369 QuickSort(num, right, keys, indices, cancelToken);
370 }
371 right = num2;
372 }
373 }
374 while (left < right);
375 }
376}
static unsafe void Copy(Array sourceArray, Array destinationArray, int length)
Definition Array.cs:624
void CopyTo(KeyValuePair< TKey, TValue >[] array, int index)
void Add(TKey key, TValue value)
bool MoveNext([MaybeNullWhen(false)][AllowNull] ref TElement currentElement, [AllowNull] ref TKey currentKey)
void BuildKeysFromSource(ref GrowingArray< TKey > keys, ref List< TInputOutput > values)
static SortHelper< TInputOutput, TKey >[] GenerateSortHelpers(PartitionedStream< TInputOutput, TKey > partitions, QueryTaskGroupState groupState)
Definition SortHelper.cs:46
readonly TInputOutput[][] _sharedValues
Definition SortHelper.cs:24
readonly int[][] _sharedIndices
Definition SortHelper.cs:20
override TInputOutput[] Sort()
Definition SortHelper.cs:98
void QuickSortIndicesInPlace(GrowingArray< TKey > keys, List< TInputOutput > values, OrdinalIndexState ordinalIndexState)
readonly Barrier[][] _sharedBarriers
Definition SortHelper.cs:26
readonly OrdinalIndexState _indexState
Definition SortHelper.cs:28
readonly QueryOperatorEnumerator< TInputOutput, TKey > _source
Definition SortHelper.cs:12
SortHelper(QueryOperatorEnumerator< TInputOutput, TKey > source, int partitionCount, int partitionIndex, QueryTaskGroupState groupState, int[][] sharedIndices, OrdinalIndexState indexState, IComparer< TKey > keyComparer, GrowingArray< TKey >[] sharedkeys, TInputOutput[][] sharedValues, Barrier[][] sharedBarriers)
Definition SortHelper.cs:32
void QuickSort(int left, int right, TKey[] keys, int[] indices, CancellationToken cancelToken)
readonly IComparer< TKey > _keyComparer
Definition SortHelper.cs:30
readonly GrowingArray< TKey >[] _sharedKeys
Definition SortHelper.cs:22
readonly QueryTaskGroupState _groupState
Definition SortHelper.cs:18
static byte Min(byte val1, byte val2)
Definition Math.cs:912