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