version 1.7, 1999/05/24 17:57:18 |
version 1.8, 2001/02/04 21:27:00 |
|
|
#include <string.h> |
#include <string.h> |
|
|
u_char **keylist = 0, *buffer = 0, *linebuf = 0; |
u_char **keylist = 0, *buffer = 0, *linebuf = 0; |
|
size_t bufsize, linebuf_size; |
struct tempfile fstack[MAXFCT]; |
struct tempfile fstack[MAXFCT]; |
extern char *toutpath; |
extern char *toutpath; |
#define FSORTMAX 4 |
#define FSORTMAX 4 |
|
|
|
|
void |
void |
fsort(binno, depth, infiles, nfiles, outfp, ftbl) |
fsort(binno, depth, infiles, nfiles, outfp, ftbl) |
register int binno, depth; |
int binno, depth; |
register union f_handle infiles; |
union f_handle infiles; |
register int nfiles; |
int nfiles; |
FILE *outfp; |
FILE *outfp; |
register struct field *ftbl; |
struct field *ftbl; |
{ |
{ |
register u_char *bufend, **keypos, *tmpbuf; |
u_char *bufend, **keypos, *tmpbuf; |
u_char *weights; |
u_char *weights; |
int ntfiles, mfct = 0, total, i, maxb, lastb, panic = 0; |
int ntfiles, mfct = 0, total, i, maxb, lastb, panic = 0; |
int c, nelem; |
int c, nelem; |
int sizes [NBINS+1]; |
long sizes[NBINS+1]; |
union f_handle tfiles, mstart = {MAXFCT-16}; |
union f_handle tfiles, mstart = {MAXFCT-16}; |
register int (*get)(int, union f_handle, int, RECHEADER *, |
int (*get)(int, union f_handle, int, RECHEADER *, |
u_char *, struct field *); |
u_char *, struct field *); |
register RECHEADER *crec; |
RECHEADER *crec; |
struct field tfield[2]; |
struct field tfield[2]; |
FILE *prevfp, *tailfp[FSORTMAX+1]; |
FILE *prevfp, *tailfp[FSORTMAX+1]; |
|
|
|
|
tfield[0].icol.num = 1; |
tfield[0].icol.num = 1; |
weights = ftbl[0].weights; |
weights = ftbl[0].weights; |
if (!buffer) { |
if (!buffer) { |
if ((buffer = malloc(BUFSIZE + 1)) == NULL || |
bufsize = BUFSIZE; |
|
if ((buffer = malloc(bufsize + 1)) == NULL || |
(keylist = calloc(MAXNUM, sizeof(u_char *))) == NULL) |
(keylist = calloc(MAXNUM, sizeof(u_char *))) == NULL) |
errx(2, "cannot allocate memory"); |
errx(2, "cannot allocate memory"); |
if (!SINGL_FLD) { |
if (!SINGL_FLD) { |
if ((linebuf = malloc(MAXLLEN)) == NULL) |
linebuf_size = MAXLLEN; |
|
if ((linebuf = malloc(linebuf_size)) == NULL) |
errx(2, "cannot allocate memory"); |
errx(2, "cannot allocate memory"); |
} |
} |
} |
} |
bufend = buffer + BUFSIZE; |
bufend = buffer + bufsize; |
if (binno >= 0) { |
if (binno >= 0) { |
tfiles.top = infiles.top + nfiles; |
tfiles.top = infiles.top + nfiles; |
get = getnext; |
get = getnext; |
|
|
crec =(RECHEADER *) ((char *) crec + |
crec =(RECHEADER *) ((char *) crec + |
SALIGN(crec->length) + sizeof(TRECHEADER)); |
SALIGN(crec->length) + sizeof(TRECHEADER)); |
} |
} |
|
/* |
|
* buffer was too small for data, allocate |
|
* a bigger buffer. |
|
*/ |
|
if (c == BUFFEND && nelem == 0) { |
|
bufsize *= 2; |
|
buffer = realloc(buffer, bufsize); |
|
if (!buffer) |
|
err(2, "failed to realloc buffer"); |
|
bufend = buffer + bufsize; |
|
continue; |
|
} |
if (c == BUFFEND || ntfiles || mfct) { /* push */ |
if (c == BUFFEND || ntfiles || mfct) { /* push */ |
if (panic >= PANIC) { |
if (panic >= PANIC) { |
fstack[MAXFCT-16+mfct].fp = ftmp(); |
fstack[MAXFCT-16+mfct].fp = ftmp(); |
|
|
onepass(a, depth, n, sizes, tr, fp) |
onepass(a, depth, n, sizes, tr, fp) |
u_char **a; |
u_char **a; |
int depth; |
int depth; |
int n; |
long n; |
int sizes[]; |
long sizes[]; |
u_char *tr; |
u_char *tr; |
FILE *fp; |
FILE *fp; |
{ |
{ |
int tsizes[NBINS+1]; |
size_t tsizes[NBINS+1]; |
u_char **bin[257], **top[256], ***bp, ***bpmax, ***tp; |
u_char **bin[257], **top[256], ***bp, ***bpmax, ***tp; |
static int histo[256]; |
static int histo[256]; |
int *hp; |
int *hp; |
register int c; |
int c; |
u_char **an, *t, **aj; |
u_char **an, *t, **aj; |
register u_char **ak, *r; |
u_char **ak, *r; |
|
|
memset(tsizes, 0, sizeof(tsizes)); |
memset(tsizes, 0, sizeof(tsizes)); |
depth += sizeof(TRECHEADER); |
depth += sizeof(TRECHEADER); |
an = a + n; |
an = &a[n]; |
for (ak = a; ak < an; ak++) { |
for (ak = a; ak < an; ak++) { |
histo[c = tr[**ak]]++; |
histo[c = tr[**ak]]++; |
tsizes[c] += ((RECHEADER *) (*ak -= depth))->length; |
tsizes[c] += ((RECHEADER *) (*ak -= depth))->length; |
|
|
n = an - ak; |
n = an - ak; |
tsizes[c] += n * sizeof(TRECHEADER); |
tsizes[c] += n * sizeof(TRECHEADER); |
/* tell getnext how many elements in this bin, this segment. */ |
/* tell getnext how many elements in this bin, this segment. */ |
EWRITE(tsizes+c, sizeof(int), 1, fp); |
EWRITE(&tsizes[c], sizeof(size_t), 1, fp); |
sizes[c] += tsizes[c]; |
sizes[c] += tsizes[c]; |
for (; ak < an; ++ak) |
for (; ak < an; ++ak) |
putrec((RECHEADER *) *ak, fp); |
putrec((RECHEADER *) *ak, fp); |