Home | History | Annotate | Line # | Download | only in sort
msort.c revision 1.4
      1  1.4  jdolecek /*	$NetBSD: msort.c,v 1.4 2000/10/15 20:46:33 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.4  jdolecek __RCSID("$NetBSD: msort.c,v 1.4 2000/10/15 20:46:33 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.1     bjh21 typedef struct mfile {
     56  1.1     bjh21 	u_char *end;
     57  1.1     bjh21 	short flno;
     58  1.1     bjh21 	struct recheader rec[1];
     59  1.1     bjh21 } MFILE;
     60  1.1     bjh21 typedef struct tmfile {
     61  1.1     bjh21 	u_char *end;
     62  1.1     bjh21 	short flno;
     63  1.1     bjh21 	struct trecheader rec[1];
     64  1.1     bjh21 } TMFILE;
     65  1.1     bjh21 u_char *wts, *wts1 = 0;
     66  1.1     bjh21 struct mfile *cfilebuf;
     67  1.1     bjh21 
     68  1.1     bjh21 static int cmp __P((struct recheader *, struct recheader *));
     69  1.1     bjh21 static int insert __P((struct mfile **, struct mfile **, int, int));
     70  1.1     bjh21 
     71  1.1     bjh21 void
     72  1.3     bjh21 fmerge(binno, files, nfiles, get, outfp, fput, ftbl)
     73  1.1     bjh21 	union f_handle files;
     74  1.1     bjh21 	int binno, nfiles;
     75  1.2     bjh21 	int (*get) __P((int, union f_handle, int, struct recheader *, u_char *,
     76  1.2     bjh21 	    struct field *));
     77  1.3     bjh21 	FILE *outfp;
     78  1.2     bjh21 	void (*fput) __P((struct recheader *, FILE *));
     79  1.1     bjh21 	struct field *ftbl;
     80  1.1     bjh21 {
     81  1.1     bjh21 	FILE *tout;
     82  1.1     bjh21 	int i, j, last;
     83  1.1     bjh21 	void (*put)(struct recheader *, FILE *);
     84  1.1     bjh21 	struct tempfile *l_fstack;
     85  1.1     bjh21 
     86  1.1     bjh21 	wts = ftbl->weights;
     87  1.1     bjh21 	if (!UNIQUE && SINGL_FLD && ftbl->flags & F)
     88  1.1     bjh21 		wts1 = (ftbl->flags & R) ? Rascii : ascii;
     89  1.1     bjh21 	if (!cfilebuf)
     90  1.1     bjh21 		cfilebuf = malloc(MAXLLEN + sizeof(TMFILE));
     91  1.1     bjh21 
     92  1.1     bjh21 	i = min(16, nfiles) * LALIGN(MAXLLEN+sizeof(TMFILE));
     93  1.1     bjh21 	if (!buffer || i > BUFSIZE) {
     94  1.1     bjh21 		buffer = buffer ? realloc(buffer, i) : malloc(i);
     95  1.1     bjh21 		if (!buffer)
     96  1.1     bjh21 			err(2, NULL);
     97  1.1     bjh21 		if (!SINGL_FLD)
     98  1.1     bjh21 			linebuf = malloc(MAXLLEN);
     99  1.1     bjh21 	}
    100  1.1     bjh21 
    101  1.1     bjh21 	if (binno >= 0)
    102  1.1     bjh21 		l_fstack = fstack + files.top;
    103  1.1     bjh21 	else
    104  1.1     bjh21 		l_fstack = fstack;
    105  1.1     bjh21 	while (nfiles) {
    106  1.1     bjh21 		put = putrec;
    107  1.1     bjh21 		for (j = 0; j < nfiles; j += 16) {
    108  1.1     bjh21 			if (nfiles <= 16) {
    109  1.3     bjh21 				tout = outfp;
    110  1.1     bjh21 				put = fput;
    111  1.1     bjh21 			}
    112  1.1     bjh21 			else
    113  1.1     bjh21 				tout = ftmp();
    114  1.1     bjh21 			last = min(16, nfiles - j);
    115  1.1     bjh21 			if (binno < 0) {
    116  1.1     bjh21 				for (i = 0; i < last; i++)
    117  1.3     bjh21 					if (!(l_fstack[i+MAXFCT-1-16].fp =
    118  1.1     bjh21 					    fopen(files.names[j + i], "r")))
    119  1.1     bjh21 						err(2, "%s", files.names[j+i]);
    120  1.1     bjh21 				merge(MAXFCT-1-16, last, get, tout, put, ftbl);
    121  1.1     bjh21 			}
    122  1.1     bjh21 			else {
    123  1.1     bjh21 				for (i = 0; i< last; i++)
    124  1.3     bjh21 					rewind(l_fstack[i+j].fp);
    125  1.1     bjh21 				merge(files.top+j, last, get, tout, put, ftbl);
    126  1.1     bjh21 			}
    127  1.3     bjh21 			if (nfiles > 16) l_fstack[j/16].fp = tout;
    128  1.1     bjh21 		}
    129  1.1     bjh21 		nfiles = (nfiles + 15) / 16;
    130  1.1     bjh21 		if (nfiles == 1)
    131  1.1     bjh21 			nfiles = 0;
    132  1.1     bjh21 		if (binno < 0) {
    133  1.1     bjh21 			binno = 0;
    134  1.1     bjh21 			get = geteasy;
    135  1.1     bjh21 			files.top = 0;
    136  1.1     bjh21 		}
    137  1.1     bjh21 	}
    138  1.1     bjh21 }
    139  1.1     bjh21 
    140  1.1     bjh21 void
    141  1.3     bjh21 merge(infl0, nfiles, get, outfp, put, ftbl)
    142  1.1     bjh21 	int infl0, nfiles;
    143  1.2     bjh21 	int (*get) __P((int, union f_handle, int, struct recheader *, u_char *,
    144  1.2     bjh21 	    struct field *));
    145  1.1     bjh21 	void (*put)(struct recheader *, FILE *);
    146  1.3     bjh21 	FILE *outfp;
    147  1.1     bjh21 	struct field *ftbl;
    148  1.1     bjh21 {
    149  1.1     bjh21 	int c, i, j;
    150  1.1     bjh21 	union f_handle dummy = {0};
    151  1.1     bjh21 	struct mfile *flist[16], *cfile;
    152  1.1     bjh21 	for (i = j = 0; i < nfiles; i++) {
    153  1.1     bjh21 		cfile = (MFILE *) (buffer +
    154  1.1     bjh21 		    i * LALIGN(MAXLLEN + sizeof(TMFILE)));
    155  1.1     bjh21 		cfile->flno = j + infl0;
    156  1.1     bjh21 		cfile->end = cfile->rec->data + MAXLLEN;
    157  1.1     bjh21 		for (c = 1; c == 1;) {
    158  1.1     bjh21 			if (EOF == (c = get(j+infl0, dummy, nfiles,
    159  1.1     bjh21 			   cfile->rec, cfile->end, ftbl))) {
    160  1.1     bjh21 				--i;
    161  1.1     bjh21 				--nfiles;
    162  1.1     bjh21 				break;
    163  1.1     bjh21 			}
    164  1.1     bjh21 			if (i)
    165  1.1     bjh21 				c = insert(flist, &cfile, i, !DELETE);
    166  1.1     bjh21 			else
    167  1.1     bjh21 				flist[0] = cfile;
    168  1.1     bjh21 		}
    169  1.1     bjh21 		j++;
    170  1.1     bjh21 	}
    171  1.1     bjh21 	cfile = cfilebuf;
    172  1.1     bjh21 	cfile->flno = flist[0]->flno;
    173  1.1     bjh21 	cfile->end = cfile->rec->data + MAXLLEN;
    174  1.1     bjh21 	while (nfiles) {
    175  1.1     bjh21 		for (c = 1; c == 1;) {
    176  1.1     bjh21 			if (EOF == (c = get(cfile->flno, dummy, nfiles,
    177  1.1     bjh21 			   cfile->rec, cfile->end, ftbl))) {
    178  1.3     bjh21 				put(flist[0]->rec, outfp);
    179  1.1     bjh21 				memmove(flist, flist + 1,
    180  1.1     bjh21 				    sizeof(MFILE *) * (--nfiles));
    181  1.1     bjh21 				cfile->flno = flist[0]->flno;
    182  1.1     bjh21 				break;
    183  1.1     bjh21 			}
    184  1.1     bjh21 			if (!(c = insert(flist, &cfile, nfiles, DELETE)))
    185  1.3     bjh21 				put(cfile->rec, outfp);
    186  1.1     bjh21 		}
    187  1.1     bjh21 	}
    188  1.1     bjh21 }
    189  1.1     bjh21 
    190  1.1     bjh21 /*
    191  1.1     bjh21  * if delete: inserts *rec in flist, deletes flist[0], and leaves it in *rec;
    192  1.1     bjh21  * otherwise just inserts *rec in flist.
    193  1.1     bjh21 */
    194  1.1     bjh21 static int
    195  1.1     bjh21 insert(flist, rec, ttop, delete)
    196  1.1     bjh21 	struct mfile **flist, **rec;
    197  1.1     bjh21 	int delete, ttop;			/* delete = 0 or 1 */
    198  1.1     bjh21 {
    199  1.4  jdolecek 	struct mfile *tmprec;
    200  1.4  jdolecek 	int top, mid, bot = 0, cmpv = 1;
    201  1.1     bjh21 	tmprec = *rec;
    202  1.1     bjh21 	top = ttop;
    203  1.1     bjh21 	for (mid = top/2; bot +1 != top; mid = (bot+top)/2) {
    204  1.1     bjh21 		cmpv = cmp(tmprec->rec, flist[mid]->rec);
    205  1.1     bjh21 		if (cmpv < 0)
    206  1.1     bjh21 			top = mid;
    207  1.1     bjh21 		else if (cmpv > 0)
    208  1.1     bjh21 			bot = mid;
    209  1.1     bjh21 		else {
    210  1.1     bjh21 			if (!UNIQUE)
    211  1.1     bjh21 				bot = mid - 1;
    212  1.1     bjh21 			break;
    213  1.1     bjh21 		}
    214  1.1     bjh21 	}
    215  1.1     bjh21 	if (delete) {
    216  1.1     bjh21 		if (UNIQUE) {
    217  1.1     bjh21 			if (!bot && cmpv)
    218  1.1     bjh21 				cmpv = cmp(tmprec->rec, flist[0]->rec);
    219  1.1     bjh21 			if (!cmpv)
    220  1.1     bjh21 				return(1);
    221  1.1     bjh21 		}
    222  1.1     bjh21 		tmprec = flist[0];
    223  1.1     bjh21 		if (bot)
    224  1.1     bjh21 			memmove(flist, flist+1, bot * sizeof(MFILE **));
    225  1.1     bjh21 		flist[bot] = *rec;
    226  1.1     bjh21 		*rec = tmprec;
    227  1.1     bjh21 		(*rec)->flno = (*flist)->flno;
    228  1.1     bjh21 		return (0);
    229  1.1     bjh21 	}
    230  1.1     bjh21 	else {
    231  1.1     bjh21 		if (!bot && !(UNIQUE && !cmpv)) {
    232  1.1     bjh21 			cmpv = cmp(tmprec->rec, flist[0]->rec);
    233  1.1     bjh21 			if (cmpv < 0)
    234  1.1     bjh21 				bot = -1;
    235  1.1     bjh21 		}
    236  1.1     bjh21 		if (UNIQUE && !cmpv)
    237  1.1     bjh21 			return (1);
    238  1.1     bjh21 		bot++;
    239  1.1     bjh21 		memmove(flist + bot+1, flist + bot,
    240  1.1     bjh21 		    (ttop - bot) * sizeof(MFILE **));
    241  1.1     bjh21 		flist[bot] = *rec;
    242  1.1     bjh21 		return (0);
    243  1.1     bjh21 	}
    244  1.1     bjh21 }
    245  1.1     bjh21 
    246  1.1     bjh21 /*
    247  1.1     bjh21  * check order on one file
    248  1.1     bjh21  */
    249  1.1     bjh21 void
    250  1.1     bjh21 order(infile, get, ftbl)
    251  1.1     bjh21 	union f_handle infile;
    252  1.2     bjh21 	int (*get) __P((int, union f_handle, int, struct recheader *, u_char *,
    253  1.2     bjh21 	    struct field *));
    254  1.1     bjh21 	struct field *ftbl;
    255  1.1     bjh21 {
    256  1.1     bjh21 	u_char *end;
    257  1.1     bjh21 	int c;
    258  1.1     bjh21 	struct recheader *crec, *prec, *trec;
    259  1.1     bjh21 
    260  1.1     bjh21 	if (!SINGL_FLD)
    261  1.1     bjh21 		linebuf = malloc(MAXLLEN);
    262  1.1     bjh21 	buffer = malloc(2 * (MAXLLEN + sizeof(TRECHEADER)));
    263  1.1     bjh21 	end = buffer + 2 * (MAXLLEN + sizeof(TRECHEADER));
    264  1.1     bjh21 	crec = (RECHEADER *) buffer;
    265  1.1     bjh21 	prec = (RECHEADER *) (buffer + MAXLLEN + sizeof(TRECHEADER));
    266  1.1     bjh21 	wts = ftbl->weights;
    267  1.1     bjh21 	if (SINGL_FLD && ftbl->flags & F)
    268  1.1     bjh21 		wts1 = ftbl->flags & R ? Rascii : ascii;
    269  1.1     bjh21 	else
    270  1.1     bjh21 		wts1 = 0;
    271  1.1     bjh21 	if (0 == get(-1, infile, 1, prec, end, ftbl))
    272  1.1     bjh21 	while (0 == get(-1, infile, 1, crec, end, ftbl)) {
    273  1.1     bjh21 		if (0 < (c = cmp(prec, crec))) {
    274  1.1     bjh21 			crec->data[crec->length-1] = 0;
    275  1.1     bjh21 			errx(1, "found disorder: %s", crec->data+crec->offset);
    276  1.1     bjh21 		}
    277  1.1     bjh21 		if (UNIQUE && !c) {
    278  1.1     bjh21 			crec->data[crec->length-1] = 0;
    279  1.1     bjh21 			errx(1, "found non-uniqueness: %s",
    280  1.1     bjh21 			    crec->data+crec->offset);
    281  1.1     bjh21 		}
    282  1.1     bjh21 		trec = prec;
    283  1.1     bjh21 		prec = crec;
    284  1.1     bjh21 		crec = trec;
    285  1.1     bjh21 	}
    286  1.1     bjh21 	exit(0);
    287  1.1     bjh21 }
    288  1.1     bjh21 
    289  1.1     bjh21 static int
    290  1.1     bjh21 cmp(rec1, rec2)
    291  1.1     bjh21 	struct recheader *rec1, *rec2;
    292  1.1     bjh21 {
    293  1.4  jdolecek 	int r;
    294  1.4  jdolecek 	u_char *pos1, *pos2, *end;
    295  1.4  jdolecek 	u_char *cwts;
    296  1.1     bjh21 	for (cwts = wts; cwts; cwts = (cwts == wts1 ? 0 : wts1)) {
    297  1.1     bjh21 		pos1 = rec1->data;
    298  1.1     bjh21 		pos2 = rec2->data;
    299  1.1     bjh21 		if (!SINGL_FLD && UNIQUE)
    300  1.1     bjh21 			end = pos1 + min(rec1->offset, rec2->offset);
    301  1.1     bjh21 		else
    302  1.1     bjh21 			end = pos1 + min(rec1->length, rec2->length);
    303  1.1     bjh21 		for (; pos1 < end; ) {
    304  1.2     bjh21 			if ((r = cwts[*pos1++] - cwts[*pos2++]))
    305  1.1     bjh21 				return (r);
    306  1.1     bjh21 		}
    307  1.1     bjh21 	}
    308  1.1     bjh21 	return (0);
    309  1.1     bjh21 }
    310