mergesort.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. #if defined(LIBC_SCCS) && !defined(lint)
  37. static char sccsid[] = "@(#)merge.c 8.2 (Berkeley) 2/14/94";
  38. #endif /* LIBC_SCCS and not lint */
  39. /*
  40. * Hybrid exponential search/linear search merge sort with hybrid
  41. * natural/pairwise first pass. Requires about .3% more comparisons
  42. * for random data than LSMS with pairwise first pass alone.
  43. * It works for objects as small as two bytes.
  44. */
  45. #define NATURAL
  46. #define THRESHOLD 16 /* Best choice for natural merge cut-off. */
  47. /* #define NATURAL to get hybrid natural merge.
  48. * (The default is pairwise merging.)
  49. */
  50. #include <sys/types.h>
  51. #include <errno.h>
  52. #include <stdlib.h>
  53. #include <string.h>
  54. #include "php.h"
  55. #ifdef PHP_WIN32
  56. #include <winsock2.h> /* Includes definition for u_char */
  57. #endif
  58. static void setup(u_char *list1, u_char *list2, size_t n, size_t size, int (*cmp)(const void *, const void *));
  59. static void insertionsort(u_char *a, size_t n, size_t size, int (*cmp)(const void *, const void *));
  60. #define ISIZE sizeof(int)
  61. #define PSIZE sizeof(u_char *)
  62. #define ICOPY_LIST(src, dst, last) \
  63. do \
  64. *(int*)dst = *(int*)src, src += ISIZE, dst += ISIZE; \
  65. while(src < last)
  66. #define ICOPY_ELT(src, dst, i) \
  67. do \
  68. *(int*) dst = *(int*) src, src += ISIZE, dst += ISIZE; \
  69. while (i -= ISIZE)
  70. #define CCOPY_LIST(src, dst, last) \
  71. do \
  72. *dst++ = *src++; \
  73. while (src < last)
  74. #define CCOPY_ELT(src, dst, i) \
  75. do \
  76. *dst++ = *src++; \
  77. while (i -= 1)
  78. /*
  79. * Find the next possible pointer head. (Trickery for forcing an array
  80. * to do double duty as a linked list when objects do not align with word
  81. * boundaries.
  82. */
  83. /* Assumption: PSIZE is a power of 2. */
  84. #define EVAL(p) (u_char **) \
  85. ((u_char *)0 + \
  86. (((u_char *)p + PSIZE - 1 - (u_char *) 0) & ~(PSIZE - 1)))
  87. /* {{{ php_mergesort
  88. * Arguments are as for qsort.
  89. */
  90. PHPAPI int php_mergesort(void *base, size_t nmemb, size_t size, int (*cmp)(const void *, const void *))
  91. {
  92. register size_t i;
  93. register int sense;
  94. int big, iflag;
  95. register u_char *f1, *f2, *t, *b, *tp2, *q, *l1, *l2;
  96. u_char *list2, *list1, *p2, *p, *last, **p1;
  97. if (size < PSIZE / 2) { /* Pointers must fit into 2 * size. */
  98. errno = EINVAL;
  99. return (-1);
  100. }
  101. if (nmemb == 0)
  102. return (0);
  103. /*
  104. * XXX
  105. * Stupid subtraction for the Cray.
  106. */
  107. iflag = 0;
  108. if (!(size % ISIZE) && !(((char *)base - (char *)0) % ISIZE))
  109. iflag = 1;
  110. if ((list2 = malloc(nmemb * size + PSIZE)) == NULL)
  111. return (-1);
  112. list1 = base;
  113. setup(list1, list2, nmemb, size, cmp);
  114. last = list2 + nmemb * size;
  115. i = big = 0;
  116. while (*EVAL(list2) != last) {
  117. l2 = list1;
  118. p1 = EVAL(list1);
  119. for (tp2 = p2 = list2; p2 != last; p1 = EVAL(l2)) {
  120. p2 = *EVAL(p2);
  121. f1 = l2;
  122. f2 = l1 = list1 + (p2 - list2);
  123. if (p2 != last)
  124. p2 = *EVAL(p2);
  125. l2 = list1 + (p2 - list2);
  126. while (f1 < l1 && f2 < l2) {
  127. if ((*cmp)(f1, f2) <= 0) {
  128. q = f2;
  129. b = f1, t = l1;
  130. sense = -1;
  131. } else {
  132. q = f1;
  133. b = f2, t = l2;
  134. sense = 0;
  135. }
  136. if (!big) { /* here i = 0 */
  137. while ((b += size) < t && cmp(q, b) >sense)
  138. if (++i == 6) {
  139. big = 1;
  140. goto EXPONENTIAL;
  141. }
  142. } else {
  143. EXPONENTIAL: for (i = size; ; i <<= 1)
  144. if ((p = (b + i)) >= t) {
  145. if ((p = t - size) > b &&
  146. (*cmp)(q, p) <= sense)
  147. t = p;
  148. else
  149. b = p;
  150. break;
  151. } else if ((*cmp)(q, p) <= sense) {
  152. t = p;
  153. if (i == size)
  154. big = 0;
  155. goto FASTCASE;
  156. } else
  157. b = p;
  158. while (t > b+size) {
  159. i = (((t - b) / size) >> 1) * size;
  160. if ((*cmp)(q, p = b + i) <= sense)
  161. t = p;
  162. else
  163. b = p;
  164. }
  165. goto COPY;
  166. FASTCASE: while (i > size)
  167. if ((*cmp)(q,
  168. p = b + (i >>= 1)) <= sense)
  169. t = p;
  170. else
  171. b = p;
  172. COPY: b = t;
  173. }
  174. i = size;
  175. if (q == f1) {
  176. if (iflag) {
  177. ICOPY_LIST(f2, tp2, b);
  178. ICOPY_ELT(f1, tp2, i);
  179. } else {
  180. CCOPY_LIST(f2, tp2, b);
  181. CCOPY_ELT(f1, tp2, i);
  182. }
  183. } else {
  184. if (iflag) {
  185. ICOPY_LIST(f1, tp2, b);
  186. ICOPY_ELT(f2, tp2, i);
  187. } else {
  188. CCOPY_LIST(f1, tp2, b);
  189. CCOPY_ELT(f2, tp2, i);
  190. }
  191. }
  192. }
  193. if (f2 < l2) {
  194. if (iflag)
  195. ICOPY_LIST(f2, tp2, l2);
  196. else
  197. CCOPY_LIST(f2, tp2, l2);
  198. } else if (f1 < l1) {
  199. if (iflag)
  200. ICOPY_LIST(f1, tp2, l1);
  201. else
  202. CCOPY_LIST(f1, tp2, l1);
  203. }
  204. *p1 = l2;
  205. }
  206. tp2 = list1; /* swap list1, list2 */
  207. list1 = list2;
  208. list2 = tp2;
  209. last = list2 + nmemb*size;
  210. }
  211. if (base == list2) {
  212. memmove(list2, list1, nmemb*size);
  213. list2 = list1;
  214. }
  215. free(list2);
  216. return (0);
  217. }
  218. /* }}} */
  219. #define swap(a, b) { \
  220. s = b; \
  221. i = size; \
  222. do { \
  223. tmp = *a; *a++ = *s; *s++ = tmp; \
  224. } while (--i); \
  225. a -= size; \
  226. }
  227. #define reverse(bot, top) { \
  228. s = top; \
  229. do { \
  230. i = size; \
  231. do { \
  232. tmp = *bot; *bot++ = *s; *s++ = tmp; \
  233. } while (--i); \
  234. s -= size2; \
  235. } while(bot < s); \
  236. }
  237. /* {{{ setup
  238. * Optional hybrid natural/pairwise first pass. Eats up list1 in runs of
  239. * increasing order, list2 in a corresponding linked list. Checks for runs
  240. * when THRESHOLD/2 pairs compare with same sense. (Only used when NATURAL
  241. * is defined. Otherwise simple pairwise merging is used.)
  242. */
  243. static void setup(u_char *list1, u_char *list2, size_t n, size_t size, int (*cmp)(const void *, const void *))
  244. {
  245. size_t i, length, size2, sense;
  246. u_char *f1, *f2, *s, *l2, *last, *p2, tmp;
  247. size2 = size*2;
  248. if (n <= 5) {
  249. insertionsort(list1, n, size, cmp);
  250. *EVAL(list2) = (u_char*) list2 + n*size;
  251. return;
  252. }
  253. /*
  254. * Avoid running pointers out of bounds; limit n to evens
  255. * for simplicity.
  256. */
  257. i = 4 + (n & 1);
  258. insertionsort(list1 + (n - i) * size, i, size, cmp);
  259. last = list1 + size * (n - i);
  260. *EVAL(list2 + (last - list1)) = list2 + n * size;
  261. #ifdef NATURAL
  262. p2 = list2;
  263. f1 = list1;
  264. sense = (cmp(f1, f1 + size) > 0);
  265. for (; f1 < last; sense = !sense) {
  266. length = 2;
  267. /* Find pairs with same sense. */
  268. for (f2 = f1 + size2; f2 < last; f2 += size2) {
  269. if ((cmp(f2, f2+ size) > 0) != sense)
  270. break;
  271. length += 2;
  272. }
  273. if (length < THRESHOLD) { /* Pairwise merge */
  274. do {
  275. p2 = *EVAL(p2) = f1 + size2 - list1 + list2;
  276. if (sense > 0)
  277. swap (f1, f1 + size);
  278. } while ((f1 += size2) < f2);
  279. } else { /* Natural merge */
  280. l2 = f2;
  281. for (f2 = f1 + size2; f2 < l2; f2 += size2) {
  282. if ((cmp(f2-size, f2) > 0) != sense) {
  283. p2 = *EVAL(p2) = f2 - list1 + list2;
  284. if (sense > 0)
  285. reverse(f1, f2-size);
  286. f1 = f2;
  287. }
  288. }
  289. if (sense > 0)
  290. reverse (f1, f2-size);
  291. f1 = f2;
  292. if (f2 < last || cmp(f2 - size, f2) > 0)
  293. p2 = *EVAL(p2) = f2 - list1 + list2;
  294. else
  295. p2 = *EVAL(p2) = list2 + n*size;
  296. }
  297. }
  298. #else /* pairwise merge only. */
  299. for (f1 = list1, p2 = list2; f1 < last; f1 += size2) {
  300. p2 = *EVAL(p2) = p2 + size2;
  301. if (cmp (f1, f1 + size) > 0)
  302. swap(f1, f1 + size);
  303. }
  304. #endif /* NATURAL */
  305. }
  306. /* }}} */
  307. /* {{{ insertionsort
  308. * This is to avoid out-of-bounds addresses in sorting the
  309. * last 4 elements.
  310. */
  311. static void insertionsort(u_char *a, size_t n, size_t size, int (*cmp)(const void *, const void *))
  312. {
  313. u_char *ai, *s, *t, *u, tmp;
  314. size_t i;
  315. for (ai = a+size; --n >= 1; ai += size)
  316. for (t = ai; t > a; t -= size) {
  317. u = t - size;
  318. if (cmp(u, t) <= 0)
  319. break;
  320. swap(u, t);
  321. }
  322. }
  323. /* }}} */
  324. /*
  325. * Local variables:
  326. * tab-width: 4
  327. * c-basic-offset: 4
  328. * End:
  329. * vim600: fdm=marker
  330. * vim: noet sw=4 ts=4
  331. */