1 /* $NetBSD: radixsort.c,v 1.21 2021/10/29 11:03:46 nia Exp $ */ 2 3 /*- 4 * Copyright (c) 1990, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * This code is derived from software contributed to Berkeley by 8 * Peter McIlroy and by Dan Bernstein at New York University, 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 */ 34 35 #include <sys/cdefs.h> 36 #if defined(LIBC_SCCS) && !defined(lint) 37 #if 0 38 static char sccsid[] = "@(#)radixsort.c 8.2 (Berkeley) 4/28/95"; 39 #else 40 __RCSID("$NetBSD: radixsort.c,v 1.21 2021/10/29 11:03:46 nia Exp $"); 41 #endif 42 #endif /* LIBC_SCCS and not lint */ 43 44 /* 45 * Radixsort routines. 46 * 47 * Program r_sort_a() is unstable but uses O(logN) extra memory for a stack. 48 * Use radixsort(a, n, trace, endchar) for this case. 49 * 50 * For stable sorting (using N extra pointers) use sradixsort(), which calls 51 * r_sort_b(). 52 * 53 * For a description of this code, see D. McIlroy, P. McIlroy, K. Bostic, 54 * "Engineering Radix Sort". 55 */ 56 57 #include "namespace.h" 58 #include <sys/types.h> 59 60 #include <assert.h> 61 #include <errno.h> 62 #include <stdlib.h> 63 64 #ifdef __weak_alias 65 __weak_alias(radixsort,_radixsort) 66 __weak_alias(sradixsort,_sradixsort) 67 #endif 68 69 typedef struct { 70 const u_char **sa; 71 int sn, si; 72 } stack; 73 74 static inline void simplesort(const u_char **, int, int, const u_char *, u_int); 75 static void r_sort_a(const u_char **, int, int, const u_char *, u_int); 76 static void r_sort_b(const u_char **, 77 const u_char **, int, int, const u_char *, u_int); 78 79 #define THRESHOLD 20 /* Divert to simplesort(). */ 80 #define SIZE 512 /* Default stack size. */ 81 82 #define SETUP { \ 83 if (tab == NULL) { \ 84 tr = tr0; \ 85 for (c = 0; c < endch; c++) \ 86 tr0[c] = c + 1; \ 87 tr0[c] = 0; \ 88 for (c++; c < 256; c++) \ 89 tr0[c] = c; \ 90 endch = 0; \ 91 } else { \ 92 endch = tab[endch]; \ 93 tr = tab; \ 94 if (endch != 0 && endch != 255) { \ 95 errno = EINVAL; \ 96 return (-1); \ 97 } \ 98 } \ 99 } 100 101 int 102 radixsort(const u_char **a, int n, const u_char *tab, u_int endch) 103 { 104 const u_char *tr; 105 u_int c; 106 u_char tr0[256]; 107 108 _DIAGASSERT(a != NULL); 109 110 SETUP; 111 r_sort_a(a, n, 0, tr, endch); 112 return (0); 113 } 114 115 int 116 sradixsort(const u_char **a, int n, const u_char *tab, u_int endch) 117 { 118 const u_char *tr, **ta; 119 u_int c; 120 u_char tr0[256]; 121 122 _DIAGASSERT(a != NULL); 123 if (a == NULL) { 124 errno = EFAULT; 125 return (-1); 126 } 127 128 SETUP; 129 if (n < THRESHOLD) 130 simplesort(a, n, 0, tr, endch); 131 else { 132 ta = NULL; 133 if (reallocarr(&ta, n, sizeof(a)) != 0) { 134 errno = ENOMEM; 135 return (-1); 136 } 137 r_sort_b(a, ta, n, 0, tr, endch); 138 free(ta); 139 } 140 return (0); 141 } 142 143 #define empty(s) (s >= sp) 144 #define pop(a, n, i) a = (--sp)->sa, n = sp->sn, i = sp->si 145 #define push(a, n, i) sp->sa = a, sp->sn = n, (sp++)->si = i 146 #define swap(a, b, t) t = a, a = b, b = t 147 148 /* Unstable, in-place sort. */ 149 static void 150 r_sort_a(const u_char **a, int n, int i, const u_char *tr, u_int endch) 151 { 152 static u_int count[256], nc, bmin; 153 u_int c; 154 const u_char **ak, *r; 155 stack s[SIZE], *sp, *sp0, *sp1, temp; 156 u_int *cp, bigc; 157 const u_char **an, *t, **aj, **top[256]; 158 159 _DIAGASSERT(a != NULL); 160 _DIAGASSERT(tr != NULL); 161 162 /* Set up stack. */ 163 sp = s; 164 push(a, n, i); 165 while (!empty(s)) { 166 pop(a, n, i); 167 if (n < THRESHOLD) { 168 simplesort(a, n, i, tr, endch); 169 continue; 170 } 171 an = a + n; 172 173 /* Make character histogram. */ 174 if (nc == 0) { 175 bmin = 255; /* First occupied bin, excluding eos. */ 176 for (ak = a; ak < an;) { 177 c = tr[(*ak++)[i]]; 178 if (++count[c] == 1 && c != endch) { 179 if (c < bmin) 180 bmin = c; 181 nc++; 182 } 183 } 184 if (sp + nc > s + SIZE) { /* Get more stack. */ 185 r_sort_a(a, n, i, tr, endch); 186 continue; 187 } 188 } 189 190 /* 191 * Set top[]; push incompletely sorted bins onto stack. 192 * top[] = pointers to last out-of-place element in bins. 193 * count[] = counts of elements in bins. 194 * Before permuting: top[c-1] + count[c] = top[c]; 195 * during deal: top[c] counts down to top[c-1]. 196 */ 197 sp0 = sp1 = sp; /* Stack position of biggest bin. */ 198 bigc = 2; /* Size of biggest bin. */ 199 if (endch == 0) /* Special case: set top[eos]. */ 200 top[0] = ak = a + count[0]; 201 else { 202 ak = a; 203 top[255] = an; 204 } 205 for (cp = count + bmin; nc > 0; cp++) { 206 while (*cp == 0) /* Find next non-empty pile. */ 207 cp++; 208 if (*cp > 1) { 209 if (*cp > bigc) { 210 bigc = *cp; 211 sp1 = sp; 212 } 213 push(ak, *cp, i+1); 214 } 215 top[cp-count] = ak += *cp; 216 nc--; 217 } 218 swap(*sp0, *sp1, temp); /* Play it safe -- biggest bin last. */ 219 220 /* 221 * Permute misplacements home. Already home: everything 222 * before aj, and in bin[c], items from top[c] on. 223 * Inner loop: 224 * r = next element to put in place; 225 * ak = top[r[i]] = location to put the next element. 226 * aj = bottom of 1st disordered bin. 227 * Outer loop: 228 * Once the 1st disordered bin is done, ie. aj >= ak, 229 * aj<-aj + count[c] connects the bins in a linked list; 230 * reset count[c]. 231 */ 232 for (aj = a; aj < an; *aj = r, aj += count[c], count[c] = 0) 233 for (r = *aj; aj < (ak = --top[c = tr[r[i]]]);) 234 swap(*ak, r, t); 235 } 236 } 237 238 /* Stable sort, requiring additional memory. */ 239 static void 240 r_sort_b(const u_char **a, const u_char **ta, int n, int i, const u_char *tr, 241 u_int endch) 242 { 243 static u_int count[256], nc, bmin; 244 u_int c; 245 const u_char **ak, **ai; 246 stack s[512], *sp, *sp0, *sp1, temp; 247 const u_char **top[256]; 248 u_int *cp, bigc; 249 250 _DIAGASSERT(a != NULL); 251 _DIAGASSERT(ta != NULL); 252 _DIAGASSERT(tr != NULL); 253 254 sp = s; 255 push(a, n, i); 256 while (!empty(s)) { 257 pop(a, n, i); 258 if (n < THRESHOLD) { 259 simplesort(a, n, i, tr, endch); 260 continue; 261 } 262 263 if (nc == 0) { 264 bmin = 255; 265 for (ak = a + n; --ak >= a;) { 266 c = tr[(*ak)[i]]; 267 if (++count[c] == 1 && c != endch) { 268 if (c < bmin) 269 bmin = c; 270 nc++; 271 } 272 } 273 if (sp + nc > s + SIZE) { 274 r_sort_b(a, ta, n, i, tr, endch); 275 continue; 276 } 277 } 278 279 sp0 = sp1 = sp; 280 bigc = 2; 281 if (endch == 0) { 282 top[0] = ak = a + count[0]; 283 count[0] = 0; 284 } else { 285 ak = a; 286 top[255] = a + n; 287 count[255] = 0; 288 } 289 for (cp = count + bmin; nc > 0; cp++) { 290 while (*cp == 0) 291 cp++; 292 if ((c = *cp) > 1) { 293 if (c > bigc) { 294 bigc = c; 295 sp1 = sp; 296 } 297 push(ak, c, i+1); 298 } 299 top[cp-count] = ak += c; 300 *cp = 0; /* Reset count[]. */ 301 nc--; 302 } 303 swap(*sp0, *sp1, temp); 304 305 for (ak = ta + n, ai = a+n; ak > ta;) /* Copy to temp. */ 306 *--ak = *--ai; 307 for (ak = ta+n; --ak >= ta;) /* Deal to piles. */ 308 *--top[tr[(*ak)[i]]] = *ak; 309 } 310 } 311 312 /* insertion sort */ 313 static inline void 314 simplesort(const u_char **a, int n, int b, const u_char *tr, u_int endch) 315 { 316 u_char ch; 317 const u_char **ak, **ai, *s, *t; 318 319 _DIAGASSERT(a != NULL); 320 _DIAGASSERT(tr != NULL); 321 322 for (ak = a+1; --n >= 1; ak++) 323 for (ai = ak; ai > a; ai--) { 324 for (s = ai[0] + b, t = ai[-1] + b; 325 (ch = tr[*s]) != endch; s++, t++) 326 if (ch != tr[*t]) 327 break; 328 if (ch >= tr[*t]) 329 break; 330 swap(ai[0], ai[-1], s); 331 } 332 } 333