5 |
|
* Sorting routines for meta-files |
6 |
|
*/ |
7 |
|
|
8 |
+ |
#include "paths.h" |
9 |
+ |
#include "meta.h" |
10 |
|
|
11 |
+ |
#ifdef _WIN32 |
12 |
+ |
#include <process.h> /* getpid() */ |
13 |
+ |
#endif |
14 |
+ |
|
15 |
+ |
|
16 |
|
#define PBSIZE 1000 /* max size of sort block */ |
17 |
|
/* maxalloc must be >= this */ |
18 |
|
|
12 |
– |
|
19 |
|
#define NFILES 4 /* number of sort files */ |
20 |
|
|
21 |
|
|
22 |
+ |
static void treemerge(int height, int nt, int nf, PLIST *pl, |
23 |
+ |
int (*pcmp)(), FILE *ofp); |
24 |
+ |
static void sendsort( PLIST *pl, int (*pcmp)()); |
25 |
+ |
static void order(PRIMITIVE *pa[], int n, PLIST *pl); |
26 |
+ |
static char * tfname( int lvl, int num); |
27 |
|
|
17 |
– |
#include "meta.h" |
18 |
– |
|
19 |
– |
|
20 |
– |
|
21 |
– |
|
22 |
– |
|
23 |
– |
|
24 |
– |
|
28 |
|
/* |
29 |
|
* This sort routine does a combination quicksort and |
30 |
|
* n-ary mergesort |
31 |
|
*/ |
32 |
|
|
33 |
|
|
34 |
< |
sort(infp, pcmp) /* sort primitives according to pcmp */ |
35 |
< |
|
36 |
< |
FILE *infp; |
37 |
< |
int (*pcmp)(); /* compares pointers to pointers to primitives! */ |
38 |
< |
|
34 |
> |
void |
35 |
> |
sort( /* sort primitives according to pcmp */ |
36 |
> |
FILE *infp, |
37 |
> |
int (*pcmp)() /* compares pointers to pointers to primitives! */ |
38 |
> |
) |
39 |
|
{ |
40 |
|
PRIMITIVE *prims[PBSIZE]; /* pointers to primitives */ |
41 |
|
PLIST primlist; /* our primitives list */ |
82 |
|
* 0 or 1 global commands. |
83 |
|
*/ |
84 |
|
|
85 |
< |
mergesort(fi, nf, pl, pcmp, ofp) /* merge sorted files with list */ |
85 |
> |
void |
86 |
> |
pmergesort( /* merge sorted files with list */ |
87 |
|
|
88 |
< |
FILE *fi[]; /* array of input files */ |
89 |
< |
int nf; /* number of input files */ |
90 |
< |
PLIST *pl; /* sorted list */ |
91 |
< |
int (*pcmp)(); /* comparison function, takes primitive handles */ |
92 |
< |
FILE *ofp; /* output file */ |
93 |
< |
|
88 |
> |
FILE *fi[], /* array of input files */ |
89 |
> |
int nf, /* number of input files */ |
90 |
> |
PLIST *pl, /* sorted list */ |
91 |
> |
int (*pcmp)(), /* comparison function, takes primitive handles */ |
92 |
> |
FILE *ofp /* output file */ |
93 |
> |
) |
94 |
|
{ |
95 |
|
PRIMITIVE *plp; /* position in list */ |
96 |
|
PRIMITIVE *pp[NFILES]; /* input primitives */ |
105 |
|
|
106 |
|
for (i = 0; i < nf; i++) { /* initialize input files */ |
107 |
|
if ((pp[i] = palloc()) == NULL) |
108 |
< |
error(SYSTEM, "memory exhausted in mergesort"); |
108 |
> |
error(SYSTEM, "memory exhausted in pmergesort"); |
109 |
|
readp(pp[i], fi[i]); |
110 |
|
} |
111 |
|
|
147 |
|
|
148 |
|
|
149 |
|
|
150 |
< |
static |
151 |
< |
treemerge(height, nt, nf, pl, pcmp, ofp) /* merge into one file */ |
150 |
> |
static void |
151 |
> |
treemerge( /* merge into one file */ |
152 |
|
|
153 |
< |
int height, nt, nf; |
154 |
< |
PLIST *pl; |
155 |
< |
int (*pcmp)(); |
156 |
< |
FILE *ofp; |
157 |
< |
|
153 |
> |
int height, int nt, int nf, |
154 |
> |
PLIST *pl, |
155 |
> |
int (*pcmp)(), |
156 |
> |
FILE *ofp |
157 |
> |
) |
158 |
|
{ |
155 |
– |
char *tfname(); |
159 |
|
FILE *fi[NFILES], *fp; |
160 |
|
int i; |
161 |
|
|
167 |
|
if ((fp = ofp) == NULL) |
168 |
|
fp = efopen(tfname(height + 1, nt), "w"); |
169 |
|
|
170 |
< |
mergesort(fi, nf, pl, pcmp, fp); |
170 |
> |
pmergesort(fi, nf, pl, pcmp, fp); |
171 |
|
|
172 |
|
for (i = 0; i < nf; i++) { |
173 |
|
fclose(fi[i]); |
194 |
|
|
195 |
|
|
196 |
|
|
197 |
< |
static |
198 |
< |
sendsort(pl, pcmp) /* send a sorted list */ |
197 |
> |
static void |
198 |
> |
sendsort( /* send a sorted list */ |
199 |
|
|
200 |
< |
PLIST *pl; |
201 |
< |
int (*pcmp)(); |
202 |
< |
|
200 |
> |
PLIST *pl, |
201 |
> |
int (*pcmp)() |
202 |
> |
) |
203 |
|
{ |
204 |
|
static int nf = 0, |
205 |
|
intree = FALSE; |
250 |
|
|
251 |
|
|
252 |
|
|
253 |
+ |
static void |
254 |
+ |
order( /* order the first n array primitives into list */ |
255 |
|
|
256 |
< |
static |
257 |
< |
order(pa, n, pl) /* order the first n array primitives into list */ |
258 |
< |
|
259 |
< |
PRIMITIVE *pa[]; |
255 |
< |
int n; |
256 |
< |
PLIST *pl; |
257 |
< |
|
256 |
> |
PRIMITIVE *pa[], |
257 |
> |
int n, |
258 |
> |
PLIST *pl |
259 |
> |
) |
260 |
|
{ |
261 |
|
register int i; |
262 |
|
|
273 |
|
|
274 |
|
|
275 |
|
static char * |
276 |
< |
tfname(lvl, num) /* create temporary file name */ |
276 |
> |
tfname( /* create temporary file name */ |
277 |
|
|
278 |
< |
int lvl, num; |
279 |
< |
|
278 |
> |
int lvl, int num |
279 |
> |
) |
280 |
|
{ |
281 |
< |
static char fnbuf[32]; |
281 |
> |
static char pathbuf[PATH_MAX]; |
282 |
> |
static char fnbuf[PATH_MAX]; |
283 |
> |
static size_t psiz; |
284 |
|
|
285 |
< |
sprintf(fnbuf, "%sS%d%c%d", TDIR, getpid(), lvl+'A', num); |
285 |
> |
if (pathbuf[0] == '\0') { /* first time */ |
286 |
> |
temp_directory(pathbuf, sizeof(pathbuf)); |
287 |
> |
psiz = strlen(pathbuf); |
288 |
> |
} |
289 |
> |
snprintf(fnbuf, sizeof(pathbuf)-psiz, |
290 |
> |
"%s/S%d%c%d", pathbuf, getpid(), lvl+'A', num); |
291 |
> |
/*sprintf(fnbuf, "%sS%d%c%d", TDIR, getpid(), lvl+'A', num);*/ |
292 |
|
|
293 |
|
return(fnbuf); |
294 |
|
} |