Annotation of src/usr.bin/sort/fsort.c, Revision 1.5
1.5 ! millert 1: /* $OpenBSD: fsort.c,v 1.4 1997/06/16 02:21:55 millert Exp $ */
1.1 millert 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: #ifndef lint
40: #if 0
41: static char sccsid[] = "@(#)fsort.c 8.1 (Berkeley) 6/6/93";
42: #else
1.5 ! millert 43: static char rcsid[] = "$OpenBSD: fsort.c,v 1.4 1997/06/16 02:21:55 millert Exp $";
1.1 millert 44: #endif
45: #endif /* not lint */
46:
47: /*
48: * Read in the next bin. If it fits in one segment sort it;
49: * otherwise refine it by segment deeper by one character,
50: * and try again on smaller bins. Sort the final bin at this level
51: * of recursion to keep the head of fstack at 0.
52: * After PANIC passes, abort to merge sort.
1.4 millert 53: */
1.1 millert 54: #include "sort.h"
55: #include "fsort.h"
56:
57: #include <stdlib.h>
58: #include <string.h>
59:
60: u_char **keylist = 0, *buffer = 0, *linebuf = 0;
61: struct tempfile fstack[MAXFCT];
62: extern char *toutpath;
63: #define FSORTMAX 4
64: int PANIC = FSORTMAX;
65:
66: void
67: fsort(binno, depth, infiles, nfiles, outfp, ftbl)
1.5 ! millert 68: register int binno, depth;
1.1 millert 69: register union f_handle infiles;
1.5 ! millert 70: register int nfiles;
1.1 millert 71: FILE *outfp;
72: register struct field *ftbl;
73: {
74: register u_char *bufend, **keypos, *tmpbuf;
75: u_char *weights;
76: int ntfiles, mfct = 0, total, i, maxb, lastb, panic = 0;
1.5 ! millert 77: int c, nelem;
! 78: int sizes [NBINS+1];
1.1 millert 79: union f_handle tfiles, mstart = {MAXFCT-16};
80: register int (*get)(int, union f_handle, int, RECHEADER *,
81: u_char *, struct field *);
1.5 ! millert 82: register RECHEADER *crec;
1.1 millert 83: struct field tfield[2];
84: FILE *prevfp, *tailfp[FSORTMAX+1];
85:
86: memset(tailfp, 0, sizeof(tailfp));
87: prevfp = outfp;
88: memset(tfield, 0, sizeof(tfield));
89: if (ftbl[0].flags & R)
90: tfield[0].weights = Rascii;
91: else
92: tfield[0].weights = ascii;
93: tfield[0].icol.num = 1;
94: weights = ftbl[0].weights;
95: if (!buffer) {
1.3 millert 96: buffer = malloc(BUFSIZE + 1);
1.1 millert 97: keylist = malloc(MAXNUM * sizeof(u_char *));
98: if (!SINGL_FLD)
99: linebuf = malloc(MAXLLEN);
100: }
1.3 millert 101: bufend = buffer + BUFSIZE;
1.1 millert 102: if (binno >= 0) {
103: tfiles.top = infiles.top + nfiles;
104: get = getnext;
105: } else {
106: tfiles.top = 0;
107: if (SINGL_FLD)
108: get = makeline;
109: else
110: get = makekey;
111: }
112: for (;;) {
113: memset(sizes, 0, sizeof(sizes));
114: c = ntfiles = 0;
115: if (binno == weights[REC_D] &&
116: !(SINGL_FLD && ftbl[0].flags & F)) { /* pop */
117: rd_append(weights[REC_D],
118: infiles, nfiles, prevfp, buffer, bufend);
119: break;
120: } else if (binno == weights[REC_D]) {
121: depth = 0; /* start over on flat weights */
122: ftbl = tfield;
123: weights = ftbl[0].weights;
124: }
125: while (c != EOF) {
126: keypos = keylist;
127: nelem = 0;
128: crec = (RECHEADER *) buffer;
129: while((c = get(binno, infiles, nfiles, crec, bufend,
130: ftbl)) == 0) {
131: *keypos++ = crec->data + depth;
132: if (++nelem == MAXNUM) {
133: c = BUFFEND;
134: break;
135: }
136: crec =(RECHEADER *) ((char *) crec +
137: SALIGN(crec->length) + sizeof(TRECHEADER));
138: }
139: if (c == BUFFEND || ntfiles || mfct) { /* push */
140: if (panic >= PANIC) {
141: fstack[MAXFCT-16+mfct].fp = ftmp();
142: if (radixsort((const u_char **)keylist,
143: nelem, weights, REC_D))
144: err(2, NULL);
145: append(keylist, nelem, depth, fstack[
146: MAXFCT-16+mfct].fp, putrec, ftbl);
147: mfct++;
148: /* reduce number of open files */
149: if (mfct == 16 ||(c == EOF && ntfiles)) {
150: tmpbuf = malloc(bufend -
151: crec->data);
152: memmove(tmpbuf, crec->data,
153: bufend - crec->data);
154: fstack[tfiles.top + ntfiles].fp
155: = ftmp();
156: fmerge(0, mstart, mfct, geteasy,
157: fstack[tfiles.top+ntfiles].fp,
158: putrec, ftbl);
1.4 millert 159: ntfiles++;
1.1 millert 160: mfct = 0;
161: memmove(crec->data, tmpbuf,
162: bufend - crec->data);
163: free(tmpbuf);
164: }
165: } else {
166: fstack[tfiles.top + ntfiles].fp= ftmp();
167: onepass(keylist, depth, nelem, sizes,
168: weights, fstack[tfiles.top+ntfiles].fp);
1.4 millert 169: ntfiles++;
1.1 millert 170: }
171: }
172: }
173: get = getnext;
174: if (!ntfiles && !mfct) { /* everything in memory--pop */
175: if (nelem > 1 && radixsort((const u_char **)keylist,
176: nelem, weights, REC_D))
177: err(2, NULL);
178: append(keylist, nelem, depth, outfp, putline, ftbl);
179: break; /* pop */
180: }
181: if (panic >= PANIC) {
182: if (!ntfiles)
183: fmerge(0, mstart, mfct, geteasy,
184: outfp, putline, ftbl);
185: else
186: fmerge(0, tfiles, ntfiles, geteasy,
187: outfp, putline, ftbl);
188: break;
189:
190: }
191: total = maxb = lastb = 0; /* find if one bin dominates */
192: for (i = 0; i < NBINS; i++)
193: if (sizes[i]) {
194: if (sizes[i] > sizes[maxb])
195: maxb = i;
196: lastb = i;
197: total += sizes[i];
198: }
199: if (sizes[maxb] < max((total / 2) , BUFSIZE))
200: maxb = lastb; /* otherwise pop after last bin */
201: fstack[tfiles.top].lastb = lastb;
202: fstack[tfiles.top].maxb = maxb;
203:
204: /* start refining next level. */
205: get(-1, tfiles, ntfiles, crec, bufend, 0); /* rewind */
206: for (i = 0; i < maxb; i++) {
207: if (!sizes[i]) /* bin empty; step ahead file offset */
208: get(i, tfiles, ntfiles, crec, bufend, 0);
209: else
210: fsort(i, depth+1, tfiles, ntfiles, outfp, ftbl);
211: }
212: if (lastb != maxb) {
213: if (prevfp != outfp)
214: tailfp[panic] = prevfp;
215: prevfp = ftmp();
216: for (i = maxb+1; i <= lastb; i++)
217: if (!sizes[i])
218: get(i, tfiles, ntfiles, crec, bufend,0);
219: else
220: fsort(i, depth+1, tfiles, ntfiles,
221: prevfp, ftbl);
222: }
223:
224: /* sort biggest (or last) bin at this level */
225: depth++;
226: panic++;
227: binno = maxb;
228: infiles.top = tfiles.top; /* getnext will free tfiles, */
229: nfiles = ntfiles; /* so overwrite them */
230: }
231: if (prevfp != outfp) {
232: concat(outfp, prevfp);
233: fclose(prevfp);
234: }
235: for (i = panic; i >= 0; --i)
236: if (tailfp[i]) {
237: concat(outfp, tailfp[i]);
238: fclose(tailfp[i]);
239: }
240: }
241:
242: /*
1.4 millert 243: * This is one pass of radix exchange, dumping the bins to disk.
1.1 millert 244: */
245: #define swap(a, b, t) t = a, a = b, b = t
246: void
247: onepass(a, depth, n, sizes, tr, fp)
248: u_char **a;
249: int depth;
1.5 ! millert 250: int n;
! 251: int sizes[];
1.1 millert 252: u_char *tr;
253: FILE *fp;
254: {
1.5 ! millert 255: int tsizes[NBINS+1];
1.1 millert 256: u_char **bin[257], **top[256], ***bp, ***bpmax, ***tp;
257: static histo[256];
258: int *hp;
259: register int c;
260: u_char **an, *t, **aj;
261: register u_char **ak, *r;
262:
263: memset(tsizes, 0, sizeof(tsizes));
264: depth += sizeof(TRECHEADER);
265: an = a + n;
266: for (ak = a; ak < an; ak++) {
267: histo[c = tr[**ak]]++;
268: tsizes[c] += ((RECHEADER *) (*ak -= depth))->length;
269: }
270:
271: bin[0] = a;
272: bpmax = bin + 256;
273: tp = top, hp = histo;
274: for (bp = bin; bp < bpmax; bp++) {
275: *tp++ = *(bp+1) = *bp + (c = *hp);
276: *hp++ = 0;
277: if (c <= 1)
278: continue;
279: }
1.4 millert 280: for (aj = a; aj < an; *aj = r, aj = bin[c+1])
1.1 millert 281: for(r = *aj; aj < (ak = --top[c = tr[r[depth]]]) ;)
282: swap(*ak, r, t);
283:
284: for (ak = a, c = 0; c < 256; c++) {
285: an = bin[c+1];
286: n = an - ak;
287: tsizes[c] += n * sizeof(TRECHEADER);
288: /* tell getnext how many elements in this bin, this segment. */
1.5 ! millert 289: EWRITE(tsizes+c, sizeof(int), 1, fp);
1.1 millert 290: sizes[c] += tsizes[c];
291: for (; ak < an; ++ak)
292: putrec((RECHEADER *) *ak, fp);
293: }
294: }