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