radixsort.c revision 251672
11590Srgrimes/*- 21590Srgrimes * Copyright (c) 1990, 1993 31590Srgrimes * The Regents of the University of California. All rights reserved. 41590Srgrimes * 51590Srgrimes * This code is derived from software contributed to Berkeley by 61590Srgrimes * Peter McIlroy and by Dan Bernstein at New York University, 71590Srgrimes * 81590Srgrimes * Redistribution and use in source and binary forms, with or without 91590Srgrimes * modification, are permitted provided that the following conditions 101590Srgrimes * are met: 111590Srgrimes * 1. Redistributions of source code must retain the above copyright 121590Srgrimes * notice, this list of conditions and the following disclaimer. 131590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 141590Srgrimes * notice, this list of conditions and the following disclaimer in the 151590Srgrimes * documentation and/or other materials provided with the distribution. 161590Srgrimes * 3. Neither the name of the University nor the names of its contributors 171590Srgrimes * may be used to endorse or promote products derived from this software 181590Srgrimes * without specific prior written permission. 191590Srgrimes * 201590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 211590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 221590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 231590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 241590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 251590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 261590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 271590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 281590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 291590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30207705Sdelphij * SUCH DAMAGE. 31207705Sdelphij */ 32207705Sdelphij 33207705Sdelphij#if defined(LIBC_SCCS) && !defined(lint) 34207705Sdelphijstatic char sccsid[] = "@(#)radixsort.c 8.2 (Berkeley) 4/28/95"; 35207705Sdelphij#endif /* LIBC_SCCS and not lint */ 3693604Sobrien#include <sys/cdefs.h> 3793604Sobrien__FBSDID("$FreeBSD: head/lib/libc/stdlib/radixsort.c 251672 2013-06-13 00:19:30Z emaste $"); 381590Srgrimes 391590Srgrimes/* 401590Srgrimes * Radixsort routines. 411590Srgrimes * 421590Srgrimes * Program r_sort_a() is unstable but uses O(logN) extra memory for a stack. 43200462Sdelphij * Use radixsort(a, n, trace, endchar) for this case. 4490878Simp * 4590878Simp * For stable sorting (using N extra pointers) use sradixsort(), which calls 46116333Smarkm * r_sort_b(). 4774584Sache * 4890878Simp * For a description of this code, see D. McIlroy, P. McIlroy, K. Bostic, 491590Srgrimes * "Engineering Radix Sort". 501590Srgrimes */ 511590Srgrimes 521590Srgrimes#include <sys/types.h> 531590Srgrimes#include <stdlib.h> 5490878Simp#include <stddef.h> 5590878Simp#include <errno.h> 561590Srgrimes 571590Srgrimestypedef struct { 5892786Smarkm const u_char **sa; 5992786Smarkm int sn, si; 601590Srgrimes} stack; 611590Srgrimes 62116333Smarkmstatic inline void simplesort 631590Srgrimes(const u_char **, int, int, const u_char *, u_int); 6490878Simpstatic void r_sort_a(const u_char **, int, int, const u_char *, u_int); 651590Srgrimesstatic void r_sort_b(const u_char **, const u_char **, int, int, 66241015Smdf const u_char *, u_int); 671590Srgrimes 68100495Srobert#define THRESHOLD 20 /* Divert to simplesort(). */ 69100495Srobert#define SIZE 512 /* Default stack size. */ 701590Srgrimes 711590Srgrimes#define SETUP { \ 721590Srgrimes if (tab == NULL) { \ 73225847Sed tr = tr0; \ 741590Srgrimes for (c = 0; c < endch; c++) \ 7594792Sobrien tr0[c] = c + 1; \ 761590Srgrimes tr0[c] = 0; \ 771590Srgrimes for (c++; c < 256; c++) \ 781590Srgrimes tr0[c] = c; \ 791590Srgrimes endch = 0; \ 801590Srgrimes } else { \ 811590Srgrimes endch = tab[endch]; \ 821590Srgrimes tr = tab; \ 831590Srgrimes if (endch != 0 && endch != 255) { \ 84116333Smarkm errno = EINVAL; \ 851590Srgrimes return (-1); \ 8617534Sache } \ 87116333Smarkm } \ 8874584Sache} 8974584Sache 901590Srgrimesint 9174584Sacheradixsort(a, n, tab, endch) 9274584Sache const u_char **a, *tab; 93116333Smarkm int n; 94116333Smarkm u_int endch; 951590Srgrimes{ 969987Swollman const u_char *tr; 97116333Smarkm int c; 9874584Sache u_char tr0[256]; 9974584Sache 10074584Sache SETUP; 10174584Sache r_sort_a(a, n, 0, tr, endch); 10274584Sache return (0); 10374584Sache} 10474584Sache 1051590Srgrimesint 1061590Srgrimessradixsort(a, n, tab, endch) 1071590Srgrimes const u_char **a, *tab; 108116333Smarkm int n; 1091590Srgrimes u_int endch; 1101590Srgrimes{ 11173258Simp const u_char *tr, **ta; 1121590Srgrimes int c; 11336787Simp u_char tr0[256]; 1141590Srgrimes 1151590Srgrimes SETUP; 1161590Srgrimes if (n < THRESHOLD) 1171590Srgrimes simplesort(a, n, 0, tr, endch); 1181590Srgrimes else { 1191590Srgrimes if ((ta = malloc(n * sizeof(a))) == NULL) 120 return (-1); 121 r_sort_b(a, ta, n, 0, tr, endch); 122 free(ta); 123 } 124 return (0); 125} 126 127#define empty(s) (s >= sp) 128#define pop(a, n, i) a = (--sp)->sa, n = sp->sn, i = sp->si 129#define push(a, n, i) sp->sa = a, sp->sn = n, (sp++)->si = i 130#define swap(a, b, t) t = a, a = b, b = t 131 132/* Unstable, in-place sort. */ 133static void 134r_sort_a(a, n, i, tr, endch) 135 const u_char **a; 136 int n, i; 137 const u_char *tr; 138 u_int endch; 139{ 140 static int count[256], nc, bmin; 141 int c; 142 const u_char **ak, *r; 143 stack s[SIZE], *sp, *sp0, *sp1, temp; 144 int *cp, bigc; 145 const u_char **an, *t, **aj, **top[256]; 146 147 /* Set up stack. */ 148 sp = s; 149 push(a, n, i); 150 while (!empty(s)) { 151 pop(a, n, i); 152 if (n < THRESHOLD) { 153 simplesort(a, n, i, tr, endch); 154 continue; 155 } 156 an = a + n; 157 158 /* Make character histogram. */ 159 if (nc == 0) { 160 bmin = 255; /* First occupied bin, excluding eos. */ 161 for (ak = a; ak < an;) { 162 c = tr[(*ak++)[i]]; 163 if (++count[c] == 1 && c != endch) { 164 if (c < bmin) 165 bmin = c; 166 nc++; 167 } 168 } 169 if (sp + nc > s + SIZE) { /* Get more stack. */ 170 r_sort_a(a, n, i, tr, endch); 171 continue; 172 } 173 } 174 175 /* 176 * Special case: if all strings have the same 177 * character at position i, move on to the next 178 * character. 179 */ 180 if (nc == 1 && count[bmin] == n) { 181 push(a, n, i+1); 182 nc = count[bmin] = 0; 183 continue; 184 } 185 186 /* 187 * Set top[]; push incompletely sorted bins onto stack. 188 * top[] = pointers to last out-of-place element in bins. 189 * count[] = counts of elements in bins. 190 * Before permuting: top[c-1] + count[c] = top[c]; 191 * during deal: top[c] counts down to top[c-1]. 192 */ 193 sp0 = sp1 = sp; /* Stack position of biggest bin. */ 194 bigc = 2; /* Size of biggest bin. */ 195 if (endch == 0) /* Special case: set top[eos]. */ 196 top[0] = ak = a + count[0]; 197 else { 198 ak = a; 199 top[255] = an; 200 } 201 for (cp = count + bmin; nc > 0; cp++) { 202 while (*cp == 0) /* Find next non-empty pile. */ 203 cp++; 204 if (*cp > 1) { 205 if (*cp > bigc) { 206 bigc = *cp; 207 sp1 = sp; 208 } 209 push(ak, *cp, i+1); 210 } 211 top[cp-count] = ak += *cp; 212 nc--; 213 } 214 swap(*sp0, *sp1, temp); /* Play it safe -- biggest bin last. */ 215 216 /* 217 * Permute misplacements home. Already home: everything 218 * before aj, and in bin[c], items from top[c] on. 219 * Inner loop: 220 * r = next element to put in place; 221 * ak = top[r[i]] = location to put the next element. 222 * aj = bottom of 1st disordered bin. 223 * Outer loop: 224 * Once the 1st disordered bin is done, ie. aj >= ak, 225 * aj<-aj + count[c] connects the bins in a linked list; 226 * reset count[c]. 227 */ 228 for (aj = a; aj < an; *aj = r, aj += count[c], count[c] = 0) 229 for (r = *aj; aj < (ak = --top[c = tr[r[i]]]);) 230 swap(*ak, r, t); 231 } 232} 233 234/* Stable sort, requiring additional memory. */ 235static void 236r_sort_b(a, ta, n, i, tr, endch) 237 const u_char **a, **ta; 238 int n, i; 239 const u_char *tr; 240 u_int endch; 241{ 242 static int count[256], nc, bmin; 243 int c; 244 const u_char **ak, **ai; 245 stack s[512], *sp, *sp0, *sp1, temp; 246 const u_char **top[256]; 247 int *cp, bigc; 248 249 sp = s; 250 push(a, n, i); 251 while (!empty(s)) { 252 pop(a, n, i); 253 if (n < THRESHOLD) { 254 simplesort(a, n, i, tr, endch); 255 continue; 256 } 257 258 if (nc == 0) { 259 bmin = 255; 260 for (ak = a + n; --ak >= a;) { 261 c = tr[(*ak)[i]]; 262 if (++count[c] == 1 && c != endch) { 263 if (c < bmin) 264 bmin = c; 265 nc++; 266 } 267 } 268 if (sp + nc > s + SIZE) { 269 r_sort_b(a, ta, n, i, tr, endch); 270 continue; 271 } 272 } 273 274 sp0 = sp1 = sp; 275 bigc = 2; 276 if (endch == 0) { 277 top[0] = ak = a + count[0]; 278 count[0] = 0; 279 } else { 280 ak = a; 281 top[255] = a + n; 282 count[255] = 0; 283 } 284 for (cp = count + bmin; nc > 0; cp++) { 285 while (*cp == 0) 286 cp++; 287 if ((c = *cp) > 1) { 288 if (c > bigc) { 289 bigc = c; 290 sp1 = sp; 291 } 292 push(ak, c, i+1); 293 } 294 top[cp-count] = ak += c; 295 *cp = 0; /* Reset count[]. */ 296 nc--; 297 } 298 swap(*sp0, *sp1, temp); 299 300 for (ak = ta + n, ai = a+n; ak > ta;) /* Copy to temp. */ 301 *--ak = *--ai; 302 for (ak = ta+n; --ak >= ta;) /* Deal to piles. */ 303 *--top[tr[(*ak)[i]]] = *ak; 304 } 305} 306 307static inline void 308simplesort(a, n, b, tr, endch) /* insertion sort */ 309 const u_char **a; 310 int n, b; 311 const u_char *tr; 312 u_int endch; 313{ 314 u_char ch; 315 const u_char **ak, **ai, *s, *t; 316 317 for (ak = a+1; --n >= 1; ak++) 318 for (ai = ak; ai > a; ai--) { 319 for (s = ai[0] + b, t = ai[-1] + b; 320 (ch = tr[*s]) != endch; s++, t++) 321 if (ch != tr[*t]) 322 break; 323 if (ch >= tr[*t]) 324 break; 325 swap(ai[0], ai[-1], s); 326 } 327} 328