Annotation of src/usr.bin/sort/fsort.c, Revision 1.7
1.7 ! millert 1: /* $OpenBSD: fsort.c,v 1.6 1998/05/02 02:41:56 mickey 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.7 ! millert 43: static char rcsid[] = "$OpenBSD: fsort.c,v 1.6 1998/05/02 02:41:56 mickey 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.7 ! millert 96: if ((buffer = malloc(BUFSIZE + 1)) == NULL ||
! 97: (keylist = calloc(MAXNUM, sizeof(u_char *))) == NULL)
! 98: errx(2, "cannot allocate memory");
! 99: if (!SINGL_FLD) {
! 100: if ((linebuf = malloc(MAXLLEN)) == NULL)
! 101: errx(2, "cannot allocate memory");
! 102: }
1.1 millert 103: }
1.3 millert 104: bufend = buffer + BUFSIZE;
1.1 millert 105: if (binno >= 0) {
106: tfiles.top = infiles.top + nfiles;
107: get = getnext;
108: } else {
109: tfiles.top = 0;
110: if (SINGL_FLD)
111: get = makeline;
112: else
113: get = makekey;
114: }
115: for (;;) {
116: memset(sizes, 0, sizeof(sizes));
117: c = ntfiles = 0;
118: if (binno == weights[REC_D] &&
119: !(SINGL_FLD && ftbl[0].flags & F)) { /* pop */
120: rd_append(weights[REC_D],
121: infiles, nfiles, prevfp, buffer, bufend);
122: break;
123: } else if (binno == weights[REC_D]) {
124: depth = 0; /* start over on flat weights */
125: ftbl = tfield;
126: weights = ftbl[0].weights;
127: }
128: while (c != EOF) {
129: keypos = keylist;
130: nelem = 0;
131: crec = (RECHEADER *) buffer;
132: while((c = get(binno, infiles, nfiles, crec, bufend,
133: ftbl)) == 0) {
134: *keypos++ = crec->data + depth;
135: if (++nelem == MAXNUM) {
136: c = BUFFEND;
137: break;
138: }
139: crec =(RECHEADER *) ((char *) crec +
140: SALIGN(crec->length) + sizeof(TRECHEADER));
141: }
142: if (c == BUFFEND || ntfiles || mfct) { /* push */
143: if (panic >= PANIC) {
144: fstack[MAXFCT-16+mfct].fp = ftmp();
145: if (radixsort((const u_char **)keylist,
146: nelem, weights, REC_D))
147: err(2, NULL);
148: append(keylist, nelem, depth, fstack[
149: MAXFCT-16+mfct].fp, putrec, ftbl);
150: mfct++;
151: /* reduce number of open files */
152: if (mfct == 16 ||(c == EOF && ntfiles)) {
153: tmpbuf = malloc(bufend -
154: crec->data);
1.7 ! millert 155: if (tmpbuf == NULL)
! 156: errx(2, "cannot "
! 157: "allocate memory");
1.1 millert 158: memmove(tmpbuf, crec->data,
159: bufend - crec->data);
160: fstack[tfiles.top + ntfiles].fp
161: = ftmp();
162: fmerge(0, mstart, mfct, geteasy,
163: fstack[tfiles.top+ntfiles].fp,
164: putrec, ftbl);
1.4 millert 165: ntfiles++;
1.1 millert 166: mfct = 0;
167: memmove(crec->data, tmpbuf,
168: bufend - crec->data);
169: free(tmpbuf);
170: }
171: } else {
172: fstack[tfiles.top + ntfiles].fp= ftmp();
173: onepass(keylist, depth, nelem, sizes,
174: weights, fstack[tfiles.top+ntfiles].fp);
1.4 millert 175: ntfiles++;
1.1 millert 176: }
177: }
178: }
179: get = getnext;
180: if (!ntfiles && !mfct) { /* everything in memory--pop */
181: if (nelem > 1 && radixsort((const u_char **)keylist,
182: nelem, weights, REC_D))
183: err(2, NULL);
184: append(keylist, nelem, depth, outfp, putline, ftbl);
185: break; /* pop */
186: }
187: if (panic >= PANIC) {
188: if (!ntfiles)
189: fmerge(0, mstart, mfct, geteasy,
190: outfp, putline, ftbl);
191: else
192: fmerge(0, tfiles, ntfiles, geteasy,
193: outfp, putline, ftbl);
194: break;
195:
196: }
197: total = maxb = lastb = 0; /* find if one bin dominates */
198: for (i = 0; i < NBINS; i++)
199: if (sizes[i]) {
200: if (sizes[i] > sizes[maxb])
201: maxb = i;
202: lastb = i;
203: total += sizes[i];
204: }
205: if (sizes[maxb] < max((total / 2) , BUFSIZE))
206: maxb = lastb; /* otherwise pop after last bin */
207: fstack[tfiles.top].lastb = lastb;
208: fstack[tfiles.top].maxb = maxb;
209:
210: /* start refining next level. */
211: get(-1, tfiles, ntfiles, crec, bufend, 0); /* rewind */
212: for (i = 0; i < maxb; i++) {
213: if (!sizes[i]) /* bin empty; step ahead file offset */
214: get(i, tfiles, ntfiles, crec, bufend, 0);
215: else
216: fsort(i, depth+1, tfiles, ntfiles, outfp, ftbl);
217: }
218: if (lastb != maxb) {
219: if (prevfp != outfp)
220: tailfp[panic] = prevfp;
221: prevfp = ftmp();
222: for (i = maxb+1; i <= lastb; i++)
223: if (!sizes[i])
224: get(i, tfiles, ntfiles, crec, bufend,0);
225: else
226: fsort(i, depth+1, tfiles, ntfiles,
227: prevfp, ftbl);
228: }
229:
230: /* sort biggest (or last) bin at this level */
231: depth++;
232: panic++;
233: binno = maxb;
234: infiles.top = tfiles.top; /* getnext will free tfiles, */
235: nfiles = ntfiles; /* so overwrite them */
236: }
237: if (prevfp != outfp) {
238: concat(outfp, prevfp);
239: fclose(prevfp);
240: }
241: for (i = panic; i >= 0; --i)
242: if (tailfp[i]) {
243: concat(outfp, tailfp[i]);
244: fclose(tailfp[i]);
245: }
246: }
247:
248: /*
1.4 millert 249: * This is one pass of radix exchange, dumping the bins to disk.
1.1 millert 250: */
251: #define swap(a, b, t) t = a, a = b, b = t
252: void
253: onepass(a, depth, n, sizes, tr, fp)
254: u_char **a;
255: int depth;
1.5 millert 256: int n;
257: int sizes[];
1.1 millert 258: u_char *tr;
259: FILE *fp;
260: {
1.5 millert 261: int tsizes[NBINS+1];
1.1 millert 262: u_char **bin[257], **top[256], ***bp, ***bpmax, ***tp;
1.7 ! millert 263: static int histo[256];
1.1 millert 264: int *hp;
265: register int c;
266: u_char **an, *t, **aj;
267: register u_char **ak, *r;
268:
269: memset(tsizes, 0, sizeof(tsizes));
270: depth += sizeof(TRECHEADER);
271: an = a + n;
272: for (ak = a; ak < an; ak++) {
273: histo[c = tr[**ak]]++;
274: tsizes[c] += ((RECHEADER *) (*ak -= depth))->length;
275: }
276:
277: bin[0] = a;
278: bpmax = bin + 256;
279: tp = top, hp = histo;
280: for (bp = bin; bp < bpmax; bp++) {
281: *tp++ = *(bp+1) = *bp + (c = *hp);
282: *hp++ = 0;
283: if (c <= 1)
284: continue;
285: }
1.4 millert 286: for (aj = a; aj < an; *aj = r, aj = bin[c+1])
1.1 millert 287: for(r = *aj; aj < (ak = --top[c = tr[r[depth]]]) ;)
288: swap(*ak, r, t);
289:
290: for (ak = a, c = 0; c < 256; c++) {
291: an = bin[c+1];
292: n = an - ak;
293: tsizes[c] += n * sizeof(TRECHEADER);
294: /* tell getnext how many elements in this bin, this segment. */
1.5 millert 295: EWRITE(tsizes+c, sizeof(int), 1, fp);
1.1 millert 296: sizes[c] += tsizes[c];
297: for (; ak < an; ++ak)
298: putrec((RECHEADER *) *ak, fp);
299: }
300: }