Source code pulled from OpenBSD for OpenNTPD. The place to contribute to this code is via the OpenBSD CVS tree.
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.

318 lines
8.0 KiB

  1. /*-
  2. * Copyright (c) 1990, 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 and by Dan Bernstein at New York University,
  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[] = "from: @(#)radixsort.c 8.1 (Berkeley) 6/4/93";*/
  38. static char *rcsid = "$Id: radixsort.c,v 1.1.1.1 1995/10/18 08:42:19 deraadt Exp $";
  39. #endif /* LIBC_SCCS and not lint */
  40. /*
  41. * Radixsort routines.
  42. *
  43. * Program r_sort_a() is unstable but uses O(logN) extra memory for a stack.
  44. * Use radixsort(a, n, trace, endchar) for this case.
  45. *
  46. * For stable sorting (using N extra pointers) use sradixsort(), which calls
  47. * r_sort_b().
  48. *
  49. * For a description of this code, see D. McIlroy, P. McIlroy, K. Bostic,
  50. * "Engineering Radix Sort".
  51. */
  52. #include <sys/types.h>
  53. #include <stdlib.h>
  54. #include <errno.h>
  55. typedef struct {
  56. const u_char **sa;
  57. int sn, si;
  58. } stack;
  59. static inline void simplesort
  60. __P((const u_char **, int, int, const u_char *, u_int));
  61. static void r_sort_a __P((const u_char **, int, int, const u_char *, u_int));
  62. static void r_sort_b __P((const u_char **,
  63. const u_char **, int, int, const u_char *, u_int));
  64. #define THRESHOLD 20 /* Divert to simplesort(). */
  65. #define SIZE 512 /* Default stack size. */
  66. #define SETUP { \
  67. if (tab == NULL) { \
  68. tr = tr0; \
  69. for (c = 0; c < endch; c++) \
  70. tr0[c] = c + 1; \
  71. tr0[c] = 0; \
  72. for (c++; c < 256; c++) \
  73. tr0[c] = c; \
  74. endch = 0; \
  75. } else { \
  76. endch = tab[endch]; \
  77. tr = tab; \
  78. if (endch != 0 && endch != 255) { \
  79. errno = EINVAL; \
  80. return (-1); \
  81. } \
  82. } \
  83. }
  84. int
  85. radixsort(a, n, tab, endch)
  86. const u_char **a, *tab;
  87. int n;
  88. u_int endch;
  89. {
  90. const u_char *tr;
  91. int c;
  92. u_char tr0[256];
  93. SETUP;
  94. r_sort_a(a, n, 0, tr, endch);
  95. return (0);
  96. }
  97. int
  98. sradixsort(a, n, tab, endch)
  99. const u_char **a, *tab;
  100. int n;
  101. u_int endch;
  102. {
  103. const u_char *tr, **ta;
  104. int c;
  105. u_char tr0[256];
  106. SETUP;
  107. if (n < THRESHOLD)
  108. simplesort(a, n, 0, tr, endch);
  109. else {
  110. if ((ta = malloc(n * sizeof(a))) == NULL)
  111. return (-1);
  112. r_sort_b(a, ta, n, 0, tr, endch);
  113. free(ta);
  114. }
  115. return (0);
  116. }
  117. #define empty(s) (s >= sp)
  118. #define pop(a, n, i) a = (--sp)->sa, n = sp->sn, i = sp->si
  119. #define push(a, n, i) sp->sa = a, sp->sn = n, (sp++)->si = i
  120. #define swap(a, b, t) t = a, a = b, b = t
  121. /* Unstable, in-place sort. */
  122. void
  123. r_sort_a(a, n, i, tr, endch)
  124. const u_char **a;
  125. int n, i;
  126. const u_char *tr;
  127. u_int endch;
  128. {
  129. static int count[256], nc, bmin;
  130. register int c;
  131. register const u_char **ak, *r;
  132. stack s[SIZE], *sp, *sp0, *sp1, temp;
  133. int *cp, bigc;
  134. const u_char **an, *t, **aj, **top[256];
  135. /* Set up stack. */
  136. sp = s;
  137. push(a, n, i);
  138. while (!empty(s)) {
  139. pop(a, n, i);
  140. if (n < THRESHOLD) {
  141. simplesort(a, n, i, tr, endch);
  142. continue;
  143. }
  144. an = a + n;
  145. /* Make character histogram. */
  146. if (nc == 0) {
  147. bmin = 255; /* First occupied bin, excluding eos. */
  148. for (ak = a; ak < an;) {
  149. c = tr[(*ak++)[i]];
  150. if (++count[c] == 1 && c != endch) {
  151. if (c < bmin)
  152. bmin = c;
  153. nc++;
  154. }
  155. }
  156. if (sp + nc > s + SIZE) { /* Get more stack. */
  157. r_sort_a(a, n, i, tr, endch);
  158. continue;
  159. }
  160. }
  161. /*
  162. * Set top[]; push incompletely sorted bins onto stack.
  163. * top[] = pointers to last out-of-place element in bins.
  164. * count[] = counts of elements in bins.
  165. * Before permuting: top[c-1] + count[c] = top[c];
  166. * during deal: top[c] counts down to top[c-1].
  167. */
  168. sp0 = sp1 = sp; /* Stack position of biggest bin. */
  169. bigc = 2; /* Size of biggest bin. */
  170. if (endch == 0) /* Special case: set top[eos]. */
  171. top[0] = ak = a + count[0];
  172. else {
  173. ak = a;
  174. top[255] = an;
  175. }
  176. for (cp = count + bmin; nc > 0; cp++) {
  177. while (*cp == 0) /* Find next non-empty pile. */
  178. cp++;
  179. if (*cp > 1) {
  180. if (*cp > bigc) {
  181. bigc = *cp;
  182. sp1 = sp;
  183. }
  184. push(ak, *cp, i+1);
  185. }
  186. top[cp-count] = ak += *cp;
  187. nc--;
  188. }
  189. swap(*sp0, *sp1, temp); /* Play it safe -- biggest bin last. */
  190. /*
  191. * Permute misplacements home. Already home: everything
  192. * before aj, and in bin[c], items from top[c] on.
  193. * Inner loop:
  194. * r = next element to put in place;
  195. * ak = top[r[i]] = location to put the next element.
  196. * aj = bottom of 1st disordered bin.
  197. * Outer loop:
  198. * Once the 1st disordered bin is done, ie. aj >= ak,
  199. * aj<-aj + count[c] connects the bins in a linked list;
  200. * reset count[c].
  201. */
  202. for (aj = a; aj < an; *aj = r, aj += count[c], count[c] = 0)
  203. for (r = *aj; aj < (ak = --top[c = tr[r[i]]]);)
  204. swap(*ak, r, t);
  205. }
  206. }
  207. /* Stable sort, requiring additional memory. */
  208. void
  209. r_sort_b(a, ta, n, i, tr, endch)
  210. const u_char **a, **ta;
  211. int n, i;
  212. const u_char *tr;
  213. u_int endch;
  214. {
  215. static int count[256], nc, bmin;
  216. register int c;
  217. register const u_char **ak, **ai;
  218. stack s[512], *sp, *sp0, *sp1, temp;
  219. const u_char **top[256];
  220. int *cp, bigc;
  221. sp = s;
  222. push(a, n, i);
  223. while (!empty(s)) {
  224. pop(a, n, i);
  225. if (n < THRESHOLD) {
  226. simplesort(a, n, i, tr, endch);
  227. continue;
  228. }
  229. if (nc == 0) {
  230. bmin = 255;
  231. for (ak = a + n; --ak >= a;) {
  232. c = tr[(*ak)[i]];
  233. if (++count[c] == 1 && c != endch) {
  234. if (c < bmin)
  235. bmin = c;
  236. nc++;
  237. }
  238. }
  239. if (sp + nc > s + SIZE) {
  240. r_sort_b(a, ta, n, i, tr, endch);
  241. continue;
  242. }
  243. }
  244. sp0 = sp1 = sp;
  245. bigc = 2;
  246. if (endch == 0) {
  247. top[0] = ak = a + count[0];
  248. count[0] = 0;
  249. } else {
  250. ak = a;
  251. top[255] = a + n;
  252. count[255] = 0;
  253. }
  254. for (cp = count + bmin; nc > 0; cp++) {
  255. while (*cp == 0)
  256. cp++;
  257. if ((c = *cp) > 1) {
  258. if (c > bigc) {
  259. bigc = c;
  260. sp1 = sp;
  261. }
  262. push(ak, c, i+1);
  263. }
  264. top[cp-count] = ak += c;
  265. *cp = 0; /* Reset count[]. */
  266. nc--;
  267. }
  268. swap(*sp0, *sp1, temp);
  269. for (ak = ta + n, ai = a+n; ak > ta;) /* Copy to temp. */
  270. *--ak = *--ai;
  271. for (ak = ta+n; --ak >= ta;) /* Deal to piles. */
  272. *--top[tr[(*ak)[i]]] = *ak;
  273. }
  274. }
  275. static inline void
  276. simplesort(a, n, b, tr, endch) /* insertion sort */
  277. register const u_char **a;
  278. int n, b;
  279. register const u_char *tr;
  280. u_int endch;
  281. {
  282. register u_char ch;
  283. const u_char **ak, **ai, *s, *t;
  284. for (ak = a+1; --n >= 1; ak++)
  285. for (ai = ak; ai > a; ai--) {
  286. for (s = ai[0] + b, t = ai[-1] + b;
  287. (ch = tr[*s]) != endch; s++, t++)
  288. if (ch != tr[*t])
  289. break;
  290. if (ch >= tr[*t])
  291. break;
  292. swap(ai[0], ai[-1], s);
  293. }
  294. }