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