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