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