[BACK]Return to compress.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / window

Annotation of src/usr.bin/window/compress.c, Revision 1.3

1.3     ! downsj      1: /*     $OpenBSD$       */
1.1       deraadt     2: /*     $NetBSD: compress.c,v 1.3 1995/09/28 10:34:13 tls Exp $ */
                      3:
                      4: /*
                      5:  * Copyright (c) 1989, 1993
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * This code is derived from software contributed to Berkeley by
                      9:  * Edward Wang at The University of California, Berkeley.
                     10:  *
                     11:  * Redistribution and use in source and binary forms, with or without
                     12:  * modification, are permitted provided that the following conditions
                     13:  * are met:
                     14:  * 1. Redistributions of source code must retain the above copyright
                     15:  *    notice, this list of conditions and the following disclaimer.
                     16:  * 2. Redistributions in binary form must reproduce the above copyright
                     17:  *    notice, this list of conditions and the following disclaimer in the
                     18:  *    documentation and/or other materials provided with the distribution.
                     19:  * 3. All advertising materials mentioning features or use of this software
                     20:  *    must display the following acknowledgement:
                     21:  *     This product includes software developed by the University of
                     22:  *     California, Berkeley and its contributors.
                     23:  * 4. Neither the name of the University nor the names of its contributors
                     24:  *    may be used to endorse or promote products derived from this software
                     25:  *    without specific prior written permission.
                     26:  *
                     27:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     28:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     29:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     30:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     31:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     32:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     33:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     34:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     35:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     36:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     37:  * SUCH DAMAGE.
                     38:  */
                     39:
                     40: #ifndef lint
                     41: #if 0
                     42: static char sccsid[] = "@(#)compress.c 8.1 (Berkeley) 6/6/93";
                     43: #else
1.3     ! downsj     44: static char rcsid[] = "$OpenBSD$";
1.1       deraadt    45: #endif
                     46: #endif /* not lint */
1.3     ! downsj     47:
        !            48: #include <stdlib.h>
1.1       deraadt    49:
                     50: #include "ww.h"
                     51: #include "tt.h"
                     52:
                     53:        /* special */
                     54: #include <stdio.h>
                     55: #include <fcntl.h>
                     56: int cc_trace = 0;
                     57: FILE *cc_trace_fp;
                     58:
                     59:        /* tunable parameters */
                     60:
                     61: int cc_reverse = 1;
                     62: int cc_sort = 0;
                     63: int cc_chop = 0;
                     64:
                     65: int cc_token_max = 8;          /* <= TOKEN_MAX */
                     66: int cc_token_min = 2;          /* > tt.tt_put_token_cost */
                     67: int cc_npass0 = 1;
                     68: int cc_npass1 = 1;
                     69:
                     70: int cc_bufsize = 1024 * 3;     /* XXX, or 80 * 24 * 2 */
                     71:
                     72: int cc_ntoken = 8192;
                     73:
                     74: #define cc_weight XXX
                     75: #ifndef cc_weight
                     76: int cc_weight = 0;
                     77: #endif
                     78:
                     79: #define TOKEN_MAX 16
                     80:
                     81: struct cc {
                     82:        char string[TOKEN_MAX];
                     83:        char length;
                     84:        char flag;
                     85: #ifndef cc_weight
                     86:        short weight;
                     87: #endif
                     88:        long time;              /* time last seen */
                     89:        short bcount;           /* count in this buffer */
                     90:        short ccount;           /* count in compression */
                     91:        short places;           /* places in the buffer */
                     92:        short code;             /* token code */
                     93:        struct cc *qforw, *qback;
                     94:        struct cc *hforw, **hback;
                     95: };
                     96:
                     97: short cc_thresholds[TOKEN_MAX + 1];
                     98: #define thresh(length) (cc_thresholds[length])
                     99: #define threshp(code, count, length) \
                    100:        ((code) >= 0 || (short) (count) >= cc_thresholds[length])
                    101:
                    102: #ifndef cc_weight
                    103: short cc_wthresholds[TOKEN_MAX + 1];
                    104: #define wthresh(length) (cc_wthresholds[length])
                    105: #define wthreshp(weight, length) ((short) (weight) >= cc_wthresholds[length])
                    106: #else
                    107: #define wthreshp(weight, length) (0)
                    108: #endif
                    109:
                    110: #ifndef cc_weight
                    111: short cc_wlimits[TOKEN_MAX + 1];
                    112: #define wlimit(length) (cc_wlimits[length])
                    113: #endif
                    114:
                    115: #define put_token_score(length) ((length) - tt.tt_put_token_cost)
                    116:
                    117: int cc_score_adjustments[TOKEN_MAX + 1][8]; /* XXX, 8 > max of cc_thresholds */
                    118: #define score_adjust(score, p) \
                    119:        do { \
                    120:                int length = (p)->length; \
                    121:                int ccount = (p)->ccount; \
                    122:                if (threshp((p)->code, ccount, length) || \
                    123:                    wthreshp((p)->weight, length)) /* XXX */ \
                    124:                        (score) -= length - tt.tt_put_token_cost; \
                    125:                else \
                    126:                        (score) += cc_score_adjustments[length][ccount]; \
                    127:        } while (0)
                    128:
                    129: int cc_initial_scores[TOKEN_MAX + 1][8]; /* XXX, 8 > max of cc_thresholds */
                    130:
                    131: struct cc cc_q0a, cc_q0b, cc_q1a, cc_q1b;
                    132:
                    133: #define qinsert(p1, p2) \
                    134:        do { \
                    135:                register struct cc *forw = (p1)->qforw; \
                    136:                register struct cc *back = (p1)->qback; \
                    137:                back->qforw = forw; \
                    138:                forw->qback = back; \
                    139:                forw = (p2)->qforw; \
                    140:                (p1)->qforw = forw; \
                    141:                forw->qback = (p1); \
                    142:                (p2)->qforw = (p1); \
                    143:                (p1)->qback = (p2); \
                    144:        } while (0)
                    145:
                    146: #define qinsertq(q, p) \
                    147:        ((q)->qforw == (q) ? 0 : \
                    148:         ((q)->qback->qforw = (p)->qforw, \
                    149:          (p)->qforw->qback = (q)->qback, \
                    150:          (q)->qforw->qback = (p), \
                    151:          (p)->qforw = (q)->qforw, \
                    152:          (q)->qforw = (q), \
                    153:          (q)->qback = (q)))
                    154:
                    155: #define H              (14)
                    156: #define HSIZE          (1 << H)
                    157: #define hash(h, c)     ((((h) >> H - 8 | (h) << 8) ^ (c)) & HSIZE - 1)
                    158:
                    159: char *cc_buffer;
                    160: struct cc **cc_output;                 /* the output array */
                    161: short *cc_places[TOKEN_MAX + 1];
                    162: short *cc_hashcodes;                   /* for computing hashcodes */
                    163: struct cc **cc_htab;                   /* the hash table */
                    164: struct cc **cc_tokens;                 /* holds all the active tokens */
                    165: struct cc_undo {
                    166:        struct cc **pos;
                    167:        struct cc *val;
                    168: } *cc_undo;
                    169:
                    170: long cc_time, cc_time0;
                    171:
                    172: char *cc_tt_ob, *cc_tt_obe;
                    173:
                    174: ccinit()
                    175: {
                    176:        register i, j;
                    177:        register struct cc *p;
                    178:
                    179:        if (tt.tt_token_max > cc_token_max)
                    180:                tt.tt_token_max = cc_token_max;
                    181:        if (tt.tt_token_min < cc_token_min)
                    182:                tt.tt_token_min = cc_token_min;
                    183:        if (tt.tt_token_min > tt.tt_token_max) {
                    184:                tt.tt_ntoken = 0;
                    185:                return 0;
                    186:        }
                    187:        if (tt.tt_ntoken > cc_ntoken / 2)       /* not likely */
                    188:                tt.tt_ntoken = cc_ntoken / 2;
                    189: #define C(x) (sizeof (x) / sizeof *(x))
                    190:        for (i = 0; i < C(cc_thresholds); i++) {
                    191:                int h = i - tt.tt_put_token_cost;
                    192:                if (h > 0)
                    193:                        cc_thresholds[i] =
                    194:                                (tt.tt_set_token_cost + 1 + h - 1) / h + 1;
                    195:                else
                    196:                        cc_thresholds[i] = 0;
                    197:        }
                    198:        for (i = 0; i < C(cc_score_adjustments); i++) {
                    199:                int t = cc_thresholds[i];
                    200:                for (j = 0; j < C(*cc_score_adjustments); j++) {
                    201:                        if (j >= t)
                    202:                                cc_score_adjustments[i][j] =
                    203:                                        - (i - tt.tt_put_token_cost);
                    204:                        else if (j < t - 1)
                    205:                                cc_score_adjustments[i][j] = 0;
                    206:                        else
                    207:                                /*
                    208:                                 * cost now is
                    209:                                 *      length * (ccount + 1)           a
                    210:                                 * cost before was
                    211:                                 *      set-token-cost + length +
                    212:                                 *              ccount * put-token-cost b
                    213:                                 * the score adjustment is (b - a)
                    214:                                 */
                    215:                                cc_score_adjustments[i][j] =
                    216:                                        tt.tt_set_token_cost + i +
                    217:                                                j * tt.tt_put_token_cost -
                    218:                                                        i * (j + 1);
                    219:                        if (j >= t)
                    220:                                cc_initial_scores[i][j] = 0;
                    221:                        else
                    222:                                /*
                    223:                                 * - (set-token-cost +
                    224:                                 *      (length - put-token-cost) -
                    225:                                 *      (length - put-token-cost) * ccount)
                    226:                                 */
                    227:                                cc_initial_scores[i][j] =
                    228:                                        - (tt.tt_set_token_cost +
                    229:                                           (i - tt.tt_put_token_cost) -
                    230:                                           (i - tt.tt_put_token_cost) * j);
                    231:                }
                    232:        }
                    233: #ifndef cc_weight
                    234:        for (i = 1; i < C(cc_wthresholds); i++) {
                    235:                cc_wthresholds[i] =
                    236:                        ((tt.tt_set_token_cost + tt.tt_put_token_cost) / i +
                    237:                                i / 5 + 1) *
                    238:                                cc_weight + 1;
                    239:                cc_wlimits[i] = cc_wthresholds[i] + cc_weight;
                    240:        }
                    241: #endif
                    242: #undef C
                    243:        if ((cc_output = (struct cc **)
                    244:             malloc((unsigned) cc_bufsize * sizeof *cc_output)) == 0)
                    245:                goto nomem;
                    246:        if ((cc_hashcodes = (short *)
                    247:             malloc((unsigned) cc_bufsize * sizeof *cc_hashcodes)) == 0)
                    248:                goto nomem;
                    249:        if ((cc_htab = (struct cc **) malloc(HSIZE * sizeof *cc_htab)) == 0)
                    250:                goto nomem;
                    251:        if ((cc_tokens = (struct cc **)
                    252:             malloc((unsigned)
                    253:                    (cc_ntoken + tt.tt_token_max - tt.tt_token_min + 1) *
                    254:                    sizeof *cc_tokens)) == 0)
                    255:                goto nomem;
                    256:        if ((cc_undo = (struct cc_undo *)
                    257:             malloc((unsigned) cc_bufsize * sizeof *cc_undo)) == 0)
                    258:                goto nomem;
                    259:        for (i = tt.tt_token_min; i <= tt.tt_token_max; i++)
                    260:                if ((cc_places[i] = (short *)
                    261:                     malloc((unsigned) cc_bufsize * sizeof **cc_places)) == 0)
                    262:                        goto nomem;
                    263:        cc_q0a.qforw = cc_q0a.qback = &cc_q0a;
                    264:        cc_q0b.qforw = cc_q0b.qback = &cc_q0b;
                    265:        cc_q1a.qforw = cc_q1a.qback = &cc_q1a;
                    266:        cc_q1b.qforw = cc_q1b.qback = &cc_q1b;
                    267:        if ((p = (struct cc *) malloc((unsigned) cc_ntoken * sizeof *p)) == 0)
                    268:                goto nomem;
                    269:        for (i = 0; i < tt.tt_ntoken; i++) {
                    270:                p->code = i;
                    271:                p->time = -1;
                    272:                p->qback = cc_q0a.qback;
                    273:                p->qforw = &cc_q0a;
                    274:                p->qback->qforw = p;
                    275:                cc_q0a.qback = p;
                    276:                p++;
                    277:        }
                    278:        for (; i < cc_ntoken; i++) {
                    279:                p->code = -1;
                    280:                p->time = -1;
                    281:                p->qback = cc_q1a.qback;
                    282:                p->qforw = &cc_q1a;
                    283:                p->qback->qforw = p;
                    284:                cc_q1a.qback = p;
                    285:                p++;
                    286:        }
                    287:        cc_tt_ob = tt_ob;
                    288:        cc_tt_obe = tt_obe;
                    289:        if ((cc_buffer = malloc((unsigned) cc_bufsize)) == 0)
                    290:                goto nomem;
                    291:        return 0;
                    292: nomem:
                    293:        wwerrno = WWE_NOMEM;
                    294:        return -1;
                    295: }
                    296:
                    297: ccstart()
                    298: {
                    299:        int ccflush();
                    300:
                    301:        ttflush();
                    302:        tt_obp = tt_ob = cc_buffer;
                    303:        tt_obe = tt_ob + cc_bufsize;
                    304:        tt.tt_flush = ccflush;
                    305:        if (cc_trace) {
                    306:                cc_trace_fp = fopen("window-trace", "a");
                    307:                (void) fcntl(fileno(cc_trace_fp), F_SETFD, 1);
                    308:        }
                    309:        ccreset();
                    310: }
                    311:
                    312: ccreset()
                    313: {
                    314:        register struct cc *p;
                    315:
                    316:        bzero((char *) cc_htab, HSIZE * sizeof *cc_htab);
                    317:        for (p = cc_q0a.qforw; p != &cc_q0a; p = p->qforw)
                    318:                p->hback = 0;
                    319:        for (p = cc_q1a.qforw; p != &cc_q1a; p = p->qforw)
                    320:                p->hback = 0;
                    321: }
                    322:
                    323: ccend()
                    324: {
                    325:
                    326:        ttflush();
                    327:        tt_obp = tt_ob = cc_tt_ob;
                    328:        tt_obe = cc_tt_obe;
                    329:        tt.tt_flush = 0;
                    330:        if (cc_trace_fp != NULL) {
                    331:                (void) fclose(cc_trace_fp);
                    332:                cc_trace_fp = NULL;
                    333:        }
                    334: }
                    335:
                    336: ccflush()
                    337: {
                    338:        int bufsize = tt_obp - tt_ob;
                    339:        int n;
                    340:
                    341:        if (tt_ob != cc_buffer)
                    342:                abort();
                    343:        if (cc_trace_fp != NULL) {
                    344:                (void) fwrite(tt_ob, 1, bufsize, cc_trace_fp);
                    345:                (void) putc(-1, cc_trace_fp);
                    346:        }
                    347:        tt.tt_flush = 0;
                    348:        (*tt.tt_compress)(1);
                    349:        if (bufsize < tt.tt_token_min) {
                    350:                ttflush();
                    351:                goto out;
                    352:        }
                    353:        tt_obp = tt_ob = cc_tt_ob;
                    354:        tt_obe = cc_tt_obe;
                    355:        cc_time0 = cc_time;
                    356:        cc_time += bufsize;
                    357:        n = cc_sweep_phase(cc_buffer, bufsize, cc_tokens);
                    358:        cc_compress_phase(cc_output, bufsize, cc_tokens, n);
                    359:        cc_output_phase(cc_buffer, cc_output, bufsize);
                    360:        ttflush();
                    361:        tt_obp = tt_ob = cc_buffer;
                    362:        tt_obe = cc_buffer + cc_bufsize;
                    363: out:
                    364:        (*tt.tt_compress)(0);
                    365:        tt.tt_flush = ccflush;
                    366: }
                    367:
                    368: cc_sweep_phase(buffer, bufsize, tokens)
                    369:        char *buffer;
                    370:        struct cc **tokens;
                    371: {
                    372:        register struct cc **pp = tokens;
                    373:        register i, n;
                    374: #ifdef STATS
                    375:        int nn, ii;
                    376: #endif
                    377:
                    378: #ifdef STATS
                    379:        if (verbose >= 0)
                    380:                time_begin();
                    381:        if (verbose > 0)
                    382:                printf("Sweep:");
                    383: #endif
                    384:        cc_sweep0(buffer, bufsize, tt.tt_token_min - 1);
                    385: #ifdef STATS
                    386:        ntoken_stat = 0;
                    387:        nn = 0;
                    388:        ii = 0;
                    389: #endif
                    390:        for (i = tt.tt_token_min; i <= tt.tt_token_max; i++) {
                    391: #ifdef STATS
                    392:                if (verbose > 0) {
                    393:                        if (ii > 7) {
                    394:                                printf("\n      ");
                    395:                                ii = 0;
                    396:                        }
                    397:                        ii++;
                    398:                        printf(" (%d", i);
                    399:                        (void) fflush(stdout);
                    400:                }
                    401: #endif
                    402:                n = cc_sweep(buffer, bufsize, pp, i);
                    403:                pp += n;
                    404: #ifdef STATS
                    405:                if (verbose > 0) {
                    406:                        if (--n > 0) {
                    407:                                printf(" %d", n);
                    408:                                nn += n;
                    409:                        }
                    410:                        putchar(')');
                    411:                }
                    412: #endif
                    413:        }
                    414:        qinsertq(&cc_q1b, &cc_q1a);
                    415: #ifdef STATS
                    416:        if (verbose > 0)
                    417:                printf("\n       %d tokens, %d candidates\n",
                    418:                        ntoken_stat, nn);
                    419:        if (verbose >= 0)
                    420:                time_end();
                    421: #endif
                    422:        return pp - tokens;
                    423: }
                    424:
                    425: cc_sweep0(buffer, n, length)
                    426:        char *buffer;
                    427: {
                    428:        register char *p;
                    429:        register short *hc;
                    430:        register i;
                    431:        register short c;
                    432:        register short pc = tt.tt_padc;
                    433:
                    434:        /* n and length are at least 1 */
                    435:        p = buffer++;
                    436:        hc = cc_hashcodes;
                    437:        i = n;
                    438:        do {
                    439:                if ((*hc++ = *p++) == pc)
                    440:                        hc[-1] = -1;
                    441:        } while (--i);
                    442:        while (--length) {
                    443:                p = buffer++;
                    444:                hc = cc_hashcodes;
                    445:                for (i = n--; --i;) {
                    446:                        if ((c = *p++) == pc || *hc < 0)
                    447:                                c = -1;
                    448:                        else
                    449:                                c = hash(*hc, c);
                    450:                        *hc++ = c;
                    451:                }
                    452:        }
                    453: }
                    454:
                    455: cc_sweep(buffer, bufsize, tokens, length)
                    456:        char *buffer;
                    457:        struct cc **tokens;
                    458:        register length;
                    459: {
                    460:        register struct cc *p;
                    461:        register char *cp;
                    462:        register i;
                    463:        short *hc;
                    464:        short *places = cc_places[length];
                    465:        struct cc **pp = tokens;
                    466:        short threshold = thresh(length);
                    467: #ifndef cc_weight
                    468:        short wthreshold = wthresh(length);
                    469:        short limit = wlimit(length);
                    470: #endif
                    471:        int time;
                    472:        short pc = tt.tt_padc;
                    473:
                    474:        i = length - 1;
                    475:        bufsize -= i;
                    476:        cp = buffer + i;
                    477:        hc = cc_hashcodes;
                    478:        time = cc_time0;
                    479:        for (i = 0; i < bufsize; i++, time++) {
                    480:                struct cc **h;
                    481:
                    482:                {
                    483:                        register short *hc1 = hc;
                    484:                        register short c = *cp++;
                    485:                        register short hh;
                    486:                        if ((hh = *hc1) < 0 || c == pc) {
                    487:                                *hc1++ = -1;
                    488:                                hc = hc1;
                    489:                                continue;
                    490:                        }
                    491:                        h = cc_htab + (*hc1++ = hash(hh, c));
                    492:                        hc = hc1;
                    493:                }
                    494:                for (p = *h; p != 0; p = p->hforw)
                    495:                        if (p->length == (char) length) {
                    496:                                register char *p1 = p->string;
                    497:                                register char *p2 = cp - length;
                    498:                                register n = length;
                    499:                                do
                    500:                                        if (*p1++ != *p2++)
                    501:                                                goto fail;
                    502:                                while (--n);
                    503:                                break;
                    504:                        fail:;
                    505:                        }
                    506:                if (p == 0) {
                    507:                        p = cc_q1a.qback;
                    508:                        if (p == &cc_q1a ||
                    509:                            p->time >= cc_time0 && p->length == (char) length)
                    510:                                continue;
                    511:                        if (p->hback != 0)
                    512:                                if ((*p->hback = p->hforw) != 0)
                    513:                                        p->hforw->hback = p->hback;
                    514:                        {
                    515:                                register char *p1 = p->string;
                    516:                                register char *p2 = cp - length;
                    517:                                register n = length;
                    518:                                do
                    519:                                        *p1++ = *p2++;
                    520:                                while (--n);
                    521:                        }
                    522:                        p->length = length;
                    523: #ifndef cc_weight
                    524:                        p->weight = cc_weight;
                    525: #endif
                    526:                        p->time = time;
                    527:                        p->bcount = 1;
                    528:                        p->ccount = 0;
                    529:                        p->flag = 0;
                    530:                        if ((p->hforw = *h) != 0)
                    531:                                p->hforw->hback = &p->hforw;
                    532:                        *h = p;
                    533:                        p->hback = h;
                    534:                        qinsert(p, &cc_q1a);
                    535:                        places[i] = -1;
                    536:                        p->places = i;
                    537: #ifdef STATS
                    538:                        ntoken_stat++;
                    539: #endif
                    540:                } else if (p->time < cc_time0) {
                    541: #ifndef cc_weight
                    542:                        if ((p->weight += p->time - time) < 0)
                    543:                                p->weight = cc_weight;
                    544:                        else if ((p->weight += cc_weight) > limit)
                    545:                                p->weight = limit;
                    546: #endif
                    547:                        p->time = time;
                    548:                        p->bcount = 1;
                    549:                        p->ccount = 0;
                    550:                        if (p->code >= 0) {
                    551:                                p->flag = 1;
                    552:                                *pp++ = p;
                    553:                        } else
                    554: #ifndef cc_weight
                    555:                        if (p->weight >= wthreshold) {
                    556:                                p->flag = 1;
                    557:                                *pp++ = p;
                    558:                                qinsert(p, &cc_q1b);
                    559:                        } else
                    560: #endif
                    561:                        {
                    562:                                p->flag = 0;
                    563:                                qinsert(p, &cc_q1a);
                    564:                        }
                    565:                        places[i] = -1;
                    566:                        p->places = i;
                    567: #ifdef STATS
                    568:                        ntoken_stat++;
                    569: #endif
                    570:                } else if (p->time + length > time) {
                    571:                        /*
                    572:                         * overlapping token, don't count as two and
                    573:                         * don't update time, but do adjust weight to offset
                    574:                         * the difference
                    575:                         */
                    576: #ifndef cc_weight
                    577:                        if (cc_weight != 0) {   /* XXX */
                    578:                                p->weight += time - p->time;
                    579:                                if (!p->flag && p->weight >= wthreshold) {
                    580:                                        p->flag = 1;
                    581:                                        *pp++ = p;
                    582:                                        qinsert(p, &cc_q1b);
                    583:                                }
                    584:                        }
                    585: #endif
                    586:                        places[i] = p->places;
                    587:                        p->places = i;
                    588:                } else {
                    589: #ifndef cc_weight
                    590:                        if ((p->weight += p->time - time) < 0)
                    591:                                p->weight = cc_weight;
                    592:                        else if ((p->weight += cc_weight) > limit)
                    593:                                p->weight = limit;
                    594: #endif
                    595:                        p->time = time;
                    596:                        p->bcount++;
                    597:                        if (!p->flag &&
                    598:                            /* code must be < 0 if flag false here */
                    599:                            (p->bcount >= threshold
                    600: #ifndef cc_weight
                    601:                             || p->weight >= wthreshold
                    602: #endif
                    603:                             )) {
                    604:                                p->flag = 1;
                    605:                                *pp++ = p;
                    606:                                qinsert(p, &cc_q1b);
                    607:                        }
                    608:                        places[i] = p->places;
                    609:                        p->places = i;
                    610:                }
                    611:        }
                    612:        if ((i = pp - tokens) > 0) {
                    613:                *pp = 0;
                    614:                if (cc_reverse)
                    615:                        cc_sweep_reverse(tokens, places);
                    616:                if (cc_sort && i > 1) {
                    617:                        int cc_token_compare();
                    618:                        qsort((char *) tokens, i, sizeof *tokens,
                    619:                              cc_token_compare);
                    620:                }
                    621:                if (cc_chop) {
                    622:                        if ((i = i * cc_chop / 100) == 0)
                    623:                                i = 1;
                    624:                        tokens[i] = 0;
                    625:                }
                    626:                i++;
                    627:        }
                    628:        return i;
                    629: }
                    630:
                    631: cc_sweep_reverse(pp, places)
                    632:        register struct cc **pp;
                    633:        register short *places;
                    634: {
                    635:        register struct cc *p;
                    636:        register short front, back, t;
                    637:
                    638:        while ((p = *pp++) != 0) {
                    639:                back = -1;
                    640:                t = p->places;
                    641:                /* the list is never empty */
                    642:                do {
                    643:                        front = places[t];
                    644:                        places[t] = back;
                    645:                        back = t;
                    646:                } while ((t = front) >= 0);
                    647:                p->places = back;
                    648:        }
                    649: }
                    650:
                    651: cc_compress_phase(output, bufsize, tokens, ntoken)
                    652:        struct cc **output;
                    653:        struct cc **tokens;
                    654: {
                    655:        register i;
                    656:
                    657:        bzero((char *) output, bufsize * sizeof *output);
                    658:        for (i = 0; i < cc_npass0; i++)
                    659:                cc_compress_phase1(output, tokens, ntoken, 0);
                    660:        for (i = 0; i < cc_npass1; i++)
                    661:                cc_compress_phase1(output, tokens, ntoken, 1);
                    662:        cc_compress_cleanup(output, bufsize);
                    663: }
                    664:
                    665: cc_compress_phase1(output, tokens, ntoken, flag)
                    666:        register struct cc **output;
                    667:        struct cc **tokens;
                    668: {
                    669:        register struct cc **pp;
                    670: #ifdef STATS
                    671:        register int i = 0;
                    672:        int nt = 0, cc = 0, nc = 0;
                    673: #endif
                    674:
                    675: #ifdef STATS
                    676:        if (verbose >= 0)
                    677:                time_begin();
                    678:        if (verbose > 0)
                    679:                printf("Compress:");
                    680: #endif
                    681:        pp = tokens;
                    682:        while (pp < tokens + ntoken) {
                    683: #ifdef STATS
                    684:                if (verbose > 0) {
                    685:                        ntoken_stat = 0;
                    686:                        ccount_stat = 0;
                    687:                        ncover_stat = 0;
                    688:                        if (i > 2) {
                    689:                                printf("\n         ");
                    690:                                i = 0;
                    691:                        }
                    692:                        i++;
                    693:                        printf(" (%d", (*pp)->length);
                    694:                        (void) fflush(stdout);
                    695:                }
                    696: #endif
                    697:                pp += cc_compress(output, pp, flag);
                    698: #ifdef STATS
                    699:                if (verbose > 0) {
                    700:                        printf(" %dt %du %dc)", ntoken_stat, ccount_stat,
                    701:                               ncover_stat);
                    702:                        nt += ntoken_stat;
                    703:                        cc += ccount_stat;
                    704:                        nc += ncover_stat;
                    705:                }
                    706: #endif
                    707:        }
                    708: #ifdef STATS
                    709:        if (verbose > 0)
                    710:                printf("\n   total: (%dt %du %dc)\n", nt, cc, nc);
                    711:        if (verbose >= 0)
                    712:                time_end();
                    713: #endif
                    714: }
                    715:
                    716: cc_compress_cleanup(output, bufsize)
                    717:        register struct cc **output;
                    718: {
                    719:        register struct cc **end;
                    720:
                    721:        /* the previous output phase may have been interrupted */
                    722:        qinsertq(&cc_q0b, &cc_q0a);
                    723:        for (end = output + bufsize; output < end;) {
                    724:                register struct cc *p;
                    725:                register length;
                    726:                if ((p = *output) == 0) {
                    727:                        output++;
                    728:                        continue;
                    729:                }
                    730:                length = p->length;
                    731:                if (!p->flag) {
                    732:                } else if (p->code >= 0) {
                    733:                        qinsert(p, &cc_q0b);
                    734:                        p->flag = 0;
                    735:                } else if (p->ccount == 0) {
                    736:                        *output = 0;
                    737:                } else if (p->ccount >= thresh(length)
                    738: #ifndef cc_weight
                    739:                           || wthreshp(p->weight, length)
                    740: #endif
                    741:                           ) {
                    742:                        p->flag = 0;
                    743:                } else {
                    744:                        p->ccount = 0;
                    745:                        *output = 0;
                    746:                }
                    747:                output += length;
                    748:        }
                    749: }
                    750:
                    751: cc_compress(output, tokens, flag)
                    752:        struct cc **output;
                    753:        struct cc **tokens;
                    754:        char flag;
                    755: {
                    756:        struct cc **pp = tokens;
                    757:        register struct cc *p = *pp++;
                    758:        int length = p->length;
                    759:        int threshold = thresh(length);
                    760: #ifndef cc_weight
                    761:        short wthreshold = wthresh(length);
                    762: #endif
                    763:        short *places = cc_places[length];
                    764:        int *initial_scores = cc_initial_scores[length];
                    765:        int initial_score0 = put_token_score(length);
                    766:
                    767:        do {
                    768:                int score;
                    769:                register struct cc_undo *undop;
                    770:                int ccount;
                    771: #ifdef STATS
                    772:                int ncover;
                    773: #endif
                    774:                int i;
                    775:
                    776:                ccount = p->ccount;
                    777:                if ((short) ccount >= p->bcount)
                    778:                        continue;
                    779:                if (p->code >= 0 || ccount >= threshold)
                    780:                        score = 0;
                    781: #ifndef cc_weight
                    782:                else if (p->weight >= wthreshold)
                    783:                        /* allow one fewer match than normal */
                    784:                        /* XXX, should adjust for ccount */
                    785:                        score = - tt.tt_set_token_cost;
                    786: #endif
                    787:                else
                    788:                        score = initial_scores[ccount];
                    789:                undop = cc_undo;
                    790: #ifdef STATS
                    791:                ncover = 0;
                    792: #endif
                    793:                for (i = p->places; i >= 0; i = places[i]) {
                    794:                        register struct cc **jp;
                    795:                        register struct cc *x;
                    796:                        register struct cc **ip = output + i;
                    797:                        register score0 = initial_score0;
                    798:                        struct cc **iip = ip + length;
                    799:                        struct cc_undo *undop1 = undop;
                    800:
                    801:                        if ((x = *(jp = ip)) != 0)
                    802:                                goto z;
                    803:                        while (--jp >= output)
                    804:                                if ((x = *jp) != 0) {
                    805:                                        if (jp + x->length > ip)
                    806:                                                goto z;
                    807:                                        break;
                    808:                                }
                    809:                        jp = ip + 1;
                    810:                        while (jp < iip) {
                    811:                                if ((x = *jp) == 0) {
                    812:                                        jp++;
                    813:                                        continue;
                    814:                                }
                    815:                        z:
                    816:                                if (x == p)
                    817:                                        goto undo;
                    818: #ifdef STATS
                    819:                                ncover++;
                    820: #endif
                    821:                                undop->pos = jp;
                    822:                                undop->val = x;
                    823:                                undop++;
                    824:                                *jp = 0;
                    825:                                x->ccount--;
                    826:                                score_adjust(score0, x);
                    827:                                if (score0 < 0 && flag)
                    828:                                        goto undo;
                    829:                                jp += x->length;
                    830:                        }
                    831:                        undop->pos = ip;
                    832:                        undop->val = 0;
                    833:                        undop++;
                    834:                        *ip = p;
                    835:                        ccount++;
                    836:                        score += score0;
                    837:                        continue;
                    838:                undo:
                    839:                        while (--undop >= undop1)
                    840:                                if (*undop->pos = x = undop->val)
                    841:                                        x->ccount++;
                    842:                        undop++;
                    843:                }
                    844:                if (score > 0) {
                    845: #ifdef STATS
                    846:                        ccount_stat += ccount - p->ccount;
                    847:                        ntoken_stat++;
                    848:                        ncover_stat += ncover;
                    849: #endif
                    850:                        p->ccount = ccount;
                    851:                } else {
                    852:                        register struct cc_undo *u = cc_undo;
                    853:                        while (--undop >= u) {
                    854:                                register struct cc *x;
                    855:                                if (*undop->pos = x = undop->val)
                    856:                                        x->ccount++;
                    857:                        }
                    858:                }
                    859:        } while ((p = *pp++) != 0);
                    860:        return pp - tokens;
                    861: }
                    862:
                    863: cc_output_phase(buffer, output, bufsize)
                    864:        register char *buffer;
                    865:        register struct cc **output;
                    866:        register bufsize;
                    867: {
                    868:        register i;
                    869:        register struct cc *p, *p1;
                    870:
                    871:        for (i = 0; i < bufsize;) {
                    872:                if ((p = output[i]) == 0) {
                    873:                        ttputc(buffer[i]);
                    874:                        i++;
                    875:                } else if (p->code >= 0) {
                    876:                        if (--p->ccount == 0)
                    877:                                qinsert(p, &cc_q0a);
                    878:                        (*tt.tt_put_token)(p->code, p->string, p->length);
                    879:                        wwntokuse++;
                    880:                        wwntoksave += put_token_score(p->length);
                    881:                        i += p->length;
                    882:                } else if ((p1 = cc_q0a.qback) != &cc_q0a) {
                    883:                        p->code = p1->code;
                    884:                        p1->code = -1;
                    885:                        qinsert(p1, &cc_q1a);
                    886:                        if (--p->ccount == 0)
                    887:                                qinsert(p, &cc_q0a);
                    888:                        else
                    889:                                qinsert(p, &cc_q0b);
                    890:                        (*tt.tt_set_token)(p->code, p->string, p->length);
                    891:                        wwntokdef++;
                    892:                        wwntoksave -= tt.tt_set_token_cost;
                    893:                        i += p->length;
                    894:                } else {
                    895:                        p->ccount--;
                    896:                        ttwrite(p->string, p->length);
                    897:                        wwntokbad++;
                    898:                        i += p->length;
                    899:                }
                    900:        }
                    901:        wwntokc += bufsize;
                    902: }
                    903:
                    904: cc_token_compare(p1, p2)
                    905:        struct cc **p1, **p2;
                    906: {
                    907:        return (*p2)->bcount - (*p1)->bcount;
                    908: }