radixsort.c revision 1.11 1 /* $NetBSD: radixsort.c,v 1.11 1998/02/03 18:44:19 perry 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.11 1998/02/03 18:44:19 perry 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 #include <stdlib.h>
64 #include <errno.h>
65
66 #ifdef __weak_alias
67 __weak_alias(radixsort,_radixsort);
68 __weak_alias(sradixsort,_sradixsort);
69 #endif
70
71 typedef struct {
72 const u_char **sa;
73 int sn, si;
74 } stack;
75
76 static __inline void simplesort
77 __P((const u_char **, int, int, const u_char *, u_int));
78 static void r_sort_a __P((const u_char **, int, int, const u_char *, u_int));
79 static void r_sort_b __P((const u_char **,
80 const u_char **, int, int, const u_char *, u_int));
81
82 #define THRESHOLD 20 /* Divert to simplesort(). */
83 #define SIZE 512 /* Default stack size. */
84
85 #define SETUP { \
86 if (tab == NULL) { \
87 tr = tr0; \
88 for (c = 0; c < endch; c++) \
89 tr0[c] = c + 1; \
90 tr0[c] = 0; \
91 for (c++; c < 256; c++) \
92 tr0[c] = c; \
93 endch = 0; \
94 } else { \
95 endch = tab[endch]; \
96 tr = tab; \
97 if (endch != 0 && endch != 255) { \
98 errno = EINVAL; \
99 return (-1); \
100 } \
101 } \
102 }
103
104 int
105 radixsort(a, n, tab, endch)
106 const u_char **a, *tab;
107 int n;
108 u_int endch;
109 {
110 const u_char *tr;
111 int c;
112 u_char tr0[256];
113
114 SETUP;
115 r_sort_a(a, n, 0, tr, endch);
116 return (0);
117 }
118
119 int
120 sradixsort(a, n, tab, endch)
121 const u_char **a, *tab;
122 int n;
123 u_int endch;
124 {
125 const u_char *tr, **ta;
126 int c;
127 u_char tr0[256];
128
129 SETUP;
130 if (n < THRESHOLD)
131 simplesort(a, n, 0, tr, endch);
132 else {
133 if ((ta = malloc(n * sizeof(a))) == NULL)
134 return (-1);
135 r_sort_b(a, ta, n, 0, tr, endch);
136 free(ta);
137 }
138 return (0);
139 }
140
141 #define empty(s) (s >= sp)
142 #define pop(a, n, i) a = (--sp)->sa, n = sp->sn, i = sp->si
143 #define push(a, n, i) sp->sa = a, sp->sn = n, (sp++)->si = i
144 #define swap(a, b, t) t = a, a = b, b = t
145
146 /* Unstable, in-place sort. */
147 static void
148 r_sort_a(a, n, i, tr, endch)
149 const u_char **a;
150 int n, i;
151 const u_char *tr;
152 u_int endch;
153 {
154 static int count[256], nc, bmin;
155 int c;
156 const u_char **ak, *r;
157 stack s[SIZE], *sp, *sp0, *sp1, temp;
158 int *cp, bigc;
159 const u_char **an, *t, **aj, **top[256];
160
161 /* Set up stack. */
162 sp = s;
163 push(a, n, i);
164 while (!empty(s)) {
165 pop(a, n, i);
166 if (n < THRESHOLD) {
167 simplesort(a, n, i, tr, endch);
168 continue;
169 }
170 an = a + n;
171
172 /* Make character histogram. */
173 if (nc == 0) {
174 bmin = 255; /* First occupied bin, excluding eos. */
175 for (ak = a; ak < an;) {
176 c = tr[(*ak++)[i]];
177 if (++count[c] == 1 && c != endch) {
178 if (c < bmin)
179 bmin = c;
180 nc++;
181 }
182 }
183 if (sp + nc > s + SIZE) { /* Get more stack. */
184 r_sort_a(a, n, i, tr, endch);
185 continue;
186 }
187 }
188
189 /*
190 * Set top[]; push incompletely sorted bins onto stack.
191 * top[] = pointers to last out-of-place element in bins.
192 * count[] = counts of elements in bins.
193 * Before permuting: top[c-1] + count[c] = top[c];
194 * during deal: top[c] counts down to top[c-1].
195 */
196 sp0 = sp1 = sp; /* Stack position of biggest bin. */
197 bigc = 2; /* Size of biggest bin. */
198 if (endch == 0) /* Special case: set top[eos]. */
199 top[0] = ak = a + count[0];
200 else {
201 ak = a;
202 top[255] = an;
203 }
204 for (cp = count + bmin; nc > 0; cp++) {
205 while (*cp == 0) /* Find next non-empty pile. */
206 cp++;
207 if (*cp > 1) {
208 if (*cp > bigc) {
209 bigc = *cp;
210 sp1 = sp;
211 }
212 push(ak, *cp, i+1);
213 }
214 top[cp-count] = ak += *cp;
215 nc--;
216 }
217 swap(*sp0, *sp1, temp); /* Play it safe -- biggest bin last. */
218
219 /*
220 * Permute misplacements home. Already home: everything
221 * before aj, and in bin[c], items from top[c] on.
222 * Inner loop:
223 * r = next element to put in place;
224 * ak = top[r[i]] = location to put the next element.
225 * aj = bottom of 1st disordered bin.
226 * Outer loop:
227 * Once the 1st disordered bin is done, ie. aj >= ak,
228 * aj<-aj + count[c] connects the bins in a linked list;
229 * reset count[c].
230 */
231 for (aj = a; aj < an; *aj = r, aj += count[c], count[c] = 0)
232 for (r = *aj; aj < (ak = --top[c = tr[r[i]]]);)
233 swap(*ak, r, t);
234 }
235 }
236
237 /* Stable sort, requiring additional memory. */
238 static void
239 r_sort_b(a, ta, n, i, tr, endch)
240 const u_char **a, **ta;
241 int n, i;
242 const u_char *tr;
243 u_int endch;
244 {
245 static int count[256], nc, bmin;
246 int c;
247 const u_char **ak, **ai;
248 stack s[512], *sp, *sp0, *sp1, temp;
249 const u_char **top[256];
250 int *cp, bigc;
251
252 sp = s;
253 push(a, n, i);
254 while (!empty(s)) {
255 pop(a, n, i);
256 if (n < THRESHOLD) {
257 simplesort(a, n, i, tr, endch);
258 continue;
259 }
260
261 if (nc == 0) {
262 bmin = 255;
263 for (ak = a + n; --ak >= a;) {
264 c = tr[(*ak)[i]];
265 if (++count[c] == 1 && c != endch) {
266 if (c < bmin)
267 bmin = c;
268 nc++;
269 }
270 }
271 if (sp + nc > s + SIZE) {
272 r_sort_b(a, ta, n, i, tr, endch);
273 continue;
274 }
275 }
276
277 sp0 = sp1 = sp;
278 bigc = 2;
279 if (endch == 0) {
280 top[0] = ak = a + count[0];
281 count[0] = 0;
282 } else {
283 ak = a;
284 top[255] = a + n;
285 count[255] = 0;
286 }
287 for (cp = count + bmin; nc > 0; cp++) {
288 while (*cp == 0)
289 cp++;
290 if ((c = *cp) > 1) {
291 if (c > bigc) {
292 bigc = c;
293 sp1 = sp;
294 }
295 push(ak, c, i+1);
296 }
297 top[cp-count] = ak += c;
298 *cp = 0; /* Reset count[]. */
299 nc--;
300 }
301 swap(*sp0, *sp1, temp);
302
303 for (ak = ta + n, ai = a+n; ak > ta;) /* Copy to temp. */
304 *--ak = *--ai;
305 for (ak = ta+n; --ak >= ta;) /* Deal to piles. */
306 *--top[tr[(*ak)[i]]] = *ak;
307 }
308 }
309
310 static __inline void
311 simplesort(a, n, b, tr, endch) /* insertion sort */
312 const u_char **a;
313 int n, b;
314 const u_char *tr;
315 u_int endch;
316 {
317 u_char ch;
318 const u_char **ak, **ai, *s, *t;
319
320 for (ak = a+1; --n >= 1; ak++)
321 for (ai = ak; ai > a; ai--) {
322 for (s = ai[0] + b, t = ai[-1] + b;
323 (ch = tr[*s]) != endch; s++, t++)
324 if (ch != tr[*t])
325 break;
326 if (ch >= tr[*t])
327 break;
328 swap(ai[0], ai[-1], s);
329 }
330 }
331