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