ReactOS 0.4.16-dev-927-g467dec4
qsort.cpp File Reference
#include <corecrt_internal.h>
#include <search.h>
Include dependency graph for qsort.cpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define BEGIN_PRAGMA_OPTIMIZE_DISABLE(flags, bug, reason)    __pragma(optimize(flags, off))
 
#define BEGIN_PRAGMA_OPTIMIZE_ENABLE(flags, bug, reason)    __pragma(optimize(flags, on))
 
#define END_PRAGMA_OPTIMIZE()    __pragma(optimize("", on))
 
#define __fileDECL   __cdecl
 
#define __COMPARE(context, p1, p2)   comp(p1, p2)
 
#define __SHORTSORT(lo, hi, width, comp, context)   shortsort(lo, hi, width, comp);
 
#define _QSORT_SWAP_DEFINED
 
#define CUTOFF   8
 
#define STKSIZ   (8 * sizeof(void*) - 2)
 

Functions

static _CRT_SECURITYSAFECRITICAL_ATTRIBUTE void __fileDECL swap (_Inout_updates_(width) char *a, _Inout_updates_(width) char *b, size_t width)
 
static _CRT_SECURITYSAFECRITICAL_ATTRIBUTE void __fileDECL shortsort (_Inout_updates_(hi - lo+1) char *lo, _Inout_updates_(width) char *hi, size_t const width, int(__fileDECL *comp)(void const *, void const *))
 
_CRT_SECURITYSAFECRITICAL_ATTRIBUTE void __fileDECL qsort (void *const base, size_t const num, size_t const width, int(__fileDECL *const comp)(void const *, void const *))
 

Macro Definition Documentation

◆ __COMPARE

#define __COMPARE (   context,
  p1,
  p2 
)    comp(p1, p2)

Definition at line 40 of file qsort.cpp.

◆ __fileDECL

#define __fileDECL   __cdecl

Definition at line 31 of file qsort.cpp.

◆ __SHORTSORT

#define __SHORTSORT (   lo,
  hi,
  width,
  comp,
  context 
)    shortsort(lo, hi, width, comp);

Definition at line 41 of file qsort.cpp.

◆ _QSORT_SWAP_DEFINED

#define _QSORT_SWAP_DEFINED

Definition at line 48 of file qsort.cpp.

◆ BEGIN_PRAGMA_OPTIMIZE_DISABLE

#define BEGIN_PRAGMA_OPTIMIZE_DISABLE (   flags,
  bug,
  reason 
)     __pragma(optimize(flags, off))

Definition at line 14 of file qsort.cpp.

◆ BEGIN_PRAGMA_OPTIMIZE_ENABLE

#define BEGIN_PRAGMA_OPTIMIZE_ENABLE (   flags,
  bug,
  reason 
)     __pragma(optimize(flags, on))

Definition at line 16 of file qsort.cpp.

◆ CUTOFF

#define CUTOFF   8

Definition at line 131 of file qsort.cpp.

◆ END_PRAGMA_OPTIMIZE

#define END_PRAGMA_OPTIMIZE ( )     __pragma(optimize("", on))

Definition at line 18 of file qsort.cpp.

◆ STKSIZ

#define STKSIZ   (8 * sizeof(void*) - 2)

Definition at line 138 of file qsort.cpp.

Function Documentation

◆ qsort()

Definition at line 164 of file qsort.cpp.

171{
172 _VALIDATE_RETURN_VOID(base != nullptr || num == 0, EINVAL);
174 _VALIDATE_RETURN_VOID(comp != nullptr, EINVAL);
175
176 // A stack for saving the sub-arrays yet to be processed:
177 char* lostk[STKSIZ];
178 char* histk[STKSIZ];
179 int stkptr = 0;
180
181 if (num < 2)
182 return; // Nothing to do:
183
184 // The ends of the sub-array currently being sorted (note that 'hi' points
185 // to the last element, not one-past-the-end):
186 char* lo = static_cast<char*>(base);
187 char* hi = static_cast<char*>(base) + width * (num-1);
188
189 // This entry point is for pseudo-recursion calling: setting
190 // lo and hi and jumping to here is like recursion, but stkptr is
191 // preserved, locals aren't, so we preserve stuff on the stack.
192recurse:
193
194 // The number of elements in the sub-array currently being sorted:
195 size_t const size = (hi - lo) / width + 1;
196
197 // Below a certain size, it is faster to use a O(n^2) sorting method:
198 if (size <= CUTOFF)
199 {
200 __SHORTSORT(lo, hi, width, comp, context);
201 }
202 else
203 {
204 // First we pick a partitioning element. The efficiency of the
205 // algorithm demands that we find one that is approximately the median
206 // of the values, but also that we select one fast. We choose the
207 // median of the first, middle, and last elements, to avoid bad
208 // performance in the face of already sorted data, or data that is made
209 // up of multiple sorted runs appended together. Testing shows that a
210 // median-of-three algorithm provides better performance than simply
211 // picking the middle element for the latter case.
212
213 // Find the middle element:
214 char* mid = lo + (size / 2) * width;
215
216 // Sort the first, middle, last elements into order:
217 if (__COMPARE(context, lo, mid) > 0)
218 swap(lo, mid, width);
219
220 if (__COMPARE(context, lo, hi) > 0)
221 swap(lo, hi, width);
222
223 if (__COMPARE(context, mid, hi) > 0)
224 swap(mid, hi, width);
225
226 // We now wish to partition the array into three pieces, one consisting
227 // of elements <= partition element, one of elements equal to the
228 // partition element, and one of elements > than it. This is done
229 // below; comments indicate conditions established at every step.
230
231 char* loguy = lo;
232 char* higuy = hi;
233
234 // Note that higuy decreases and loguy increases on every iteration,
235 // so loop must terminate.
236 for (;;)
237 {
238 // lo <= loguy < hi, lo < higuy <= hi,
239 // A[i] <= A[mid] for lo <= i <= loguy,
240 // A[i] > A[mid] for higuy <= i < hi,
241 // A[hi] >= A[mid]
242
243 // The doubled loop is to avoid calling comp(mid,mid), since some
244 // existing comparison funcs don't work when passed the same
245 // value for both pointers.
246
247 if (mid > loguy)
248 {
249 do
250 {
251 loguy += width;
252 }
253 while (loguy < mid && __COMPARE(context, loguy, mid) <= 0);
254 }
255 if (mid <= loguy)
256 {
257 do
258 {
259 loguy += width;
260 }
261 while (loguy <= hi && __COMPARE(context, loguy, mid) <= 0);
262 }
263
264 // lo < loguy <= hi+1, A[i] <= A[mid] for lo <= i < loguy,
265 // either loguy > hi or A[loguy] > A[mid]
266
267 do
268 {
269 higuy -= width;
270 }
271 while (higuy > mid && __COMPARE(context, higuy, mid) > 0);
272
273 // lo <= higuy < hi, A[i] > A[mid] for higuy < i < hi,
274 // either higuy == lo or A[higuy] <= A[mid]
275
276 if (higuy < loguy)
277 break;
278
279 // if loguy > hi or higuy == lo, then we would have exited, so
280 // A[loguy] > A[mid], A[higuy] <= A[mid],
281 // loguy <= hi, higuy > lo
282
283 swap(loguy, higuy, width);
284
285 // If the partition element was moved, follow it. Only need
286 // to check for mid == higuy, since before the swap,
287 // A[loguy] > A[mid] implies loguy != mid.
288
289 if (mid == higuy)
290 mid = loguy;
291
292 // A[loguy] <= A[mid], A[higuy] > A[mid]; so condition at top
293 // of loop is re-established
294 }
295
296 // A[i] <= A[mid] for lo <= i < loguy,
297 // A[i] > A[mid] for higuy < i < hi,
298 // A[hi] >= A[mid]
299 // higuy < loguy
300 // implying:
301 // higuy == loguy-1
302 // or higuy == hi - 1, loguy == hi + 1, A[hi] == A[mid]
303
304 // Find adjacent elements equal to the partition element. The
305 // doubled loop is to avoid calling comp(mid,mid), since some
306 // existing comparison funcs don't work when passed the same value
307 // for both pointers.
308
309 higuy += width;
310 if (mid < higuy)
311 {
312 do
313 {
314 higuy -= width;
315 }
316 while (higuy > mid && __COMPARE(context, higuy, mid) == 0);
317 }
318 if (mid >= higuy)
319 {
320 do
321 {
322 higuy -= width;
323 }
324 while (higuy > lo && __COMPARE(context, higuy, mid) == 0);
325 }
326
327 // OK, now we have the following:
328 // higuy < loguy
329 // lo <= higuy <= hi
330 // A[i] <= A[mid] for lo <= i <= higuy
331 // A[i] == A[mid] for higuy < i < loguy
332 // A[i] > A[mid] for loguy <= i < hi
333 // A[hi] >= A[mid] */
334
335 // We've finished the partition, now we want to sort the subarrays
336 // [lo, higuy] and [loguy, hi].
337 // We do the smaller one first to minimize stack usage.
338 // We only sort arrays of length 2 or more.
339
340 if (higuy - lo >= hi - loguy)
341 {
342 if (lo < higuy)
343 {
344 // Save the big recursion for later:
345 lostk[stkptr] = lo;
346 histk[stkptr] = higuy;
347 ++stkptr;
348 }
349
350 if (loguy < hi)
351 {
352 // Do the small recursion:
353 lo = loguy;
354 goto recurse;
355 }
356 }
357 else
358 {
359 if (loguy < hi)
360 {
361 // Save the big recursion for later:
362 lostk[stkptr] = loguy;
363 histk[stkptr] = hi;
364 ++stkptr;
365 }
366
367 if (lo < higuy)
368 {
369 // Do the small recursion:
370 hi = higuy;
371 goto recurse;
372 }
373 }
374 }
375
376 // We have sorted the array, except for any pending sorts on the stack.
377 // Check if there are any, and sort them:
378 --stkptr;
379 if (stkptr >= 0)
380 {
381 // Pop sub-array from the stack:
382 lo = lostk[stkptr];
383 hi = histk[stkptr];
384 goto recurse;
385 }
386 else
387 {
388 // Otherwise, all sub-arrays have been sorted:
389 return;
390 }
391}
#define EINVAL
Definition: acclib.h:90
#define _VALIDATE_RETURN_VOID(expr, errorcode)
GLint GLint GLsizei width
Definition: gl.h:1546
GLsizeiptr size
Definition: glext.h:5919
GLuint GLuint num
Definition: glext.h:9618
#define swap(a, b)
Definition: qsort.c:63
#define CUTOFF
Definition: qsort.cpp:131
#define STKSIZ
Definition: qsort.cpp:138
#define __SHORTSORT(lo, hi, width, comp, context)
Definition: qsort.cpp:41
#define __COMPARE(context, p1, p2)
Definition: qsort.cpp:40
Definition: http.c:7252

◆ shortsort()

static _CRT_SECURITYSAFECRITICAL_ATTRIBUTE void __fileDECL shortsort ( _Inout_updates_(hi - lo+1) char lo,
_Inout_updates_(width) char hi,
size_t const  width,
int(__fileDECL *comp)(void const *, void const *)   
)
static

Definition at line 83 of file qsort.cpp.

90{
91 // Note: in assertions below, i and j are alway inside original bound of
92 // array to sort.
93
94 // Reentrancy diligence: Save (and unset) global-state mode to the stack before making callout to 'compare'
95 __crt_state_management::scoped_global_state_reset saved_state;
96
97 while (hi > lo)
98 {
99 // A[i] <= A[j] for i <= j, j > hi
100 char* max = lo;
101 for (char* p = lo+width; p <= hi; p += width)
102 {
103 // A[i] <= A[max] for lo <= i < p
104 if (__COMPARE(context, p, max) > 0)
105 {
106 max = p;
107 }
108 // A[i] <= A[max] for lo <= i <= p
109 }
110
111 // A[i] <= A[max] for lo <= i <= hi
112
113 swap(max, hi, width);
114
115 // A[i] <= A[hi] for i <= hi, so A[i] <= A[j] for i <= j, j >= hi
116
117 hi -= width;
118
119 // A[i] <= A[j] for i <= j, j > hi, loop top condition established
120 }
121
122 // A[i] <= A[j] for i <= j, j > lo, which implies A[i] <= A[j] for i < j,
123 // so array is sorted
124}
GLfloat GLfloat p
Definition: glext.h:8902
#define max(a, b)
Definition: svc.c:63

◆ swap()

Definition at line 50 of file qsort.cpp.

51{
52 if (a != b)
53 {
54 // Do the swap one character at a time to avoid potential alignment
55 // problems:
56 while (width--)
57 {
58 char const tmp = *a;
59 *a++ = *b;
60 *b++ = tmp;
61 }
62 }
63}
GLboolean GLboolean GLboolean b
Definition: glext.h:6204
GLboolean GLboolean GLboolean GLboolean a
Definition: glext.h:6204
#define a
Definition: ke_i.h:78
#define b
Definition: ke_i.h:79