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.

308 lines
10 KiB

  1. /**
  2. @file
  3. @ingroup util
  4. @brief Ancient implementation of qsort.
  5. @details This is shipped with CUDD so that results of reordering may
  6. be more reproducible across different platforms.
  7. qsort.c 4.2 (Berkeley) 3/9/83
  8. Our own version of the system qsort routine which is faster by an average
  9. of 25%, with lows and highs of 10% and 50%.
  10. The THRESHold below is the insertion sort threshold, and has been adjusted
  11. for records of size 48 bytes.
  12. The MTHREShold is where we stop finding a better median.
  13. @copyright@parblock
  14. Copyright (c) 1994-1998 The Regents of the Univ. of California.
  15. All rights reserved.
  16. Permission is hereby granted, without written agreement and without license
  17. or royalty fees, to use, copy, modify, and distribute this software and its
  18. documentation for any purpose, provided that the above copyright notice and
  19. the following two paragraphs appear in all copies of this software.
  20. IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  21. DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  22. OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  23. CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  25. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  26. FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
  27. "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE
  28. MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  29. @endparblock
  30. @copyright@parblock
  31. Copyright (c) 1999-2015, Regents of the University of Colorado
  32. All rights reserved.
  33. Redistribution and use in source and binary forms, with or without
  34. modification, are permitted provided that the following conditions
  35. are met:
  36. Redistributions of source code must retain the above copyright
  37. notice, this list of conditions and the following disclaimer.
  38. Redistributions in binary form must reproduce the above copyright
  39. notice, this list of conditions and the following disclaimer in the
  40. documentation and/or other materials provided with the distribution.
  41. Neither the name of the University of Colorado nor the names of its
  42. contributors may be used to endorse or promote products derived from
  43. this software without specific prior written permission.
  44. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  45. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  46. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  47. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  48. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  49. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  50. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  51. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  52. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  53. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  54. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  55. POSSIBILITY OF SUCH DAMAGE.
  56. @endparblock
  57. */
  58. #include "util.h"
  59. #ifndef USE_SYSTEM_QSORT
  60. /**
  61. @brief Threshold for insertion.
  62. */
  63. #define THRESH 4
  64. /**
  65. @brief Threshold for median.
  66. */
  67. #define MTHRESH 6
  68. /**
  69. @brief Miscellaneous information.
  70. */
  71. typedef struct {
  72. QSFP qcmp; /**< the comparison routine */
  73. int qsz; /**< size of each record */
  74. int thresh; /**< THRESHold in chars */
  75. int mthresh; /**< MTHRESHold in chars */
  76. } info_t;
  77. /*---------------------------------------------------------------------------*/
  78. /* Static function prototypes */
  79. /*---------------------------------------------------------------------------*/
  80. /** \cond */
  81. static void qst (char *base, char *max, info_t const * info);
  82. /** \endcond */
  83. /*---------------------------------------------------------------------------*/
  84. /* Definition of exported functions */
  85. /*---------------------------------------------------------------------------*/
  86. #undef min
  87. #undef max
  88. #endif
  89. /**
  90. * @brief Implements the quicksort algorithm.
  91. *
  92. * @details First, set up some global parameters for qst to share.
  93. * Then, quicksort with qst(), and then a cleanup insertion sort
  94. * ourselves. Sound simple? It's not...
  95. */
  96. void
  97. util_qsort(
  98. void *vbase /**< start address of array */,
  99. int n /**< number of items */,
  100. int size /**< size of each item */,
  101. QSFP compar /**< comparison function */)
  102. {
  103. #ifdef USE_SYSTEM_QSORT
  104. qsort(vbase, n, size, compar);
  105. #else
  106. char *base = (char *) vbase;
  107. char c, *i, *j, *lo, *hi;
  108. char *min, *max;
  109. info_t info;
  110. if (n <= 1)
  111. return;
  112. info.qsz = size;
  113. info.qcmp = compar;
  114. info.thresh = size * THRESH;
  115. info.mthresh = size * MTHRESH;
  116. max = base + n * size;
  117. if (n >= THRESH) {
  118. qst(base, max, &info);
  119. hi = base + info.thresh;
  120. } else {
  121. hi = max;
  122. }
  123. /*
  124. * First put smallest element, which must be in the first THRESH, in
  125. * the first position as a sentinel. This is done just by searching
  126. * the first THRESH elements (or the first n if n < THRESH), finding
  127. * the min, and swapping it into the first position.
  128. */
  129. for (j = lo = base; (lo += size) < hi; )
  130. if ((*compar)(j, lo) > 0)
  131. j = lo;
  132. if (j != base) {
  133. /* swap j into place */
  134. for (i = base, hi = base + size; i < hi; ) {
  135. c = *j;
  136. *j++ = *i;
  137. *i++ = c;
  138. }
  139. }
  140. /*
  141. * With our sentinel in place, we now run the following hyper-fast
  142. * insertion sort. For each remaining element, min, from [1] to [n-1],
  143. * set hi to the index of the element AFTER which this one goes.
  144. * Then, do the standard insertion sort shift on a character at a time
  145. * basis for each element in the frob.
  146. */
  147. for (min = base; (hi = min += size) < max; ) {
  148. while ((*compar)(hi -= size, min) > 0)
  149. /* void */;
  150. if ((hi += size) != min) {
  151. for (lo = min + size; --lo >= min; ) {
  152. c = *lo;
  153. for (i = j = lo; (j -= size) >= hi; i = j)
  154. *i = *j;
  155. *i = c;
  156. }
  157. }
  158. }
  159. #endif
  160. }
  161. /*---------------------------------------------------------------------------*/
  162. /* Definition of static functions */
  163. /*---------------------------------------------------------------------------*/
  164. /**
  165. * @brief Do a quicksort.
  166. *
  167. * @details First, find the median element, and put that one in the
  168. * first place as the discriminator. (This "median" is just the
  169. * median of the first, last and middle elements). (Using this median
  170. * instead of the first element is a big win). Then, the usual
  171. * partitioning/swapping, followed by moving the discriminator into
  172. * the right place. Then, figure out the sizes of the two partions,
  173. * do the smaller one recursively and the larger one via a repeat of
  174. * this code. Stopping when there are less than THRESH elements in a
  175. * partition and cleaning up with an insertion sort (in our caller) is
  176. * a huge win. All data swaps are done in-line, which is space-losing
  177. * but time-saving. (And there are only three places where this is
  178. * done).
  179. */
  180. #ifndef USE_SYSTEM_QSORT
  181. static void
  182. qst(char *base, char *max, info_t const * info)
  183. {
  184. char c, *i, *j, *jj;
  185. int ii;
  186. char *mid, *tmp;
  187. intptr_t lo, hi;
  188. /*
  189. * At the top here, lo is the number of characters of elements in the
  190. * current partition. (Which should be max - base).
  191. * Find the median of the first, last, and middle element and make
  192. * that the middle element. Set j to largest of first and middle.
  193. * If max is larger than that guy, then it's that guy, else compare
  194. * max with loser of first and take larger. Things are set up to
  195. * prefer the middle, then the first in case of ties.
  196. */
  197. lo = max - base; /* number of elements as chars */
  198. do {
  199. mid = i = base + info->qsz * ((lo / info->qsz) >> 1);
  200. if (lo >= info->mthresh) {
  201. j = ((*info->qcmp)((jj = base), i) > 0 ? jj : i);
  202. if ((*info->qcmp)(j, (tmp = max - info->qsz)) > 0) {
  203. /* switch to first loser */
  204. j = (j == jj ? i : jj);
  205. if ((*info->qcmp)(j, tmp) < 0)
  206. j = tmp;
  207. }
  208. if (j != i) {
  209. ii = info->qsz;
  210. do {
  211. c = *i;
  212. *i++ = *j;
  213. *j++ = c;
  214. } while (--ii);
  215. }
  216. }
  217. /*
  218. * Semi-standard quicksort partitioning/swapping
  219. */
  220. for (i = base, j = max - info->qsz; ; ) {
  221. while (i < mid && (*info->qcmp)(i, mid) <= 0)
  222. i += info->qsz;
  223. while (j > mid) {
  224. if ((*info->qcmp)(mid, j) <= 0) {
  225. j -= info->qsz;
  226. continue;
  227. }
  228. tmp = i + info->qsz; /* value of i after swap */
  229. if (i == mid) {
  230. /* j <-> mid, new mid is j */
  231. mid = jj = j;
  232. } else {
  233. /* i <-> j */
  234. jj = j;
  235. j -= info->qsz;
  236. }
  237. goto swap;
  238. }
  239. if (i == mid) {
  240. break;
  241. } else {
  242. /* i <-> mid, new mid is i */
  243. jj = mid;
  244. tmp = mid = i; /* value of i after swap */
  245. j -= info->qsz;
  246. }
  247. swap:
  248. ii = info->qsz;
  249. do {
  250. c = *i;
  251. *i++ = *jj;
  252. *jj++ = c;
  253. } while (--ii);
  254. i = tmp;
  255. }
  256. /*
  257. * Look at sizes of the two partitions, do the smaller
  258. * one first by recursion, then do the larger one by
  259. * making sure lo is its size, base and max are update
  260. * correctly, and branching back. But only repeat
  261. * (recursively or by branching) if the partition is
  262. * of at least size THRESH.
  263. */
  264. i = (j = mid) + info->qsz;
  265. if ((lo = j - base) <= (hi = max - i)) {
  266. if (lo >= info->thresh)
  267. qst(base, j, info);
  268. base = i;
  269. lo = hi;
  270. } else {
  271. if (hi >= info->thresh)
  272. qst(i, max, info);
  273. max = j;
  274. }
  275. } while (lo >= info->thresh);
  276. }
  277. #endif