1
0

sort.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. #include <bfc/platform/types.h>
  2. #include "sort.h"
  3. #include <assert.h>
  4. /***
  5. *qsort.c - quicksort algorithm; qsort() library function for sorting arrays
  6. *
  7. * Copyright (c) Microsoft Corporation. All rights reserved.
  8. *
  9. *Purpose:
  10. * To implement the qsort() routine for sorting arrays.
  11. *
  12. *******************************************************************************/
  13. /* Always compile this module for speed, not size */
  14. #pragma optimize("t", on)
  15. /* prototypes for local routines */
  16. static void shortsort(uint8_t *lo, uint8_t *hi, size_t width, const void *context,
  17. int (__fastcall *comp)(const void *, const void *, const void *));
  18. static void swap(uint8_t *p, uint8_t *q, size_t 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, context, comp) - quicksort function for sorting arrays
  25. *
  26. *Purpose:
  27. * quicksort the array of elements
  28. * side effects: sorts in place
  29. * maximum array size is number of elements times size of elements,
  30. * but is limited by the virtual address space of the processor
  31. *
  32. *Entry:
  33. * char *base = pointer to base of array
  34. * size_t num = number of elements in the array
  35. * size_t width = width in bytes of each array element
  36. * int (*comp)() = pointer to function returning analog of strcmp for
  37. * strings, but supplied by user for comparing the array elements.
  38. * it accepts 2 pointers to elements and returns neg if 1<2, 0 if
  39. * 1=2, pos if 1>2.
  40. *
  41. *Exit:
  42. * returns void
  43. *
  44. *Exceptions:
  45. *
  46. *******************************************************************************/
  47. /* sort the array between lo and hi (inclusive) */
  48. #define STKSIZ (8*sizeof(void*) - 2)
  49. void __cdecl nu::qsort (
  50. void *base,
  51. size_t num,
  52. size_t width,
  53. const void *context,
  54. int (__fastcall *comp)(const void *, const void *, const void *)
  55. )
  56. {
  57. /* Note: the number of stack entries required is no more than
  58. 1 + log2(num), so 30 is sufficient for any array */
  59. uint8_t *lo, *hi; /* ends of sub-array currently sorting */
  60. uint8_t *mid; /* points to middle of subarray */
  61. uint8_t *loguy, *higuy; /* traveling pointers for partition step */
  62. size_t size; /* size of the sub-array */
  63. uint8_t *lostk[STKSIZ] = {0}, *histk[STKSIZ] = {0};
  64. int stkptr; /* stack for saving sub-array to be processed */
  65. assert((width % sizeof(void *)) == 0);
  66. if (num < 2 || width == 0)
  67. return; /* nothing to do */
  68. stkptr = 0; /* initialize stack */
  69. lo = static_cast<uint8_t *>(base);
  70. hi = (uint8_t *)base + width * (num-1); /* initialize limits */
  71. /* this entry point is for pseudo-recursion calling: setting
  72. lo and hi and jumping to here is like recursion, but stkptr is
  73. preserved, locals aren't, so we preserve stuff on the stack */
  74. recurse:
  75. size = (hi - lo) / width + 1; /* number of el's to sort */
  76. /* below a certain size, it is faster to use a O(n^2) sorting method */
  77. if (size <= CUTOFF) {
  78. shortsort(lo, hi, width, context, comp);
  79. }
  80. else {
  81. /* First we pick a partitioning element. The efficiency of the
  82. algorithm demands that we find one that is approximately the median
  83. of the values, but also that we select one fast. We choose the
  84. median of the first, middle, and last elements, to avoid bad
  85. performance in the face of already sorted data, or data that is made
  86. up of multiple sorted runs appended together. Testing shows that a
  87. median-of-three algorithm provides better performance than simply
  88. picking the middle element for the latter case. */
  89. mid = lo + (size / 2) * width; /* find middle element */
  90. /* Sort the first, middle, last elements into order */
  91. if (comp(lo, mid, context) > 0) {
  92. swap(lo, mid, width);
  93. }
  94. if (comp(lo, hi, context) > 0) {
  95. swap(lo, hi, width);
  96. }
  97. if (comp(mid, hi, context) > 0) {
  98. swap(mid, hi, width);
  99. }
  100. /* We now wish to partition the array into three pieces, one consisting
  101. of elements <= partition element, one of elements equal to the
  102. partition element, and one of elements > than it. This is done
  103. below; comments indicate conditions established at every step. */
  104. loguy = lo;
  105. higuy = hi;
  106. /* Note that higuy decreases and loguy increases on every iteration,
  107. so loop must terminate. */
  108. for (;;) {
  109. /* lo <= loguy < hi, lo < higuy <= hi,
  110. A[i] <= A[mid] for lo <= i <= loguy,
  111. A[i] > A[mid] for higuy <= i < hi,
  112. A[hi] >= A[mid] */
  113. /* The doubled loop is to avoid calling comp(mid,mid), since some
  114. existing comparison funcs don't work when passed the same
  115. value for both pointers. */
  116. if (mid > loguy) {
  117. do {
  118. loguy += width;
  119. } while (loguy < mid && comp(loguy, mid, context) <= 0);
  120. }
  121. if (mid <= loguy) {
  122. do {
  123. loguy += width;
  124. } while (loguy <= hi && comp(loguy, mid, context) <= 0);
  125. }
  126. /* lo < loguy <= hi+1, A[i] <= A[mid] for lo <= i < loguy,
  127. either loguy > hi or A[loguy] > A[mid] */
  128. do {
  129. higuy -= width;
  130. } while (higuy > mid && comp(higuy, mid, context) > 0);
  131. /* lo <= higuy < hi, A[i] > A[mid] for higuy < i < hi,
  132. either higuy == lo or A[higuy] <= A[mid] */
  133. if (higuy < loguy)
  134. break;
  135. /* if loguy > hi or higuy == lo, then we would have exited, so
  136. A[loguy] > A[mid], A[higuy] <= A[mid],
  137. loguy <= hi, higuy > lo */
  138. swap(loguy, higuy, width);
  139. /* If the partition element was moved, follow it. Only need
  140. to check for mid == higuy, since before the swap,
  141. A[loguy] > A[mid] implies loguy != mid. */
  142. if (mid == higuy)
  143. mid = loguy;
  144. /* A[loguy] <= A[mid], A[higuy] > A[mid]; so condition at top
  145. of loop is re-established */
  146. }
  147. /* A[i] <= A[mid] for lo <= i < loguy,
  148. A[i] > A[mid] for higuy < i < hi,
  149. A[hi] >= A[mid]
  150. higuy < loguy
  151. implying:
  152. higuy == loguy-1
  153. or higuy == hi - 1, loguy == hi + 1, A[hi] == A[mid] */
  154. /* Find adjacent elements equal to the partition element. The
  155. doubled loop is to avoid calling comp(mid,mid), since some
  156. existing comparison funcs don't work when passed the same value
  157. for both pointers. */
  158. higuy += width;
  159. if (mid < higuy) {
  160. do {
  161. higuy -= width;
  162. } while (higuy > mid && comp(higuy, mid, context) == 0);
  163. }
  164. if (mid >= higuy) {
  165. do {
  166. higuy -= width;
  167. } while (higuy > lo && comp(higuy, mid, context) == 0);
  168. }
  169. /* OK, now we have the following:
  170. higuy < loguy
  171. lo <= higuy <= hi
  172. A[i] <= A[mid] for lo <= i <= higuy
  173. A[i] == A[mid] for higuy < i < loguy
  174. A[i] > A[mid] for loguy <= i < hi
  175. A[hi] >= A[mid] */
  176. /* We've finished the partition, now we want to sort the subarrays
  177. [lo, higuy] and [loguy, hi].
  178. We do the smaller one first to minimize stack usage.
  179. We only sort arrays of length 2 or more.*/
  180. if ( higuy - lo >= hi - loguy ) {
  181. if (lo < higuy) {
  182. lostk[stkptr] = lo;
  183. histk[stkptr] = higuy;
  184. ++stkptr;
  185. } /* save big recursion for later */
  186. if (loguy < hi) {
  187. lo = loguy;
  188. goto recurse; /* do small recursion */
  189. }
  190. }
  191. else {
  192. if (loguy < hi) {
  193. lostk[stkptr] = loguy;
  194. histk[stkptr] = hi;
  195. ++stkptr; /* save big recursion for later */
  196. }
  197. if (lo < higuy) {
  198. hi = higuy;
  199. goto recurse; /* do small recursion */
  200. }
  201. }
  202. }
  203. /* We have sorted the array, except for any pending sorts on the stack.
  204. Check if there are any, and do them. */
  205. --stkptr;
  206. if (stkptr >= 0) {
  207. lo = lostk[stkptr];
  208. hi = histk[stkptr];
  209. goto recurse; /* pop subarray from stack */
  210. }
  211. else
  212. return; /* all subarrays done */
  213. }
  214. /***
  215. *shortsort(hi, lo, width, comp) - insertion sort for sorting short arrays
  216. *
  217. *Purpose:
  218. * sorts the sub-array of elements between lo and hi (inclusive)
  219. * side effects: sorts in place
  220. * assumes that lo < hi
  221. *
  222. *Entry:
  223. * char *lo = pointer to low element to sort
  224. * char *hi = pointer to high element to sort
  225. * size_t width = width in bytes of each array element
  226. * int (*comp)() = pointer to function returning analog of strcmp for
  227. * strings, but supplied by user for comparing the array elements.
  228. * it accepts 2 pointers to elements and returns neg if 1<2, 0 if
  229. * 1=2, pos if 1>2.
  230. *
  231. *Exit:
  232. * returns void
  233. *
  234. *Exceptions:
  235. *
  236. *******************************************************************************/
  237. static void __cdecl shortsort (
  238. uint8_t *lo,
  239. uint8_t *hi,
  240. size_t width,
  241. const void *context,
  242. int (__fastcall *comp)(const void *, const void *, const void *)
  243. )
  244. {
  245. uint8_t *p;
  246. /* Note: in assertions below, i and j are alway inside original bound of
  247. array to sort. */
  248. while (hi > lo) {
  249. /* A[i] <= A[j] for i <= j, j > hi */
  250. uint8_t *max = lo;
  251. for (p = lo+width; p <= hi; p += width) {
  252. /* A[i] <= A[max] for lo <= i < p */
  253. if (comp(p, max, context) > 0) {
  254. max = p;
  255. }
  256. /* A[i] <= A[max] for lo <= i <= p */
  257. }
  258. /* A[i] <= A[max] for lo <= i <= hi */
  259. swap(max, hi, width);
  260. /* A[i] <= A[hi] for i <= hi, so A[i] <= A[j] for i <= j, j >= hi */
  261. hi -= width;
  262. /* A[i] <= A[j] for i <= j, j > hi, loop top condition established */
  263. }
  264. /* A[i] <= A[j] for i <= j, j > lo, which implies A[i] <= A[j] for i < j,
  265. so array is sorted */
  266. }
  267. /***
  268. *swap(a, b, width) - swap two elements
  269. *
  270. *Purpose:
  271. * swaps the two array elements of size width
  272. *
  273. *Entry:
  274. * char *a, *b = pointer to two elements to swap
  275. * size_t width = width in bytes of each array element
  276. *
  277. *Exit:
  278. * returns void
  279. *
  280. *Exceptions:
  281. *
  282. *******************************************************************************/
  283. static void swap (
  284. uint8_t *_a,
  285. uint8_t *_b,
  286. size_t width
  287. )
  288. {
  289. #if 1
  290. void *tmp;
  291. void **a = (void **)_a;
  292. void **b = (void **)_b;
  293. if ( a != b )
  294. /* Do the swap one character at a time to avoid potential alignment
  295. problems. */
  296. do {
  297. tmp = *a;
  298. *a++ = *b;
  299. *b++ = tmp;
  300. width-=sizeof(void *);
  301. } while (width);
  302. #else
  303. //void *temp = alloca(width);
  304. memcpy(temp, a, width);
  305. memcpy(a, b, width);
  306. memcpy(b, temp, width);
  307. #endif
  308. }