int radixsort(u_char const **a, int n, u_char const *tab, u_int endch) { u_char const *tr; u_int c; u_char tr0[256]; SETUP; r_sort_a(a, n, 0, tr, endch); return (0); }
int radixsort(const u_char **a, int n, const u_char *tab, u_int endch) { const u_char *tr; int c; u_char tr0[256]; SETUP; r_sort_a(a, n, 0, tr, endch); return (0); }
/* Unstable, in-place sort. */ static void r_sort_a(u_char const **a, int n, int i, u_char const *tr, u_int endch) { static u_int count[256]; static int nc; static u_int bmin; u_int c; u_char const **ak; u_char const *r; stack s[SIZE]; stack *sp; stack *sp0; stack *sp1; stack temp; u_int *cp; u_int bigc; u_char const **an; u_char const *t; u_char const **aj; u_char const **top[256]; /* Set up stack. */ sp = s; push(a, n, i); while (!empty(s)) { pop(a, n, i); if (n < THRESHOLD) { simplesort(a, n, i, tr, endch); continue; } an = a + n; /* Make character histogram. */ if (nc == 0) { bmin = 255; /* First occupied bin, excluding eos. */ for (ak = a; ak < an;) { c = tr[(*ak++)[i]]; if (++count[c] == 1 && c != endch) { if (c < bmin) bmin = c; nc++; } } if (sp + nc > s + SIZE) { /* Get more stack. */ r_sort_a(a, n, i, tr, endch); continue; } } /* * Set top[]; push incompletely sorted bins onto stack. * top[] = pointers to last out-of-place element in bins. * count[] = counts of elements in bins. * Before permuting: top[c-1] + count[c] = top[c]; * during deal: top[c] counts down to top[c-1]. */ sp0 = sp1 = sp; /* Stack position of biggest bin. */ bigc = 2; /* Size of biggest bin. */ if (endch == 0) /* Special case: set top[eos]. */ top[0] = ak = a + count[0]; else { ak = a; top[255] = an; } for (cp = count + bmin; nc > 0; cp++) { while (*cp == 0) /* Find next non-empty pile. */ cp++; if (*cp > 1) { if (*cp > bigc) { bigc = *cp; sp1 = sp; } push(ak, *cp, i+1); } top[cp-count] = ak += *cp; nc--; } swap(*sp0, *sp1, temp); /* Play it safe -- biggest bin last. */ /* * Permute misplacements home. Already home: everything * before aj, and in bin[c], items from top[c] on. * Inner loop: * r = next element to put in place; * ak = top[r[i]] = location to put the next element. * aj = bottom of 1st disordered bin. * Outer loop: * Once the 1st disordered bin is done, ie. aj >= ak, * aj<-aj + count[c] connects the bins in a linked list; * reset count[c]. */ for (aj = a; aj < an; *aj = r, aj += count[c], count[c] = 0) for (r = *aj; aj < (ak = --top[c = tr[r[i]]]);) swap(*ak, r, t); } }