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