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