nsQuickSort.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*-
  3. * Copyright (c) 1992, 1993
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the University nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. /* We need this because Solaris' version of qsort is broken and
  31. * causes array bounds reads.
  32. */
  33. #include <stdlib.h>
  34. #include "nsAlgorithm.h"
  35. #include "nsQuickSort.h"
  36. extern "C" {
  37. #if !defined(DEBUG) && (defined(__cplusplus) || defined(__gcc))
  38. # ifndef INLINE
  39. # define INLINE inline
  40. # endif
  41. #else
  42. # define INLINE
  43. #endif
  44. typedef int cmp_t(const void *, const void *, void *);
  45. static INLINE char *med3(char *, char *, char *, cmp_t *, void *);
  46. static INLINE void swapfunc(char *, char *, int, int);
  47. /*
  48. * Qsort routine from Bentley & McIlroy's "Engineering a Sort Function".
  49. */
  50. #define swapcode(TYPE, parmi, parmj, n) { \
  51. long i = (n) / sizeof (TYPE); \
  52. TYPE *pi = (TYPE *) (parmi); \
  53. TYPE *pj = (TYPE *) (parmj); \
  54. do { \
  55. TYPE t = *pi; \
  56. *pi++ = *pj; \
  57. *pj++ = t; \
  58. } while (--i > 0); \
  59. }
  60. #define SWAPINIT(a, es) swaptype = ((char *)a - (char *)0) % sizeof(long) || \
  61. es % sizeof(long) ? 2 : es == sizeof(long)? 0 : 1;
  62. static INLINE void
  63. swapfunc(char *a, char *b, int n, int swaptype)
  64. {
  65. if(swaptype <= 1)
  66. swapcode(long, a, b, n)
  67. else
  68. swapcode(char, a, b, n)
  69. }
  70. #define swap(a, b) \
  71. if (swaptype == 0) { \
  72. long t = *(long *)(a); \
  73. *(long *)(a) = *(long *)(b); \
  74. *(long *)(b) = t; \
  75. } else \
  76. swapfunc((char *)a, (char*)b, (int)es, swaptype)
  77. #define vecswap(a, b, n) if ((n) > 0) swapfunc((char *)a, (char *)b, (int)n, swaptype)
  78. static INLINE char *
  79. med3(char *a, char *b, char *c, cmp_t* cmp, void *data)
  80. {
  81. return cmp(a, b, data) < 0 ?
  82. (cmp(b, c, data) < 0 ? b : (cmp(a, c, data) < 0 ? c : a ))
  83. :(cmp(b, c, data) > 0 ? b : (cmp(a, c, data) < 0 ? a : c ));
  84. }
  85. void NS_QuickSort (
  86. void *a,
  87. unsigned int n,
  88. unsigned int es,
  89. cmp_t *cmp,
  90. void *data
  91. )
  92. {
  93. char *pa, *pb, *pc, *pd, *pl, *pm, *pn;
  94. int d, r, swaptype;
  95. loop: SWAPINIT(a, es);
  96. /* Use insertion sort when input is small */
  97. if (n < 7) {
  98. for (pm = (char *)a + es; pm < (char *)a + n * es; pm += es)
  99. for (pl = pm; pl > (char *)a && cmp(pl - es, pl, data) > 0;
  100. pl -= es)
  101. swap(pl, pl - es);
  102. return;
  103. }
  104. /* Choose pivot */
  105. pm = (char *)a + (n / 2) * es;
  106. if (n > 7) {
  107. pl = (char *)a;
  108. pn = (char *)a + (n - 1) * es;
  109. if (n > 40) {
  110. d = (n / 8) * es;
  111. pl = med3(pl, pl + d, pl + 2 * d, cmp, data);
  112. pm = med3(pm - d, pm, pm + d, cmp, data);
  113. pn = med3(pn - 2 * d, pn - d, pn, cmp, data);
  114. }
  115. pm = med3(pl, pm, pn, cmp, data);
  116. }
  117. swap(a, pm);
  118. pa = pb = (char *)a + es;
  119. pc = pd = (char *)a + (n - 1) * es;
  120. /* loop invariants:
  121. * [a, pa) = pivot
  122. * [pa, pb) < pivot
  123. * [pb, pc + es) unprocessed
  124. * [pc + es, pd + es) > pivot
  125. * [pd + es, pn) = pivot
  126. */
  127. for (;;) {
  128. while (pb <= pc && (r = cmp(pb, a, data)) <= 0) {
  129. if (r == 0) {
  130. swap(pa, pb);
  131. pa += es;
  132. }
  133. pb += es;
  134. }
  135. while (pb <= pc && (r = cmp(pc, a, data)) >= 0) {
  136. if (r == 0) {
  137. swap(pc, pd);
  138. pd -= es;
  139. }
  140. pc -= es;
  141. }
  142. if (pb > pc)
  143. break;
  144. swap(pb, pc);
  145. pb += es;
  146. pc -= es;
  147. }
  148. /* Move pivot values */
  149. pn = (char *)a + n * es;
  150. r = XPCOM_MIN(pa - (char *)a, pb - pa);
  151. vecswap(a, pb - r, r);
  152. r = XPCOM_MIN<size_t>(pd - pc, pn - pd - es);
  153. vecswap(pb, pn - r, r);
  154. /* Recursively process partitioned items */
  155. if ((r = pb - pa) > (int)es)
  156. NS_QuickSort(a, r / es, es, cmp, data);
  157. if ((r = pd - pc) > (int)es) {
  158. /* Iterate rather than recurse to save stack space */
  159. a = pn - r;
  160. n = r / es;
  161. goto loop;
  162. }
  163. /* NS_QuickSort(pn - r, r / es, es, cmp, data);*/
  164. }
  165. }
  166. #undef INLINE
  167. #undef swapcode
  168. #undef SWAPINIT
  169. #undef swap
  170. #undef vecswap