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