1 /*-
2 * SPDX-License-Identifier: BSD-3-Clause
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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 /*
36 * Radixsort routines.
37 *
38 * Program r_sort_a() is unstable but uses O(logN) extra memory for a stack.
39 * Use radixsort(a, n, trace, endchar) for this case.
40 *
41 * For stable sorting (using N extra pointers) use sradixsort(), which calls
42 * r_sort_b().
43 *
44 * For a description of this code, see D. McIlroy, P. McIlroy, K. Bostic,
45 * "Engineering Radix Sort".
46 */
47
48 #include <sys/types.h>
49 #include <stdlib.h>
50 #include <stddef.h>
51 #include <errno.h>
52
53 typedef struct {
54 const u_char **sa;
55 int sn, si;
56 } stack;
57
58 static inline void simplesort
59 (const u_char **, int, int, const u_char *, u_int);
60 static void r_sort_a(const u_char **, int, int, const u_char *, u_int);
61 static void r_sort_b(const u_char **, const u_char **, int, int,
62 const u_char *, u_int);
63
64 #define THRESHOLD 20 /* Divert to simplesort(). */
65 #define SIZE 512 /* Default stack size. */
66
67 #define SETUP { \
68 if (tab == NULL) { \
69 tr = tr0; \
70 for (c = 0; c < endch; c++) \
71 tr0[c] = c + 1; \
72 tr0[c] = 0; \
73 for (c++; c < 256; c++) \
74 tr0[c] = c; \
75 endch = 0; \
76 } else { \
77 endch = tab[endch]; \
78 tr = tab; \
79 if (endch != 0 && endch != 255) { \
80 errno = EINVAL; \
81 return (-1); \
82 } \
83 } \
84 }
85
86 int
radixsort(const u_char ** a,int n,const u_char * tab,u_int endch)87 radixsort(const u_char **a, int n, const u_char *tab, u_int endch)
88 {
89 const u_char *tr;
90 int c;
91 u_char tr0[256];
92
93 SETUP;
94 r_sort_a(a, n, 0, tr, endch);
95 return (0);
96 }
97
98 int
sradixsort(const u_char ** a,int n,const u_char * tab,u_int endch)99 sradixsort(const u_char **a, int n, const u_char *tab, u_int endch)
100 {
101 const u_char *tr, **ta;
102 int c;
103 u_char tr0[256];
104
105 SETUP;
106 if (n < THRESHOLD)
107 simplesort(a, n, 0, tr, endch);
108 else {
109 if ((ta = malloc(n * sizeof(a))) == NULL)
110 return (-1);
111 r_sort_b(a, ta, n, 0, tr, endch);
112 free(ta);
113 }
114 return (0);
115 }
116
117 #define empty(s) (s >= sp)
118 #define pop(a, n, i) a = (--sp)->sa, n = sp->sn, i = sp->si
119 #define push(a, n, i) sp->sa = a, sp->sn = n, (sp++)->si = i
120 #define swap(a, b, t) t = a, a = b, b = t
121
122 /* Unstable, in-place sort. */
123 static void
r_sort_a(const u_char ** a,int n,int i,const u_char * tr,u_int endch)124 r_sort_a(const u_char **a, int n, int i, const u_char *tr, u_int endch)
125 {
126 static int count[256], nc, bmin;
127 int c;
128 const u_char **ak, *r;
129 stack s[SIZE], *sp, *sp0, *sp1, temp;
130 int *cp, bigc;
131 const u_char **an, *t, **aj, **top[256];
132
133 /* Set up stack. */
134 sp = s;
135 push(a, n, i);
136 while (!empty(s)) {
137 pop(a, n, i);
138 if (n < THRESHOLD) {
139 simplesort(a, n, i, tr, endch);
140 continue;
141 }
142 an = a + n;
143
144 /* Make character histogram. */
145 if (nc == 0) {
146 bmin = 255; /* First occupied bin, excluding eos. */
147 for (ak = a; ak < an;) {
148 c = tr[(*ak++)[i]];
149 if (++count[c] == 1 && c != endch) {
150 if (c < bmin)
151 bmin = c;
152 nc++;
153 }
154 }
155 if (sp + nc > s + SIZE) { /* Get more stack. */
156 r_sort_a(a, n, i, tr, endch);
157 continue;
158 }
159 }
160
161 /*
162 * Special case: if all strings have the same
163 * character at position i, move on to the next
164 * character.
165 */
166 if (nc == 1 && count[bmin] == n) {
167 push(a, n, i+1);
168 nc = count[bmin] = 0;
169 continue;
170 }
171
172 /*
173 * Set top[]; push incompletely sorted bins onto stack.
174 * top[] = pointers to last out-of-place element in bins.
175 * count[] = counts of elements in bins.
176 * Before permuting: top[c-1] + count[c] = top[c];
177 * during deal: top[c] counts down to top[c-1].
178 */
179 sp0 = sp1 = sp; /* Stack position of biggest bin. */
180 bigc = 2; /* Size of biggest bin. */
181 if (endch == 0) /* Special case: set top[eos]. */
182 top[0] = ak = a + count[0];
183 else {
184 ak = a;
185 top[255] = an;
186 }
187 for (cp = count + bmin; nc > 0; cp++) {
188 while (*cp == 0) /* Find next non-empty pile. */
189 cp++;
190 if (*cp > 1) {
191 if (*cp > bigc) {
192 bigc = *cp;
193 sp1 = sp;
194 }
195 push(ak, *cp, i+1);
196 }
197 top[cp-count] = ak += *cp;
198 nc--;
199 }
200 swap(*sp0, *sp1, temp); /* Play it safe -- biggest bin last. */
201
202 /*
203 * Permute misplacements home. Already home: everything
204 * before aj, and in bin[c], items from top[c] on.
205 * Inner loop:
206 * r = next element to put in place;
207 * ak = top[r[i]] = location to put the next element.
208 * aj = bottom of 1st disordered bin.
209 * Outer loop:
210 * Once the 1st disordered bin is done, ie. aj >= ak,
211 * aj<-aj + count[c] connects the bins in a linked list;
212 * reset count[c].
213 */
214 for (aj = a; aj < an; *aj = r, aj += count[c], count[c] = 0)
215 for (r = *aj; aj < (ak = --top[c = tr[r[i]]]);)
216 swap(*ak, r, t);
217 }
218 }
219
220 /* Stable sort, requiring additional memory. */
221 static void
r_sort_b(const u_char ** a,const u_char ** ta,int n,int i,const u_char * tr,u_int endch)222 r_sort_b(const u_char **a, const u_char **ta, int n, int i, const u_char *tr,
223 u_int endch)
224 {
225 static int count[256], nc, bmin;
226 int c;
227 const u_char **ak, **ai;
228 stack s[512], *sp, *sp0, *sp1, temp;
229 const u_char **top[256];
230 int *cp, bigc;
231
232 sp = s;
233 push(a, n, i);
234 while (!empty(s)) {
235 pop(a, n, i);
236 if (n < THRESHOLD) {
237 simplesort(a, n, i, tr, endch);
238 continue;
239 }
240
241 if (nc == 0) {
242 bmin = 255;
243 for (ak = a + n; --ak >= a;) {
244 c = tr[(*ak)[i]];
245 if (++count[c] == 1 && c != endch) {
246 if (c < bmin)
247 bmin = c;
248 nc++;
249 }
250 }
251 if (sp + nc > s + SIZE) {
252 r_sort_b(a, ta, n, i, tr, endch);
253 continue;
254 }
255 }
256
257 sp0 = sp1 = sp;
258 bigc = 2;
259 if (endch == 0) {
260 top[0] = ak = a + count[0];
261 count[0] = 0;
262 } else {
263 ak = a;
264 top[255] = a + n;
265 count[255] = 0;
266 }
267 for (cp = count + bmin; nc > 0; cp++) {
268 while (*cp == 0)
269 cp++;
270 if ((c = *cp) > 1) {
271 if (c > bigc) {
272 bigc = c;
273 sp1 = sp;
274 }
275 push(ak, c, i+1);
276 }
277 top[cp-count] = ak += c;
278 *cp = 0; /* Reset count[]. */
279 nc--;
280 }
281 swap(*sp0, *sp1, temp);
282
283 for (ak = ta + n, ai = a+n; ak > ta;) /* Copy to temp. */
284 *--ak = *--ai;
285 for (ak = ta+n; --ak >= ta;) /* Deal to piles. */
286 *--top[tr[(*ak)[i]]] = *ak;
287 }
288 }
289
290 /* insertion sort */
291 static inline void
simplesort(const u_char ** a,int n,int b,const u_char * tr,u_int endch)292 simplesort(const u_char **a, int n, int b, const u_char *tr, u_int endch)
293 {
294 u_char ch;
295 const u_char **ak, **ai, *s, *t;
296
297 for (ak = a+1; --n >= 1; ak++)
298 for (ai = ak; ai > a; ai--) {
299 for (s = ai[0] + b, t = ai[-1] + b;
300 (ch = tr[*s]) != endch; s++, t++)
301 if (ch != tr[*t])
302 break;
303 if (ch >= tr[*t])
304 break;
305 swap(ai[0], ai[-1], s);
306 }
307 }
308