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