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