Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

300 lines
9.7 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // File: qsort.cpp
  4. //
  5. // Module: CMPBK32.DLL
  6. //
  7. // Synopsis: Quick Sort Implementation (taken from libc)
  8. //
  9. // Copyright (c) 1997-1998 Microsoft Corporation
  10. //
  11. // Author: quintinb created header 08/17/99
  12. //
  13. //+----------------------------------------------------------------------------
  14. #include "cmmaster.h"
  15. /* prototypes for local routines */
  16. static void __cdecl shortsort(char *lo, char *hi, unsigned width,
  17. int (__cdecl *comp)(const void *, const void *));
  18. static void __cdecl swap(char *p, char *q, unsigned int width);
  19. /* this parameter defines the cutoff between using quick sort and
  20. insertion sort for arrays; arrays with lengths shorter or equal to the
  21. below value use insertion sort */
  22. #define CUTOFF 8 /* testing shows that this is good value */
  23. /***
  24. *qsort(base, num, wid, comp) - quicksort function for sorting arrays
  25. *
  26. *Purpose:
  27. * quicksort the array of elements
  28. * side effects: sorts in place
  29. *
  30. *Entry:
  31. * char *base = pointer to base of array
  32. * unsigned num = number of elements in the array
  33. * unsigned width = width in bytes of each array element
  34. * int (*comp)() = pointer to function returning analog of strcmp for
  35. * strings, but supplied by user for comparing the array elements.
  36. * it accepts 2 pointers to elements and returns neg if 1<2, 0 if
  37. * 1=2, pos if 1>2.
  38. *
  39. *Exit:
  40. * returns void
  41. *
  42. *Exceptions:
  43. *
  44. *******************************************************************************/
  45. /* sort the array between lo and hi (inclusive) */
  46. void __cdecl CmQSort (
  47. void *base,
  48. unsigned num,
  49. unsigned width,
  50. int (__cdecl *comp)(const void *, const void *)
  51. )
  52. {
  53. char *lo, *hi; /* ends of sub-array currently sorting */
  54. char *mid; /* points to middle of subarray */
  55. char *loguy, *higuy; /* traveling pointers for partition step */
  56. unsigned size; /* size of the sub-array */
  57. char *lostk[30], *histk[30];
  58. int stkptr; /* stack for saving sub-array to be processed */
  59. /* Note: the number of stack entries required is no more than
  60. 1 + log2(size), so 30 is sufficient for any array */
  61. if (num < 2 || width == 0)
  62. return; /* nothing to do */
  63. stkptr = 0; /* initialize stack */
  64. lo = (char *)base;
  65. hi = (char *)base + width * (num-1); /* initialize limits */
  66. /* this entry point is for pseudo-recursion calling: setting
  67. lo and hi and jumping to here is like recursion, but stkptr is
  68. prserved, locals aren't, so we preserve stuff on the stack */
  69. recurse:
  70. size = PtrToUlong((PVOID)(hi - lo)) / width + 1; /* number of el's to sort */
  71. /* below a certain size, it is faster to use a O(n^2) sorting method */
  72. if (size <= CUTOFF) {
  73. shortsort(lo, hi, width, comp);
  74. }
  75. else {
  76. /* First we pick a partititioning element. The efficiency of the
  77. algorithm demands that we find one that is approximately the
  78. median of the values, but also that we select one fast. Using
  79. the first one produces bad performace if the array is already
  80. sorted, so we use the middle one, which would require a very
  81. wierdly arranged array for worst case performance. Testing shows
  82. that a median-of-three algorithm does not, in general, increase
  83. performance. */
  84. mid = lo + (size / 2) * width; /* find middle element */
  85. swap(mid, lo, width); /* swap it to beginning of array */
  86. /* We now wish to partition the array into three pieces, one
  87. consisiting of elements <= partition element, one of elements
  88. equal to the parition element, and one of element >= to it. This
  89. is done below; comments indicate conditions established at every
  90. step. */
  91. loguy = lo;
  92. higuy = hi + width;
  93. /* Note that higuy decreases and loguy increases on every iteration,
  94. so loop must terminate. */
  95. for (;;) {
  96. /* lo <= loguy < hi, lo < higuy <= hi + 1,
  97. A[i] <= A[lo] for lo <= i <= loguy,
  98. A[i] >= A[lo] for higuy <= i <= hi */
  99. do {
  100. loguy += width;
  101. } while (loguy <= hi && comp(loguy, lo) <= 0);
  102. /* lo < loguy <= hi+1, A[i] <= A[lo] for lo <= i < loguy,
  103. either loguy > hi or A[loguy] > A[lo] */
  104. do {
  105. higuy -= width;
  106. } while (higuy > lo && comp(higuy, lo) >= 0);
  107. /* lo-1 <= higuy <= hi, A[i] >= A[lo] for higuy < i <= hi,
  108. either higuy <= lo or A[higuy] < A[lo] */
  109. if (higuy < loguy)
  110. break;
  111. /* if loguy > hi or higuy <= lo, then we would have exited, so
  112. A[loguy] > A[lo], A[higuy] < A[lo],
  113. loguy < hi, highy > lo */
  114. swap(loguy, higuy, width);
  115. /* A[loguy] < A[lo], A[higuy] > A[lo]; so condition at top
  116. of loop is re-established */
  117. }
  118. /* A[i] >= A[lo] for higuy < i <= hi,
  119. A[i] <= A[lo] for lo <= i < loguy,
  120. higuy < loguy, lo <= higuy <= hi
  121. implying:
  122. A[i] >= A[lo] for loguy <= i <= hi,
  123. A[i] <= A[lo] for lo <= i <= higuy,
  124. A[i] = A[lo] for higuy < i < loguy */
  125. swap(lo, higuy, width); /* put partition element in place */
  126. /* OK, now we have the following:
  127. A[i] >= A[higuy] for loguy <= i <= hi,
  128. A[i] <= A[higuy] for lo <= i < higuy
  129. A[i] = A[lo] for higuy <= i < loguy */
  130. /* We've finished the partition, now we want to sort the subarrays
  131. [lo, higuy-1] and [loguy, hi].
  132. We do the smaller one first to minimize stack usage.
  133. We only sort arrays of length 2 or more.*/
  134. if ( higuy - 1 - lo >= hi - loguy ) {
  135. if (lo + width < higuy) {
  136. lostk[stkptr] = lo;
  137. histk[stkptr] = higuy - width;
  138. ++stkptr;
  139. } /* save big recursion for later */
  140. if (loguy < hi) {
  141. lo = loguy;
  142. goto recurse; /* do small recursion */
  143. }
  144. }
  145. else {
  146. if (loguy < hi) {
  147. lostk[stkptr] = loguy;
  148. histk[stkptr] = hi;
  149. ++stkptr; /* save big recursion for later */
  150. }
  151. if (lo + width < higuy) {
  152. hi = higuy - width;
  153. goto recurse; /* do small recursion */
  154. }
  155. }
  156. }
  157. /* We have sorted the array, except for any pending sorts on the stack.
  158. Check if there are any, and do them. */
  159. --stkptr;
  160. if (stkptr >= 0) {
  161. lo = lostk[stkptr];
  162. hi = histk[stkptr];
  163. goto recurse; /* pop subarray from stack */
  164. }
  165. else
  166. return; /* all subarrays done */
  167. }
  168. /***
  169. *shortsort(hi, lo, width, comp) - insertion sort for sorting short arrays
  170. *
  171. *Purpose:
  172. * sorts the sub-array of elements between lo and hi (inclusive)
  173. * side effects: sorts in place
  174. * assumes that lo < hi
  175. *
  176. *Entry:
  177. * char *lo = pointer to low element to sort
  178. * char *hi = pointer to high element to sort
  179. * unsigned width = width in bytes of each array element
  180. * int (*comp)() = pointer to function returning analog of strcmp for
  181. * strings, but supplied by user for comparing the array elements.
  182. * it accepts 2 pointers to elements and returns neg if 1<2, 0 if
  183. * 1=2, pos if 1>2.
  184. *
  185. *Exit:
  186. * returns void
  187. *
  188. *Exceptions:
  189. *
  190. *******************************************************************************/
  191. static void __cdecl shortsort (
  192. char *lo,
  193. char *hi,
  194. unsigned width,
  195. int (__cdecl *comp)(const void *, const void *)
  196. )
  197. {
  198. char *p, *max;
  199. /* Note: in assertions below, i and j are alway inside original bound of
  200. array to sort. */
  201. while (hi > lo) {
  202. /* A[i] <= A[j] for i <= j, j > hi */
  203. max = lo;
  204. for (p = lo+width; p <= hi; p += width) {
  205. /* A[i] <= A[max] for lo <= i < p */
  206. if (comp(p, max) > 0) {
  207. max = p;
  208. }
  209. /* A[i] <= A[max] for lo <= i <= p */
  210. }
  211. /* A[i] <= A[max] for lo <= i <= hi */
  212. swap(max, hi, width);
  213. /* A[i] <= A[hi] for i <= hi, so A[i] <= A[j] for i <= j, j >= hi */
  214. hi -= width;
  215. /* A[i] <= A[j] for i <= j, j > hi, loop top condition established */
  216. }
  217. /* A[i] <= A[j] for i <= j, j > lo, which implies A[i] <= A[j] for i < j,
  218. so array is sorted */
  219. }
  220. /***
  221. *swap(a, b, width) - swap two elements
  222. *
  223. *Purpose:
  224. * swaps the two array elements of size width
  225. *
  226. *Entry:
  227. * char *a, *b = pointer to two elements to swap
  228. * unsigned width = width in bytes of each array element
  229. *
  230. *Exit:
  231. * returns void
  232. *
  233. *Exceptions:
  234. *
  235. *******************************************************************************/
  236. static void __cdecl swap (
  237. char *a,
  238. char *b,
  239. unsigned width
  240. )
  241. {
  242. char tmp;
  243. if ( a != b )
  244. /* Do the swap one character at a time to avoid potential alignment
  245. problems. */
  246. while ( width-- ) {
  247. tmp = *a;
  248. *a++ = *b;
  249. *b++ = tmp;
  250. }
  251. }