msort.c revision 1.7 1 1.7 jdolecek /* $NetBSD: msort.c,v 1.7 2001/01/11 14:05:24 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.7 jdolecek __RCSID("$NetBSD: msort.c,v 1.7 2001/01/11 14:05:24 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.7 jdolecek fmerge(binno, top, filelist, nfiles, get, outfp, fput, ftbl)
73 1.7 jdolecek int binno, top;
74 1.7 jdolecek struct filelist *filelist;
75 1.7 jdolecek int nfiles;
76 1.7 jdolecek get_func_t get;
77 1.3 bjh21 FILE *outfp;
78 1.7 jdolecek put_func_t fput;
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.7 jdolecek put_func_t put;
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.5 jdolecek cfilebuf = malloc(DEFLLEN + sizeof(TMFILE));
91 1.1 bjh21
92 1.5 jdolecek i = min(16, nfiles) * LALIGN(DEFLLEN+sizeof(TMFILE));
93 1.5 jdolecek 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.5 jdolecek if (!linebuf && !SINGL_FLD) {
98 1.5 jdolecek linebuf_size = DEFLLEN;
99 1.5 jdolecek linebuf = malloc(linebuf_size);
100 1.5 jdolecek }
101 1.1 bjh21 }
102 1.1 bjh21
103 1.1 bjh21 if (binno >= 0)
104 1.7 jdolecek l_fstack = fstack + top;
105 1.1 bjh21 else
106 1.1 bjh21 l_fstack = fstack;
107 1.1 bjh21 while (nfiles) {
108 1.1 bjh21 put = putrec;
109 1.1 bjh21 for (j = 0; j < nfiles; j += 16) {
110 1.1 bjh21 if (nfiles <= 16) {
111 1.3 bjh21 tout = outfp;
112 1.1 bjh21 put = fput;
113 1.1 bjh21 }
114 1.1 bjh21 else
115 1.1 bjh21 tout = ftmp();
116 1.1 bjh21 last = min(16, nfiles - j);
117 1.1 bjh21 if (binno < 0) {
118 1.7 jdolecek FILE *fp;
119 1.7 jdolecek for (i = 0; i < last; i++) {
120 1.7 jdolecek fp = fopen(filelist->names[j+i], "r");
121 1.7 jdolecek if (!fp) {
122 1.7 jdolecek err(2, "%s",
123 1.7 jdolecek filelist->names[j+i]);
124 1.7 jdolecek }
125 1.7 jdolecek l_fstack[i+MAXFCT-1-16].fp = fp;
126 1.7 jdolecek }
127 1.1 bjh21 merge(MAXFCT-1-16, last, get, tout, put, ftbl);
128 1.1 bjh21 }
129 1.1 bjh21 else {
130 1.1 bjh21 for (i = 0; i< last; i++)
131 1.3 bjh21 rewind(l_fstack[i+j].fp);
132 1.7 jdolecek merge(top+j, last, get, tout, put, ftbl);
133 1.1 bjh21 }
134 1.3 bjh21 if (nfiles > 16) l_fstack[j/16].fp = tout;
135 1.1 bjh21 }
136 1.1 bjh21 nfiles = (nfiles + 15) / 16;
137 1.1 bjh21 if (nfiles == 1)
138 1.1 bjh21 nfiles = 0;
139 1.1 bjh21 if (binno < 0) {
140 1.1 bjh21 binno = 0;
141 1.1 bjh21 get = geteasy;
142 1.7 jdolecek top = 0;
143 1.1 bjh21 }
144 1.1 bjh21 }
145 1.1 bjh21 }
146 1.1 bjh21
147 1.1 bjh21 void
148 1.3 bjh21 merge(infl0, nfiles, get, outfp, put, ftbl)
149 1.1 bjh21 int infl0, nfiles;
150 1.7 jdolecek get_func_t get;
151 1.7 jdolecek put_func_t put;
152 1.3 bjh21 FILE *outfp;
153 1.1 bjh21 struct field *ftbl;
154 1.1 bjh21 {
155 1.1 bjh21 int c, i, j;
156 1.1 bjh21 struct mfile *flist[16], *cfile;
157 1.1 bjh21 for (i = j = 0; i < nfiles; i++) {
158 1.1 bjh21 cfile = (MFILE *) (buffer +
159 1.5 jdolecek i * LALIGN(DEFLLEN + sizeof(TMFILE)));
160 1.1 bjh21 cfile->flno = j + infl0;
161 1.5 jdolecek cfile->end = cfile->rec->data + DEFLLEN;
162 1.1 bjh21 for (c = 1; c == 1;) {
163 1.7 jdolecek if (EOF == (c = get(j+infl0, 0, NULL, nfiles,
164 1.1 bjh21 cfile->rec, cfile->end, ftbl))) {
165 1.1 bjh21 --i;
166 1.1 bjh21 --nfiles;
167 1.1 bjh21 break;
168 1.1 bjh21 }
169 1.1 bjh21 if (i)
170 1.1 bjh21 c = insert(flist, &cfile, i, !DELETE);
171 1.1 bjh21 else
172 1.1 bjh21 flist[0] = cfile;
173 1.1 bjh21 }
174 1.1 bjh21 j++;
175 1.1 bjh21 }
176 1.1 bjh21 cfile = cfilebuf;
177 1.1 bjh21 cfile->flno = flist[0]->flno;
178 1.5 jdolecek cfile->end = cfile->rec->data + DEFLLEN;
179 1.1 bjh21 while (nfiles) {
180 1.1 bjh21 for (c = 1; c == 1;) {
181 1.7 jdolecek if (EOF == (c = get(cfile->flno, 0, NULL, nfiles,
182 1.1 bjh21 cfile->rec, cfile->end, ftbl))) {
183 1.3 bjh21 put(flist[0]->rec, outfp);
184 1.1 bjh21 memmove(flist, flist + 1,
185 1.1 bjh21 sizeof(MFILE *) * (--nfiles));
186 1.1 bjh21 cfile->flno = flist[0]->flno;
187 1.1 bjh21 break;
188 1.1 bjh21 }
189 1.1 bjh21 if (!(c = insert(flist, &cfile, nfiles, DELETE)))
190 1.3 bjh21 put(cfile->rec, outfp);
191 1.1 bjh21 }
192 1.1 bjh21 }
193 1.1 bjh21 }
194 1.1 bjh21
195 1.1 bjh21 /*
196 1.1 bjh21 * if delete: inserts *rec in flist, deletes flist[0], and leaves it in *rec;
197 1.1 bjh21 * otherwise just inserts *rec in flist.
198 1.1 bjh21 */
199 1.1 bjh21 static int
200 1.1 bjh21 insert(flist, rec, ttop, delete)
201 1.1 bjh21 struct mfile **flist, **rec;
202 1.1 bjh21 int delete, ttop; /* delete = 0 or 1 */
203 1.1 bjh21 {
204 1.4 jdolecek struct mfile *tmprec;
205 1.4 jdolecek int top, mid, bot = 0, cmpv = 1;
206 1.1 bjh21 tmprec = *rec;
207 1.1 bjh21 top = ttop;
208 1.1 bjh21 for (mid = top/2; bot +1 != top; mid = (bot+top)/2) {
209 1.1 bjh21 cmpv = cmp(tmprec->rec, flist[mid]->rec);
210 1.1 bjh21 if (cmpv < 0)
211 1.1 bjh21 top = mid;
212 1.1 bjh21 else if (cmpv > 0)
213 1.1 bjh21 bot = mid;
214 1.1 bjh21 else {
215 1.1 bjh21 if (!UNIQUE)
216 1.1 bjh21 bot = mid - 1;
217 1.1 bjh21 break;
218 1.1 bjh21 }
219 1.1 bjh21 }
220 1.1 bjh21 if (delete) {
221 1.1 bjh21 if (UNIQUE) {
222 1.1 bjh21 if (!bot && cmpv)
223 1.1 bjh21 cmpv = cmp(tmprec->rec, flist[0]->rec);
224 1.1 bjh21 if (!cmpv)
225 1.1 bjh21 return(1);
226 1.1 bjh21 }
227 1.1 bjh21 tmprec = flist[0];
228 1.1 bjh21 if (bot)
229 1.1 bjh21 memmove(flist, flist+1, bot * sizeof(MFILE **));
230 1.1 bjh21 flist[bot] = *rec;
231 1.1 bjh21 *rec = tmprec;
232 1.1 bjh21 (*rec)->flno = (*flist)->flno;
233 1.1 bjh21 return (0);
234 1.1 bjh21 }
235 1.1 bjh21 else {
236 1.1 bjh21 if (!bot && !(UNIQUE && !cmpv)) {
237 1.1 bjh21 cmpv = cmp(tmprec->rec, flist[0]->rec);
238 1.1 bjh21 if (cmpv < 0)
239 1.1 bjh21 bot = -1;
240 1.1 bjh21 }
241 1.1 bjh21 if (UNIQUE && !cmpv)
242 1.1 bjh21 return (1);
243 1.1 bjh21 bot++;
244 1.1 bjh21 memmove(flist + bot+1, flist + bot,
245 1.1 bjh21 (ttop - bot) * sizeof(MFILE **));
246 1.1 bjh21 flist[bot] = *rec;
247 1.1 bjh21 return (0);
248 1.1 bjh21 }
249 1.1 bjh21 }
250 1.1 bjh21
251 1.1 bjh21 /*
252 1.1 bjh21 * check order on one file
253 1.1 bjh21 */
254 1.1 bjh21 void
255 1.7 jdolecek order(filelist, get, ftbl)
256 1.7 jdolecek struct filelist *filelist;
257 1.7 jdolecek get_func_t get;
258 1.1 bjh21 struct field *ftbl;
259 1.1 bjh21 {
260 1.6 jdolecek u_char *crec_end, *prec_end, *trec_end;
261 1.1 bjh21 int c;
262 1.1 bjh21 struct recheader *crec, *prec, *trec;
263 1.1 bjh21
264 1.1 bjh21 if (!SINGL_FLD)
265 1.5 jdolecek linebuf = malloc(DEFLLEN);
266 1.5 jdolecek buffer = malloc(2 * (DEFLLEN + sizeof(TRECHEADER)));
267 1.1 bjh21 crec = (RECHEADER *) buffer;
268 1.6 jdolecek crec_end = buffer + DEFLLEN + sizeof(TRECHEADER);
269 1.5 jdolecek prec = (RECHEADER *) (buffer + DEFLLEN + sizeof(TRECHEADER));
270 1.6 jdolecek prec_end = buffer + 2*(DEFLLEN + sizeof(TRECHEADER));
271 1.1 bjh21 wts = ftbl->weights;
272 1.6 jdolecek if (SINGL_FLD && (ftbl->flags & F))
273 1.1 bjh21 wts1 = ftbl->flags & R ? Rascii : ascii;
274 1.1 bjh21 else
275 1.1 bjh21 wts1 = 0;
276 1.7 jdolecek if (0 == get(-1, 0, filelist, 1, prec, prec_end, ftbl))
277 1.7 jdolecek while (0 == get(-1, 0, filelist, 1, crec, crec_end, ftbl)) {
278 1.1 bjh21 if (0 < (c = cmp(prec, crec))) {
279 1.1 bjh21 crec->data[crec->length-1] = 0;
280 1.1 bjh21 errx(1, "found disorder: %s", crec->data+crec->offset);
281 1.1 bjh21 }
282 1.1 bjh21 if (UNIQUE && !c) {
283 1.1 bjh21 crec->data[crec->length-1] = 0;
284 1.1 bjh21 errx(1, "found non-uniqueness: %s",
285 1.1 bjh21 crec->data+crec->offset);
286 1.1 bjh21 }
287 1.6 jdolecek /*
288 1.6 jdolecek * Swap pointers so that this record is on place pointed
289 1.6 jdolecek * to by prec and new record is read to place pointed to by
290 1.6 jdolecek * crec.
291 1.6 jdolecek */
292 1.1 bjh21 trec = prec;
293 1.1 bjh21 prec = crec;
294 1.1 bjh21 crec = trec;
295 1.6 jdolecek trec_end = prec_end;
296 1.6 jdolecek prec_end = crec_end;
297 1.6 jdolecek crec_end = trec_end;
298 1.1 bjh21 }
299 1.1 bjh21 exit(0);
300 1.1 bjh21 }
301 1.1 bjh21
302 1.1 bjh21 static int
303 1.1 bjh21 cmp(rec1, rec2)
304 1.1 bjh21 struct recheader *rec1, *rec2;
305 1.1 bjh21 {
306 1.4 jdolecek int r;
307 1.4 jdolecek u_char *pos1, *pos2, *end;
308 1.4 jdolecek u_char *cwts;
309 1.1 bjh21 for (cwts = wts; cwts; cwts = (cwts == wts1 ? 0 : wts1)) {
310 1.1 bjh21 pos1 = rec1->data;
311 1.1 bjh21 pos2 = rec2->data;
312 1.1 bjh21 if (!SINGL_FLD && UNIQUE)
313 1.1 bjh21 end = pos1 + min(rec1->offset, rec2->offset);
314 1.1 bjh21 else
315 1.1 bjh21 end = pos1 + min(rec1->length, rec2->length);
316 1.1 bjh21 for (; pos1 < end; ) {
317 1.2 bjh21 if ((r = cwts[*pos1++] - cwts[*pos2++]))
318 1.1 bjh21 return (r);
319 1.1 bjh21 }
320 1.1 bjh21 }
321 1.1 bjh21 return (0);
322 1.1 bjh21 }
323