1 /* @(#)compress.c 8.1 (Berkeley) 6/6/93 */
2 /* $NetBSD: compress.c,v 1.7 2009/04/14 08:50:06 lukem Exp $ */
5 * Copyright (c) 1989, 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Edward Wang at The University of California, Berkeley.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 /* tunable parameters */
53 int cc_token_max
= 8; /* <= TOKEN_MAX */
54 int cc_token_min
= 2; /* > tt.tt_put_token_cost */
58 int cc_bufsize
= 1024 * 3; /* XXX, or 80 * 24 * 2 */
70 char string
[TOKEN_MAX
];
76 long time
; /* time last seen */
77 short bcount
; /* count in this buffer */
78 short ccount
; /* count in compression */
79 short places
; /* places in the buffer */
80 short code
; /* token code */
81 struct cc
*qforw
, *qback
;
82 struct cc
*hforw
, **hback
;
85 short cc_thresholds
[TOKEN_MAX
+ 1];
86 #define thresh(length) (cc_thresholds[length])
87 #define threshp(code, count, length) \
88 ((code) >= 0 || (short) (count) >= cc_thresholds[length])
91 short cc_wthresholds
[TOKEN_MAX
+ 1];
92 #define wthresh(length) (cc_wthresholds[length])
93 #define wthreshp(weight, length) ((short) (weight) >= cc_wthresholds[length])
95 #define wthreshp(weight, length) (0)
99 short cc_wlimits
[TOKEN_MAX
+ 1];
100 #define wlimit(length) (cc_wlimits[length])
103 #define put_token_score(length) ((length) - tt.tt_put_token_cost)
105 int cc_score_adjustments
[TOKEN_MAX
+ 1][8]; /* XXX, 8 > max of cc_thresholds */
106 #define score_adjust(score, p) \
108 int _length = (p)->length; \
109 int _ccount = (p)->ccount; \
110 if (threshp((p)->code, _ccount, _length) || \
111 wthreshp((p)->weight, _length)) /* XXX */ \
112 (score) -= _length - tt.tt_put_token_cost; \
114 (score) += cc_score_adjustments[_length][_ccount]; \
117 int cc_initial_scores
[TOKEN_MAX
+ 1][8]; /* XXX, 8 > max of cc_thresholds */
119 struct cc cc_q0a
, cc_q0b
, cc_q1a
, cc_q1b
;
121 #define qinsert(p1, p2) \
123 struct cc *forw = (p1)->qforw; \
124 struct cc *back = (p1)->qback; \
125 back->qforw = forw; \
126 forw->qback = back; \
127 forw = (p2)->qforw; \
128 (p1)->qforw = forw; \
129 forw->qback = (p1); \
130 (p2)->qforw = (p1); \
131 (p1)->qback = (p2); \
134 #define qinsertq(q, p) \
135 ((q)->qforw == (q) ? 0 : \
136 ((q)->qback->qforw = (p)->qforw, \
137 (p)->qforw->qback = (q)->qback, \
138 (q)->qforw->qback = (p), \
139 (p)->qforw = (q)->qforw, \
144 #define HSIZE (1 << H)
145 #define hash(h, c) (((((h) >> (H - 8)) | (h) << 8) ^ (c)) & (HSIZE - 1))
148 struct cc
**cc_output
; /* the output array */
149 short *cc_places
[TOKEN_MAX
+ 1];
150 short *cc_hashcodes
; /* for computing hashcodes */
151 struct cc
**cc_htab
; /* the hash table */
152 struct cc
**cc_tokens
; /* holds all the active tokens */
158 long cc_time
, cc_time0
;
160 char *cc_tt_ob
, *cc_tt_obe
;
162 int cc_compress(struct cc
**, struct cc
**, char);
163 void cc_compress_cleanup(struct cc
**, int);
164 void cc_compress_phase(struct cc
**, int, struct cc
**, int);
165 void cc_compress_phase1(struct cc
**, struct cc
**, int, int);
166 void cc_output_phase(char *, struct cc
**, int);
167 int cc_sweep(char *, int, struct cc
**, int);
168 void cc_sweep0(char *, int, int);
169 int cc_sweep_phase(char *, int, struct cc
**);
170 void cc_sweep_reverse(struct cc
**, short *);
171 int cc_token_compare(const void *, const void *);
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
) {
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
< (int)C(cc_thresholds
); i
++) {
191 int h
= i
- tt
.tt_put_token_cost
;
194 (tt
.tt_set_token_cost
+ 1 + h
- 1) / h
+ 1;
196 cc_thresholds
[i
] = 0;
198 for (i
= 0; i
< (int)C(cc_score_adjustments
); i
++) {
199 int t
= cc_thresholds
[i
];
200 for (j
= 0; j
< (int)C(*cc_score_adjustments
); j
++) {
202 cc_score_adjustments
[i
][j
] =
203 - (i
- tt
.tt_put_token_cost
);
205 cc_score_adjustments
[i
][j
] = 0;
209 * length * (ccount + 1) a
211 * set-token-cost + length +
212 * ccount * put-token-cost b
213 * the score adjustment is (b - a)
215 cc_score_adjustments
[i
][j
] =
216 tt
.tt_set_token_cost
+ i
+
217 j
* tt
.tt_put_token_cost
-
220 cc_initial_scores
[i
][j
] = 0;
223 * - (set-token-cost +
224 * (length - put-token-cost) -
225 * (length - put-token-cost) * ccount)
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
);
234 for (i
= 1; i
< C(cc_wthresholds
); i
++) {
236 ((tt
.tt_set_token_cost
+ tt
.tt_put_token_cost
) / i
+
239 cc_wlimits
[i
] = cc_wthresholds
[i
] + cc_weight
;
243 if ((cc_output
= (struct cc
**)
244 malloc((unsigned) cc_bufsize
* sizeof *cc_output
)) == NULL
)
246 if ((cc_hashcodes
= (short *)
247 malloc((unsigned) cc_bufsize
* sizeof *cc_hashcodes
)) == NULL
)
249 if ((cc_htab
= (struct cc
**) malloc(HSIZE
* sizeof *cc_htab
)) == NULL
)
251 if ((cc_tokens
= (struct cc
**)
253 (cc_ntoken
+ tt
.tt_token_max
- tt
.tt_token_min
+ 1) *
254 sizeof *cc_tokens
)) == NULL
)
256 if ((cc_undo
= (struct cc_undo
*)
257 malloc((unsigned) cc_bufsize
* sizeof *cc_undo
)) == NULL
)
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
)) == NULL
)
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
)) == NULL
)
269 for (i
= 0; i
< tt
.tt_ntoken
; i
++) {
272 p
->qback
= cc_q0a
.qback
;
278 for (; i
< cc_ntoken
; i
++) {
281 p
->qback
= cc_q1a
.qback
;
289 if ((cc_buffer
= malloc((unsigned) cc_bufsize
)) == NULL
)
301 tt_obp
= tt_ob
= cc_buffer
;
302 tt_obe
= tt_ob
+ cc_bufsize
;
303 tt
.tt_flush
= ccflush
;
305 cc_trace_fp
= fopen("window-trace", "a");
306 (void) fcntl(fileno(cc_trace_fp
), F_SETFD
, 1);
316 memset((char *) cc_htab
, 0, HSIZE
* sizeof *cc_htab
);
317 for (p
= cc_q0a
.qforw
; p
!= &cc_q0a
; p
= p
->qforw
)
319 for (p
= cc_q1a
.qforw
; p
!= &cc_q1a
; p
= p
->qforw
)
328 tt_obp
= tt_ob
= cc_tt_ob
;
331 if (cc_trace_fp
!= NULL
) {
332 (void) fclose(cc_trace_fp
);
340 int bufsize
= tt_obp
- tt_ob
;
343 if (tt_ob
!= cc_buffer
)
345 if (cc_trace_fp
!= NULL
) {
346 (void) fwrite(tt_ob
, 1, bufsize
, cc_trace_fp
);
347 (void) putc(-1, cc_trace_fp
);
350 (*tt
.tt_compress
)(1);
351 if (bufsize
< tt
.tt_token_min
) {
355 tt_obp
= tt_ob
= cc_tt_ob
;
359 n
= cc_sweep_phase(cc_buffer
, bufsize
, cc_tokens
);
360 cc_compress_phase(cc_output
, bufsize
, cc_tokens
, n
);
361 cc_output_phase(cc_buffer
, cc_output
, bufsize
);
363 tt_obp
= tt_ob
= cc_buffer
;
364 tt_obe
= cc_buffer
+ cc_bufsize
;
366 (*tt
.tt_compress
)(0);
367 tt
.tt_flush
= ccflush
;
371 cc_sweep_phase(char *buffer
, int bufsize
, struct cc
**tokens
)
373 struct cc
**pp
= tokens
;
385 cc_sweep0(buffer
, bufsize
, tt
.tt_token_min
- 1);
391 for (i
= tt
.tt_token_min
; i
<= tt
.tt_token_max
; i
++) {
400 (void) fflush(stdout
);
403 n
= cc_sweep(buffer
, bufsize
, pp
, i
);
415 qinsertq(&cc_q1b
, &cc_q1a
);
418 printf("\n %d tokens, %d candidates\n",
427 cc_sweep0(char *buffer
, int n
, int length
)
433 short pc
= tt
.tt_padc
;
435 /* n and length are at least 1 */
440 if ((*hc
++ = *p
++) == pc
)
446 for (i
= n
--; --i
;) {
447 if ((c
= *p
++) == pc
|| *hc
< 0)
457 cc_sweep(char *buffer
, int bufsize
, struct cc
**tokens
, int length
)
463 short *places
= cc_places
[length
];
464 struct cc
**pp
= tokens
;
465 short threshold
= thresh(length
);
467 short wthreshold
= wthresh(length
);
468 short limit
= wlimit(length
);
471 short pc
= tt
.tt_padc
;
478 for (i
= 0; i
< bufsize
; i
++, time0
++) {
485 if ((hh
= *hc1
) < 0 || c
== pc
) {
490 h
= cc_htab
+ (*hc1
++ = hash(hh
, c
));
493 for (p
= *h
; p
!= NULL
; p
= p
->hforw
)
494 if (p
->length
== (char) length
) {
495 char *p1
= p
->string
;
496 char *p2
= cp
- length
;
508 (p
->time
>= cc_time0
&& p
->length
== (char) length
))
510 if (p
->hback
!= NULL
)
511 if ((*p
->hback
= p
->hforw
) != NULL
)
512 p
->hforw
->hback
= p
->hback
;
514 char *p1
= p
->string
;
515 char *p2
= cp
- length
;
523 p
->weight
= cc_weight
;
529 if ((p
->hforw
= *h
) != NULL
)
530 p
->hforw
->hback
= &p
->hforw
;
539 } else if (p
->time
< cc_time0
) {
541 if ((p
->weight
+= p
->time
- time0
) < 0)
542 p
->weight
= cc_weight
;
543 else if ((p
->weight
+= cc_weight
) > limit
)
554 if (p
->weight
>= wthreshold
) {
569 } else if (p
->time
+ length
> time0
) {
571 * overlapping token, don't count as two and
572 * don't update time0, but do adjust weight to offset
576 if (cc_weight
!= 0) { /* XXX */
577 p
->weight
+= time0
- p
->time
;
578 if (!p
->flag
&& p
->weight
>= wthreshold
) {
585 places
[i
] = p
->places
;
589 if ((p
->weight
+= p
->time
- time0
) < 0)
590 p
->weight
= cc_weight
;
591 else if ((p
->weight
+= cc_weight
) > limit
)
597 /* code must be < 0 if flag false here */
598 (p
->bcount
>= threshold
600 || p
->weight
>= wthreshold
607 places
[i
] = p
->places
;
611 if ((i
= pp
- tokens
) > 0) {
614 cc_sweep_reverse(tokens
, places
);
615 if (cc_sort
&& i
> 1) {
616 qsort((char *) tokens
, i
, sizeof *tokens
,
620 if ((i
= i
* cc_chop
/ 100) == 0)
630 cc_sweep_reverse(struct cc
**pp
, short int *places
)
635 while ((p
= *pp
++) != NULL
) {
638 /* the list is never empty */
643 } while ((t
= frnt
) >= 0);
649 cc_compress_phase(struct cc
**output
, int bufsize
, struct cc
**tokens
, int ntoken
)
653 memset((char *) output
, 0, bufsize
* sizeof *output
);
654 for (i
= 0; i
< cc_npass0
; i
++)
655 cc_compress_phase1(output
, tokens
, ntoken
, 0);
656 for (i
= 0; i
< cc_npass1
; i
++)
657 cc_compress_phase1(output
, tokens
, ntoken
, 1);
658 cc_compress_cleanup(output
, bufsize
);
662 cc_compress_phase1(struct cc
**output
, struct cc
**tokens
, int ntoken
, int flag
)
667 int nt
= 0, cc
= 0, nc
= 0;
677 while (pp
< tokens
+ ntoken
) {
688 printf(" (%d", (*pp
)->length
);
689 (void) fflush(stdout
);
692 pp
+= cc_compress(output
, pp
, flag
);
695 printf(" %dt %du %dc)", ntoken_stat
, ccount_stat
,
705 printf("\n total: (%dt %du %dc)\n", nt
, cc
, nc
);
712 cc_compress_cleanup(struct cc
**output
, int bufsize
)
716 /* the previous output phase may have been interrupted */
717 qinsertq(&cc_q0b
, &cc_q0a
);
718 for (end
= output
+ bufsize
; output
< end
;) {
721 if ((p
= *output
) == NULL
) {
727 } else if (p
->code
>= 0) {
730 } else if (p
->ccount
== 0) {
732 } else if (p
->ccount
>= thresh(length
)
734 || wthreshp(p
->weight
, length
)
747 cc_compress(struct cc
**output
, struct cc
**tokens
, char flag
)
749 struct cc
**pp
= tokens
;
750 struct cc
*p
= *pp
++;
751 int length
= p
->length
;
752 int threshold
= thresh(length
);
754 short wthreshold
= wthresh(length
);
756 short *places
= cc_places
[length
];
757 int *initial_scores
= cc_initial_scores
[length
];
758 int initial_score0
= put_token_score(length
);
762 struct cc_undo
*undop
;
770 if ((short) ccount
>= p
->bcount
)
772 if (p
->code
>= 0 || ccount
>= threshold
)
775 else if (p
->weight
>= wthreshold
)
776 /* allow one fewer match than normal */
777 /* XXX, should adjust for ccount */
778 score
= - tt
.tt_set_token_cost
;
781 score
= initial_scores
[ccount
];
786 for (i
= p
->places
; i
>= 0; i
= places
[i
]) {
789 struct cc
**ip
= output
+ i
;
790 int score0
= initial_score0
;
791 struct cc
**iip
= ip
+ length
;
792 struct cc_undo
*undop1
= undop
;
794 if ((x
= *(jp
= ip
)) != NULL
)
796 while (--jp
>= output
)
797 if ((x
= *jp
) != NULL
) {
798 if (jp
+ x
->length
> ip
)
804 if ((x
= *jp
) == NULL
) {
819 score_adjust(score0
, x
);
820 if (score0
< 0 && flag
)
832 while (--undop
>= undop1
)
833 if ((*undop
->pos
= x
= undop
->val
))
839 ccount_stat
+= ccount
- p
->ccount
;
841 ncover_stat
+= ncover
;
845 struct cc_undo
*u
= cc_undo
;
846 while (--undop
>= u
) {
848 if ((*undop
->pos
= x
= undop
->val
))
852 } while ((p
= *pp
++) != NULL
);
857 cc_output_phase(char *buffer
, struct cc
**output
, int bufsize
)
862 for (i
= 0; i
< bufsize
;) {
863 if ((p
= output
[i
]) == NULL
) {
866 } else if (p
->code
>= 0) {
867 if (--p
->ccount
== 0)
869 (*tt
.tt_put_token
)(p
->code
, p
->string
, p
->length
);
871 wwntoksave
+= put_token_score(p
->length
);
873 } else if ((p1
= cc_q0a
.qback
) != &cc_q0a
) {
876 qinsert(p1
, &cc_q1a
);
877 if (--p
->ccount
== 0)
881 (*tt
.tt_set_token
)(p
->code
, p
->string
, p
->length
);
883 wwntoksave
-= tt
.tt_set_token_cost
;
887 ttwrite(p
->string
, p
->length
);
896 cc_token_compare(const void *p1
, const void *p2
)
898 const struct cc
* const * vp1
= p1
;
899 const struct cc
* const * vp2
= p2
;
900 return (*vp2
)->bcount
- (*vp1
)->bcount
;