2003-08-07 15:32:34 +04:00
|
|
|
/* $NetBSD: fsort.c,v 1.26 2003/08/07 11:32:34 jdolecek Exp $ */
|
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2000-2003 The NetBSD Foundation, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to The NetBSD Foundation
|
|
|
|
* by Ben Harris and Jaromir Dolecek.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by the NetBSD
|
|
|
|
* Foundation, Inc. and its contributors.
|
|
|
|
* 4. Neither the name of The NetBSD Foundation nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
|
|
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
2000-10-07 22:37:09 +04:00
|
|
|
|
2000-10-07 20:39:34 +04:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Peter McIlroy.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 15:13:06 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
2000-10-07 20:39:34 +04:00
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read in the next bin. If it fits in one segment sort it;
|
|
|
|
* otherwise refine it by segment deeper by one character,
|
|
|
|
* and try again on smaller bins. Sort the final bin at this level
|
|
|
|
* of recursion to keep the head of fstack at 0.
|
|
|
|
* After PANIC passes, abort to merge sort.
|
2001-02-19 23:50:17 +03:00
|
|
|
*/
|
2000-10-07 20:39:34 +04:00
|
|
|
#include "sort.h"
|
|
|
|
#include "fsort.h"
|
|
|
|
|
2000-10-07 22:37:09 +04:00
|
|
|
#ifndef lint
|
2003-08-07 15:32:34 +04:00
|
|
|
__RCSID("$NetBSD: fsort.c,v 1.26 2003/08/07 11:32:34 jdolecek Exp $");
|
2000-10-07 22:37:09 +04:00
|
|
|
__SCCSID("@(#)fsort.c 8.1 (Berkeley) 6/6/93");
|
|
|
|
#endif /* not lint */
|
|
|
|
|
2000-10-07 20:39:34 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2001-02-19 22:31:29 +03:00
|
|
|
static const u_char **keylist = 0;
|
2000-10-07 22:37:09 +04:00
|
|
|
u_char *buffer = 0, *linebuf = 0;
|
2001-05-15 01:45:19 +04:00
|
|
|
size_t bufsize = DEFBUFSIZE;
|
2001-01-19 00:40:15 +03:00
|
|
|
size_t linebuf_size;
|
2000-10-07 20:39:34 +04:00
|
|
|
#define FSORTMAX 4
|
|
|
|
int PANIC = FSORTMAX;
|
|
|
|
|
2002-12-24 17:43:37 +03:00
|
|
|
struct tempfile fstack[MAXFCT];
|
2001-01-19 13:13:21 +03:00
|
|
|
#define MSTART (MAXFCT - MERGE_FNUM)
|
2002-12-24 17:43:37 +03:00
|
|
|
#define CHECKFSTACK(n) \
|
2002-12-24 18:15:01 +03:00
|
|
|
if (n >= MAXFCT) \
|
2002-12-24 17:43:37 +03:00
|
|
|
errx(2, "fstack: too many temporary files; use -H or sort in pieces")
|
|
|
|
|
2001-02-19 22:31:29 +03:00
|
|
|
#define SALIGN(n) ((n+sizeof(length_t)-1) & ~(sizeof(length_t)-1))
|
2001-01-11 17:05:24 +03:00
|
|
|
|
2000-10-07 20:39:34 +04:00
|
|
|
void
|
2001-01-11 17:05:24 +03:00
|
|
|
fsort(binno, depth, top, filelist, nfiles, outfp, ftbl)
|
|
|
|
int binno, depth, top;
|
|
|
|
struct filelist *filelist;
|
|
|
|
int nfiles;
|
2000-10-08 00:37:06 +04:00
|
|
|
FILE *outfp;
|
2000-10-16 00:46:33 +04:00
|
|
|
struct field *ftbl;
|
2000-10-07 20:39:34 +04:00
|
|
|
{
|
2000-10-16 00:46:33 +04:00
|
|
|
const u_char **keypos;
|
|
|
|
u_char *bufend, *tmpbuf;
|
2000-10-07 20:39:34 +04:00
|
|
|
u_char *weights;
|
|
|
|
int ntfiles, mfct = 0, total, i, maxb, lastb, panic = 0;
|
2001-01-11 17:05:24 +03:00
|
|
|
int c, nelem, base;
|
2000-10-07 20:39:34 +04:00
|
|
|
long sizes [NBINS+1];
|
2001-01-11 17:05:24 +03:00
|
|
|
get_func_t get;
|
2000-10-16 00:46:33 +04:00
|
|
|
struct recheader *crec;
|
2000-10-07 20:39:34 +04:00
|
|
|
struct field tfield[2];
|
2000-10-08 00:37:06 +04:00
|
|
|
FILE *prevfp, *tailfp[FSORTMAX+1];
|
2000-10-07 20:39:34 +04:00
|
|
|
|
2000-10-08 00:37:06 +04:00
|
|
|
memset(tailfp, 0, sizeof(tailfp));
|
|
|
|
prevfp = outfp;
|
2000-10-07 20:39:34 +04:00
|
|
|
memset(tfield, 0, sizeof(tfield));
|
|
|
|
if (ftbl[0].flags & R)
|
|
|
|
tfield[0].weights = Rascii;
|
|
|
|
else
|
|
|
|
tfield[0].weights = ascii;
|
|
|
|
tfield[0].icol.num = 1;
|
|
|
|
weights = ftbl[0].weights;
|
|
|
|
if (!buffer) {
|
2000-10-17 01:53:19 +04:00
|
|
|
buffer = malloc(bufsize);
|
2000-10-07 20:39:34 +04:00
|
|
|
keylist = malloc(MAXNUM * sizeof(u_char *));
|
2001-02-05 17:25:34 +03:00
|
|
|
memset(keylist, 0, MAXNUM * sizeof(u_char *));
|
2000-10-17 01:53:19 +04:00
|
|
|
if (!SINGL_FLD) {
|
|
|
|
linebuf_size = DEFLLEN;
|
2001-02-19 23:50:17 +03:00
|
|
|
if ((linebuf = malloc(linebuf_size)) == NULL)
|
|
|
|
errx(2, "cannot allocate memory");
|
2000-10-17 01:53:19 +04:00
|
|
|
}
|
2000-10-07 20:39:34 +04:00
|
|
|
}
|
2000-10-17 01:53:19 +04:00
|
|
|
bufend = buffer + bufsize;
|
2000-10-07 20:39:34 +04:00
|
|
|
if (binno >= 0) {
|
2001-01-11 17:05:24 +03:00
|
|
|
base = top + nfiles;
|
2000-10-07 20:39:34 +04:00
|
|
|
get = getnext;
|
|
|
|
} else {
|
2001-01-11 17:05:24 +03:00
|
|
|
base = 0;
|
2000-10-07 20:39:34 +04:00
|
|
|
if (SINGL_FLD)
|
|
|
|
get = makeline;
|
|
|
|
else
|
|
|
|
get = makekey;
|
|
|
|
}
|
|
|
|
for (;;) {
|
|
|
|
memset(sizes, 0, sizeof(sizes));
|
|
|
|
c = ntfiles = 0;
|
|
|
|
if (binno == weights[REC_D] &&
|
|
|
|
!(SINGL_FLD && ftbl[0].flags & F)) { /* pop */
|
2001-01-11 17:05:24 +03:00
|
|
|
rd_append(weights[REC_D], top,
|
|
|
|
nfiles, prevfp, buffer, bufend);
|
2000-10-07 20:39:34 +04:00
|
|
|
break;
|
|
|
|
} else if (binno == weights[REC_D]) {
|
|
|
|
depth = 0; /* start over on flat weights */
|
|
|
|
ftbl = tfield;
|
|
|
|
weights = ftbl[0].weights;
|
|
|
|
}
|
|
|
|
while (c != EOF) {
|
|
|
|
keypos = keylist;
|
|
|
|
nelem = 0;
|
|
|
|
crec = (RECHEADER *) buffer;
|
2001-01-19 00:40:15 +03:00
|
|
|
|
|
|
|
do_read:
|
2001-01-11 17:05:24 +03:00
|
|
|
while((c = get(binno, top, filelist, nfiles, crec,
|
|
|
|
bufend, ftbl)) == 0) {
|
2000-10-07 20:39:34 +04:00
|
|
|
*keypos++ = crec->data + depth;
|
|
|
|
if (++nelem == MAXNUM) {
|
|
|
|
c = BUFFEND;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
crec =(RECHEADER *) ((char *) crec +
|
|
|
|
SALIGN(crec->length) + sizeof(TRECHEADER));
|
|
|
|
}
|
2001-02-19 22:31:29 +03:00
|
|
|
|
2001-05-15 01:45:19 +04:00
|
|
|
if (c == BUFFEND && nelem < MAXNUM
|
|
|
|
&& bufsize < MAXBUFSIZE) {
|
2001-01-19 00:40:15 +03:00
|
|
|
const u_char **keyp;
|
|
|
|
u_char *oldb = buffer;
|
|
|
|
|
2000-10-17 01:53:19 +04:00
|
|
|
/* buffer was too small for data, allocate
|
|
|
|
* bigger buffer */
|
|
|
|
bufsize *= 2;
|
|
|
|
buffer = realloc(buffer, bufsize);
|
2000-10-17 19:22:57 +04:00
|
|
|
if (!buffer) {
|
2000-10-17 01:53:19 +04:00
|
|
|
err(2, "failed to realloc buffer to %ld bytes",
|
|
|
|
(unsigned long) bufsize);
|
2000-10-17 19:22:57 +04:00
|
|
|
}
|
|
|
|
bufend = buffer + bufsize;
|
2001-01-19 00:40:15 +03:00
|
|
|
|
|
|
|
/* patch up keylist[] */
|
|
|
|
for(keyp = &keypos[-1]; keyp >= keylist; keyp--)
|
|
|
|
*keyp = buffer + (*keyp - oldb);
|
|
|
|
|
|
|
|
crec = (RECHEADER *) (buffer + ((u_char *)crec - oldb));
|
|
|
|
goto do_read;
|
2000-10-17 01:53:19 +04:00
|
|
|
}
|
2001-05-15 15:19:45 +04:00
|
|
|
|
|
|
|
if (c != BUFFEND && !ntfiles && !mfct) {
|
|
|
|
/* do not push */
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* push */
|
|
|
|
if (panic >= PANIC) {
|
|
|
|
fstack[MSTART + mfct].fp = ftmp();
|
|
|
|
if ((stable_sort)
|
|
|
|
? sradixsort(keylist, nelem,
|
|
|
|
weights, REC_D)
|
|
|
|
: radixsort(keylist, nelem,
|
|
|
|
weights, REC_D) )
|
|
|
|
err(2, NULL);
|
|
|
|
append(keylist, nelem, depth,
|
|
|
|
fstack[MSTART + mfct].fp, putrec,
|
|
|
|
ftbl);
|
|
|
|
mfct++;
|
|
|
|
/* reduce number of open files */
|
|
|
|
if (mfct == MERGE_FNUM ||(c == EOF && ntfiles)) {
|
2001-05-15 15:49:25 +04:00
|
|
|
/*
|
|
|
|
* Only copy extra incomplete crec
|
|
|
|
* data if there are any.
|
|
|
|
*/
|
|
|
|
int nodata = (bufend >= (u_char *)crec
|
|
|
|
&& bufend <= crec->data);
|
|
|
|
|
|
|
|
if (!nodata) {
|
|
|
|
tmpbuf = malloc(bufend -
|
|
|
|
crec->data);
|
|
|
|
memmove(tmpbuf, crec->data,
|
|
|
|
bufend - crec->data);
|
|
|
|
}
|
|
|
|
|
2002-12-24 17:43:37 +03:00
|
|
|
CHECKFSTACK(base + ntfiles);
|
2001-05-15 15:49:25 +04:00
|
|
|
fstack[base + ntfiles].fp = ftmp();
|
2001-05-15 15:19:45 +04:00
|
|
|
fmerge(0, MSTART, filelist,
|
2002-09-30 09:09:25 +04:00
|
|
|
mfct, geteasy,
|
|
|
|
fstack[base + ntfiles].fp,
|
2001-05-15 15:19:45 +04:00
|
|
|
putrec, ftbl);
|
2001-02-19 23:50:17 +03:00
|
|
|
ntfiles++;
|
2001-05-15 15:19:45 +04:00
|
|
|
mfct = 0;
|
2001-05-15 15:49:25 +04:00
|
|
|
|
|
|
|
if (!nodata) {
|
|
|
|
memmove(crec->data, tmpbuf,
|
|
|
|
bufend - crec->data);
|
|
|
|
free(tmpbuf);
|
|
|
|
}
|
2000-10-07 20:39:34 +04:00
|
|
|
}
|
2001-05-15 15:19:45 +04:00
|
|
|
} else {
|
2002-12-24 17:43:37 +03:00
|
|
|
CHECKFSTACK(base + ntfiles);
|
2002-12-24 18:15:01 +03:00
|
|
|
fstack[base + ntfiles].fp = ftmp();
|
2001-05-15 15:19:45 +04:00
|
|
|
onepass(keylist, depth, nelem, sizes,
|
|
|
|
weights, fstack[base + ntfiles].fp);
|
|
|
|
ntfiles++;
|
2000-10-07 20:39:34 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!ntfiles && !mfct) { /* everything in memory--pop */
|
2001-02-19 23:50:17 +03:00
|
|
|
if (nelem > 1
|
|
|
|
&& ((stable_sort)
|
2001-01-08 21:00:31 +03:00
|
|
|
? sradixsort(keylist, nelem, weights, REC_D)
|
2001-02-19 23:50:17 +03:00
|
|
|
: radixsort(keylist, nelem, weights, REC_D) ))
|
2000-10-07 20:39:34 +04:00
|
|
|
err(2, NULL);
|
2001-02-20 21:33:09 +03:00
|
|
|
if (nelem > 0)
|
|
|
|
append(keylist, nelem, depth, outfp, putline, ftbl);
|
2000-10-07 20:39:34 +04:00
|
|
|
break; /* pop */
|
|
|
|
}
|
|
|
|
if (panic >= PANIC) {
|
|
|
|
if (!ntfiles)
|
2001-01-11 17:05:24 +03:00
|
|
|
fmerge(0, MSTART, filelist, mfct, geteasy,
|
2000-10-08 00:37:06 +04:00
|
|
|
outfp, putline, ftbl);
|
2000-10-07 20:39:34 +04:00
|
|
|
else
|
2001-01-11 17:05:24 +03:00
|
|
|
fmerge(0, base, filelist, ntfiles, geteasy,
|
2000-10-08 00:37:06 +04:00
|
|
|
outfp, putline, ftbl);
|
2000-10-07 20:39:34 +04:00
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
total = maxb = lastb = 0; /* find if one bin dominates */
|
|
|
|
for (i = 0; i < NBINS; i++)
|
2001-01-13 20:27:21 +03:00
|
|
|
if (sizes[i]) {
|
|
|
|
if (sizes[i] > sizes[maxb])
|
|
|
|
maxb = i;
|
|
|
|
lastb = i;
|
|
|
|
total += sizes[i];
|
|
|
|
}
|
2000-10-07 20:39:34 +04:00
|
|
|
if (sizes[maxb] < max((total / 2) , BUFSIZE))
|
|
|
|
maxb = lastb; /* otherwise pop after last bin */
|
2001-01-11 17:05:24 +03:00
|
|
|
fstack[base].lastb = lastb;
|
|
|
|
fstack[base].maxb = maxb;
|
2000-10-07 20:39:34 +04:00
|
|
|
|
2001-01-11 17:05:24 +03:00
|
|
|
/* start refining next level. */
|
|
|
|
getnext(-1, base, NULL, ntfiles, crec, bufend, 0); /* rewind */
|
2000-10-07 20:39:34 +04:00
|
|
|
for (i = 0; i < maxb; i++) {
|
|
|
|
if (!sizes[i]) /* bin empty; step ahead file offset */
|
2001-01-11 17:05:24 +03:00
|
|
|
getnext(i, base, NULL,ntfiles, crec, bufend, 0);
|
2001-02-19 23:50:17 +03:00
|
|
|
else
|
2001-01-11 17:05:24 +03:00
|
|
|
fsort(i, depth+1, base, filelist, ntfiles,
|
|
|
|
outfp, ftbl);
|
2000-10-07 20:39:34 +04:00
|
|
|
}
|
2001-01-11 17:05:24 +03:00
|
|
|
|
|
|
|
get = getnext;
|
|
|
|
|
2000-10-07 20:39:34 +04:00
|
|
|
if (lastb != maxb) {
|
2000-10-08 00:37:06 +04:00
|
|
|
if (prevfp != outfp)
|
|
|
|
tailfp[panic] = prevfp;
|
|
|
|
prevfp = ftmp();
|
2000-10-07 20:39:34 +04:00
|
|
|
for (i = maxb+1; i <= lastb; i++)
|
2001-02-19 23:50:17 +03:00
|
|
|
if (!sizes[i])
|
2001-01-11 17:05:24 +03:00
|
|
|
getnext(i, base, NULL, ntfiles, crec,
|
2001-01-13 20:27:21 +03:00
|
|
|
bufend,0);
|
2001-02-19 23:50:17 +03:00
|
|
|
else
|
2001-01-11 17:05:24 +03:00
|
|
|
fsort(i, depth+1, base, filelist,
|
2001-01-13 20:27:21 +03:00
|
|
|
ntfiles, prevfp, ftbl);
|
2000-10-07 20:39:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* sort biggest (or last) bin at this level */
|
|
|
|
depth++;
|
|
|
|
panic++;
|
|
|
|
binno = maxb;
|
2001-01-11 17:05:24 +03:00
|
|
|
top = base;
|
2000-10-07 20:39:34 +04:00
|
|
|
nfiles = ntfiles; /* so overwrite them */
|
|
|
|
}
|
2000-10-08 00:37:06 +04:00
|
|
|
if (prevfp != outfp) {
|
|
|
|
concat(outfp, prevfp);
|
|
|
|
fclose(prevfp);
|
2000-10-07 20:39:34 +04:00
|
|
|
}
|
|
|
|
for (i = panic; i >= 0; --i)
|
2000-10-08 00:37:06 +04:00
|
|
|
if (tailfp[i]) {
|
|
|
|
concat(outfp, tailfp[i]);
|
|
|
|
fclose(tailfp[i]);
|
2000-10-07 20:39:34 +04:00
|
|
|
}
|
2001-05-15 15:19:45 +04:00
|
|
|
|
|
|
|
/* If on top level, free our structures */
|
|
|
|
if (depth == 0) {
|
|
|
|
free(keylist), keylist = NULL;
|
|
|
|
free(buffer), buffer = NULL;
|
|
|
|
}
|
2000-10-07 20:39:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2001-02-19 23:50:17 +03:00
|
|
|
* This is one pass of radix exchange, dumping the bins to disk.
|
2000-10-07 20:39:34 +04:00
|
|
|
*/
|
|
|
|
#define swap(a, b, t) t = a, a = b, b = t
|
|
|
|
void
|
2000-10-08 00:37:06 +04:00
|
|
|
onepass(a, depth, n, sizes, tr, fp)
|
2000-10-07 22:37:09 +04:00
|
|
|
const u_char **a;
|
2000-10-07 20:39:34 +04:00
|
|
|
int depth;
|
|
|
|
long n, sizes[];
|
|
|
|
u_char *tr;
|
2000-10-08 00:37:06 +04:00
|
|
|
FILE *fp;
|
2000-10-07 20:39:34 +04:00
|
|
|
{
|
2000-10-17 19:22:57 +04:00
|
|
|
size_t tsizes[NBINS+1];
|
2000-10-07 22:37:09 +04:00
|
|
|
const u_char **bin[257], ***bp, ***bpmax, **top[256], ***tp;
|
|
|
|
static int histo[256];
|
2000-10-07 20:39:34 +04:00
|
|
|
int *hp;
|
2000-10-16 00:46:33 +04:00
|
|
|
int c;
|
2000-10-07 22:37:09 +04:00
|
|
|
const u_char **an, *t, **aj;
|
2000-10-16 00:46:33 +04:00
|
|
|
const u_char **ak, *r;
|
2000-10-07 20:39:34 +04:00
|
|
|
|
|
|
|
memset(tsizes, 0, sizeof(tsizes));
|
|
|
|
depth += sizeof(TRECHEADER);
|
2000-10-17 19:22:57 +04:00
|
|
|
an = &a[n];
|
2000-10-07 20:39:34 +04:00
|
|
|
for (ak = a; ak < an; ak++) {
|
|
|
|
histo[c = tr[**ak]]++;
|
2000-10-17 01:53:19 +04:00
|
|
|
tsizes[c] += ((const RECHEADER *) (*ak -= depth))->length;
|
2000-10-07 20:39:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bin[0] = a;
|
|
|
|
bpmax = bin + 256;
|
|
|
|
tp = top, hp = histo;
|
|
|
|
for (bp = bin; bp < bpmax; bp++) {
|
|
|
|
*tp++ = *(bp+1) = *bp + (c = *hp);
|
|
|
|
*hp++ = 0;
|
|
|
|
if (c <= 1)
|
|
|
|
continue;
|
|
|
|
}
|
2001-01-13 20:27:21 +03:00
|
|
|
for (aj = a; aj < an; *aj = r, aj = bin[c+1])
|
|
|
|
for (r = *aj; aj < (ak = --top[c = tr[r[depth]]]) ;)
|
2000-10-07 20:39:34 +04:00
|
|
|
swap(*ak, r, t);
|
|
|
|
|
|
|
|
for (ak = a, c = 0; c < 256; c++) {
|
|
|
|
an = bin[c+1];
|
|
|
|
n = an - ak;
|
|
|
|
tsizes[c] += n * sizeof(TRECHEADER);
|
|
|
|
/* tell getnext how many elements in this bin, this segment. */
|
2000-10-17 19:22:57 +04:00
|
|
|
EWRITE(&tsizes[c], sizeof(size_t), 1, fp);
|
2000-10-07 20:39:34 +04:00
|
|
|
sizes[c] += tsizes[c];
|
|
|
|
for (; ak < an; ++ak)
|
2000-10-17 01:53:19 +04:00
|
|
|
putrec((const RECHEADER *) *ak, fp);
|
2000-10-07 20:39:34 +04:00
|
|
|
}
|
|
|
|
}
|