mergesort.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*-
  2. * Copyright (c) 1992, 1993
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * This code is derived from software contributed to Berkeley by
  6. * Peter McIlroy.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. All advertising materials mentioning features or use of this software
  17. * must display the following acknowledgement:
  18. * This product includes software developed by the University of
  19. * California, Berkeley and its contributors.
  20. * 4. Neither the name of the University nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  25. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  26. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  27. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  28. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  29. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  30. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  31. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  33. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  34. * SUCH DAMAGE.
  35. */
  36. /* $Id$ */
  37. #if defined(LIBC_SCCS) && !defined(lint)
  38. static char sccsid[] = "@(#)merge.c 8.2 (Berkeley) 2/14/94";
  39. #endif /* LIBC_SCCS and not lint */
  40. /*
  41. * Hybrid exponential search/linear search merge sort with hybrid
  42. * natural/pairwise first pass. Requires about .3% more comparisons
  43. * for random data than LSMS with pairwise first pass alone.
  44. * It works for objects as small as two bytes.
  45. */
  46. #define NATURAL
  47. #define THRESHOLD 16 /* Best choice for natural merge cut-off. */
  48. /* #define NATURAL to get hybrid natural merge.
  49. * (The default is pairwise merging.)
  50. */
  51. #include <sys/types.h>
  52. #include <errno.h>
  53. #include <stdlib.h>
  54. #include <string.h>
  55. #include "php.h"
  56. #ifdef PHP_WIN32
  57. #include <winsock2.h> /* Includes definition for u_char */
  58. #endif
  59. static void setup(u_char *list1, u_char *list2, size_t n, size_t size, int (*cmp)(const void *, const void * TSRMLS_DC) TSRMLS_DC);
  60. static void insertionsort(u_char *a, size_t n, size_t size, int (*cmp)(const void *, const void * TSRMLS_DC) TSRMLS_DC);
  61. #define ISIZE sizeof(int)
  62. #define PSIZE sizeof(u_char *)
  63. #define ICOPY_LIST(src, dst, last) \
  64. do \
  65. *(int*)dst = *(int*)src, src += ISIZE, dst += ISIZE; \
  66. while(src < last)
  67. #define ICOPY_ELT(src, dst, i) \
  68. do \
  69. *(int*) dst = *(int*) src, src += ISIZE, dst += ISIZE; \
  70. while (i -= ISIZE)
  71. #define CCOPY_LIST(src, dst, last) \
  72. do \
  73. *dst++ = *src++; \
  74. while (src < last)
  75. #define CCOPY_ELT(src, dst, i) \
  76. do \
  77. *dst++ = *src++; \
  78. while (i -= 1)
  79. /*
  80. * Find the next possible pointer head. (Trickery for forcing an array
  81. * to do double duty as a linked list when objects do not align with word
  82. * boundaries.
  83. */
  84. /* Assumption: PSIZE is a power of 2. */
  85. #define EVAL(p) (u_char **) \
  86. ((u_char *)0 + \
  87. (((u_char *)p + PSIZE - 1 - (u_char *) 0) & ~(PSIZE - 1)))
  88. /* {{{ php_mergesort
  89. * Arguments are as for qsort.
  90. */
  91. PHPAPI int php_mergesort(void *base, size_t nmemb, size_t size, int (*cmp)(const void *, const void * TSRMLS_DC) TSRMLS_DC)
  92. {
  93. register unsigned int i;
  94. register int sense;
  95. int big, iflag;
  96. register u_char *f1, *f2, *t, *b, *tp2, *q, *l1, *l2;
  97. u_char *list2, *list1, *p2, *p, *last, **p1;
  98. if (size < PSIZE / 2) { /* Pointers must fit into 2 * size. */
  99. errno = EINVAL;
  100. return (-1);
  101. }
  102. if (nmemb == 0)
  103. return (0);
  104. /*
  105. * XXX
  106. * Stupid subtraction for the Cray.
  107. */
  108. iflag = 0;
  109. if (!(size % ISIZE) && !(((char *)base - (char *)0) % ISIZE))
  110. iflag = 1;
  111. if ((list2 = malloc(nmemb * size + PSIZE)) == NULL)
  112. return (-1);
  113. list1 = base;
  114. setup(list1, list2, nmemb, size, cmp TSRMLS_CC);
  115. last = list2 + nmemb * size;
  116. i = big = 0;
  117. while (*EVAL(list2) != last) {
  118. l2 = list1;
  119. p1 = EVAL(list1);
  120. for (tp2 = p2 = list2; p2 != last; p1 = EVAL(l2)) {
  121. p2 = *EVAL(p2);
  122. f1 = l2;
  123. f2 = l1 = list1 + (p2 - list2);
  124. if (p2 != last)
  125. p2 = *EVAL(p2);
  126. l2 = list1 + (p2 - list2);
  127. while (f1 < l1 && f2 < l2) {
  128. if ((*cmp)(f1, f2 TSRMLS_CC) <= 0) {
  129. q = f2;
  130. b = f1, t = l1;
  131. sense = -1;
  132. } else {
  133. q = f1;
  134. b = f2, t = l2;
  135. sense = 0;
  136. }
  137. if (!big) { /* here i = 0 */
  138. while ((b += size) < t && cmp(q, b TSRMLS_CC) >sense)
  139. if (++i == 6) {
  140. big = 1;
  141. goto EXPONENTIAL;
  142. }
  143. } else {
  144. EXPONENTIAL: for (i = size; ; i <<= 1)
  145. if ((p = (b + i)) >= t) {
  146. if ((p = t - size) > b &&
  147. (*cmp)(q, p TSRMLS_CC) <= sense)
  148. t = p;
  149. else
  150. b = p;
  151. break;
  152. } else if ((*cmp)(q, p TSRMLS_CC) <= sense) {
  153. t = p;
  154. if (i == size)
  155. big = 0;
  156. goto FASTCASE;
  157. } else
  158. b = p;
  159. while (t > b+size) {
  160. i = (((t - b) / size) >> 1) * size;
  161. if ((*cmp)(q, p = b + i TSRMLS_CC) <= sense)
  162. t = p;
  163. else
  164. b = p;
  165. }
  166. goto COPY;
  167. FASTCASE: while (i > size)
  168. if ((*cmp)(q,
  169. p = b + (i >>= 1) TSRMLS_CC) <= sense)
  170. t = p;
  171. else
  172. b = p;
  173. COPY: b = t;
  174. }
  175. i = size;
  176. if (q == f1) {
  177. if (iflag) {
  178. ICOPY_LIST(f2, tp2, b);
  179. ICOPY_ELT(f1, tp2, i);
  180. } else {
  181. CCOPY_LIST(f2, tp2, b);
  182. CCOPY_ELT(f1, tp2, i);
  183. }
  184. } else {
  185. if (iflag) {
  186. ICOPY_LIST(f1, tp2, b);
  187. ICOPY_ELT(f2, tp2, i);
  188. } else {
  189. CCOPY_LIST(f1, tp2, b);
  190. CCOPY_ELT(f2, tp2, i);
  191. }
  192. }
  193. }
  194. if (f2 < l2) {
  195. if (iflag)
  196. ICOPY_LIST(f2, tp2, l2);
  197. else
  198. CCOPY_LIST(f2, tp2, l2);
  199. } else if (f1 < l1) {
  200. if (iflag)
  201. ICOPY_LIST(f1, tp2, l1);
  202. else
  203. CCOPY_LIST(f1, tp2, l1);
  204. }
  205. *p1 = l2;
  206. }
  207. tp2 = list1; /* swap list1, list2 */
  208. list1 = list2;
  209. list2 = tp2;
  210. last = list2 + nmemb*size;
  211. }
  212. if (base == list2) {
  213. memmove(list2, list1, nmemb*size);
  214. list2 = list1;
  215. }
  216. free(list2);
  217. return (0);
  218. }
  219. /* }}} */
  220. #define swap(a, b) { \
  221. s = b; \
  222. i = size; \
  223. do { \
  224. tmp = *a; *a++ = *s; *s++ = tmp; \
  225. } while (--i); \
  226. a -= size; \
  227. }
  228. #define reverse(bot, top) { \
  229. s = top; \
  230. do { \
  231. i = size; \
  232. do { \
  233. tmp = *bot; *bot++ = *s; *s++ = tmp; \
  234. } while (--i); \
  235. s -= size2; \
  236. } while(bot < s); \
  237. }
  238. /* {{{ setup
  239. * Optional hybrid natural/pairwise first pass. Eats up list1 in runs of
  240. * increasing order, list2 in a corresponding linked list. Checks for runs
  241. * when THRESHOLD/2 pairs compare with same sense. (Only used when NATURAL
  242. * is defined. Otherwise simple pairwise merging is used.)
  243. */
  244. static void setup(u_char *list1, u_char *list2, size_t n, size_t size, int (*cmp)(const void *, const void * TSRMLS_DC) TSRMLS_DC)
  245. {
  246. int i, length, size2, tmp, sense;
  247. u_char *f1, *f2, *s, *l2, *last, *p2;
  248. size2 = size*2;
  249. if (n <= 5) {
  250. insertionsort(list1, n, size, cmp TSRMLS_CC);
  251. *EVAL(list2) = (u_char*) list2 + n*size;
  252. return;
  253. }
  254. /*
  255. * Avoid running pointers out of bounds; limit n to evens
  256. * for simplicity.
  257. */
  258. i = 4 + (n & 1);
  259. insertionsort(list1 + (n - i) * size, i, size, cmp TSRMLS_CC);
  260. last = list1 + size * (n - i);
  261. *EVAL(list2 + (last - list1)) = list2 + n * size;
  262. #ifdef NATURAL
  263. p2 = list2;
  264. f1 = list1;
  265. sense = (cmp(f1, f1 + size TSRMLS_CC) > 0);
  266. for (; f1 < last; sense = !sense) {
  267. length = 2;
  268. /* Find pairs with same sense. */
  269. for (f2 = f1 + size2; f2 < last; f2 += size2) {
  270. if ((cmp(f2, f2+ size TSRMLS_CC) > 0) != sense)
  271. break;
  272. length += 2;
  273. }
  274. if (length < THRESHOLD) { /* Pairwise merge */
  275. do {
  276. p2 = *EVAL(p2) = f1 + size2 - list1 + list2;
  277. if (sense > 0)
  278. swap (f1, f1 + size);
  279. } while ((f1 += size2) < f2);
  280. } else { /* Natural merge */
  281. l2 = f2;
  282. for (f2 = f1 + size2; f2 < l2; f2 += size2) {
  283. if ((cmp(f2-size, f2 TSRMLS_CC) > 0) != sense) {
  284. p2 = *EVAL(p2) = f2 - list1 + list2;
  285. if (sense > 0)
  286. reverse(f1, f2-size);
  287. f1 = f2;
  288. }
  289. }
  290. if (sense > 0)
  291. reverse (f1, f2-size);
  292. f1 = f2;
  293. if (f2 < last || cmp(f2 - size, f2 TSRMLS_CC) > 0)
  294. p2 = *EVAL(p2) = f2 - list1 + list2;
  295. else
  296. p2 = *EVAL(p2) = list2 + n*size;
  297. }
  298. }
  299. #else /* pairwise merge only. */
  300. for (f1 = list1, p2 = list2; f1 < last; f1 += size2) {
  301. p2 = *EVAL(p2) = p2 + size2;
  302. if (cmp (f1, f1 + size TSRMLS_CC) > 0)
  303. swap(f1, f1 + size);
  304. }
  305. #endif /* NATURAL */
  306. }
  307. /* }}} */
  308. /* {{{ insertionsort
  309. * This is to avoid out-of-bounds addresses in sorting the
  310. * last 4 elements.
  311. */
  312. static void insertionsort(u_char *a, size_t n, size_t size, int (*cmp)(const void *, const void * TSRMLS_DC) TSRMLS_DC)
  313. {
  314. u_char *ai, *s, *t, *u, tmp;
  315. int i;
  316. for (ai = a+size; --n >= 1; ai += size)
  317. for (t = ai; t > a; t -= size) {
  318. u = t - size;
  319. if (cmp(u, t TSRMLS_CC) <= 0)
  320. break;
  321. swap(u, t);
  322. }
  323. }
  324. /* }}} */
  325. /*
  326. * Local variables:
  327. * tab-width: 4
  328. * c-basic-offset: 4
  329. * End:
  330. * vim600: fdm=marker
  331. * vim: noet sw=4 ts=4
  332. */