1*3249d3dcSchristos /* $NetBSD: radixsort.c,v 1.22 2022/03/12 17:31:39 christos Exp $ */
26dda330eSthorpej
361f28255Scgd /*-
42f86deeaSmycroft * Copyright (c) 1990, 1993
52f86deeaSmycroft * The Regents of the University of California. All rights reserved.
62f86deeaSmycroft *
72f86deeaSmycroft * This code is derived from software contributed to Berkeley by
82f86deeaSmycroft * Peter McIlroy and by Dan Bernstein at New York University,
961f28255Scgd *
1061f28255Scgd * Redistribution and use in source and binary forms, with or without
1161f28255Scgd * modification, are permitted provided that the following conditions
1261f28255Scgd * are met:
1361f28255Scgd * 1. Redistributions of source code must retain the above copyright
1461f28255Scgd * notice, this list of conditions and the following disclaimer.
1561f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright
1661f28255Scgd * notice, this list of conditions and the following disclaimer in the
1761f28255Scgd * documentation and/or other materials provided with the distribution.
18eb7c1594Sagc * 3. Neither the name of the University nor the names of its contributors
1961f28255Scgd * may be used to endorse or promote products derived from this software
2061f28255Scgd * without specific prior written permission.
2161f28255Scgd *
2261f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2361f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2461f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2561f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2661f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2761f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2861f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2961f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3061f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3161f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3261f28255Scgd * SUCH DAMAGE.
3361f28255Scgd */
3461f28255Scgd
35bd906777Schristos #include <sys/cdefs.h>
3661f28255Scgd #if defined(LIBC_SCCS) && !defined(lint)
376dda330eSthorpej #if 0
385f8a6c06Sperry static char sccsid[] = "@(#)radixsort.c 8.2 (Berkeley) 4/28/95";
396dda330eSthorpej #else
40*3249d3dcSchristos __RCSID("$NetBSD: radixsort.c,v 1.22 2022/03/12 17:31:39 christos Exp $");
416dda330eSthorpej #endif
4261f28255Scgd #endif /* LIBC_SCCS and not lint */
4361f28255Scgd
442f86deeaSmycroft /*
452f86deeaSmycroft * Radixsort routines.
462f86deeaSmycroft *
472f86deeaSmycroft * Program r_sort_a() is unstable but uses O(logN) extra memory for a stack.
482f86deeaSmycroft * Use radixsort(a, n, trace, endchar) for this case.
492f86deeaSmycroft *
502f86deeaSmycroft * For stable sorting (using N extra pointers) use sradixsort(), which calls
512f86deeaSmycroft * r_sort_b().
522f86deeaSmycroft *
532f86deeaSmycroft * For a description of this code, see D. McIlroy, P. McIlroy, K. Bostic,
542f86deeaSmycroft * "Engineering Radix Sort".
552f86deeaSmycroft */
562f86deeaSmycroft
5743fa6fe3Sjtc #include "namespace.h"
5861f28255Scgd #include <sys/types.h>
59b48252f3Slukem
60b48252f3Slukem #include <assert.h>
612f86deeaSmycroft #include <errno.h>
62b48252f3Slukem #include <stdlib.h>
6361f28255Scgd
6443fa6fe3Sjtc #ifdef __weak_alias
6560549036Smycroft __weak_alias(radixsort,_radixsort)
6660549036Smycroft __weak_alias(sradixsort,_sradixsort)
6743fa6fe3Sjtc #endif
6843fa6fe3Sjtc
692f86deeaSmycroft typedef struct {
702f86deeaSmycroft const u_char **sa;
712f86deeaSmycroft int sn, si;
722f86deeaSmycroft } stack;
7361f28255Scgd
74602976ffSdsl static inline void simplesort(const u_char **, int, int, const u_char *, u_int);
75602976ffSdsl static void r_sort_a(const u_char **, int, int, const u_char *, u_int);
76602976ffSdsl static void r_sort_b(const u_char **,
77602976ffSdsl const u_char **, int, int, const u_char *, u_int);
7861f28255Scgd
792f86deeaSmycroft #define THRESHOLD 20 /* Divert to simplesort(). */
802f86deeaSmycroft #define SIZE 512 /* Default stack size. */
8161f28255Scgd
822f86deeaSmycroft #define SETUP { \
832f86deeaSmycroft if (tab == NULL) { \
842f86deeaSmycroft tr = tr0; \
852f86deeaSmycroft for (c = 0; c < endch; c++) \
862f86deeaSmycroft tr0[c] = c + 1; \
872f86deeaSmycroft tr0[c] = 0; \
882f86deeaSmycroft for (c++; c < 256; c++) \
892f86deeaSmycroft tr0[c] = c; \
902f86deeaSmycroft endch = 0; \
912f86deeaSmycroft } else { \
922f86deeaSmycroft endch = tab[endch]; \
932f86deeaSmycroft tr = tab; \
942f86deeaSmycroft if (endch != 0 && endch != 255) { \
952f86deeaSmycroft errno = EINVAL; \
962f86deeaSmycroft return (-1); \
972f86deeaSmycroft } \
982f86deeaSmycroft } \
9961f28255Scgd }
10061f28255Scgd
10161f28255Scgd int
radixsort(const u_char ** a,int n,const u_char * tab,u_int endch)102602976ffSdsl radixsort(const u_char **a, int n, const u_char *tab, u_int endch)
10361f28255Scgd {
1042f86deeaSmycroft const u_char *tr;
1059d1671f4Slukem u_int c;
1062f86deeaSmycroft u_char tr0[256];
10761f28255Scgd
108b48252f3Slukem _DIAGASSERT(a != NULL);
109b48252f3Slukem
1102f86deeaSmycroft SETUP;
1112f86deeaSmycroft r_sort_a(a, n, 0, tr, endch);
11261f28255Scgd return (0);
1132f86deeaSmycroft }
11461f28255Scgd
1152f86deeaSmycroft int
sradixsort(const u_char ** a,int n,const u_char * tab,u_int endch)116602976ffSdsl sradixsort(const u_char **a, int n, const u_char *tab, u_int endch)
1172f86deeaSmycroft {
1182f86deeaSmycroft const u_char *tr, **ta;
1199d1671f4Slukem u_int c;
1202f86deeaSmycroft u_char tr0[256];
12161f28255Scgd
122b48252f3Slukem _DIAGASSERT(a != NULL);
12311bcd8adSdsl if (a == NULL) {
124b48252f3Slukem errno = EFAULT;
125b48252f3Slukem return (-1);
126b48252f3Slukem }
127b48252f3Slukem
1282f86deeaSmycroft SETUP;
1292f86deeaSmycroft if (n < THRESHOLD)
1302f86deeaSmycroft simplesort(a, n, 0, tr, endch);
13161f28255Scgd else {
1321aca51f7Snia ta = NULL;
133*3249d3dcSchristos errno = reallocarr(&ta, n, sizeof(*ta));
134*3249d3dcSchristos if (errno)
135*3249d3dcSchristos return -1;
1362f86deeaSmycroft r_sort_b(a, ta, n, 0, tr, endch);
1372f86deeaSmycroft free(ta);
13861f28255Scgd }
13961f28255Scgd return (0);
14061f28255Scgd }
14161f28255Scgd
1422f86deeaSmycroft #define empty(s) (s >= sp)
1432f86deeaSmycroft #define pop(a, n, i) a = (--sp)->sa, n = sp->sn, i = sp->si
1442f86deeaSmycroft #define push(a, n, i) sp->sa = a, sp->sn = n, (sp++)->si = i
1452f86deeaSmycroft #define swap(a, b, t) t = a, a = b, b = t
14661f28255Scgd
1472f86deeaSmycroft /* Unstable, in-place sort. */
1485f8a6c06Sperry static void
r_sort_a(const u_char ** a,int n,int i,const u_char * tr,u_int endch)149602976ffSdsl r_sort_a(const u_char **a, int n, int i, const u_char *tr, u_int endch)
1502f86deeaSmycroft {
1519d1671f4Slukem static u_int count[256], nc, bmin;
1529d1671f4Slukem u_int c;
153c8bafd62Sperry const u_char **ak, *r;
1542f86deeaSmycroft stack s[SIZE], *sp, *sp0, *sp1, temp;
1559d1671f4Slukem u_int *cp, bigc;
1562f86deeaSmycroft const u_char **an, *t, **aj, **top[256];
1572f86deeaSmycroft
158b48252f3Slukem _DIAGASSERT(a != NULL);
159b48252f3Slukem _DIAGASSERT(tr != NULL);
160b48252f3Slukem
1612f86deeaSmycroft /* Set up stack. */
1622f86deeaSmycroft sp = s;
1632f86deeaSmycroft push(a, n, i);
1642f86deeaSmycroft while (!empty(s)) {
1652f86deeaSmycroft pop(a, n, i);
1662f86deeaSmycroft if (n < THRESHOLD) {
1672f86deeaSmycroft simplesort(a, n, i, tr, endch);
1682f86deeaSmycroft continue;
1692f86deeaSmycroft }
1702f86deeaSmycroft an = a + n;
1712f86deeaSmycroft
1722f86deeaSmycroft /* Make character histogram. */
1732f86deeaSmycroft if (nc == 0) {
1742f86deeaSmycroft bmin = 255; /* First occupied bin, excluding eos. */
1752f86deeaSmycroft for (ak = a; ak < an;) {
1762f86deeaSmycroft c = tr[(*ak++)[i]];
1772f86deeaSmycroft if (++count[c] == 1 && c != endch) {
1782f86deeaSmycroft if (c < bmin)
1792f86deeaSmycroft bmin = c;
1802f86deeaSmycroft nc++;
1812f86deeaSmycroft }
1822f86deeaSmycroft }
1832f86deeaSmycroft if (sp + nc > s + SIZE) { /* Get more stack. */
1842f86deeaSmycroft r_sort_a(a, n, i, tr, endch);
1852f86deeaSmycroft continue;
1862f86deeaSmycroft }
1872f86deeaSmycroft }
1882f86deeaSmycroft
1892f86deeaSmycroft /*
1902f86deeaSmycroft * Set top[]; push incompletely sorted bins onto stack.
1912f86deeaSmycroft * top[] = pointers to last out-of-place element in bins.
1922f86deeaSmycroft * count[] = counts of elements in bins.
1932f86deeaSmycroft * Before permuting: top[c-1] + count[c] = top[c];
1942f86deeaSmycroft * during deal: top[c] counts down to top[c-1].
1952f86deeaSmycroft */
1962f86deeaSmycroft sp0 = sp1 = sp; /* Stack position of biggest bin. */
1972f86deeaSmycroft bigc = 2; /* Size of biggest bin. */
1982f86deeaSmycroft if (endch == 0) /* Special case: set top[eos]. */
1992f86deeaSmycroft top[0] = ak = a + count[0];
2002f86deeaSmycroft else {
2012f86deeaSmycroft ak = a;
2022f86deeaSmycroft top[255] = an;
2032f86deeaSmycroft }
2042f86deeaSmycroft for (cp = count + bmin; nc > 0; cp++) {
2052f86deeaSmycroft while (*cp == 0) /* Find next non-empty pile. */
2062f86deeaSmycroft cp++;
2072f86deeaSmycroft if (*cp > 1) {
2082f86deeaSmycroft if (*cp > bigc) {
2092f86deeaSmycroft bigc = *cp;
2102f86deeaSmycroft sp1 = sp;
2112f86deeaSmycroft }
2122f86deeaSmycroft push(ak, *cp, i+1);
2132f86deeaSmycroft }
2142f86deeaSmycroft top[cp-count] = ak += *cp;
2152f86deeaSmycroft nc--;
2162f86deeaSmycroft }
2172f86deeaSmycroft swap(*sp0, *sp1, temp); /* Play it safe -- biggest bin last. */
2182f86deeaSmycroft
2192f86deeaSmycroft /*
2202f86deeaSmycroft * Permute misplacements home. Already home: everything
2212f86deeaSmycroft * before aj, and in bin[c], items from top[c] on.
2222f86deeaSmycroft * Inner loop:
2232f86deeaSmycroft * r = next element to put in place;
2242f86deeaSmycroft * ak = top[r[i]] = location to put the next element.
2252f86deeaSmycroft * aj = bottom of 1st disordered bin.
2262f86deeaSmycroft * Outer loop:
2272f86deeaSmycroft * Once the 1st disordered bin is done, ie. aj >= ak,
2282f86deeaSmycroft * aj<-aj + count[c] connects the bins in a linked list;
2292f86deeaSmycroft * reset count[c].
2302f86deeaSmycroft */
2312f86deeaSmycroft for (aj = a; aj < an; *aj = r, aj += count[c], count[c] = 0)
2322f86deeaSmycroft for (r = *aj; aj < (ak = --top[c = tr[r[i]]]);)
2332f86deeaSmycroft swap(*ak, r, t);
2342f86deeaSmycroft }
2352f86deeaSmycroft }
2362f86deeaSmycroft
2372f86deeaSmycroft /* Stable sort, requiring additional memory. */
2385f8a6c06Sperry static void
r_sort_b(const u_char ** a,const u_char ** ta,int n,int i,const u_char * tr,u_int endch)239602976ffSdsl r_sort_b(const u_char **a, const u_char **ta, int n, int i, const u_char *tr,
240602976ffSdsl u_int endch)
2412f86deeaSmycroft {
2429d1671f4Slukem static u_int count[256], nc, bmin;
2439d1671f4Slukem u_int c;
244c8bafd62Sperry const u_char **ak, **ai;
2452f86deeaSmycroft stack s[512], *sp, *sp0, *sp1, temp;
2462f86deeaSmycroft const u_char **top[256];
2479d1671f4Slukem u_int *cp, bigc;
2482f86deeaSmycroft
249b48252f3Slukem _DIAGASSERT(a != NULL);
250b48252f3Slukem _DIAGASSERT(ta != NULL);
251b48252f3Slukem _DIAGASSERT(tr != NULL);
252b48252f3Slukem
2532f86deeaSmycroft sp = s;
2542f86deeaSmycroft push(a, n, i);
2552f86deeaSmycroft while (!empty(s)) {
2562f86deeaSmycroft pop(a, n, i);
2572f86deeaSmycroft if (n < THRESHOLD) {
2582f86deeaSmycroft simplesort(a, n, i, tr, endch);
2592f86deeaSmycroft continue;
2602f86deeaSmycroft }
2612f86deeaSmycroft
2622f86deeaSmycroft if (nc == 0) {
2632f86deeaSmycroft bmin = 255;
2642f86deeaSmycroft for (ak = a + n; --ak >= a;) {
2652f86deeaSmycroft c = tr[(*ak)[i]];
2662f86deeaSmycroft if (++count[c] == 1 && c != endch) {
2672f86deeaSmycroft if (c < bmin)
2682f86deeaSmycroft bmin = c;
2692f86deeaSmycroft nc++;
2702f86deeaSmycroft }
2712f86deeaSmycroft }
2722f86deeaSmycroft if (sp + nc > s + SIZE) {
2732f86deeaSmycroft r_sort_b(a, ta, n, i, tr, endch);
2742f86deeaSmycroft continue;
2752f86deeaSmycroft }
2762f86deeaSmycroft }
2772f86deeaSmycroft
2782f86deeaSmycroft sp0 = sp1 = sp;
2792f86deeaSmycroft bigc = 2;
2802f86deeaSmycroft if (endch == 0) {
2812f86deeaSmycroft top[0] = ak = a + count[0];
2822f86deeaSmycroft count[0] = 0;
2832f86deeaSmycroft } else {
2842f86deeaSmycroft ak = a;
2852f86deeaSmycroft top[255] = a + n;
2862f86deeaSmycroft count[255] = 0;
2872f86deeaSmycroft }
2882f86deeaSmycroft for (cp = count + bmin; nc > 0; cp++) {
2892f86deeaSmycroft while (*cp == 0)
2902f86deeaSmycroft cp++;
2912f86deeaSmycroft if ((c = *cp) > 1) {
2922f86deeaSmycroft if (c > bigc) {
2932f86deeaSmycroft bigc = c;
2942f86deeaSmycroft sp1 = sp;
2952f86deeaSmycroft }
2962f86deeaSmycroft push(ak, c, i+1);
2972f86deeaSmycroft }
2982f86deeaSmycroft top[cp-count] = ak += c;
2992f86deeaSmycroft *cp = 0; /* Reset count[]. */
3002f86deeaSmycroft nc--;
3012f86deeaSmycroft }
3022f86deeaSmycroft swap(*sp0, *sp1, temp);
3032f86deeaSmycroft
3042f86deeaSmycroft for (ak = ta + n, ai = a+n; ak > ta;) /* Copy to temp. */
3052f86deeaSmycroft *--ak = *--ai;
3062f86deeaSmycroft for (ak = ta+n; --ak >= ta;) /* Deal to piles. */
3072f86deeaSmycroft *--top[tr[(*ak)[i]]] = *ak;
3082f86deeaSmycroft }
3092f86deeaSmycroft }
3102f86deeaSmycroft
311602976ffSdsl /* insertion sort */
3124e11af46Sperry static inline void
simplesort(const u_char ** a,int n,int b,const u_char * tr,u_int endch)313602976ffSdsl simplesort(const u_char **a, int n, int b, const u_char *tr, u_int endch)
3142f86deeaSmycroft {
315c8bafd62Sperry u_char ch;
3162f86deeaSmycroft const u_char **ak, **ai, *s, *t;
3172f86deeaSmycroft
318b48252f3Slukem _DIAGASSERT(a != NULL);
319b48252f3Slukem _DIAGASSERT(tr != NULL);
320b48252f3Slukem
3212f86deeaSmycroft for (ak = a+1; --n >= 1; ak++)
3222f86deeaSmycroft for (ai = ak; ai > a; ai--) {
3232f86deeaSmycroft for (s = ai[0] + b, t = ai[-1] + b;
3242f86deeaSmycroft (ch = tr[*s]) != endch; s++, t++)
3252f86deeaSmycroft if (ch != tr[*t])
32661f28255Scgd break;
3272f86deeaSmycroft if (ch >= tr[*t])
3282f86deeaSmycroft break;
3292f86deeaSmycroft swap(ai[0], ai[-1], s);
33061f28255Scgd }
33161f28255Scgd }
332