2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Edward Wang at The University of California, Berkeley.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#)compress.c 8.1 (Berkeley) 6/6/93
37 * $FreeBSD: src/usr.bin/window/compress.c,v 1.2.12.1 2001/05/17 09:45:00 obrien Exp $
38 * $DragonFly: src/usr.bin/window/compress.c,v 1.2 2003/06/17 04:29:33 dillon Exp $
51 /* tunable parameters */
57 int cc_token_max
= 8; /* <= TOKEN_MAX */
58 int cc_token_min
= 2; /* > tt.tt_put_token_cost */
62 int cc_bufsize
= 1024 * 3; /* XXX, or 80 * 24 * 2 */
74 char string
[TOKEN_MAX
];
80 long time
; /* time last seen */
81 short bcount
; /* count in this buffer */
82 short ccount
; /* count in compression */
83 short places
; /* places in the buffer */
84 short code
; /* token code */
85 struct cc
*qforw
, *qback
;
86 struct cc
*hforw
, **hback
;
89 short cc_thresholds
[TOKEN_MAX
+ 1];
90 #define thresh(length) (cc_thresholds[length])
91 #define threshp(code, count, length) \
92 ((code) >= 0 || (short) (count) >= cc_thresholds[length])
95 short cc_wthresholds
[TOKEN_MAX
+ 1];
96 #define wthresh(length) (cc_wthresholds[length])
97 #define wthreshp(weight, length) ((short) (weight) >= cc_wthresholds[length])
99 #define wthreshp(weight, length) (0)
103 short cc_wlimits
[TOKEN_MAX
+ 1];
104 #define wlimit(length) (cc_wlimits[length])
107 #define put_token_score(length) ((length) - tt.tt_put_token_cost)
109 int cc_score_adjustments
[TOKEN_MAX
+ 1][8]; /* XXX, 8 > max of cc_thresholds */
110 #define score_adjust(score, p) \
112 int length = (p)->length; \
113 int ccount = (p)->ccount; \
114 if (threshp((p)->code, ccount, length) || \
115 wthreshp((p)->weight, length)) /* XXX */ \
116 (score) -= length - tt.tt_put_token_cost; \
118 (score) += cc_score_adjustments[length][ccount]; \
121 int cc_initial_scores
[TOKEN_MAX
+ 1][8]; /* XXX, 8 > max of cc_thresholds */
123 struct cc cc_q0a
, cc_q0b
, cc_q1a
, cc_q1b
;
125 #define qinsert(p1, p2) \
127 register struct cc *forw = (p1)->qforw; \
128 register struct cc *back = (p1)->qback; \
129 back->qforw = forw; \
130 forw->qback = back; \
131 forw = (p2)->qforw; \
132 (p1)->qforw = forw; \
133 forw->qback = (p1); \
134 (p2)->qforw = (p1); \
135 (p1)->qback = (p2); \
138 #define qinsertq(q, p) \
139 ((q)->qforw == (q) ? 0 : \
140 ((q)->qback->qforw = (p)->qforw, \
141 (p)->qforw->qback = (q)->qback, \
142 (q)->qforw->qback = (p), \
143 (p)->qforw = (q)->qforw, \
148 #define HSIZE (1 << H)
149 #define hash(h, c) ((((h) >> H - 8 | (h) << 8) ^ (c)) & HSIZE - 1)
152 struct cc
**cc_output
; /* the output array */
153 short *cc_places
[TOKEN_MAX
+ 1];
154 short *cc_hashcodes
; /* for computing hashcodes */
155 struct cc
**cc_htab
; /* the hash table */
156 struct cc
**cc_tokens
; /* holds all the active tokens */
162 long cc_time
, cc_time0
;
164 char *cc_tt_ob
, *cc_tt_obe
;
169 register struct cc
*p
;
171 if (tt
.tt_token_max
> cc_token_max
)
172 tt
.tt_token_max
= cc_token_max
;
173 if (tt
.tt_token_min
< cc_token_min
)
174 tt
.tt_token_min
= cc_token_min
;
175 if (tt
.tt_token_min
> tt
.tt_token_max
) {
179 if (tt
.tt_ntoken
> cc_ntoken
/ 2) /* not likely */
180 tt
.tt_ntoken
= cc_ntoken
/ 2;
181 #define C(x) (sizeof (x) / sizeof *(x))
182 for (i
= 0; i
< C(cc_thresholds
); i
++) {
183 int h
= i
- tt
.tt_put_token_cost
;
186 (tt
.tt_set_token_cost
+ 1 + h
- 1) / h
+ 1;
188 cc_thresholds
[i
] = 0;
190 for (i
= 0; i
< C(cc_score_adjustments
); i
++) {
191 int t
= cc_thresholds
[i
];
192 for (j
= 0; j
< C(*cc_score_adjustments
); j
++) {
194 cc_score_adjustments
[i
][j
] =
195 - (i
- tt
.tt_put_token_cost
);
197 cc_score_adjustments
[i
][j
] = 0;
201 * length * (ccount + 1) a
203 * set-token-cost + length +
204 * ccount * put-token-cost b
205 * the score adjustment is (b - a)
207 cc_score_adjustments
[i
][j
] =
208 tt
.tt_set_token_cost
+ i
+
209 j
* tt
.tt_put_token_cost
-
212 cc_initial_scores
[i
][j
] = 0;
215 * - (set-token-cost +
216 * (length - put-token-cost) -
217 * (length - put-token-cost) * ccount)
219 cc_initial_scores
[i
][j
] =
220 - (tt
.tt_set_token_cost
+
221 (i
- tt
.tt_put_token_cost
) -
222 (i
- tt
.tt_put_token_cost
) * j
);
226 for (i
= 1; i
< C(cc_wthresholds
); i
++) {
228 ((tt
.tt_set_token_cost
+ tt
.tt_put_token_cost
) / i
+
231 cc_wlimits
[i
] = cc_wthresholds
[i
] + cc_weight
;
235 if ((cc_output
= (struct cc
**)
236 malloc((unsigned) cc_bufsize
* sizeof *cc_output
)) == 0)
238 if ((cc_hashcodes
= (short *)
239 malloc((unsigned) cc_bufsize
* sizeof *cc_hashcodes
)) == 0)
241 if ((cc_htab
= (struct cc
**) malloc(HSIZE
* sizeof *cc_htab
)) == 0)
243 if ((cc_tokens
= (struct cc
**)
245 (cc_ntoken
+ tt
.tt_token_max
- tt
.tt_token_min
+ 1) *
246 sizeof *cc_tokens
)) == 0)
248 if ((cc_undo
= (struct cc_undo
*)
249 malloc((unsigned) cc_bufsize
* sizeof *cc_undo
)) == 0)
251 for (i
= tt
.tt_token_min
; i
<= tt
.tt_token_max
; i
++)
252 if ((cc_places
[i
] = (short *)
253 malloc((unsigned) cc_bufsize
* sizeof **cc_places
)) == 0)
255 cc_q0a
.qforw
= cc_q0a
.qback
= &cc_q0a
;
256 cc_q0b
.qforw
= cc_q0b
.qback
= &cc_q0b
;
257 cc_q1a
.qforw
= cc_q1a
.qback
= &cc_q1a
;
258 cc_q1b
.qforw
= cc_q1b
.qback
= &cc_q1b
;
259 if ((p
= (struct cc
*) malloc((unsigned) cc_ntoken
* sizeof *p
)) == 0)
261 for (i
= 0; i
< tt
.tt_ntoken
; i
++) {
264 p
->qback
= cc_q0a
.qback
;
270 for (; i
< cc_ntoken
; i
++) {
273 p
->qback
= cc_q1a
.qback
;
281 if ((cc_buffer
= malloc((unsigned) cc_bufsize
)) == 0)
294 tt_obp
= tt_ob
= cc_buffer
;
295 tt_obe
= tt_ob
+ cc_bufsize
;
296 tt
.tt_flush
= ccflush
;
298 cc_trace_fp
= fopen("window-trace", "a");
299 (void) fcntl(fileno(cc_trace_fp
), F_SETFD
, 1);
306 register struct cc
*p
;
308 bzero((char *) cc_htab
, HSIZE
* sizeof *cc_htab
);
309 for (p
= cc_q0a
.qforw
; p
!= &cc_q0a
; p
= p
->qforw
)
311 for (p
= cc_q1a
.qforw
; p
!= &cc_q1a
; p
= p
->qforw
)
319 tt_obp
= tt_ob
= cc_tt_ob
;
322 if (cc_trace_fp
!= NULL
) {
323 (void) fclose(cc_trace_fp
);
330 int bufsize
= tt_obp
- tt_ob
;
333 if (tt_ob
!= cc_buffer
)
335 if (cc_trace_fp
!= NULL
) {
336 (void) fwrite(tt_ob
, 1, bufsize
, cc_trace_fp
);
337 (void) putc(-1, cc_trace_fp
);
340 (*tt
.tt_compress
)(1);
341 if (bufsize
< tt
.tt_token_min
) {
345 tt_obp
= tt_ob
= cc_tt_ob
;
349 n
= cc_sweep_phase(cc_buffer
, bufsize
, cc_tokens
);
350 cc_compress_phase(cc_output
, bufsize
, cc_tokens
, n
);
351 cc_output_phase(cc_buffer
, cc_output
, bufsize
);
353 tt_obp
= tt_ob
= cc_buffer
;
354 tt_obe
= cc_buffer
+ cc_bufsize
;
356 (*tt
.tt_compress
)(0);
357 tt
.tt_flush
= ccflush
;
360 cc_sweep_phase(buffer
, bufsize
, tokens
)
364 register struct cc
**pp
= tokens
;
376 cc_sweep0(buffer
, bufsize
, tt
.tt_token_min
- 1);
382 for (i
= tt
.tt_token_min
; i
<= tt
.tt_token_max
; i
++) {
391 (void) fflush(stdout
);
394 n
= cc_sweep(buffer
, bufsize
, pp
, i
);
406 qinsertq(&cc_q1b
, &cc_q1a
);
409 printf("\n %d tokens, %d candidates\n",
417 cc_sweep0(buffer
, n
, length
)
424 register short pc
= tt
.tt_padc
;
426 /* n and length are at least 1 */
431 if ((*hc
++ = *p
++) == pc
)
437 for (i
= n
--; --i
;) {
438 if ((c
= *p
++) == pc
|| *hc
< 0)
447 cc_sweep(buffer
, bufsize
, tokens
, length
)
452 register struct cc
*p
;
456 short *places
= cc_places
[length
];
457 struct cc
**pp
= tokens
;
458 short threshold
= thresh(length
);
460 short wthreshold
= wthresh(length
);
461 short limit
= wlimit(length
);
464 short pc
= tt
.tt_padc
;
471 for (i
= 0; i
< bufsize
; i
++, time
++) {
475 register short *hc1
= hc
;
476 register short c
= *cp
++;
478 if ((hh
= *hc1
) < 0 || c
== pc
) {
483 h
= cc_htab
+ (*hc1
++ = hash(hh
, c
));
486 for (p
= *h
; p
!= 0; p
= p
->hforw
)
487 if (p
->length
== (char) length
) {
488 register char *p1
= p
->string
;
489 register char *p2
= cp
- length
;
501 p
->time
>= cc_time0
&& p
->length
== (char) length
)
504 if ((*p
->hback
= p
->hforw
) != 0)
505 p
->hforw
->hback
= p
->hback
;
507 register char *p1
= p
->string
;
508 register char *p2
= cp
- length
;
516 p
->weight
= cc_weight
;
522 if ((p
->hforw
= *h
) != 0)
523 p
->hforw
->hback
= &p
->hforw
;
532 } else if (p
->time
< cc_time0
) {
534 if ((p
->weight
+= p
->time
- time
) < 0)
535 p
->weight
= cc_weight
;
536 else if ((p
->weight
+= cc_weight
) > limit
)
547 if (p
->weight
>= wthreshold
) {
562 } else if (p
->time
+ length
> time
) {
564 * overlapping token, don't count as two and
565 * don't update time, but do adjust weight to offset
569 if (cc_weight
!= 0) { /* XXX */
570 p
->weight
+= time
- p
->time
;
571 if (!p
->flag
&& p
->weight
>= wthreshold
) {
578 places
[i
] = p
->places
;
582 if ((p
->weight
+= p
->time
- time
) < 0)
583 p
->weight
= cc_weight
;
584 else if ((p
->weight
+= cc_weight
) > limit
)
590 /* code must be < 0 if flag false here */
591 (p
->bcount
>= threshold
593 || p
->weight
>= wthreshold
600 places
[i
] = p
->places
;
604 if ((i
= pp
- tokens
) > 0) {
607 cc_sweep_reverse(tokens
, places
);
608 if (cc_sort
&& i
> 1) {
609 int cc_token_compare();
610 qsort((char *) tokens
, i
, sizeof *tokens
,
614 if ((i
= i
* cc_chop
/ 100) == 0)
623 cc_sweep_reverse(pp
, places
)
624 register struct cc
**pp
;
625 register short *places
;
627 register struct cc
*p
;
628 register short front
, back
, t
;
630 while ((p
= *pp
++) != 0) {
633 /* the list is never empty */
638 } while ((t
= front
) >= 0);
643 cc_compress_phase(output
, bufsize
, tokens
, ntoken
)
649 bzero((char *) output
, bufsize
* sizeof *output
);
650 for (i
= 0; i
< cc_npass0
; i
++)
651 cc_compress_phase1(output
, tokens
, ntoken
, 0);
652 for (i
= 0; i
< cc_npass1
; i
++)
653 cc_compress_phase1(output
, tokens
, ntoken
, 1);
654 cc_compress_cleanup(output
, bufsize
);
657 cc_compress_phase1(output
, tokens
, ntoken
, flag
)
658 register struct cc
**output
;
661 register struct cc
**pp
;
664 int nt
= 0, cc
= 0, nc
= 0;
674 while (pp
< tokens
+ ntoken
) {
685 printf(" (%d", (*pp
)->length
);
686 (void) fflush(stdout
);
689 pp
+= cc_compress(output
, pp
, flag
);
692 printf(" %dt %du %dc)", ntoken_stat
, ccount_stat
,
702 printf("\n total: (%dt %du %dc)\n", nt
, cc
, nc
);
708 cc_compress_cleanup(output
, bufsize
)
709 register struct cc
**output
;
711 register struct cc
**end
;
713 /* the previous output phase may have been interrupted */
714 qinsertq(&cc_q0b
, &cc_q0a
);
715 for (end
= output
+ bufsize
; output
< end
;) {
716 register struct cc
*p
;
718 if ((p
= *output
) == 0) {
724 } else if (p
->code
>= 0) {
727 } else if (p
->ccount
== 0) {
729 } else if (p
->ccount
>= thresh(length
)
731 || wthreshp(p
->weight
, length
)
743 cc_compress(output
, tokens
, flag
)
748 struct cc
**pp
= tokens
;
749 register struct cc
*p
= *pp
++;
750 int length
= p
->length
;
751 int threshold
= thresh(length
);
753 short wthreshold
= wthresh(length
);
755 short *places
= cc_places
[length
];
756 int *initial_scores
= cc_initial_scores
[length
];
757 int initial_score0
= put_token_score(length
);
761 register struct cc_undo
*undop
;
769 if ((short) ccount
>= p
->bcount
)
771 if (p
->code
>= 0 || ccount
>= threshold
)
774 else if (p
->weight
>= wthreshold
)
775 /* allow one fewer match than normal */
776 /* XXX, should adjust for ccount */
777 score
= - tt
.tt_set_token_cost
;
780 score
= initial_scores
[ccount
];
785 for (i
= p
->places
; i
>= 0; i
= places
[i
]) {
786 register struct cc
**jp
;
787 register struct cc
*x
;
788 register struct cc
**ip
= output
+ i
;
789 register score0
= initial_score0
;
790 struct cc
**iip
= ip
+ length
;
791 struct cc_undo
*undop1
= undop
;
793 if ((x
= *(jp
= ip
)) != 0)
795 while (--jp
>= output
)
796 if ((x
= *jp
) != 0) {
797 if (jp
+ x
->length
> ip
)
803 if ((x
= *jp
) == 0) {
818 score_adjust(score0
, x
);
819 if (score0
< 0 && flag
)
831 while (--undop
>= undop1
)
832 if (*undop
->pos
= x
= undop
->val
)
838 ccount_stat
+= ccount
- p
->ccount
;
840 ncover_stat
+= ncover
;
844 register struct cc_undo
*u
= cc_undo
;
845 while (--undop
>= u
) {
846 register struct cc
*x
;
847 if (*undop
->pos
= x
= undop
->val
)
851 } while ((p
= *pp
++) != 0);
855 cc_output_phase(buffer
, output
, bufsize
)
856 register char *buffer
;
857 register struct cc
**output
;
861 register struct cc
*p
, *p1
;
863 for (i
= 0; i
< bufsize
;) {
864 if ((p
= output
[i
]) == 0) {
867 } else if (p
->code
>= 0) {
868 if (--p
->ccount
== 0)
870 (*tt
.tt_put_token
)(p
->code
, p
->string
, p
->length
);
872 wwntoksave
+= put_token_score(p
->length
);
874 } else if ((p1
= cc_q0a
.qback
) != &cc_q0a
) {
877 qinsert(p1
, &cc_q1a
);
878 if (--p
->ccount
== 0)
882 (*tt
.tt_set_token
)(p
->code
, p
->string
, p
->length
);
884 wwntoksave
-= tt
.tt_set_token_cost
;
888 ttwrite(p
->string
, p
->length
);
896 cc_token_compare(p1
, p2
)
897 struct cc
**p1
, **p2
;
899 return (*p2
)->bcount
- (*p1
)->bcount
;