]>
git.saurik.com Git - apple/libc.git/blob - stdlib/FreeBSD/radixsort.c
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Peter McIlroy and by Dan Bernstein at New York University,
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 #if defined(LIBC_SCCS) && !defined(lint)
38 static char sccsid
[] = "@(#)radixsort.c 8.2 (Berkeley) 4/28/95";
39 #endif /* LIBC_SCCS and not lint */
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD: src/lib/libc/stdlib/radixsort.c,v 1.6 2002/03/22 09:18:34 obrien Exp $");
46 * Program r_sort_a() is unstable but uses O(logN) extra memory for a stack.
47 * Use radixsort(a, n, trace, endchar) for this case.
49 * For stable sorting (using N extra pointers) use sradixsort(), which calls
52 * For a description of this code, see D. McIlroy, P. McIlroy, K. Bostic,
53 * "Engineering Radix Sort".
56 #include <sys/types.h>
66 static inline void simplesort
67 (const u_char
**, int, int, const u_char
*, u_int
);
68 static void r_sort_a(const u_char
**, int, int, const u_char
*, u_int
);
69 static void r_sort_b(const u_char
**, const u_char
**, int, int,
70 const u_char
*, u_int
);
72 #define THRESHOLD 20 /* Divert to simplesort(). */
73 #define SIZE 512 /* Default stack size. */
78 for (c = 0; c < endch; c++) \
81 for (c++; c < 256; c++) \
87 if (endch != 0 && endch != 255) { \
95 radixsort(a
, n
, tab
, endch
)
96 const u_char
**a
, *tab
;
105 r_sort_a(a
, n
, 0, tr
, endch
);
110 sradixsort(a
, n
, tab
, endch
)
111 const u_char
**a
, *tab
;
115 const u_char
*tr
, **ta
;
121 simplesort(a
, n
, 0, tr
, endch
);
123 if ((ta
= malloc(n
* sizeof(a
))) == NULL
)
125 r_sort_b(a
, ta
, n
, 0, tr
, endch
);
131 #define empty(s) (s >= sp)
132 #define pop(a, n, i) a = (--sp)->sa, n = sp->sn, i = sp->si
133 #define push(a, n, i) sp->sa = a, sp->sn = n, (sp++)->si = i
134 #define swap(a, b, t) t = a, a = b, b = t
136 /* Unstable, in-place sort. */
138 r_sort_a(a
, n
, i
, tr
, endch
)
144 static int count
[256], nc
, bmin
;
146 const u_char
**ak
, *r
;
147 stack s
[SIZE
], *sp
, *sp0
, *sp1
, temp
;
149 const u_char
**an
, *t
, **aj
, **top
[256];
157 simplesort(a
, n
, i
, tr
, endch
);
162 /* Make character histogram. */
164 bmin
= 255; /* First occupied bin, excluding eos. */
165 for (ak
= a
; ak
< an
;) {
167 if (++count
[c
] == 1 && c
!= endch
) {
173 if (sp
+ nc
> s
+ SIZE
) { /* Get more stack. */
174 r_sort_a(a
, n
, i
, tr
, endch
);
180 * Set top[]; push incompletely sorted bins onto stack.
181 * top[] = pointers to last out-of-place element in bins.
182 * count[] = counts of elements in bins.
183 * Before permuting: top[c-1] + count[c] = top[c];
184 * during deal: top[c] counts down to top[c-1].
186 sp0
= sp1
= sp
; /* Stack position of biggest bin. */
187 bigc
= 2; /* Size of biggest bin. */
188 if (endch
== 0) /* Special case: set top[eos]. */
189 top
[0] = ak
= a
+ count
[0];
194 for (cp
= count
+ bmin
; nc
> 0; cp
++) {
195 while (*cp
== 0) /* Find next non-empty pile. */
204 top
[cp
-count
] = ak
+= *cp
;
207 swap(*sp0
, *sp1
, temp
); /* Play it safe -- biggest bin last. */
210 * Permute misplacements home. Already home: everything
211 * before aj, and in bin[c], items from top[c] on.
213 * r = next element to put in place;
214 * ak = top[r[i]] = location to put the next element.
215 * aj = bottom of 1st disordered bin.
217 * Once the 1st disordered bin is done, ie. aj >= ak,
218 * aj<-aj + count[c] connects the bins in a linked list;
221 for (aj
= a
; aj
< an
; *aj
= r
, aj
+= count
[c
], count
[c
] = 0)
222 for (r
= *aj
; aj
< (ak
= --top
[c
= tr
[r
[i
]]]);)
227 /* Stable sort, requiring additional memory. */
229 r_sort_b(a
, ta
, n
, i
, tr
, endch
)
230 const u_char
**a
, **ta
;
235 static int count
[256], nc
, bmin
;
237 const u_char
**ak
, **ai
;
238 stack s
[512], *sp
, *sp0
, *sp1
, temp
;
239 const u_char
**top
[256];
247 simplesort(a
, n
, i
, tr
, endch
);
253 for (ak
= a
+ n
; --ak
>= a
;) {
255 if (++count
[c
] == 1 && c
!= endch
) {
261 if (sp
+ nc
> s
+ SIZE
) {
262 r_sort_b(a
, ta
, n
, i
, tr
, endch
);
270 top
[0] = ak
= a
+ count
[0];
277 for (cp
= count
+ bmin
; nc
> 0; cp
++) {
287 top
[cp
-count
] = ak
+= c
;
288 *cp
= 0; /* Reset count[]. */
291 swap(*sp0
, *sp1
, temp
);
293 for (ak
= ta
+ n
, ai
= a
+n
; ak
> ta
;) /* Copy to temp. */
295 for (ak
= ta
+n
; --ak
>= ta
;) /* Deal to piles. */
296 *--top
[tr
[(*ak
)[i
]]] = *ak
;
301 simplesort(a
, n
, b
, tr
, endch
) /* insertion sort */
308 const u_char
**ak
, **ai
, *s
, *t
;
310 for (ak
= a
+1; --n
>= 1; ak
++)
311 for (ai
= ak
; ai
> a
; ai
--) {
312 for (s
= ai
[0] + b
, t
= ai
[-1] + b
;
313 (ch
= tr
[*s
]) != endch
; s
++, t
++)
318 swap(ai
[0], ai
[-1], s
);