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