]>
git.saurik.com Git - apple/libc.git/blob - stdlib/radixsort.c
44a39d0cf0285447136aad3f46f86cf2e7374ca4
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1990, 1993
24 * The Regents of the University of California. All rights reserved.
26 * This code is derived from software contributed to Berkeley by
27 * Peter McIlroy and by Dan Bernstein at New York University,
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * Program r_sort_a() is unstable but uses O(logN) extra memory for a stack.
63 * Use radixsort(a, n, trace, endchar) for this case.
65 * For stable sorting (using N extra pointers) use sradixsort(), which calls
68 * For a description of this code, see D. McIlroy, P. McIlroy, K. Bostic,
69 * "Engineering Radix Sort".
72 #include <sys/types.h>
82 static inline void simplesort
83 __P((const u_char
**, int, int, const u_char
*, u_int
));
84 static void r_sort_a
__P((const u_char
**, int, int, const u_char
*, u_int
));
85 static void r_sort_b
__P((const u_char
**,
86 const u_char
**, int, int, const u_char
*, u_int
));
88 #define THRESHOLD 20 /* Divert to simplesort(). */
89 #define SIZE 512 /* Default stack size. */
94 for (c = 0; c < endch; c++) \
97 for (c++; c < 256; c++) \
101 endch = tab[endch]; \
103 if (endch != 0 && endch != 255) { \
111 radixsort(a
, n
, tab
, endch
)
112 const u_char
**a
, *tab
;
121 r_sort_a(a
, n
, 0, tr
, endch
);
126 sradixsort(a
, n
, tab
, endch
)
127 const u_char
**a
, *tab
;
131 const u_char
*tr
, **ta
;
137 simplesort(a
, n
, 0, tr
, endch
);
139 if ((ta
= malloc(n
* sizeof(a
))) == NULL
)
141 r_sort_b(a
, ta
, n
, 0, tr
, endch
);
147 #define empty(s) (s >= sp)
148 #define pop(a, n, i) a = (--sp)->sa, n = sp->sn, i = sp->si
149 #define push(a, n, i) sp->sa = a, sp->sn = n, (sp++)->si = i
150 #define swap(a, b, t) t = a, a = b, b = t
152 /* Unstable, in-place sort. */
154 r_sort_a(a
, n
, i
, tr
, endch
)
160 static int *count
= NULL
, nc
, bmin
;
162 register const u_char
**ak
, *r
;
163 stack s
[SIZE
], *sp
, *sp0
, *sp1
, temp
;
165 const u_char
**an
, *t
, **aj
, **top
[256];
167 if( count
== NULL
) {
168 count
= malloc(256*sizeof(int));
179 simplesort(a
, n
, i
, tr
, endch
);
184 /* Make character histogram. */
186 bmin
= 255; /* First occupied bin, excluding eos. */
187 for (ak
= a
; ak
< an
;) {
189 if (++count
[c
] == 1 && c
!= endch
) {
195 if (sp
+ nc
> s
+ SIZE
) { /* Get more stack. */
196 r_sort_a(a
, n
, i
, tr
, endch
);
202 * Set top[]; push incompletely sorted bins onto stack.
203 * top[] = pointers to last out-of-place element in bins.
204 * count[] = counts of elements in bins.
205 * Before permuting: top[c-1] + count[c] = top[c];
206 * during deal: top[c] counts down to top[c-1].
208 sp0
= sp1
= sp
; /* Stack position of biggest bin. */
209 bigc
= 2; /* Size of biggest bin. */
210 if (endch
== 0) /* Special case: set top[eos]. */
211 top
[0] = ak
= a
+ count
[0];
216 for (cp
= count
+ bmin
; nc
> 0; cp
++) {
217 while (*cp
== 0) /* Find next non-empty pile. */
226 top
[cp
-count
] = ak
+= *cp
;
229 swap(*sp0
, *sp1
, temp
); /* Play it safe -- biggest bin last. */
232 * Permute misplacements home. Already home: everything
233 * before aj, and in bin[c], items from top[c] on.
235 * r = next element to put in place;
236 * ak = top[r[i]] = location to put the next element.
237 * aj = bottom of 1st disordered bin.
239 * Once the 1st disordered bin is done, ie. aj >= ak,
240 * aj<-aj + count[c] connects the bins in a linked list;
243 for (aj
= a
; aj
< an
; *aj
= r
, aj
+= count
[c
], count
[c
] = 0)
244 for (r
= *aj
; aj
< (ak
= --top
[c
= tr
[r
[i
]]]);)
249 /* Stable sort, requiring additional memory. */
251 r_sort_b(a
, ta
, n
, i
, tr
, endch
)
252 const u_char
**a
, **ta
;
257 static int *count
= NULL
, nc
, bmin
;
259 register const u_char
**ak
, **ai
;
260 stack s
[512], *sp
, *sp0
, *sp1
, temp
;
261 const u_char
**top
[256];
264 if( count
== NULL
) {
265 count
= malloc(256*sizeof(int));
275 simplesort(a
, n
, i
, tr
, endch
);
281 for (ak
= a
+ n
; --ak
>= a
;) {
283 if (++count
[c
] == 1 && c
!= endch
) {
289 if (sp
+ nc
> s
+ SIZE
) {
290 r_sort_b(a
, ta
, n
, i
, tr
, endch
);
298 top
[0] = ak
= a
+ count
[0];
305 for (cp
= count
+ bmin
; nc
> 0; cp
++) {
315 top
[cp
-count
] = ak
+= c
;
316 *cp
= 0; /* Reset count[]. */
319 swap(*sp0
, *sp1
, temp
);
321 for (ak
= ta
+ n
, ai
= a
+n
; ak
> ta
;) /* Copy to temp. */
323 for (ak
= ta
+n
; --ak
>= ta
;) /* Deal to piles. */
324 *--top
[tr
[(*ak
)[i
]]] = *ak
;
329 simplesort(a
, n
, b
, tr
, endch
) /* insertion sort */
330 register const u_char
**a
;
332 register const u_char
*tr
;
336 const u_char
**ak
, **ai
, *s
, *t
;
338 for (ak
= a
+1; --n
>= 1; ak
++)
339 for (ai
= ak
; ai
> a
; ai
--) {
340 for (s
= ai
[0] + b
, t
= ai
[-1] + b
;
341 (ch
= tr
[*s
]) != endch
; s
++, t
++)
346 swap(ai
[0], ai
[-1], s
);