145287Sbostic /*- 245287Sbostic * Copyright (c) 1990 The Regents of the University of California. 345287Sbostic * All rights reserved. 445287Sbostic * 550859Sbostic * This code is derived from software contributed to Berkeley by 6*54579Sbostic * Peter McIlroy and by Dan Bernstein at New York University, 750859Sbostic * 845287Sbostic * %sccs.include.redist.c% 945287Sbostic */ 1045287Sbostic 1145287Sbostic #if defined(LIBC_SCCS) && !defined(lint) 12*54579Sbostic static char sccsid[] = "@(#)radixsort.c 5.13 (Berkeley) 06/30/92"; 1345287Sbostic #endif /* LIBC_SCCS and not lint */ 1445287Sbostic 15*54579Sbostic /* 16*54579Sbostic * Radixsort routines. 17*54579Sbostic * 18*54579Sbostic * Program r_sort_a() is unstable but uses O(logN) extra memory for a stack. 19*54579Sbostic * Use radixsort(a, n, trace, endchar) for this case. 20*54579Sbostic * 21*54579Sbostic * For stable sorting (using N extra pointers) use sradixsort(), which calls 22*54579Sbostic * r_sort_b(). 23*54579Sbostic * 24*54579Sbostic * For a description of this code, see D. McIlroy, P. McIlroy, K. Bostic, 25*54579Sbostic * "Engineering Radix Sort". 26*54579Sbostic */ 27*54579Sbostic 2845287Sbostic #include <sys/types.h> 2945287Sbostic #include <stdlib.h> 3045287Sbostic #include <stddef.h> 3151687Sbostic #include <errno.h> 3245287Sbostic 33*54579Sbostic typedef struct { 34*54579Sbostic const u_char **sa; 35*54579Sbostic int sn, si; 36*54579Sbostic } stack; 3751687Sbostic 38*54579Sbostic static void simplesort __P((const u_char **, int, int, const u_char *, u_int)); 39*54579Sbostic static void r_sort_a __P((const u_char **, int, int, const u_char *, u_int)); 40*54579Sbostic static void r_sort_b __P((const u_char **, 41*54579Sbostic const u_char **, int, int, const u_char *, u_int)); 4251687Sbostic 43*54579Sbostic #define THRESHOLD 20 /* Divert to simplesort(). */ 44*54579Sbostic #define SIZE 512 /* Default stack size. */ 4545287Sbostic 46*54579Sbostic #define SETUP { \ 47*54579Sbostic if (tab == NULL) { \ 48*54579Sbostic tr = tr0; \ 49*54579Sbostic for (c = 0; c < endch; c++) \ 50*54579Sbostic tr0[c] = c + 1; \ 51*54579Sbostic tr0[c] = 0; \ 52*54579Sbostic for (c++; c < 256; c++) \ 53*54579Sbostic tr0[c] = c; \ 54*54579Sbostic endch = 0; \ 55*54579Sbostic } else { \ 56*54579Sbostic endch = tab[endch]; \ 57*54579Sbostic tr = tab; \ 58*54579Sbostic if (endch != 0 && endch != 255) { \ 59*54579Sbostic errno = EINVAL; \ 60*54579Sbostic return (-1); \ 61*54579Sbostic } \ 62*54579Sbostic } \ 63*54579Sbostic } 6445427Sbostic 65*54579Sbostic int 66*54579Sbostic radixsort(a, n, tab, endch) 67*54579Sbostic const u_char **a, *tab; 68*54579Sbostic int n; 69*54579Sbostic u_int endch; 70*54579Sbostic { 71*54579Sbostic const u_char *tr; 72*54579Sbostic int c; 73*54579Sbostic u_char tr0[256]; 7445287Sbostic 75*54579Sbostic SETUP; 76*54579Sbostic r_sort_a(a, n, 0, tr, endch); 77*54579Sbostic return (0); 7845287Sbostic } 79*54579Sbostic 8046599Sdonn int 81*54579Sbostic sradixsort(a, n, tab, endch) 82*54579Sbostic const u_char **a, *tab; 83*54579Sbostic int n; 84*54579Sbostic u_int endch; 8545287Sbostic { 86*54579Sbostic const u_char *tr, **ta; 87*54579Sbostic int c; 88*54579Sbostic u_char tr0[256]; 8945287Sbostic 90*54579Sbostic SETUP; 91*54579Sbostic if (n < THRESHOLD) 92*54579Sbostic simplesort(a, n, 0, tr, endch); 93*54579Sbostic else { 94*54579Sbostic if ((ta = malloc(n * sizeof(a))) == NULL) 9551687Sbostic return (-1); 96*54579Sbostic r_sort_b(a, ta, n, 0, tr, endch); 97*54579Sbostic free(ta); 9851687Sbostic } 99*54579Sbostic return (0); 100*54579Sbostic } 10145345Sbostic 102*54579Sbostic #define empty(s) (s >= sp) 103*54579Sbostic #define pop(a, n, i) a = (--sp)->sa, n = sp->sn, i = sp->si 104*54579Sbostic #define push(a, n, i) sp->sa = a, sp->sn = n, (sp++)->si = i 105*54579Sbostic #define swap(a, b, t) t = a, a = b, b = t 10645287Sbostic 107*54579Sbostic /* Unstable, in-place sort. */ 108*54579Sbostic void 109*54579Sbostic r_sort_a(a, n, i, tr, endch) 110*54579Sbostic const u_char **a; 111*54579Sbostic int n, i; 112*54579Sbostic const u_char *tr; 113*54579Sbostic u_int endch; 114*54579Sbostic { 115*54579Sbostic static int count[256], nc, bmin; 116*54579Sbostic register int c; 117*54579Sbostic register const u_char **ak, *r; 118*54579Sbostic stack s[SIZE], *sp, *sp0, *sp1, temp; 119*54579Sbostic int *cp, bigc; 120*54579Sbostic const u_char **an, *t, **aj, **top[256]; 12145287Sbostic 122*54579Sbostic sp = s; 12345287Sbostic 124*54579Sbostic /* Set up stack. */ 125*54579Sbostic push(a, n, i); 126*54579Sbostic while (!empty(s)) { 127*54579Sbostic pop(a, n, i); 128*54579Sbostic if (n < THRESHOLD) { 129*54579Sbostic simplesort(a, n, i, tr, endch); 130*54579Sbostic continue; 13151687Sbostic } 132*54579Sbostic an = a + n; 13345287Sbostic 134*54579Sbostic /* Make character histogram. */ 135*54579Sbostic if (nc == 0) { 136*54579Sbostic bmin = 255; /* First occupied bin, excluding eos. */ 137*54579Sbostic for (ak = a; ak < an;) { 138*54579Sbostic c = tr[(*ak++)[i]]; 139*54579Sbostic if (++count[c] == 1 && c != endch) { 140*54579Sbostic if (c < bmin) 141*54579Sbostic bmin = c; 142*54579Sbostic nc++; 143*54579Sbostic } 144*54579Sbostic } 145*54579Sbostic if (sp + nc > s + SIZE) { /* Get more stack. */ 146*54579Sbostic r_sort_a(a, n, i, tr, endch); 147*54579Sbostic continue; 148*54579Sbostic } 149*54579Sbostic } 150*54579Sbostic 15145287Sbostic /* 152*54579Sbostic * Set top[]; push incompletely sorted bins onto stack. 153*54579Sbostic * top[] = pointers to last out-of-place element in bins. 154*54579Sbostic * count[] = counts of elements in bins. 155*54579Sbostic * Before permuting: top[c-1] + count[c] = top[c]; 156*54579Sbostic * during deal: top[c] counts down to top[c-1]. 15745287Sbostic */ 158*54579Sbostic sp0 = sp1 = sp; /* Stack position of biggest bin. */ 159*54579Sbostic bigc = 2; /* Size of biggest bin. */ 160*54579Sbostic if (endch == 0) /* Special case: set top[eos]. */ 161*54579Sbostic top[0] = ak = a + count[0]; 162*54579Sbostic else { 163*54579Sbostic ak = a; 164*54579Sbostic top[255] = an; 165*54579Sbostic } 166*54579Sbostic for (cp = count + bmin; nc > 0; cp++) { 167*54579Sbostic while (*cp == 0) 168*54579Sbostic cp++; 169*54579Sbostic if (*cp > 1) { 170*54579Sbostic if (*cp > bigc) { 171*54579Sbostic bigc = *cp; 172*54579Sbostic sp1 = sp; 173*54579Sbostic } 174*54579Sbostic push(ak, *cp, i+1); 17545345Sbostic } 176*54579Sbostic top[cp-count] = ak += *cp; 177*54579Sbostic nc--; 17845345Sbostic } 179*54579Sbostic swap(*sp0, *sp1, temp); /* Play it safe -- biggest bin last. */ 18045287Sbostic 18145287Sbostic /* 182*54579Sbostic * Permute misplacements home. Already home: everything 183*54579Sbostic * before aj, and in bin[c], items from top[c] on. 184*54579Sbostic * Inner loop: 185*54579Sbostic * r = next element to put in place; 186*54579Sbostic * ak = top[r[i]] = location to put the next element. 187*54579Sbostic * aj = bottom of 1st disordered bin. 188*54579Sbostic * Outer loop: 189*54579Sbostic * Once the 1st disordered bin is done, ie. aj >= ak, 190*54579Sbostic * aj<-aj + count[c] connects the bins in a linked list; 191*54579Sbostic * reset count[c]. 19245287Sbostic */ 193*54579Sbostic for (aj = a; aj < an; *aj = r, aj += count[c], count[c] = 0) 194*54579Sbostic for (r = *aj; aj < (ak = --top[c = tr[r[i]]]);) 195*54579Sbostic swap(*ak, r, t); 196*54579Sbostic } 197*54579Sbostic } 19845287Sbostic 199*54579Sbostic /* Stable sort, requiring additional memory. */ 200*54579Sbostic void 201*54579Sbostic r_sort_b(a, ta, n, i, tr, endch) 202*54579Sbostic const u_char **a, **ta; 203*54579Sbostic int n, i; 204*54579Sbostic const u_char *tr; 205*54579Sbostic u_int endch; 206*54579Sbostic { 207*54579Sbostic static int count[256], nc, bmin; 208*54579Sbostic register int c; 209*54579Sbostic register const u_char **ak, **ai; 210*54579Sbostic stack s[512], *sp, *sp0, *sp1, temp; 211*54579Sbostic const u_char **top[256]; 212*54579Sbostic int *cp, bigc; 21345287Sbostic 214*54579Sbostic sp = s; 215*54579Sbostic /* Set up stack. */ 216*54579Sbostic push(a, n, i); 217*54579Sbostic while (!empty(s)) { 218*54579Sbostic pop(a, n, i); 219*54579Sbostic if (n < THRESHOLD) { 220*54579Sbostic simplesort(a, n, i, tr, endch); 221*54579Sbostic continue; 22245287Sbostic } 223*54579Sbostic 224*54579Sbostic if (nc == 0) { 225*54579Sbostic bmin = 255; 226*54579Sbostic for (ak = a + n; --ak >= a;) { 227*54579Sbostic c = tr[(*ak)[i]]; 228*54579Sbostic if (++count[c] == 1 && c != endch) { 229*54579Sbostic if (c < bmin) 230*54579Sbostic bmin = c; 231*54579Sbostic nc++; 232*54579Sbostic } 233*54579Sbostic } 234*54579Sbostic if (sp + nc > s + SIZE) { 235*54579Sbostic r_sort_b(a, ta, n, i, tr, endch); 23645345Sbostic continue; 237*54579Sbostic } 23845345Sbostic } 239*54579Sbostic 240*54579Sbostic sp0 = sp1 = sp; /* Stack position of biggest bin. */ 241*54579Sbostic bigc = 2; /* Size of biggest bin. */ 242*54579Sbostic if (endch == 0) { /* Special case: set top[eos]. */ 243*54579Sbostic top[0] = ak = a + count[0]; 244*54579Sbostic count[0] = 0; 245*54579Sbostic } else { 246*54579Sbostic ak = a; 247*54579Sbostic top[255] = a + n; 248*54579Sbostic count[255] = 0; 249*54579Sbostic } 250*54579Sbostic for (cp = count + bmin; nc-- > 0; cp++) { 251*54579Sbostic while (*cp == 0) 252*54579Sbostic cp++; 253*54579Sbostic if ((c = *cp) > 1) { 254*54579Sbostic if (c > bigc) { 255*54579Sbostic bigc = c; 256*54579Sbostic sp1 = sp; 257*54579Sbostic } 258*54579Sbostic push(ak, c, i+1); 259*54579Sbostic } 260*54579Sbostic top[cp-count] = ak += c; 261*54579Sbostic *cp = 0; 262*54579Sbostic } 263*54579Sbostic swap(*sp0, *sp1, temp); /* Play it safe -- biggest bin last. */ 264*54579Sbostic 265*54579Sbostic for (ak = ta + n, ai = a+n; ak > ta;) 266*54579Sbostic *--ak = *--ai; 267*54579Sbostic for (ak = ta+n; --ak >= ta;) 268*54579Sbostic *--top[tr[(*ak)[i]]] = *ak; 26945287Sbostic } 27045287Sbostic } 271*54579Sbostic 27245935Sbostic static void 273*54579Sbostic simplesort(a, n, b, tr, endch) /* insertion sort */ 274*54579Sbostic register const u_char **a; 275*54579Sbostic int n, b; 276*54579Sbostic register const u_char *tr; 277*54579Sbostic u_int endch; 27845935Sbostic { 27951687Sbostic register u_char ch; 280*54579Sbostic const u_char **ak, **ai, *s, *t; 28145935Sbostic 282*54579Sbostic for (ak = a+1; --n >= 1; ak++) 283*54579Sbostic for (ai = ak; ai > a; ai--) { 284*54579Sbostic for (s = ai[0] + b, t = ai[-1] + b; 285*54579Sbostic (ch = tr[*s]) != endch; s++, t++) 286*54579Sbostic if (ch != tr[*t]) 28745935Sbostic break; 288*54579Sbostic if (ch >= tr[*t]) 289*54579Sbostic break; 290*54579Sbostic swap(ai[0], ai[-1], s); 291*54579Sbostic } 29245935Sbostic } 293