Home | History | Annotate | Line # | Download | only in sort
msort.c revision 1.1
      1  1.1  bjh21 /*-
      2  1.1  bjh21  * Copyright (c) 1993
      3  1.1  bjh21  *	The Regents of the University of California.  All rights reserved.
      4  1.1  bjh21  *
      5  1.1  bjh21  * This code is derived from software contributed to Berkeley by
      6  1.1  bjh21  * Peter McIlroy.
      7  1.1  bjh21  *
      8  1.1  bjh21  * Redistribution and use in source and binary forms, with or without
      9  1.1  bjh21  * modification, are permitted provided that the following conditions
     10  1.1  bjh21  * are met:
     11  1.1  bjh21  * 1. Redistributions of source code must retain the above copyright
     12  1.1  bjh21  *    notice, this list of conditions and the following disclaimer.
     13  1.1  bjh21  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  bjh21  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  bjh21  *    documentation and/or other materials provided with the distribution.
     16  1.1  bjh21  * 3. All advertising materials mentioning features or use of this software
     17  1.1  bjh21  *    must display the following acknowledgement:
     18  1.1  bjh21  *	This product includes software developed by the University of
     19  1.1  bjh21  *	California, Berkeley and its contributors.
     20  1.1  bjh21  * 4. Neither the name of the University nor the names of its contributors
     21  1.1  bjh21  *    may be used to endorse or promote products derived from this software
     22  1.1  bjh21  *    without specific prior written permission.
     23  1.1  bjh21  *
     24  1.1  bjh21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  1.1  bjh21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  1.1  bjh21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  1.1  bjh21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  1.1  bjh21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  1.1  bjh21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  1.1  bjh21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  1.1  bjh21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  1.1  bjh21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  1.1  bjh21  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  1.1  bjh21  * SUCH DAMAGE.
     35  1.1  bjh21  */
     36  1.1  bjh21 
     37  1.1  bjh21 #ifndef lint
     38  1.1  bjh21 static char sccsid[] = "@(#)msort.c	8.1 (Berkeley) 6/6/93";
     39  1.1  bjh21 #endif /* not lint */
     40  1.1  bjh21 
     41  1.1  bjh21 #include "sort.h"
     42  1.1  bjh21 #include "fsort.h"
     43  1.1  bjh21 
     44  1.1  bjh21 #include <stdlib.h>
     45  1.1  bjh21 #include <string.h>
     46  1.1  bjh21 #include <unistd.h>
     47  1.1  bjh21 
     48  1.1  bjh21 /* Subroutines using comparisons: merge sort and check order */
     49  1.1  bjh21 #define DELETE (1)
     50  1.1  bjh21 #define LALIGN(n) ((n+3) & ~3)
     51  1.1  bjh21 
     52  1.1  bjh21 typedef struct mfile {
     53  1.1  bjh21 	u_char *end;
     54  1.1  bjh21 	short flno;
     55  1.1  bjh21 	struct recheader rec[1];
     56  1.1  bjh21 } MFILE;
     57  1.1  bjh21 typedef struct tmfile {
     58  1.1  bjh21 	u_char *end;
     59  1.1  bjh21 	short flno;
     60  1.1  bjh21 	struct trecheader rec[1];
     61  1.1  bjh21 } TMFILE;
     62  1.1  bjh21 u_char *wts, *wts1 = 0;
     63  1.1  bjh21 struct mfile *cfilebuf;
     64  1.1  bjh21 
     65  1.1  bjh21 static int cmp __P((struct recheader *, struct recheader *));
     66  1.1  bjh21 static int insert __P((struct mfile **, struct mfile **, int, int));
     67  1.1  bjh21 
     68  1.1  bjh21 void
     69  1.1  bjh21 fmerge(binno, files, nfiles, get, outfd, fput, ftbl)
     70  1.1  bjh21 	union f_handle files;
     71  1.1  bjh21 	int binno, nfiles;
     72  1.1  bjh21 	int (*get)();
     73  1.1  bjh21 	FILE *outfd;
     74  1.1  bjh21 	void (*fput)();
     75  1.1  bjh21 	struct field *ftbl;
     76  1.1  bjh21 {
     77  1.1  bjh21 	FILE *tout;
     78  1.1  bjh21 	int i, j, last;
     79  1.1  bjh21 	void (*put)(struct recheader *, FILE *);
     80  1.1  bjh21 	extern int geteasy();
     81  1.1  bjh21 	struct tempfile *l_fstack;
     82  1.1  bjh21 
     83  1.1  bjh21 	wts = ftbl->weights;
     84  1.1  bjh21 	if (!UNIQUE && SINGL_FLD && ftbl->flags & F)
     85  1.1  bjh21 		wts1 = (ftbl->flags & R) ? Rascii : ascii;
     86  1.1  bjh21 	if (!cfilebuf)
     87  1.1  bjh21 		cfilebuf = malloc(MAXLLEN + sizeof(TMFILE));
     88  1.1  bjh21 
     89  1.1  bjh21 	i = min(16, nfiles) * LALIGN(MAXLLEN+sizeof(TMFILE));
     90  1.1  bjh21 	if (!buffer || i > BUFSIZE) {
     91  1.1  bjh21 		buffer = buffer ? realloc(buffer, i) : malloc(i);
     92  1.1  bjh21 		if (!buffer)
     93  1.1  bjh21 			err(2, NULL);
     94  1.1  bjh21 		if (!SINGL_FLD)
     95  1.1  bjh21 			linebuf = malloc(MAXLLEN);
     96  1.1  bjh21 	}
     97  1.1  bjh21 
     98  1.1  bjh21 	if (binno >= 0)
     99  1.1  bjh21 		l_fstack = fstack + files.top;
    100  1.1  bjh21 	else
    101  1.1  bjh21 		l_fstack = fstack;
    102  1.1  bjh21 	while (nfiles) {
    103  1.1  bjh21 		put = putrec;
    104  1.1  bjh21 		for (j = 0; j < nfiles; j += 16) {
    105  1.1  bjh21 			if (nfiles <= 16) {
    106  1.1  bjh21 				tout = outfd;
    107  1.1  bjh21 				put = fput;
    108  1.1  bjh21 			}
    109  1.1  bjh21 			else
    110  1.1  bjh21 				tout = ftmp();
    111  1.1  bjh21 			last = min(16, nfiles - j);
    112  1.1  bjh21 			if (binno < 0) {
    113  1.1  bjh21 				for (i = 0; i < last; i++)
    114  1.1  bjh21 					if (!(l_fstack[i+MAXFCT-1-16].fd =
    115  1.1  bjh21 					    fopen(files.names[j + i], "r")))
    116  1.1  bjh21 						err(2, "%s", files.names[j+i]);
    117  1.1  bjh21 				merge(MAXFCT-1-16, last, get, tout, put, ftbl);
    118  1.1  bjh21 			}
    119  1.1  bjh21 			else {
    120  1.1  bjh21 				for (i = 0; i< last; i++)
    121  1.1  bjh21 					rewind(l_fstack[i+j].fd);
    122  1.1  bjh21 				merge(files.top+j, last, get, tout, put, ftbl);
    123  1.1  bjh21 			}
    124  1.1  bjh21 			if (nfiles > 16) l_fstack[j/16].fd = tout;
    125  1.1  bjh21 		}
    126  1.1  bjh21 		nfiles = (nfiles + 15) / 16;
    127  1.1  bjh21 		if (nfiles == 1)
    128  1.1  bjh21 			nfiles = 0;
    129  1.1  bjh21 		if (binno < 0) {
    130  1.1  bjh21 			binno = 0;
    131  1.1  bjh21 			get = geteasy;
    132  1.1  bjh21 			files.top = 0;
    133  1.1  bjh21 		}
    134  1.1  bjh21 	}
    135  1.1  bjh21 }
    136  1.1  bjh21 
    137  1.1  bjh21 void
    138  1.1  bjh21 merge(infl0, nfiles, get, outfd, put, ftbl)
    139  1.1  bjh21 	int infl0, nfiles;
    140  1.1  bjh21 	int (*get)();
    141  1.1  bjh21 	void (*put)(struct recheader *, FILE *);
    142  1.1  bjh21 	FILE *outfd;
    143  1.1  bjh21 	struct field *ftbl;
    144  1.1  bjh21 {
    145  1.1  bjh21 	int c, i, j;
    146  1.1  bjh21 	union f_handle dummy = {0};
    147  1.1  bjh21 	struct mfile *flist[16], *cfile;
    148  1.1  bjh21 	for (i = j = 0; i < nfiles; i++) {
    149  1.1  bjh21 		cfile = (MFILE *) (buffer +
    150  1.1  bjh21 		    i * LALIGN(MAXLLEN + sizeof(TMFILE)));
    151  1.1  bjh21 		cfile->flno = j + infl0;
    152  1.1  bjh21 		cfile->end = cfile->rec->data + MAXLLEN;
    153  1.1  bjh21 		for (c = 1; c == 1;) {
    154  1.1  bjh21 			if (EOF == (c = get(j+infl0, dummy, nfiles,
    155  1.1  bjh21 			   cfile->rec, cfile->end, ftbl))) {
    156  1.1  bjh21 				--i;
    157  1.1  bjh21 				--nfiles;
    158  1.1  bjh21 				break;
    159  1.1  bjh21 			}
    160  1.1  bjh21 			if (i)
    161  1.1  bjh21 				c = insert(flist, &cfile, i, !DELETE);
    162  1.1  bjh21 			else
    163  1.1  bjh21 				flist[0] = cfile;
    164  1.1  bjh21 		}
    165  1.1  bjh21 		j++;
    166  1.1  bjh21 	}
    167  1.1  bjh21 	cfile = cfilebuf;
    168  1.1  bjh21 	cfile->flno = flist[0]->flno;
    169  1.1  bjh21 	cfile->end = cfile->rec->data + MAXLLEN;
    170  1.1  bjh21 	while (nfiles) {
    171  1.1  bjh21 		for (c = 1; c == 1;) {
    172  1.1  bjh21 			if (EOF == (c = get(cfile->flno, dummy, nfiles,
    173  1.1  bjh21 			   cfile->rec, cfile->end, ftbl))) {
    174  1.1  bjh21 				put(flist[0]->rec, outfd);
    175  1.1  bjh21 				memmove(flist, flist + 1,
    176  1.1  bjh21 				    sizeof(MFILE *) * (--nfiles));
    177  1.1  bjh21 				cfile->flno = flist[0]->flno;
    178  1.1  bjh21 				break;
    179  1.1  bjh21 			}
    180  1.1  bjh21 			if (!(c = insert(flist, &cfile, nfiles, DELETE)))
    181  1.1  bjh21 				put(cfile->rec, outfd);
    182  1.1  bjh21 		}
    183  1.1  bjh21 	}
    184  1.1  bjh21 }
    185  1.1  bjh21 
    186  1.1  bjh21 /*
    187  1.1  bjh21  * if delete: inserts *rec in flist, deletes flist[0], and leaves it in *rec;
    188  1.1  bjh21  * otherwise just inserts *rec in flist.
    189  1.1  bjh21 */
    190  1.1  bjh21 static int
    191  1.1  bjh21 insert(flist, rec, ttop, delete)
    192  1.1  bjh21 	struct mfile **flist, **rec;
    193  1.1  bjh21 	int delete, ttop;			/* delete = 0 or 1 */
    194  1.1  bjh21 {
    195  1.1  bjh21 	register struct mfile *tmprec;
    196  1.1  bjh21 	register int top, mid, bot = 0, cmpv = 1;
    197  1.1  bjh21 	tmprec = *rec;
    198  1.1  bjh21 	top = ttop;
    199  1.1  bjh21 	for (mid = top/2; bot +1 != top; mid = (bot+top)/2) {
    200  1.1  bjh21 		cmpv = cmp(tmprec->rec, flist[mid]->rec);
    201  1.1  bjh21 		if (cmpv < 0)
    202  1.1  bjh21 			top = mid;
    203  1.1  bjh21 		else if (cmpv > 0)
    204  1.1  bjh21 			bot = mid;
    205  1.1  bjh21 		else {
    206  1.1  bjh21 			if (!UNIQUE)
    207  1.1  bjh21 				bot = mid - 1;
    208  1.1  bjh21 			break;
    209  1.1  bjh21 		}
    210  1.1  bjh21 	}
    211  1.1  bjh21 	if (delete) {
    212  1.1  bjh21 		if (UNIQUE) {
    213  1.1  bjh21 			if (!bot && cmpv)
    214  1.1  bjh21 				cmpv = cmp(tmprec->rec, flist[0]->rec);
    215  1.1  bjh21 			if (!cmpv)
    216  1.1  bjh21 				return(1);
    217  1.1  bjh21 		}
    218  1.1  bjh21 		tmprec = flist[0];
    219  1.1  bjh21 		if (bot)
    220  1.1  bjh21 			memmove(flist, flist+1, bot * sizeof(MFILE **));
    221  1.1  bjh21 		flist[bot] = *rec;
    222  1.1  bjh21 		*rec = tmprec;
    223  1.1  bjh21 		(*rec)->flno = (*flist)->flno;
    224  1.1  bjh21 		return (0);
    225  1.1  bjh21 	}
    226  1.1  bjh21 	else {
    227  1.1  bjh21 		if (!bot && !(UNIQUE && !cmpv)) {
    228  1.1  bjh21 			cmpv = cmp(tmprec->rec, flist[0]->rec);
    229  1.1  bjh21 			if (cmpv < 0)
    230  1.1  bjh21 				bot = -1;
    231  1.1  bjh21 		}
    232  1.1  bjh21 		if (UNIQUE && !cmpv)
    233  1.1  bjh21 			return (1);
    234  1.1  bjh21 		bot++;
    235  1.1  bjh21 		memmove(flist + bot+1, flist + bot,
    236  1.1  bjh21 		    (ttop - bot) * sizeof(MFILE **));
    237  1.1  bjh21 		flist[bot] = *rec;
    238  1.1  bjh21 		return (0);
    239  1.1  bjh21 	}
    240  1.1  bjh21 }
    241  1.1  bjh21 
    242  1.1  bjh21 /*
    243  1.1  bjh21  * check order on one file
    244  1.1  bjh21  */
    245  1.1  bjh21 void
    246  1.1  bjh21 order(infile, get, ftbl)
    247  1.1  bjh21 	union f_handle infile;
    248  1.1  bjh21 	int (*get)();
    249  1.1  bjh21 	struct field *ftbl;
    250  1.1  bjh21 {
    251  1.1  bjh21 	u_char *end;
    252  1.1  bjh21 	int c;
    253  1.1  bjh21 	struct recheader *crec, *prec, *trec;
    254  1.1  bjh21 
    255  1.1  bjh21 	if (!SINGL_FLD)
    256  1.1  bjh21 		linebuf = malloc(MAXLLEN);
    257  1.1  bjh21 	buffer = malloc(2 * (MAXLLEN + sizeof(TRECHEADER)));
    258  1.1  bjh21 	end = buffer + 2 * (MAXLLEN + sizeof(TRECHEADER));
    259  1.1  bjh21 	crec = (RECHEADER *) buffer;
    260  1.1  bjh21 	prec = (RECHEADER *) (buffer + MAXLLEN + sizeof(TRECHEADER));
    261  1.1  bjh21 	wts = ftbl->weights;
    262  1.1  bjh21 	if (SINGL_FLD && ftbl->flags & F)
    263  1.1  bjh21 		wts1 = ftbl->flags & R ? Rascii : ascii;
    264  1.1  bjh21 	else
    265  1.1  bjh21 		wts1 = 0;
    266  1.1  bjh21 	if (0 == get(-1, infile, 1, prec, end, ftbl))
    267  1.1  bjh21 	while (0 == get(-1, infile, 1, crec, end, ftbl)) {
    268  1.1  bjh21 		if (0 < (c = cmp(prec, crec))) {
    269  1.1  bjh21 			crec->data[crec->length-1] = 0;
    270  1.1  bjh21 			errx(1, "found disorder: %s", crec->data+crec->offset);
    271  1.1  bjh21 		}
    272  1.1  bjh21 		if (UNIQUE && !c) {
    273  1.1  bjh21 			crec->data[crec->length-1] = 0;
    274  1.1  bjh21 			errx(1, "found non-uniqueness: %s",
    275  1.1  bjh21 			    crec->data+crec->offset);
    276  1.1  bjh21 		}
    277  1.1  bjh21 		trec = prec;
    278  1.1  bjh21 		prec = crec;
    279  1.1  bjh21 		crec = trec;
    280  1.1  bjh21 	}
    281  1.1  bjh21 	exit(0);
    282  1.1  bjh21 }
    283  1.1  bjh21 
    284  1.1  bjh21 static int
    285  1.1  bjh21 cmp(rec1, rec2)
    286  1.1  bjh21 	struct recheader *rec1, *rec2;
    287  1.1  bjh21 {
    288  1.1  bjh21 	register r;
    289  1.1  bjh21 	register u_char *pos1, *pos2, *end;
    290  1.1  bjh21 	register u_char *cwts;
    291  1.1  bjh21 	for (cwts = wts; cwts; cwts = (cwts == wts1 ? 0 : wts1)) {
    292  1.1  bjh21 		pos1 = rec1->data;
    293  1.1  bjh21 		pos2 = rec2->data;
    294  1.1  bjh21 		if (!SINGL_FLD && UNIQUE)
    295  1.1  bjh21 			end = pos1 + min(rec1->offset, rec2->offset);
    296  1.1  bjh21 		else
    297  1.1  bjh21 			end = pos1 + min(rec1->length, rec2->length);
    298  1.1  bjh21 		for (; pos1 < end; ) {
    299  1.1  bjh21 			if (r = cwts[*pos1++] - cwts[*pos2++])
    300  1.1  bjh21 				return (r);
    301  1.1  bjh21 		}
    302  1.1  bjh21 	}
    303  1.1  bjh21 	return (0);
    304  1.1  bjh21 }
    305