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