Home | History | Annotate | Line # | Download | only in sort
fsort.c revision 1.13
      1  1.13  jdolecek /*	$NetBSD: fsort.c,v 1.13 2001/02/19 19:31:29 jdolecek 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.13  jdolecek __RCSID("$NetBSD: fsort.c,v 1.13 2001/02/19 19:31:29 jdolecek 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.13  jdolecek static const u_char **keylist = 0;
     58   1.2     bjh21 u_char *buffer = 0, *linebuf = 0;
     59  1.10  jdolecek size_t bufsize = DEFLLEN;
     60  1.10  jdolecek size_t linebuf_size;
     61   1.1     bjh21 struct tempfile fstack[MAXFCT];
     62   1.1     bjh21 extern char *toutpath;
     63   1.1     bjh21 #define FSORTMAX 4
     64   1.1     bjh21 int PANIC = FSORTMAX;
     65   1.1     bjh21 
     66  1.11  jdolecek #define MSTART		(MAXFCT - MERGE_FNUM)
     67  1.13  jdolecek #define SALIGN(n) ((n+sizeof(length_t)-1) & ~(sizeof(length_t)-1))
     68   1.8  jdolecek 
     69   1.1     bjh21 void
     70   1.8  jdolecek fsort(binno, depth, top, filelist, nfiles, outfp, ftbl)
     71   1.8  jdolecek 	int binno, depth, top;
     72   1.8  jdolecek 	struct filelist *filelist;
     73   1.8  jdolecek 	int nfiles;
     74   1.3     bjh21 	FILE *outfp;
     75   1.4  jdolecek 	struct field *ftbl;
     76   1.1     bjh21 {
     77   1.4  jdolecek 	const u_char **keypos;
     78   1.4  jdolecek 	u_char *bufend, *tmpbuf;
     79   1.1     bjh21 	u_char *weights;
     80   1.1     bjh21 	int ntfiles, mfct = 0, total, i, maxb, lastb, panic = 0;
     81   1.8  jdolecek 	int c, nelem, base;
     82   1.1     bjh21 	long sizes [NBINS+1];
     83   1.8  jdolecek 	get_func_t get;
     84   1.4  jdolecek 	struct recheader *crec;
     85   1.1     bjh21 	struct field tfield[2];
     86   1.3     bjh21 	FILE *prevfp, *tailfp[FSORTMAX+1];
     87   1.1     bjh21 
     88   1.3     bjh21 	memset(tailfp, 0, sizeof(tailfp));
     89   1.3     bjh21 	prevfp = outfp;
     90   1.1     bjh21 	memset(tfield, 0, sizeof(tfield));
     91   1.1     bjh21 	if (ftbl[0].flags & R)
     92   1.1     bjh21 		tfield[0].weights = Rascii;
     93   1.1     bjh21 	else
     94   1.1     bjh21 		tfield[0].weights = ascii;
     95   1.1     bjh21 	tfield[0].icol.num = 1;
     96   1.1     bjh21 	weights = ftbl[0].weights;
     97   1.1     bjh21 	if (!buffer) {
     98   1.5  jdolecek 		buffer = malloc(bufsize);
     99   1.1     bjh21 		keylist = malloc(MAXNUM * sizeof(u_char *));
    100  1.12    itojun 		memset(keylist, 0, MAXNUM * sizeof(u_char *));
    101   1.5  jdolecek 		if (!SINGL_FLD) {
    102   1.5  jdolecek 			linebuf_size = DEFLLEN;
    103   1.5  jdolecek 			linebuf = malloc(linebuf_size);
    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.10  jdolecek 			if (c == BUFFEND && nelem < min(9, MAXNUM)) {
    148  1.10  jdolecek 				const u_char **keyp;
    149  1.10  jdolecek 				u_char *oldb = buffer;
    150  1.10  jdolecek 
    151   1.5  jdolecek 				/* buffer was too small for data, allocate
    152   1.5  jdolecek 				 * bigger buffer */
    153   1.5  jdolecek 				bufsize *= 2;
    154   1.5  jdolecek 				buffer = realloc(buffer, bufsize);
    155   1.6  jdolecek 				if (!buffer) {
    156   1.5  jdolecek 					err(2, "failed to realloc buffer to %ld bytes",
    157   1.5  jdolecek 						(unsigned long) bufsize);
    158   1.6  jdolecek 				}
    159   1.6  jdolecek 				bufend = buffer + bufsize;
    160  1.10  jdolecek 
    161  1.10  jdolecek 				/* patch up keylist[] */
    162  1.10  jdolecek 				for(keyp = &keypos[-1]; keyp >= keylist; keyp--)
    163  1.10  jdolecek 					*keyp = buffer + (*keyp - oldb);
    164  1.10  jdolecek 
    165  1.10  jdolecek 				crec = (RECHEADER *) (buffer + ((u_char *)crec - oldb));
    166  1.10  jdolecek 				goto do_read;
    167   1.5  jdolecek 			}
    168   1.1     bjh21 			if (c == BUFFEND || ntfiles || mfct) {	/* push */
    169   1.1     bjh21 				if (panic >= PANIC) {
    170  1.11  jdolecek 					fstack[MSTART + mfct].fp = ftmp();
    171   1.7  jdolecek 					if ((stable_sort)
    172   1.7  jdolecek 						? sradixsort(keylist, nelem,
    173   1.7  jdolecek 							weights, REC_D)
    174   1.7  jdolecek 						: radixsort(keylist, nelem,
    175   1.7  jdolecek 							weights, REC_D) )
    176   1.1     bjh21 						err(2, NULL);
    177   1.9    itojun 					append(keylist, nelem, depth,
    178  1.11  jdolecek 					    fstack[MSTART + mfct].fp, putrec,
    179   1.9    itojun 					    ftbl);
    180   1.1     bjh21 					mfct++;
    181   1.1     bjh21 					/* reduce number of open files */
    182  1.11  jdolecek 					if (mfct == MERGE_FNUM ||(c == EOF && ntfiles)) {
    183   1.1     bjh21 						tmpbuf = malloc(bufend -
    184   1.1     bjh21 						    crec->data);
    185   1.1     bjh21 						memmove(tmpbuf, crec->data,
    186   1.1     bjh21 						    bufend - crec->data);
    187   1.8  jdolecek 						fstack[base + ntfiles].fp
    188   1.1     bjh21 						    = ftmp();
    189   1.8  jdolecek 						fmerge(0, MSTART, filelist,
    190   1.9    itojun 						    mfct, geteasy,
    191   1.9    itojun 						    fstack[base].fp,
    192   1.9    itojun 						    putrec, ftbl);
    193   1.1     bjh21 						++ntfiles;
    194   1.1     bjh21 						mfct = 0;
    195   1.1     bjh21 						memmove(crec->data, tmpbuf,
    196   1.1     bjh21 						    bufend - crec->data);
    197   1.1     bjh21 						free(tmpbuf);
    198   1.1     bjh21 					}
    199   1.1     bjh21 				} else {
    200   1.8  jdolecek 					fstack[base + ntfiles].fp= ftmp();
    201   1.1     bjh21 					onepass(keylist, depth, nelem, sizes,
    202   1.9    itojun 					    weights, fstack[base + ntfiles].fp);
    203   1.1     bjh21 					++ntfiles;
    204   1.1     bjh21 				}
    205   1.1     bjh21 			}
    206   1.1     bjh21 		}
    207   1.1     bjh21 		if (!ntfiles && !mfct) {	/* everything in memory--pop */
    208   1.7  jdolecek 			if (nelem > 1) {
    209   1.7  jdolecek 			   if ((stable_sort)
    210   1.7  jdolecek 				? sradixsort(keylist, nelem, weights, REC_D)
    211   1.7  jdolecek 				: radixsort(keylist, nelem, weights, REC_D) )
    212   1.1     bjh21 				err(2, NULL);
    213   1.7  jdolecek 			}
    214   1.3     bjh21 			append(keylist, nelem, depth, outfp, putline, ftbl);
    215   1.1     bjh21 			break;					/* pop */
    216   1.1     bjh21 		}
    217   1.1     bjh21 		if (panic >= PANIC) {
    218   1.1     bjh21 			if (!ntfiles)
    219   1.8  jdolecek 				fmerge(0, MSTART, filelist, mfct, geteasy,
    220   1.3     bjh21 				    outfp, putline, ftbl);
    221   1.1     bjh21 			else
    222   1.8  jdolecek 				fmerge(0, base, filelist, ntfiles, geteasy,
    223   1.3     bjh21 				    outfp, putline, ftbl);
    224   1.1     bjh21 			break;
    225   1.1     bjh21 
    226   1.1     bjh21 		}
    227   1.1     bjh21 		total = maxb = lastb = 0;	/* find if one bin dominates */
    228   1.1     bjh21 		for (i = 0; i < NBINS; i++)
    229   1.9    itojun 			if (sizes[i]) {
    230   1.9    itojun 				if (sizes[i] > sizes[maxb])
    231   1.9    itojun 					maxb = i;
    232   1.9    itojun 				lastb = i;
    233   1.9    itojun 				total += sizes[i];
    234   1.9    itojun 			}
    235   1.1     bjh21 		if (sizes[maxb] < max((total / 2) , BUFSIZE))
    236   1.1     bjh21 			maxb = lastb;	/* otherwise pop after last bin */
    237   1.8  jdolecek 		fstack[base].lastb = lastb;
    238   1.8  jdolecek 		fstack[base].maxb = maxb;
    239   1.1     bjh21 
    240   1.8  jdolecek 		/* start refining next level. */
    241   1.8  jdolecek 		getnext(-1, base, NULL, ntfiles, crec, bufend, 0); /* rewind */
    242   1.1     bjh21 		for (i = 0; i < maxb; i++) {
    243   1.1     bjh21 			if (!sizes[i])	/* bin empty; step ahead file offset */
    244   1.8  jdolecek 				getnext(i, base, NULL,ntfiles, crec, bufend, 0);
    245   1.8  jdolecek 			else {
    246   1.8  jdolecek 				fsort(i, depth+1, base, filelist, ntfiles,
    247   1.8  jdolecek 					outfp, ftbl);
    248   1.8  jdolecek 			}
    249   1.1     bjh21 		}
    250   1.8  jdolecek 
    251   1.8  jdolecek 		get = getnext;
    252   1.8  jdolecek 
    253   1.1     bjh21 		if (lastb != maxb) {
    254   1.3     bjh21 			if (prevfp != outfp)
    255   1.3     bjh21 				tailfp[panic] = prevfp;
    256   1.3     bjh21 			prevfp = ftmp();
    257   1.1     bjh21 			for (i = maxb+1; i <= lastb; i++)
    258   1.8  jdolecek 				if (!sizes[i]) {
    259   1.8  jdolecek 					getnext(i, base, NULL, ntfiles, crec,
    260   1.9    itojun 					    bufend,0);
    261   1.8  jdolecek 				} else {
    262   1.8  jdolecek 					fsort(i, depth+1, base, filelist,
    263   1.9    itojun 					    ntfiles, prevfp, ftbl);
    264   1.8  jdolecek 				}
    265   1.1     bjh21 		}
    266   1.1     bjh21 
    267   1.1     bjh21 		/* sort biggest (or last) bin at this level */
    268   1.1     bjh21 		depth++;
    269   1.1     bjh21 		panic++;
    270   1.1     bjh21 		binno = maxb;
    271   1.8  jdolecek 		top = base;
    272   1.1     bjh21 		nfiles = ntfiles;		/* so overwrite them */
    273   1.1     bjh21 	}
    274   1.3     bjh21 	if (prevfp != outfp) {
    275   1.3     bjh21 		concat(outfp, prevfp);
    276   1.3     bjh21 		fclose(prevfp);
    277   1.1     bjh21 	}
    278   1.1     bjh21 	for (i = panic; i >= 0; --i)
    279   1.3     bjh21 		if (tailfp[i]) {
    280   1.3     bjh21 			concat(outfp, tailfp[i]);
    281   1.3     bjh21 			fclose(tailfp[i]);
    282   1.1     bjh21 		}
    283   1.1     bjh21 }
    284   1.1     bjh21 
    285   1.1     bjh21 /*
    286   1.1     bjh21  This is one pass of radix exchange, dumping the bins to disk.
    287   1.1     bjh21  */
    288   1.1     bjh21 #define swap(a, b, t) t = a, a = b, b = t
    289   1.1     bjh21 void
    290   1.3     bjh21 onepass(a, depth, n, sizes, tr, fp)
    291   1.2     bjh21 	const u_char **a;
    292   1.1     bjh21 	int depth;
    293   1.1     bjh21 	long n, sizes[];
    294   1.1     bjh21 	u_char *tr;
    295   1.3     bjh21 	FILE *fp;
    296   1.1     bjh21 {
    297   1.6  jdolecek 	size_t tsizes[NBINS+1];
    298   1.2     bjh21 	const u_char **bin[257], ***bp, ***bpmax, **top[256], ***tp;
    299   1.2     bjh21 	static int histo[256];
    300   1.1     bjh21 	int *hp;
    301   1.4  jdolecek 	int c;
    302   1.2     bjh21 	const u_char **an, *t, **aj;
    303   1.4  jdolecek 	const u_char **ak, *r;
    304   1.1     bjh21 
    305   1.1     bjh21 	memset(tsizes, 0, sizeof(tsizes));
    306   1.1     bjh21 	depth += sizeof(TRECHEADER);
    307   1.6  jdolecek 	an = &a[n];
    308   1.1     bjh21 	for (ak = a; ak < an; ak++) {
    309   1.1     bjh21 		histo[c = tr[**ak]]++;
    310   1.5  jdolecek 		tsizes[c] += ((const RECHEADER *) (*ak -= depth))->length;
    311   1.1     bjh21 	}
    312   1.1     bjh21 
    313   1.1     bjh21 	bin[0] = a;
    314   1.1     bjh21 	bpmax = bin + 256;
    315   1.1     bjh21 	tp = top, hp = histo;
    316   1.1     bjh21 	for (bp = bin; bp < bpmax; bp++) {
    317   1.1     bjh21 		*tp++ = *(bp+1) = *bp + (c = *hp);
    318   1.1     bjh21 		*hp++ = 0;
    319   1.1     bjh21 		if (c <= 1)
    320   1.1     bjh21 			continue;
    321   1.1     bjh21 	}
    322   1.9    itojun 	for (aj = a; aj < an; *aj = r, aj = bin[c+1])
    323   1.9    itojun 		for (r = *aj; aj < (ak = --top[c = tr[r[depth]]]) ;)
    324   1.1     bjh21 			swap(*ak, r, t);
    325   1.1     bjh21 
    326   1.1     bjh21 	for (ak = a, c = 0; c < 256; c++) {
    327   1.1     bjh21 		an = bin[c+1];
    328   1.1     bjh21 		n = an - ak;
    329   1.1     bjh21 		tsizes[c] += n * sizeof(TRECHEADER);
    330   1.1     bjh21 		/* tell getnext how many elements in this bin, this segment. */
    331   1.6  jdolecek 		EWRITE(&tsizes[c], sizeof(size_t), 1, fp);
    332   1.1     bjh21 		sizes[c] += tsizes[c];
    333   1.1     bjh21 		for (; ak < an; ++ak)
    334   1.5  jdolecek 			putrec((const RECHEADER *) *ak, fp);
    335   1.1     bjh21 	}
    336   1.1     bjh21 }
    337