25 #include "dCoreStdafx.h"
27 #include "dProfiler.h"
28 #include "dThreadPool.h"
30 #define D_PARALLET_SORT_BATCH_SIZE 1024
33 dInt32 dgBinarySearch(T
const* array, dInt32 elements,
const T& entry, dInt32(*compare) (
const T*
const A,
const T*
const B,
void*
const context),
void*
const context =
nullptr)
36 dInt32 index2 = elements - 1;
38 while ((index2 - index0) > 4)
40 dInt32 index1 = (index0 + index2) >> 1;
41 dInt32 test = compare(&array[index1], &entry, context);
52 index0 = (index0 > 0) ? index0 - 1 : 0;
53 index2 = ((index2 + 1) < elements) ? index2 + 1 : elements;
54 dInt32 index = index0 - 1;
55 for (dInt32 i = index0; i < index2; i++)
57 dInt32 test = compare(&array[i], &entry, context);
72 dInt32 dgBinarySearchIndirect(T**
const array, dInt32 elements,
const T& entry, dInt32(*compare) (
const T*
const A,
const T*
const B,
void*
const context),
void*
const context =
nullptr)
75 dInt32 index2 = elements - 1;
77 while ((index2 - index0) > 4)
79 dInt32 index1 = (index0 + index2) >> 1;
80 dInt32 test = compare(array[index1], &entry, context);
91 index0 = (index0 > 0) ? index0 - 1 : 0;
92 index2 = ((index2 + 1) < elements) ? index2 + 1 : elements;
93 dInt32 index = index0 - 1;
94 for (dInt32 i = index0; i < index2; i++)
96 dInt32 test = compare(array[i], &entry, context);
111 void dgRadixSort(T*
const array, T*
const tmpArray, dInt32 elements, dInt32 radixPass, dInt32(*getRadixKey) (
const T*
const A,
void*
const context),
void*
const context =
nullptr)
113 dInt32 scanCount[256];
114 dInt32 histogram[256][4];
116 dAssert(radixPass >= 1);
117 dAssert(radixPass <= 4);
119 memset(histogram, 0,
sizeof(histogram));
120 for (dInt32 i = 0; i < elements; i++)
122 dInt32 key = getRadixKey(&array[i], context);
123 for (dInt32 j = 0; j < radixPass; j++)
125 dInt32 radix = (key >> (j << 3)) & 0xff;
126 histogram[radix][j] = histogram[radix][j] + 1;
130 for (dInt32 radix = 0; radix < radixPass; radix += 2)
133 for (dInt32 i = 1; i < 256; i++)
135 scanCount[i] = scanCount[i - 1] + histogram[i - 1][radix];
137 dInt32 radixShift = radix << 3;
138 for (dInt32 i = 0; i < elements; i++)
140 dInt32 key = (getRadixKey(&array[i], context) >> radixShift) & 0xff;
141 dInt32 index = scanCount[key];
142 tmpArray[index] = array[i];
143 scanCount[key] = index + 1;
146 if ((radix + 1) < radixPass)
149 for (dInt32 i = 1; i < 256; i++) {
150 scanCount[i] = scanCount[i - 1] + histogram[i - 1][radix + 1];
153 dInt32 radixShift = (radix + 1) << 3;
154 for (dInt32 i = 0; i < elements; i++)
156 dInt32 key = (getRadixKey(&array[i], context) >> radixShift) & 0xff;
157 dInt32 index = scanCount[key];
158 array[index] = tmpArray[i];
159 scanCount[key] = index + 1;
164 memcpy(array, tmpArray, elements *
sizeof(T));
169 for (dInt32 i = 0; i < (elements - 1); i++)
171 dAssert(getRadixKey(&array[i], context) <= getRadixKey(&array[i + 1], context));
177 void dSort(T*
const array, dInt32 elements, dInt32(*compare) (
const T*
const A,
const T*
const B,
void*
const context),
void*
const context =
nullptr)
179 const dInt32 batchSize = 8;
180 dInt32 stack[1024][2];
183 stack[0][1] = elements - 1;
184 dInt32 stackIndex = 1;
188 dInt32 lo = stack[stackIndex][0];
189 dInt32 hi = stack[stackIndex][1];
190 if ((hi - lo) > batchSize)
192 dInt32 mid = (lo + hi) >> 1;
193 if (compare(&array[lo], &array[mid], context) > 0)
195 dSwap(array[lo], array[mid]);
197 if (compare(&array[mid], &array[hi], context) > 0)
199 dSwap(array[mid], array[hi]);
201 if (compare(&array[lo], &array[mid], context) > 0)
203 dSwap(array[lo], array[mid]);
210 while (compare(&array[i], &pivot, context) < 0) i++;
211 while (compare(&array[j], &pivot, context) > 0) j--;
215 dSwap(array[i], array[j]);
223 stack[stackIndex][0] = i;
224 stack[stackIndex][1] = hi;
229 stack[stackIndex][0] = lo;
230 stack[stackIndex][1] = j;
233 dAssert(stackIndex < dInt32(
sizeof(stack) / (2 *
sizeof(stack[0][0]))));
237 dInt32 stride = batchSize + 1;
238 if (elements < stride)
242 for (dInt32 i = 1; i < stride; i++)
244 if (compare(&array[0], &array[i], context) > 0)
246 dSwap(array[0], array[i]);
250 for (dInt32 i = 1; i < elements; i++)
254 for (; compare(&array[j - 1], &tmp, context) > 0; j--)
257 array[j] = array[j - 1];
263 for (dInt32 i = 0; i < (elements - 1); i++)
265 dAssert(compare(&array[i], &array[i + 1], context) <= 0);
271 void dSortIndirect(T**
const array, dInt32 elements, dInt32(*compare) (
const T*
const A,
const T*
const B,
void*
const context),
void*
const context =
nullptr)
274 const dInt32 batchSize = 8;
275 dInt32 stack[1024][2];
278 stack[0][1] = elements - 1;
279 dInt32 stackIndex = 1;
283 dInt32 lo = stack[stackIndex][0];
284 dInt32 hi = stack[stackIndex][1];
285 if ((hi - lo) > batchSize)
287 dInt32 mid = (lo + hi) >> 1;
288 if (compare(array[lo], array[mid], context) > 0)
290 dSwap(array[lo], array[mid]);
292 if (compare(array[mid], array[hi], context) > 0)
294 dSwap(array[mid], array[hi]);
296 if (compare(array[lo], array[mid], context) > 0)
298 dSwap(array[lo], array[mid]);
305 while (compare(array[i], val, context) < 0) i++;
306 while (compare(array[j], val, context) > 0) j--;
310 dSwap(array[i], array[j]);
318 stack[stackIndex][0] = i;
319 stack[stackIndex][1] = hi;
324 stack[stackIndex][0] = lo;
325 stack[stackIndex][1] = j;
328 dAssert(stackIndex < dInt32(
sizeof(stack) / (2 *
sizeof(stack[0][0]))));
332 dInt32 stride = batchSize + 1;
333 if (elements < stride)
337 for (dInt32 i = 1; i < stride; i++)
339 if (compare(array[0], array[i], context) > 0)
341 dSwap(array[0], array[i]);
345 for (dInt32 i = 1; i < elements; i++)
349 for (; compare(array[j - 1], tmp, context) > 0; j--)
352 array[j] = array[j - 1];
358 for (dInt32 i = 0; i < (elements - 1); i++)
360 dAssert(compare(array[i], array[i + 1], context) <= 0);