Home | History | Annotate | Line # | Download | only in sort
msort.c revision 1.8
      1  1.8  jdolecek /*	$NetBSD: msort.c,v 1.8 2001/01/13 10:33:30 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.2     bjh21 #include "sort.h"
     40  1.2     bjh21 #include "fsort.h"
     41  1.2     bjh21 
     42  1.1     bjh21 #ifndef lint
     43  1.8  jdolecek __RCSID("$NetBSD: msort.c,v 1.8 2001/01/13 10:33:30 jdolecek Exp $");
     44  1.2     bjh21 __SCCSID("@(#)msort.c	8.1 (Berkeley) 6/6/93");
     45  1.1     bjh21 #endif /* not lint */
     46  1.1     bjh21 
     47  1.1     bjh21 #include <stdlib.h>
     48  1.1     bjh21 #include <string.h>
     49  1.1     bjh21 #include <unistd.h>
     50  1.1     bjh21 
     51  1.1     bjh21 /* Subroutines using comparisons: merge sort and check order */
     52  1.1     bjh21 #define DELETE (1)
     53  1.1     bjh21 #define LALIGN(n) ((n+3) & ~3)
     54  1.1     bjh21 
     55  1.8  jdolecek /* Number of files merge() can merge in one pass. This should be power of two */
     56  1.8  jdolecek #define MERGE_FNUM	16
     57  1.8  jdolecek 
     58  1.1     bjh21 typedef struct mfile {
     59  1.1     bjh21 	u_char *end;
     60  1.1     bjh21 	short flno;
     61  1.1     bjh21 	struct recheader rec[1];
     62  1.1     bjh21 } MFILE;
     63  1.1     bjh21 typedef struct tmfile {
     64  1.1     bjh21 	u_char *end;
     65  1.1     bjh21 	short flno;
     66  1.1     bjh21 	struct trecheader rec[1];
     67  1.1     bjh21 } TMFILE;
     68  1.8  jdolecek 
     69  1.8  jdolecek static u_char *wts, *wts1 = 0;
     70  1.8  jdolecek static struct mfile *cfilebuf;
     71  1.1     bjh21 
     72  1.1     bjh21 static int cmp __P((struct recheader *, struct recheader *));
     73  1.1     bjh21 static int insert __P((struct mfile **, struct mfile **, int, int));
     74  1.1     bjh21 
     75  1.1     bjh21 void
     76  1.7  jdolecek fmerge(binno, top, filelist, nfiles, get, outfp, fput, ftbl)
     77  1.7  jdolecek 	int binno, top;
     78  1.7  jdolecek 	struct filelist *filelist;
     79  1.7  jdolecek 	int nfiles;
     80  1.7  jdolecek 	get_func_t get;
     81  1.3     bjh21 	FILE *outfp;
     82  1.7  jdolecek 	put_func_t fput;
     83  1.1     bjh21 	struct field *ftbl;
     84  1.1     bjh21 {
     85  1.1     bjh21 	FILE *tout;
     86  1.1     bjh21 	int i, j, last;
     87  1.7  jdolecek 	put_func_t put;
     88  1.1     bjh21 	struct tempfile *l_fstack;
     89  1.1     bjh21 
     90  1.1     bjh21 	wts = ftbl->weights;
     91  1.1     bjh21 	if (!UNIQUE && SINGL_FLD && ftbl->flags & F)
     92  1.1     bjh21 		wts1 = (ftbl->flags & R) ? Rascii : ascii;
     93  1.1     bjh21 	if (!cfilebuf)
     94  1.5  jdolecek 		cfilebuf = malloc(DEFLLEN + sizeof(TMFILE));
     95  1.1     bjh21 
     96  1.8  jdolecek 	i = min(MERGE_FNUM, nfiles) * LALIGN(DEFLLEN+sizeof(TMFILE));
     97  1.5  jdolecek 	if (!buffer || i > bufsize) {
     98  1.1     bjh21 		buffer = buffer ? realloc(buffer, i) : malloc(i);
     99  1.1     bjh21 		if (!buffer)
    100  1.1     bjh21 			err(2, NULL);
    101  1.5  jdolecek 		if (!linebuf && !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.1     bjh21 
    107  1.1     bjh21 	if (binno >= 0)
    108  1.7  jdolecek 		l_fstack = fstack + top;
    109  1.1     bjh21 	else
    110  1.1     bjh21 		l_fstack = fstack;
    111  1.1     bjh21 	while (nfiles) {
    112  1.1     bjh21 		put = putrec;
    113  1.8  jdolecek 		for (j = 0; j < nfiles; j += MERGE_FNUM) {
    114  1.8  jdolecek 			if (nfiles <= MERGE_FNUM) {
    115  1.3     bjh21 				tout = outfp;
    116  1.1     bjh21 				put = fput;
    117  1.1     bjh21 			}
    118  1.1     bjh21 			else
    119  1.1     bjh21 				tout = ftmp();
    120  1.8  jdolecek 			last = min(MERGE_FNUM, nfiles - j);
    121  1.1     bjh21 			if (binno < 0) {
    122  1.7  jdolecek 				FILE *fp;
    123  1.7  jdolecek 				for (i = 0; i < last; i++) {
    124  1.7  jdolecek 					fp = fopen(filelist->names[j+i], "r");
    125  1.7  jdolecek 					if (!fp) {
    126  1.7  jdolecek 						err(2, "%s",
    127  1.7  jdolecek 							filelist->names[j+i]);
    128  1.7  jdolecek 					}
    129  1.8  jdolecek 					l_fstack[i+MAXFCT-1-MERGE_FNUM].fp = fp;
    130  1.7  jdolecek 				}
    131  1.8  jdolecek 				merge(MAXFCT-1-MERGE_FNUM, last, get, tout, put, ftbl);
    132  1.1     bjh21 			}
    133  1.1     bjh21 			else {
    134  1.1     bjh21 				for (i = 0; i< last; i++)
    135  1.3     bjh21 					rewind(l_fstack[i+j].fp);
    136  1.7  jdolecek 				merge(top+j, last, get, tout, put, ftbl);
    137  1.1     bjh21 			}
    138  1.8  jdolecek 			if (nfiles > MERGE_FNUM)
    139  1.8  jdolecek 				l_fstack[j/MERGE_FNUM].fp = tout;
    140  1.1     bjh21 		}
    141  1.8  jdolecek 		nfiles = (nfiles + (MERGE_FNUM - 1)) / MERGE_FNUM;
    142  1.1     bjh21 		if (nfiles == 1)
    143  1.1     bjh21 			nfiles = 0;
    144  1.1     bjh21 		if (binno < 0) {
    145  1.1     bjh21 			binno = 0;
    146  1.1     bjh21 			get = geteasy;
    147  1.7  jdolecek 			top = 0;
    148  1.1     bjh21 		}
    149  1.1     bjh21 	}
    150  1.1     bjh21 }
    151  1.1     bjh21 
    152  1.1     bjh21 void
    153  1.3     bjh21 merge(infl0, nfiles, get, outfp, put, ftbl)
    154  1.1     bjh21 	int infl0, nfiles;
    155  1.7  jdolecek 	get_func_t get;
    156  1.7  jdolecek 	put_func_t put;
    157  1.3     bjh21 	FILE *outfp;
    158  1.1     bjh21 	struct field *ftbl;
    159  1.1     bjh21 {
    160  1.1     bjh21 	int c, i, j;
    161  1.8  jdolecek 	struct mfile *flist[MERGE_FNUM], *cfile;
    162  1.8  jdolecek 
    163  1.1     bjh21 	for (i = j = 0; i < nfiles; i++) {
    164  1.1     bjh21 		cfile = (MFILE *) (buffer +
    165  1.5  jdolecek 		    i * LALIGN(DEFLLEN + sizeof(TMFILE)));
    166  1.8  jdolecek 		cfile->flno = j;
    167  1.5  jdolecek 		cfile->end = cfile->rec->data + DEFLLEN;
    168  1.1     bjh21 		for (c = 1; c == 1;) {
    169  1.8  jdolecek 			if (EOF == (c = get(infl0+j, 0, NULL, nfiles,
    170  1.1     bjh21 			   cfile->rec, cfile->end, ftbl))) {
    171  1.1     bjh21 				--i;
    172  1.1     bjh21 				--nfiles;
    173  1.1     bjh21 				break;
    174  1.1     bjh21 			}
    175  1.1     bjh21 			if (i)
    176  1.1     bjh21 				c = insert(flist, &cfile, i, !DELETE);
    177  1.1     bjh21 			else
    178  1.1     bjh21 				flist[0] = cfile;
    179  1.1     bjh21 		}
    180  1.1     bjh21 		j++;
    181  1.1     bjh21 	}
    182  1.1     bjh21 	cfile = cfilebuf;
    183  1.1     bjh21 	cfile->flno = flist[0]->flno;
    184  1.5  jdolecek 	cfile->end = cfile->rec->data + DEFLLEN;
    185  1.1     bjh21 	while (nfiles) {
    186  1.1     bjh21 		for (c = 1; c == 1;) {
    187  1.8  jdolecek 			if (EOF == (c = get(infl0+cfile->flno, 0, NULL, nfiles,
    188  1.1     bjh21 			   cfile->rec, cfile->end, ftbl))) {
    189  1.3     bjh21 				put(flist[0]->rec, outfp);
    190  1.1     bjh21 				memmove(flist, flist + 1,
    191  1.1     bjh21 				    sizeof(MFILE *) * (--nfiles));
    192  1.1     bjh21 				cfile->flno = flist[0]->flno;
    193  1.1     bjh21 				break;
    194  1.1     bjh21 			}
    195  1.1     bjh21 			if (!(c = insert(flist, &cfile, nfiles, DELETE)))
    196  1.3     bjh21 				put(cfile->rec, outfp);
    197  1.1     bjh21 		}
    198  1.1     bjh21 	}
    199  1.1     bjh21 }
    200  1.1     bjh21 
    201  1.1     bjh21 /*
    202  1.1     bjh21  * if delete: inserts *rec in flist, deletes flist[0], and leaves it in *rec;
    203  1.1     bjh21  * otherwise just inserts *rec in flist.
    204  1.1     bjh21 */
    205  1.1     bjh21 static int
    206  1.1     bjh21 insert(flist, rec, ttop, delete)
    207  1.1     bjh21 	struct mfile **flist, **rec;
    208  1.1     bjh21 	int delete, ttop;			/* delete = 0 or 1 */
    209  1.1     bjh21 {
    210  1.8  jdolecek 	struct mfile *tmprec = *rec;
    211  1.8  jdolecek 	int mid, top = ttop, bot = 0, cmpv = 1;
    212  1.8  jdolecek 
    213  1.1     bjh21 	for (mid = top/2; bot +1 != top; mid = (bot+top)/2) {
    214  1.1     bjh21 		cmpv = cmp(tmprec->rec, flist[mid]->rec);
    215  1.1     bjh21 		if (cmpv < 0)
    216  1.1     bjh21 			top = mid;
    217  1.1     bjh21 		else if (cmpv > 0)
    218  1.1     bjh21 			bot = mid;
    219  1.1     bjh21 		else {
    220  1.8  jdolecek 			if (UNIQUE)
    221  1.8  jdolecek 				break;
    222  1.8  jdolecek 
    223  1.8  jdolecek 			if (stable_sort) {
    224  1.8  jdolecek 				/*
    225  1.8  jdolecek 				 * Apply sort by fileno, to give priority
    226  1.8  jdolecek 				 * to earlier specified files, hence providing
    227  1.8  jdolecek 				 * more stable sort.
    228  1.8  jdolecek 				 * If fileno is same, the new record should
    229  1.8  jdolecek 				 * be put _after_ the previous entry.
    230  1.8  jdolecek 				 */
    231  1.8  jdolecek 				cmpv = tmprec->flno - flist[mid]->flno;
    232  1.8  jdolecek 				if (cmpv >= 0)
    233  1.8  jdolecek 					bot = mid;
    234  1.8  jdolecek 				else /* cmpv == 0 */
    235  1.8  jdolecek 					bot = mid - 1;
    236  1.8  jdolecek 			} else {
    237  1.8  jdolecek 				/* non-stable sort */
    238  1.1     bjh21 				bot = mid - 1;
    239  1.8  jdolecek 			}
    240  1.8  jdolecek 
    241  1.1     bjh21 			break;
    242  1.1     bjh21 		}
    243  1.1     bjh21 	}
    244  1.8  jdolecek 
    245  1.1     bjh21 	if (delete) {
    246  1.1     bjh21 		if (UNIQUE) {
    247  1.1     bjh21 			if (!bot && cmpv)
    248  1.1     bjh21 				cmpv = cmp(tmprec->rec, flist[0]->rec);
    249  1.1     bjh21 			if (!cmpv)
    250  1.1     bjh21 				return(1);
    251  1.1     bjh21 		}
    252  1.1     bjh21 		tmprec = flist[0];
    253  1.1     bjh21 		if (bot)
    254  1.1     bjh21 			memmove(flist, flist+1, bot * sizeof(MFILE **));
    255  1.1     bjh21 		flist[bot] = *rec;
    256  1.1     bjh21 		*rec = tmprec;
    257  1.8  jdolecek 		(*rec)->flno = flist[0]->flno;
    258  1.1     bjh21 		return (0);
    259  1.8  jdolecek 	} else {
    260  1.1     bjh21 		if (!bot && !(UNIQUE && !cmpv)) {
    261  1.1     bjh21 			cmpv = cmp(tmprec->rec, flist[0]->rec);
    262  1.1     bjh21 			if (cmpv < 0)
    263  1.1     bjh21 				bot = -1;
    264  1.1     bjh21 		}
    265  1.1     bjh21 		if (UNIQUE && !cmpv)
    266  1.1     bjh21 			return (1);
    267  1.1     bjh21 		bot++;
    268  1.1     bjh21 		memmove(flist + bot+1, flist + bot,
    269  1.1     bjh21 		    (ttop - bot) * sizeof(MFILE **));
    270  1.1     bjh21 		flist[bot] = *rec;
    271  1.1     bjh21 		return (0);
    272  1.1     bjh21 	}
    273  1.1     bjh21 }
    274  1.1     bjh21 
    275  1.1     bjh21 /*
    276  1.1     bjh21  * check order on one file
    277  1.1     bjh21  */
    278  1.1     bjh21 void
    279  1.7  jdolecek order(filelist, get, ftbl)
    280  1.7  jdolecek 	struct filelist *filelist;
    281  1.7  jdolecek 	get_func_t get;
    282  1.1     bjh21 	struct field *ftbl;
    283  1.1     bjh21 {
    284  1.6  jdolecek 	u_char *crec_end, *prec_end, *trec_end;
    285  1.1     bjh21 	int c;
    286  1.1     bjh21 	struct recheader *crec, *prec, *trec;
    287  1.1     bjh21 
    288  1.1     bjh21 	if (!SINGL_FLD)
    289  1.5  jdolecek 		linebuf = malloc(DEFLLEN);
    290  1.5  jdolecek 	buffer = malloc(2 * (DEFLLEN + sizeof(TRECHEADER)));
    291  1.1     bjh21 	crec = (RECHEADER *) buffer;
    292  1.6  jdolecek 	crec_end = buffer + DEFLLEN + sizeof(TRECHEADER);
    293  1.5  jdolecek 	prec = (RECHEADER *) (buffer + DEFLLEN + sizeof(TRECHEADER));
    294  1.6  jdolecek 	prec_end = buffer + 2*(DEFLLEN + sizeof(TRECHEADER));
    295  1.1     bjh21 	wts = ftbl->weights;
    296  1.6  jdolecek 	if (SINGL_FLD && (ftbl->flags & F))
    297  1.8  jdolecek 		wts1 = (ftbl->flags & R) ? Rascii : ascii;
    298  1.1     bjh21 	else
    299  1.1     bjh21 		wts1 = 0;
    300  1.7  jdolecek 	if (0 == get(-1, 0, filelist, 1, prec, prec_end, ftbl))
    301  1.7  jdolecek 	while (0 == get(-1, 0, filelist, 1, crec, crec_end, ftbl)) {
    302  1.1     bjh21 		if (0 < (c = cmp(prec, crec))) {
    303  1.1     bjh21 			crec->data[crec->length-1] = 0;
    304  1.1     bjh21 			errx(1, "found disorder: %s", crec->data+crec->offset);
    305  1.1     bjh21 		}
    306  1.1     bjh21 		if (UNIQUE && !c) {
    307  1.1     bjh21 			crec->data[crec->length-1] = 0;
    308  1.1     bjh21 			errx(1, "found non-uniqueness: %s",
    309  1.1     bjh21 			    crec->data+crec->offset);
    310  1.1     bjh21 		}
    311  1.6  jdolecek 		/*
    312  1.6  jdolecek 		 * Swap pointers so that this record is on place pointed
    313  1.6  jdolecek 		 * to by prec and new record is read to place pointed to by
    314  1.6  jdolecek 		 * crec.
    315  1.6  jdolecek 		 */
    316  1.1     bjh21 		trec = prec;
    317  1.1     bjh21 		prec = crec;
    318  1.1     bjh21 		crec = trec;
    319  1.6  jdolecek 		trec_end = prec_end;
    320  1.6  jdolecek 		prec_end = crec_end;
    321  1.6  jdolecek 		crec_end = trec_end;
    322  1.1     bjh21 	}
    323  1.1     bjh21 	exit(0);
    324  1.1     bjh21 }
    325  1.1     bjh21 
    326  1.1     bjh21 static int
    327  1.1     bjh21 cmp(rec1, rec2)
    328  1.1     bjh21 	struct recheader *rec1, *rec2;
    329  1.1     bjh21 {
    330  1.4  jdolecek 	int r;
    331  1.4  jdolecek 	u_char *pos1, *pos2, *end;
    332  1.4  jdolecek 	u_char *cwts;
    333  1.1     bjh21 	for (cwts = wts; cwts; cwts = (cwts == wts1 ? 0 : wts1)) {
    334  1.1     bjh21 		pos1 = rec1->data;
    335  1.1     bjh21 		pos2 = rec2->data;
    336  1.8  jdolecek 		if (!SINGL_FLD && (UNIQUE || stable_sort))
    337  1.1     bjh21 			end = pos1 + min(rec1->offset, rec2->offset);
    338  1.1     bjh21 		else
    339  1.1     bjh21 			end = pos1 + min(rec1->length, rec2->length);
    340  1.8  jdolecek 
    341  1.1     bjh21 		for (; pos1 < end; ) {
    342  1.2     bjh21 			if ((r = cwts[*pos1++] - cwts[*pos2++]))
    343  1.1     bjh21 				return (r);
    344  1.1     bjh21 		}
    345  1.1     bjh21 	}
    346  1.1     bjh21 	return (0);
    347  1.1     bjh21 }
    348