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