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