fsort.c revision 1.34 1 1.34 dsl /* $NetBSD: fsort.c,v 1.34 2009/08/15 16:50:29 dsl Exp $ */
2 1.26 jdolecek
3 1.26 jdolecek /*-
4 1.26 jdolecek * Copyright (c) 2000-2003 The NetBSD Foundation, Inc.
5 1.26 jdolecek * All rights reserved.
6 1.26 jdolecek *
7 1.26 jdolecek * This code is derived from software contributed to The NetBSD Foundation
8 1.26 jdolecek * by Ben Harris and Jaromir Dolecek.
9 1.26 jdolecek *
10 1.26 jdolecek * Redistribution and use in source and binary forms, with or without
11 1.26 jdolecek * modification, are permitted provided that the following conditions
12 1.26 jdolecek * are met:
13 1.26 jdolecek * 1. Redistributions of source code must retain the above copyright
14 1.26 jdolecek * notice, this list of conditions and the following disclaimer.
15 1.26 jdolecek * 2. Redistributions in binary form must reproduce the above copyright
16 1.26 jdolecek * notice, this list of conditions and the following disclaimer in the
17 1.26 jdolecek * documentation and/or other materials provided with the distribution.
18 1.26 jdolecek *
19 1.26 jdolecek * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.26 jdolecek * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.26 jdolecek * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.26 jdolecek * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.26 jdolecek * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.26 jdolecek * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.26 jdolecek * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.26 jdolecek * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.26 jdolecek * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.26 jdolecek * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.26 jdolecek * POSSIBILITY OF SUCH DAMAGE.
30 1.26 jdolecek */
31 1.2 bjh21
32 1.1 bjh21 /*-
33 1.1 bjh21 * Copyright (c) 1993
34 1.1 bjh21 * The Regents of the University of California. All rights reserved.
35 1.1 bjh21 *
36 1.1 bjh21 * This code is derived from software contributed to Berkeley by
37 1.1 bjh21 * Peter McIlroy.
38 1.1 bjh21 *
39 1.1 bjh21 * Redistribution and use in source and binary forms, with or without
40 1.1 bjh21 * modification, are permitted provided that the following conditions
41 1.1 bjh21 * are met:
42 1.1 bjh21 * 1. Redistributions of source code must retain the above copyright
43 1.1 bjh21 * notice, this list of conditions and the following disclaimer.
44 1.1 bjh21 * 2. Redistributions in binary form must reproduce the above copyright
45 1.1 bjh21 * notice, this list of conditions and the following disclaimer in the
46 1.1 bjh21 * documentation and/or other materials provided with the distribution.
47 1.25 agc * 3. Neither the name of the University nor the names of its contributors
48 1.1 bjh21 * may be used to endorse or promote products derived from this software
49 1.1 bjh21 * without specific prior written permission.
50 1.1 bjh21 *
51 1.1 bjh21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 1.1 bjh21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 1.1 bjh21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 1.1 bjh21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 1.1 bjh21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 1.1 bjh21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 1.1 bjh21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 1.1 bjh21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 1.1 bjh21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 1.1 bjh21 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 1.1 bjh21 * SUCH DAMAGE.
62 1.1 bjh21 */
63 1.1 bjh21
64 1.1 bjh21 /*
65 1.1 bjh21 * Read in the next bin. If it fits in one segment sort it;
66 1.1 bjh21 * otherwise refine it by segment deeper by one character,
67 1.1 bjh21 * and try again on smaller bins. Sort the final bin at this level
68 1.1 bjh21 * of recursion to keep the head of fstack at 0.
69 1.1 bjh21 * After PANIC passes, abort to merge sort.
70 1.16 jdolecek */
71 1.1 bjh21 #include "sort.h"
72 1.1 bjh21 #include "fsort.h"
73 1.1 bjh21
74 1.2 bjh21 #ifndef lint
75 1.34 dsl __RCSID("$NetBSD: fsort.c,v 1.34 2009/08/15 16:50:29 dsl Exp $");
76 1.2 bjh21 __SCCSID("@(#)fsort.c 8.1 (Berkeley) 6/6/93");
77 1.2 bjh21 #endif /* not lint */
78 1.2 bjh21
79 1.1 bjh21 #include <stdlib.h>
80 1.1 bjh21 #include <string.h>
81 1.1 bjh21
82 1.13 jdolecek static const u_char **keylist = 0;
83 1.34 dsl u_char *buffer = 0;
84 1.18 jdolecek size_t bufsize = DEFBUFSIZE;
85 1.1 bjh21 #define FSORTMAX 4
86 1.1 bjh21 int PANIC = FSORTMAX;
87 1.1 bjh21
88 1.23 jdolecek struct tempfile fstack[MAXFCT];
89 1.11 jdolecek #define MSTART (MAXFCT - MERGE_FNUM)
90 1.23 jdolecek #define CHECKFSTACK(n) \
91 1.24 jdolecek if (n >= MAXFCT) \
92 1.23 jdolecek errx(2, "fstack: too many temporary files; use -H or sort in pieces")
93 1.23 jdolecek
94 1.13 jdolecek #define SALIGN(n) ((n+sizeof(length_t)-1) & ~(sizeof(length_t)-1))
95 1.8 jdolecek
96 1.1 bjh21 void
97 1.33 dsl fsort(int binno, int depth, int top, struct filelist *filelist, int nfiles,
98 1.33 dsl FILE *outfp, struct field *ftbl)
99 1.1 bjh21 {
100 1.4 jdolecek const u_char **keypos;
101 1.30 jdolecek u_char *bufend;
102 1.1 bjh21 u_char *weights;
103 1.1 bjh21 int ntfiles, mfct = 0, total, i, maxb, lastb, panic = 0;
104 1.8 jdolecek int c, nelem, base;
105 1.29 itojun long sizes [NBINS + 1];
106 1.8 jdolecek get_func_t get;
107 1.4 jdolecek struct recheader *crec;
108 1.1 bjh21 struct field tfield[2];
109 1.29 itojun FILE *prevfp, *tailfp[FSORTMAX + 1];
110 1.27 itojun u_char *nbuffer;
111 1.1 bjh21
112 1.3 bjh21 memset(tailfp, 0, sizeof(tailfp));
113 1.3 bjh21 prevfp = outfp;
114 1.1 bjh21 memset(tfield, 0, sizeof(tfield));
115 1.1 bjh21 if (ftbl[0].flags & R)
116 1.1 bjh21 tfield[0].weights = Rascii;
117 1.1 bjh21 else
118 1.1 bjh21 tfield[0].weights = ascii;
119 1.1 bjh21 tfield[0].icol.num = 1;
120 1.1 bjh21 weights = ftbl[0].weights;
121 1.1 bjh21 if (!buffer) {
122 1.5 jdolecek buffer = malloc(bufsize);
123 1.1 bjh21 keylist = malloc(MAXNUM * sizeof(u_char *));
124 1.12 itojun memset(keylist, 0, MAXNUM * sizeof(u_char *));
125 1.1 bjh21 }
126 1.5 jdolecek bufend = buffer + bufsize;
127 1.1 bjh21 if (binno >= 0) {
128 1.8 jdolecek base = top + nfiles;
129 1.1 bjh21 get = getnext;
130 1.1 bjh21 } else {
131 1.8 jdolecek base = 0;
132 1.1 bjh21 if (SINGL_FLD)
133 1.1 bjh21 get = makeline;
134 1.1 bjh21 else
135 1.1 bjh21 get = makekey;
136 1.1 bjh21 }
137 1.1 bjh21 for (;;) {
138 1.1 bjh21 memset(sizes, 0, sizeof(sizes));
139 1.31 jmc c = nelem = ntfiles = 0; /* XXXGCC -Wuninitialized m68k */
140 1.1 bjh21 if (binno == weights[REC_D] &&
141 1.1 bjh21 !(SINGL_FLD && ftbl[0].flags & F)) { /* pop */
142 1.8 jdolecek rd_append(weights[REC_D], top,
143 1.8 jdolecek nfiles, prevfp, buffer, bufend);
144 1.1 bjh21 break;
145 1.1 bjh21 } else if (binno == weights[REC_D]) {
146 1.1 bjh21 depth = 0; /* start over on flat weights */
147 1.1 bjh21 ftbl = tfield;
148 1.1 bjh21 weights = ftbl[0].weights;
149 1.1 bjh21 }
150 1.31 jmc keypos = keylist; /* XXXGCC -Wuninitialized m68k */
151 1.31 jmc crec = (RECHEADER *) buffer; /* XXXGCC -Wuninitialized m68k */
152 1.1 bjh21 while (c != EOF) {
153 1.1 bjh21 keypos = keylist;
154 1.1 bjh21 nelem = 0;
155 1.1 bjh21 crec = (RECHEADER *) buffer;
156 1.10 jdolecek
157 1.10 jdolecek do_read:
158 1.29 itojun while ((c = get(binno, top, filelist, nfiles, crec,
159 1.8 jdolecek bufend, ftbl)) == 0) {
160 1.1 bjh21 *keypos++ = crec->data + depth;
161 1.1 bjh21 if (++nelem == MAXNUM) {
162 1.1 bjh21 c = BUFFEND;
163 1.1 bjh21 break;
164 1.1 bjh21 }
165 1.29 itojun crec =(RECHEADER *)((char *) crec +
166 1.29 itojun SALIGN(crec->length) + sizeof(TRECHEADER));
167 1.1 bjh21 }
168 1.13 jdolecek
169 1.18 jdolecek if (c == BUFFEND && nelem < MAXNUM
170 1.18 jdolecek && bufsize < MAXBUFSIZE) {
171 1.10 jdolecek const u_char **keyp;
172 1.10 jdolecek u_char *oldb = buffer;
173 1.10 jdolecek
174 1.5 jdolecek /* buffer was too small for data, allocate
175 1.5 jdolecek * bigger buffer */
176 1.27 itojun nbuffer = realloc(buffer, bufsize * 2);
177 1.28 enami if (!nbuffer) {
178 1.27 itojun err(2, "failed to realloc buffer to %lu bytes",
179 1.27 itojun (unsigned long) bufsize * 2);
180 1.6 jdolecek }
181 1.27 itojun buffer = nbuffer;
182 1.27 itojun bufsize *= 2;
183 1.6 jdolecek bufend = buffer + bufsize;
184 1.10 jdolecek
185 1.10 jdolecek /* patch up keylist[] */
186 1.29 itojun for (keyp = &keypos[-1]; keyp >= keylist; keyp--)
187 1.10 jdolecek *keyp = buffer + (*keyp - oldb);
188 1.10 jdolecek
189 1.10 jdolecek crec = (RECHEADER *) (buffer + ((u_char *)crec - oldb));
190 1.10 jdolecek goto do_read;
191 1.5 jdolecek }
192 1.19 jdolecek
193 1.19 jdolecek if (c != BUFFEND && !ntfiles && !mfct) {
194 1.19 jdolecek /* do not push */
195 1.19 jdolecek continue;
196 1.19 jdolecek }
197 1.19 jdolecek
198 1.19 jdolecek /* push */
199 1.19 jdolecek if (panic >= PANIC) {
200 1.19 jdolecek fstack[MSTART + mfct].fp = ftmp();
201 1.19 jdolecek if ((stable_sort)
202 1.19 jdolecek ? sradixsort(keylist, nelem,
203 1.19 jdolecek weights, REC_D)
204 1.19 jdolecek : radixsort(keylist, nelem,
205 1.19 jdolecek weights, REC_D) )
206 1.19 jdolecek err(2, NULL);
207 1.19 jdolecek append(keylist, nelem, depth,
208 1.19 jdolecek fstack[MSTART + mfct].fp, putrec,
209 1.19 jdolecek ftbl);
210 1.19 jdolecek mfct++;
211 1.19 jdolecek /* reduce number of open files */
212 1.19 jdolecek if (mfct == MERGE_FNUM ||(c == EOF && ntfiles)) {
213 1.20 jdolecek /*
214 1.20 jdolecek * Only copy extra incomplete crec
215 1.20 jdolecek * data if there are any.
216 1.20 jdolecek */
217 1.20 jdolecek int nodata = (bufend >= (u_char *)crec
218 1.20 jdolecek && bufend <= crec->data);
219 1.30 jdolecek size_t sz=0;
220 1.30 jdolecek u_char *tmpbuf=NULL;
221 1.20 jdolecek
222 1.20 jdolecek if (!nodata) {
223 1.30 jdolecek sz = bufend - crec->data;
224 1.30 jdolecek tmpbuf = malloc(sz);
225 1.30 jdolecek memmove(tmpbuf, crec->data, sz);
226 1.20 jdolecek }
227 1.20 jdolecek
228 1.23 jdolecek CHECKFSTACK(base + ntfiles);
229 1.20 jdolecek fstack[base + ntfiles].fp = ftmp();
230 1.19 jdolecek fmerge(0, MSTART, filelist,
231 1.21 enami mfct, geteasy,
232 1.21 enami fstack[base + ntfiles].fp,
233 1.19 jdolecek putrec, ftbl);
234 1.16 jdolecek ntfiles++;
235 1.19 jdolecek mfct = 0;
236 1.20 jdolecek
237 1.20 jdolecek if (!nodata) {
238 1.30 jdolecek memmove(crec->data, tmpbuf, sz);
239 1.20 jdolecek free(tmpbuf);
240 1.20 jdolecek }
241 1.1 bjh21 }
242 1.19 jdolecek } else {
243 1.23 jdolecek CHECKFSTACK(base + ntfiles);
244 1.24 jdolecek fstack[base + ntfiles].fp = ftmp();
245 1.19 jdolecek onepass(keylist, depth, nelem, sizes,
246 1.19 jdolecek weights, fstack[base + ntfiles].fp);
247 1.19 jdolecek ntfiles++;
248 1.1 bjh21 }
249 1.1 bjh21 }
250 1.1 bjh21 if (!ntfiles && !mfct) { /* everything in memory--pop */
251 1.16 jdolecek if (nelem > 1
252 1.16 jdolecek && ((stable_sort)
253 1.7 jdolecek ? sradixsort(keylist, nelem, weights, REC_D)
254 1.16 jdolecek : radixsort(keylist, nelem, weights, REC_D) ))
255 1.1 bjh21 err(2, NULL);
256 1.17 jdolecek if (nelem > 0)
257 1.17 jdolecek append(keylist, nelem, depth, outfp, putline, ftbl);
258 1.1 bjh21 break; /* pop */
259 1.1 bjh21 }
260 1.1 bjh21 if (panic >= PANIC) {
261 1.1 bjh21 if (!ntfiles)
262 1.8 jdolecek fmerge(0, MSTART, filelist, mfct, geteasy,
263 1.3 bjh21 outfp, putline, ftbl);
264 1.1 bjh21 else
265 1.8 jdolecek fmerge(0, base, filelist, ntfiles, geteasy,
266 1.3 bjh21 outfp, putline, ftbl);
267 1.1 bjh21 break;
268 1.1 bjh21
269 1.1 bjh21 }
270 1.1 bjh21 total = maxb = lastb = 0; /* find if one bin dominates */
271 1.1 bjh21 for (i = 0; i < NBINS; i++)
272 1.9 itojun if (sizes[i]) {
273 1.9 itojun if (sizes[i] > sizes[maxb])
274 1.9 itojun maxb = i;
275 1.9 itojun lastb = i;
276 1.9 itojun total += sizes[i];
277 1.9 itojun }
278 1.1 bjh21 if (sizes[maxb] < max((total / 2) , BUFSIZE))
279 1.1 bjh21 maxb = lastb; /* otherwise pop after last bin */
280 1.8 jdolecek fstack[base].lastb = lastb;
281 1.8 jdolecek fstack[base].maxb = maxb;
282 1.1 bjh21
283 1.8 jdolecek /* start refining next level. */
284 1.8 jdolecek getnext(-1, base, NULL, ntfiles, crec, bufend, 0); /* rewind */
285 1.1 bjh21 for (i = 0; i < maxb; i++) {
286 1.1 bjh21 if (!sizes[i]) /* bin empty; step ahead file offset */
287 1.8 jdolecek getnext(i, base, NULL,ntfiles, crec, bufend, 0);
288 1.16 jdolecek else
289 1.29 itojun fsort(i, depth + 1, base, filelist, ntfiles,
290 1.8 jdolecek outfp, ftbl);
291 1.1 bjh21 }
292 1.8 jdolecek
293 1.8 jdolecek get = getnext;
294 1.8 jdolecek
295 1.1 bjh21 if (lastb != maxb) {
296 1.3 bjh21 if (prevfp != outfp)
297 1.3 bjh21 tailfp[panic] = prevfp;
298 1.3 bjh21 prevfp = ftmp();
299 1.29 itojun for (i = maxb + 1; i <= lastb; i++)
300 1.16 jdolecek if (!sizes[i])
301 1.8 jdolecek getnext(i, base, NULL, ntfiles, crec,
302 1.9 itojun bufend,0);
303 1.16 jdolecek else
304 1.29 itojun fsort(i, depth + 1, base, filelist,
305 1.9 itojun ntfiles, prevfp, ftbl);
306 1.1 bjh21 }
307 1.1 bjh21
308 1.1 bjh21 /* sort biggest (or last) bin at this level */
309 1.1 bjh21 depth++;
310 1.1 bjh21 panic++;
311 1.1 bjh21 binno = maxb;
312 1.8 jdolecek top = base;
313 1.1 bjh21 nfiles = ntfiles; /* so overwrite them */
314 1.1 bjh21 }
315 1.3 bjh21 if (prevfp != outfp) {
316 1.3 bjh21 concat(outfp, prevfp);
317 1.3 bjh21 fclose(prevfp);
318 1.1 bjh21 }
319 1.1 bjh21 for (i = panic; i >= 0; --i)
320 1.3 bjh21 if (tailfp[i]) {
321 1.3 bjh21 concat(outfp, tailfp[i]);
322 1.3 bjh21 fclose(tailfp[i]);
323 1.1 bjh21 }
324 1.19 jdolecek
325 1.19 jdolecek /* If on top level, free our structures */
326 1.19 jdolecek if (depth == 0) {
327 1.19 jdolecek free(keylist), keylist = NULL;
328 1.19 jdolecek free(buffer), buffer = NULL;
329 1.19 jdolecek }
330 1.1 bjh21 }
331 1.1 bjh21
332 1.1 bjh21 /*
333 1.16 jdolecek * This is one pass of radix exchange, dumping the bins to disk.
334 1.1 bjh21 */
335 1.1 bjh21 #define swap(a, b, t) t = a, a = b, b = t
336 1.1 bjh21 void
337 1.33 dsl onepass(const u_char **a, int depth, long n, long sizes[], u_char *tr, FILE *fp)
338 1.1 bjh21 {
339 1.29 itojun size_t tsizes[NBINS + 1];
340 1.2 bjh21 const u_char **bin[257], ***bp, ***bpmax, **top[256], ***tp;
341 1.2 bjh21 static int histo[256];
342 1.1 bjh21 int *hp;
343 1.4 jdolecek int c;
344 1.2 bjh21 const u_char **an, *t, **aj;
345 1.4 jdolecek const u_char **ak, *r;
346 1.1 bjh21
347 1.1 bjh21 memset(tsizes, 0, sizeof(tsizes));
348 1.1 bjh21 depth += sizeof(TRECHEADER);
349 1.6 jdolecek an = &a[n];
350 1.1 bjh21 for (ak = a; ak < an; ak++) {
351 1.1 bjh21 histo[c = tr[**ak]]++;
352 1.5 jdolecek tsizes[c] += ((const RECHEADER *) (*ak -= depth))->length;
353 1.1 bjh21 }
354 1.1 bjh21
355 1.1 bjh21 bin[0] = a;
356 1.1 bjh21 bpmax = bin + 256;
357 1.1 bjh21 tp = top, hp = histo;
358 1.1 bjh21 for (bp = bin; bp < bpmax; bp++) {
359 1.29 itojun *tp++ = *(bp + 1) = *bp + (c = *hp);
360 1.1 bjh21 *hp++ = 0;
361 1.1 bjh21 if (c <= 1)
362 1.1 bjh21 continue;
363 1.1 bjh21 }
364 1.29 itojun for (aj = a; aj < an; *aj = r, aj = bin[c + 1])
365 1.9 itojun for (r = *aj; aj < (ak = --top[c = tr[r[depth]]]) ;)
366 1.1 bjh21 swap(*ak, r, t);
367 1.1 bjh21
368 1.1 bjh21 for (ak = a, c = 0; c < 256; c++) {
369 1.29 itojun an = bin[c + 1];
370 1.1 bjh21 n = an - ak;
371 1.1 bjh21 tsizes[c] += n * sizeof(TRECHEADER);
372 1.1 bjh21 /* tell getnext how many elements in this bin, this segment. */
373 1.6 jdolecek EWRITE(&tsizes[c], sizeof(size_t), 1, fp);
374 1.1 bjh21 sizes[c] += tsizes[c];
375 1.1 bjh21 for (; ak < an; ++ak)
376 1.5 jdolecek putrec((const RECHEADER *) *ak, fp);
377 1.1 bjh21 }
378 1.1 bjh21 }
379