Reactos
at master 396 lines 12 kB view raw
1// 2// qsort.cpp 3// 4// Copyright (c) Microsoft Corporation. All rights reserved. 5// 6// Defines qsort(), a routine for sorting arrays. 7// 8#include <corecrt_internal.h> 9#include <search.h> 10 11 12/* Temporarily define optimization macros (to be removed by the build team: RsmqblCompiler alias) */ 13#if !defined(BEGIN_PRAGMA_OPTIMIZE_DISABLE) 14#define BEGIN_PRAGMA_OPTIMIZE_DISABLE(flags, bug, reason) \ 15 __pragma(optimize(flags, off)) 16#define BEGIN_PRAGMA_OPTIMIZE_ENABLE(flags, bug, reason) \ 17 __pragma(optimize(flags, on)) 18#define END_PRAGMA_OPTIMIZE() \ 19 __pragma(optimize("", on)) 20#endif 21 22 23// Always compile this module for speed, not size 24BEGIN_PRAGMA_OPTIMIZE_ENABLE("t", MSFT:4499497, "This file is performance-critical and should always be optimized for speed") 25 26 27 28#ifdef _M_CEE 29 #define __fileDECL __clrcall 30#else 31 #define __fileDECL __cdecl 32#endif 33 34 35 36#ifdef __USE_CONTEXT 37 #define __COMPARE(context, p1, p2) comp(context, p1, p2) 38 #define __SHORTSORT(lo, hi, width, comp, context) shortsort_s(lo, hi, width, comp, context); 39#else 40 #define __COMPARE(context, p1, p2) comp(p1, p2) 41 #define __SHORTSORT(lo, hi, width, comp, context) shortsort(lo, hi, width, comp); 42#endif 43 44 45 46// Swaps the objects of size 'width' that are pointed to by 'a' and 'b' 47#ifndef _QSORT_SWAP_DEFINED 48#define _QSORT_SWAP_DEFINED 49_CRT_SECURITYSAFECRITICAL_ATTRIBUTE 50static void __fileDECL swap(_Inout_updates_(width) char* a, _Inout_updates_(width) char* b, size_t width) 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} 64#endif // _QSORT_SWAP_DEFINED 65 66 67 68// An insertion sort for sorting short arrays. Sorts the sub-array of elements 69// between lo and hi (inclusive). Assumes lo < hi. lo and hi are pointers to 70// the first and last elements in the range to be sorted (note: hi does not 71// point one-past-the-end). The comp is a comparer with the same behavior as 72// specified for qsort. 73_CRT_SECURITYSAFECRITICAL_ATTRIBUTE 74#ifdef __USE_CONTEXT 75static void __fileDECL shortsort_s( 76 _Inout_updates_(hi - lo + 1) char* lo, 77 _Inout_updates_(width) char* hi, 78 size_t const width, 79 int (__fileDECL* comp)(void*, void const*, void const*), 80 void* const context 81 ) 82#else // __USE_CONTEXT 83static void __fileDECL shortsort( 84 _Inout_updates_(hi - lo + 1) char* lo, 85 _Inout_updates_(width) char* hi, 86 size_t const width, 87 int (__fileDECL* comp)(void const*, void const*) 88 ) 89#endif // __USE_CONTEXT 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} 125 126 127 128// This macro defines the cutoff between using QuickSort and insertion sort for 129// arrays; arrays with lengths shorter or equal to the below value use insertion 130// sort. 131#define CUTOFF 8 // Testing shows that this is a good value. 132 133// Note: The theoretical number of stack entries required is no more than 1 + 134// log2(num). But we switch to insertion sort for CUTOFF elements or less, so 135// we really only need 1 + log2(num) - log(CUTOFF) stack entries. For a CUTOFF 136// of 8, that means we need no more than 30 stack entries for 32-bit platforms 137// and 62 for 64-bit platforms. 138#define STKSIZ (8 * sizeof(void*) - 2) 139 140 141 142// QuickSort function for sorting arrays. The array is sorted in place. 143// Parameters: 144// * base: Pointer to the initial element of the array 145// * num: Number of elements in the array 146// * width: Width of each element in the array, in bytes 147// * comp: Pointer to a function returning analog of strcmp for strings, but 148// supplied by the caller for comparing the array elements. It 149// accepts two pointers to elements; returns negative if 1 < 2; 150// zero if 1 == 2, and positive if 1 > 2. 151#ifndef _M_CEE 152extern "C" 153#endif 154_CRT_SECURITYSAFECRITICAL_ATTRIBUTE 155#ifdef __USE_CONTEXT 156void __fileDECL qsort_s( 157 void* const base, 158 size_t const num, 159 size_t const width, 160 int (__fileDECL* const comp)(void*, void const*, void const*), 161 void* const context 162 ) 163#else // __USE_CONTEXT 164void __fileDECL qsort( 165 void* const base, 166 size_t const num, 167 size_t const width, 168 int (__fileDECL* const comp)(void const*, void const*) 169 ) 170#endif // __USE_CONTEXT 171{ 172 _VALIDATE_RETURN_VOID(base != nullptr || num == 0, EINVAL); 173 _VALIDATE_RETURN_VOID(width > 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} 392 393#undef __COMPARE 394#undef __SHORTSORT 395 396END_PRAGMA_OPTIMIZE()