]>
git.saurik.com Git - apple/shell_cmds.git/blob - window/compress.c
1 /* $NetBSD: compress.c,v 1.4 1997/11/21 08:35:56 lukem Exp $ */
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Edward Wang at The University of California, Berkeley.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 #include <sys/cdefs.h>
42 static char sccsid
[] = "@(#)compress.c 8.1 (Berkeley) 6/6/93";
44 __RCSID("$NetBSD: compress.c,v 1.4 1997/11/21 08:35:56 lukem Exp $");
59 /* tunable parameters */
65 int cc_token_max
= 8; /* <= TOKEN_MAX */
66 int cc_token_min
= 2; /* > tt.tt_put_token_cost */
70 int cc_bufsize
= 1024 * 3; /* XXX, or 80 * 24 * 2 */
82 char string
[TOKEN_MAX
];
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
;
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])
103 short cc_wthresholds
[TOKEN_MAX
+ 1];
104 #define wthresh(length) (cc_wthresholds[length])
105 #define wthreshp(weight, length) ((short) (weight) >= cc_wthresholds[length])
107 #define wthreshp(weight, length) (0)
111 short cc_wlimits
[TOKEN_MAX
+ 1];
112 #define wlimit(length) (cc_wlimits[length])
115 #define put_token_score(length) ((length) - tt.tt_put_token_cost)
117 int cc_score_adjustments
[TOKEN_MAX
+ 1][8]; /* XXX, 8 > max of cc_thresholds */
118 #define score_adjust(score, p) \
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; \
126 (score) += cc_score_adjustments[length][ccount]; \
129 int cc_initial_scores
[TOKEN_MAX
+ 1][8]; /* XXX, 8 > max of cc_thresholds */
131 struct cc cc_q0a
, cc_q0b
, cc_q1a
, cc_q1b
;
133 #define qinsert(p1, p2) \
135 struct cc *forw = (p1)->qforw; \
136 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); \
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, \
156 #define HSIZE (1 << H)
157 #define hash(h, c) (((((h) >> (H - 8)) | (h) << 8) ^ (c)) & (HSIZE - 1))
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 */
170 long cc_time
, cc_time0
;
172 char *cc_tt_ob
, *cc_tt_obe
;
174 int cc_compress
__P((struct cc
**, struct cc
**, char));
175 void cc_compress_cleanup
__P((struct cc
**, int));
176 void cc_compress_phase
__P((struct cc
**, int, struct cc
**, int));
177 void cc_compress_phase1
__P((struct cc
**, struct cc
**, int, int));
178 void cc_output_phase
__P((char *, struct cc
**, int));
179 int cc_sweep
__P((char *, int, struct cc
**, int));
180 void cc_sweep0
__P((char *, int, int));
181 int cc_sweep_phase
__P((char *, int, struct cc
**));
182 void cc_sweep_reverse
__P((struct cc
**, short *));
183 int cc_token_compare
__P((const void *, const void *));
191 if (tt
.tt_token_max
> cc_token_max
)
192 tt
.tt_token_max
= cc_token_max
;
193 if (tt
.tt_token_min
< cc_token_min
)
194 tt
.tt_token_min
= cc_token_min
;
195 if (tt
.tt_token_min
> tt
.tt_token_max
) {
199 if (tt
.tt_ntoken
> cc_ntoken
/ 2) /* not likely */
200 tt
.tt_ntoken
= cc_ntoken
/ 2;
201 #define C(x) (sizeof (x) / sizeof *(x))
202 for (i
= 0; i
< C(cc_thresholds
); i
++) {
203 int h
= i
- tt
.tt_put_token_cost
;
206 (tt
.tt_set_token_cost
+ 1 + h
- 1) / h
+ 1;
208 cc_thresholds
[i
] = 0;
210 for (i
= 0; i
< C(cc_score_adjustments
); i
++) {
211 int t
= cc_thresholds
[i
];
212 for (j
= 0; j
< C(*cc_score_adjustments
); j
++) {
214 cc_score_adjustments
[i
][j
] =
215 - (i
- tt
.tt_put_token_cost
);
217 cc_score_adjustments
[i
][j
] = 0;
221 * length * (ccount + 1) a
223 * set-token-cost + length +
224 * ccount * put-token-cost b
225 * the score adjustment is (b - a)
227 cc_score_adjustments
[i
][j
] =
228 tt
.tt_set_token_cost
+ i
+
229 j
* tt
.tt_put_token_cost
-
232 cc_initial_scores
[i
][j
] = 0;
235 * - (set-token-cost +
236 * (length - put-token-cost) -
237 * (length - put-token-cost) * ccount)
239 cc_initial_scores
[i
][j
] =
240 - (tt
.tt_set_token_cost
+
241 (i
- tt
.tt_put_token_cost
) -
242 (i
- tt
.tt_put_token_cost
) * j
);
246 for (i
= 1; i
< C(cc_wthresholds
); i
++) {
248 ((tt
.tt_set_token_cost
+ tt
.tt_put_token_cost
) / i
+
251 cc_wlimits
[i
] = cc_wthresholds
[i
] + cc_weight
;
255 if ((cc_output
= (struct cc
**)
256 malloc((unsigned) cc_bufsize
* sizeof *cc_output
)) == 0)
258 if ((cc_hashcodes
= (short *)
259 malloc((unsigned) cc_bufsize
* sizeof *cc_hashcodes
)) == 0)
261 if ((cc_htab
= (struct cc
**) malloc(HSIZE
* sizeof *cc_htab
)) == 0)
263 if ((cc_tokens
= (struct cc
**)
265 (cc_ntoken
+ tt
.tt_token_max
- tt
.tt_token_min
+ 1) *
266 sizeof *cc_tokens
)) == 0)
268 if ((cc_undo
= (struct cc_undo
*)
269 malloc((unsigned) cc_bufsize
* sizeof *cc_undo
)) == 0)
271 for (i
= tt
.tt_token_min
; i
<= tt
.tt_token_max
; i
++)
272 if ((cc_places
[i
] = (short *)
273 malloc((unsigned) cc_bufsize
* sizeof **cc_places
)) == 0)
275 cc_q0a
.qforw
= cc_q0a
.qback
= &cc_q0a
;
276 cc_q0b
.qforw
= cc_q0b
.qback
= &cc_q0b
;
277 cc_q1a
.qforw
= cc_q1a
.qback
= &cc_q1a
;
278 cc_q1b
.qforw
= cc_q1b
.qback
= &cc_q1b
;
279 if ((p
= (struct cc
*) malloc((unsigned) cc_ntoken
* sizeof *p
)) == 0)
281 for (i
= 0; i
< tt
.tt_ntoken
; i
++) {
284 p
->qback
= cc_q0a
.qback
;
290 for (; i
< cc_ntoken
; i
++) {
293 p
->qback
= cc_q1a
.qback
;
301 if ((cc_buffer
= malloc((unsigned) cc_bufsize
)) == 0)
313 tt_obp
= tt_ob
= cc_buffer
;
314 tt_obe
= tt_ob
+ cc_bufsize
;
315 tt
.tt_flush
= ccflush
;
317 cc_trace_fp
= fopen("window-trace", "a");
318 (void) fcntl(fileno(cc_trace_fp
), F_SETFD
, 1);
328 memset((char *) cc_htab
, 0, HSIZE
* sizeof *cc_htab
);
329 for (p
= cc_q0a
.qforw
; p
!= &cc_q0a
; p
= p
->qforw
)
331 for (p
= cc_q1a
.qforw
; p
!= &cc_q1a
; p
= p
->qforw
)
340 tt_obp
= tt_ob
= cc_tt_ob
;
343 if (cc_trace_fp
!= NULL
) {
344 (void) fclose(cc_trace_fp
);
352 int bufsize
= tt_obp
- tt_ob
;
355 if (tt_ob
!= cc_buffer
)
357 if (cc_trace_fp
!= NULL
) {
358 (void) fwrite(tt_ob
, 1, bufsize
, cc_trace_fp
);
359 (void) putc(-1, cc_trace_fp
);
362 (*tt
.tt_compress
)(1);
363 if (bufsize
< tt
.tt_token_min
) {
367 tt_obp
= tt_ob
= cc_tt_ob
;
371 n
= cc_sweep_phase(cc_buffer
, bufsize
, cc_tokens
);
372 cc_compress_phase(cc_output
, bufsize
, cc_tokens
, n
);
373 cc_output_phase(cc_buffer
, cc_output
, bufsize
);
375 tt_obp
= tt_ob
= cc_buffer
;
376 tt_obe
= cc_buffer
+ cc_bufsize
;
378 (*tt
.tt_compress
)(0);
379 tt
.tt_flush
= ccflush
;
383 cc_sweep_phase(buffer
, bufsize
, tokens
)
388 struct cc
**pp
= tokens
;
400 cc_sweep0(buffer
, bufsize
, tt
.tt_token_min
- 1);
406 for (i
= tt
.tt_token_min
; i
<= tt
.tt_token_max
; i
++) {
415 (void) fflush(stdout
);
418 n
= cc_sweep(buffer
, bufsize
, pp
, i
);
430 qinsertq(&cc_q1b
, &cc_q1a
);
433 printf("\n %d tokens, %d candidates\n",
442 cc_sweep0(buffer
, n
, length
)
450 short pc
= tt
.tt_padc
;
452 /* n and length are at least 1 */
457 if ((*hc
++ = *p
++) == pc
)
463 for (i
= n
--; --i
;) {
464 if ((c
= *p
++) == pc
|| *hc
< 0)
474 cc_sweep(buffer
, bufsize
, tokens
, length
)
484 short *places
= cc_places
[length
];
485 struct cc
**pp
= tokens
;
486 short threshold
= thresh(length
);
488 short wthreshold
= wthresh(length
);
489 short limit
= wlimit(length
);
492 short pc
= tt
.tt_padc
;
499 for (i
= 0; i
< bufsize
; i
++, time
++) {
506 if ((hh
= *hc1
) < 0 || c
== pc
) {
511 h
= cc_htab
+ (*hc1
++ = hash(hh
, c
));
514 for (p
= *h
; p
!= 0; p
= p
->hforw
)
515 if (p
->length
== (char) length
) {
516 char *p1
= p
->string
;
517 char *p2
= cp
- length
;
529 (p
->time
>= cc_time0
&& p
->length
== (char) length
))
532 if ((*p
->hback
= p
->hforw
) != 0)
533 p
->hforw
->hback
= p
->hback
;
535 char *p1
= p
->string
;
536 char *p2
= cp
- length
;
544 p
->weight
= cc_weight
;
550 if ((p
->hforw
= *h
) != 0)
551 p
->hforw
->hback
= &p
->hforw
;
560 } else if (p
->time
< cc_time0
) {
562 if ((p
->weight
+= p
->time
- time
) < 0)
563 p
->weight
= cc_weight
;
564 else if ((p
->weight
+= cc_weight
) > limit
)
575 if (p
->weight
>= wthreshold
) {
590 } else if (p
->time
+ length
> time
) {
592 * overlapping token, don't count as two and
593 * don't update time, but do adjust weight to offset
597 if (cc_weight
!= 0) { /* XXX */
598 p
->weight
+= time
- p
->time
;
599 if (!p
->flag
&& p
->weight
>= wthreshold
) {
606 places
[i
] = p
->places
;
610 if ((p
->weight
+= p
->time
- time
) < 0)
611 p
->weight
= cc_weight
;
612 else if ((p
->weight
+= cc_weight
) > limit
)
618 /* code must be < 0 if flag false here */
619 (p
->bcount
>= threshold
621 || p
->weight
>= wthreshold
628 places
[i
] = p
->places
;
632 if ((i
= pp
- tokens
) > 0) {
635 cc_sweep_reverse(tokens
, places
);
636 if (cc_sort
&& i
> 1) {
637 qsort((char *) tokens
, i
, sizeof *tokens
,
641 if ((i
= i
* cc_chop
/ 100) == 0)
651 cc_sweep_reverse(pp
, places
)
656 short front
, back
, t
;
658 while ((p
= *pp
++) != 0) {
661 /* the list is never empty */
666 } while ((t
= front
) >= 0);
672 cc_compress_phase(output
, bufsize
, tokens
, ntoken
)
680 memset((char *) output
, 0, bufsize
* sizeof *output
);
681 for (i
= 0; i
< cc_npass0
; i
++)
682 cc_compress_phase1(output
, tokens
, ntoken
, 0);
683 for (i
= 0; i
< cc_npass1
; i
++)
684 cc_compress_phase1(output
, tokens
, ntoken
, 1);
685 cc_compress_cleanup(output
, bufsize
);
689 cc_compress_phase1(output
, tokens
, ntoken
, flag
)
697 int nt
= 0, cc
= 0, nc
= 0;
707 while (pp
< tokens
+ ntoken
) {
718 printf(" (%d", (*pp
)->length
);
719 (void) fflush(stdout
);
722 pp
+= cc_compress(output
, pp
, flag
);
725 printf(" %dt %du %dc)", ntoken_stat
, ccount_stat
,
735 printf("\n total: (%dt %du %dc)\n", nt
, cc
, nc
);
742 cc_compress_cleanup(output
, bufsize
)
748 /* the previous output phase may have been interrupted */
749 qinsertq(&cc_q0b
, &cc_q0a
);
750 for (end
= output
+ bufsize
; output
< end
;) {
753 if ((p
= *output
) == 0) {
759 } else if (p
->code
>= 0) {
762 } else if (p
->ccount
== 0) {
764 } else if (p
->ccount
>= thresh(length
)
766 || wthreshp(p
->weight
, length
)
779 cc_compress(output
, tokens
, flag
)
784 struct cc
**pp
= tokens
;
785 struct cc
*p
= *pp
++;
786 int length
= p
->length
;
787 int threshold
= thresh(length
);
789 short wthreshold
= wthresh(length
);
791 short *places
= cc_places
[length
];
792 int *initial_scores
= cc_initial_scores
[length
];
793 int initial_score0
= put_token_score(length
);
797 struct cc_undo
*undop
;
805 if ((short) ccount
>= p
->bcount
)
807 if (p
->code
>= 0 || ccount
>= threshold
)
810 else if (p
->weight
>= wthreshold
)
811 /* allow one fewer match than normal */
812 /* XXX, should adjust for ccount */
813 score
= - tt
.tt_set_token_cost
;
816 score
= initial_scores
[ccount
];
821 for (i
= p
->places
; i
>= 0; i
= places
[i
]) {
824 struct cc
**ip
= output
+ i
;
825 int score0
= initial_score0
;
826 struct cc
**iip
= ip
+ length
;
827 struct cc_undo
*undop1
= undop
;
829 if ((x
= *(jp
= ip
)) != 0)
831 while (--jp
>= output
)
832 if ((x
= *jp
) != 0) {
833 if (jp
+ x
->length
> ip
)
839 if ((x
= *jp
) == 0) {
854 score_adjust(score0
, x
);
855 if (score0
< 0 && flag
)
867 while (--undop
>= undop1
)
868 if ((*undop
->pos
= x
= undop
->val
))
874 ccount_stat
+= ccount
- p
->ccount
;
876 ncover_stat
+= ncover
;
880 struct cc_undo
*u
= cc_undo
;
881 while (--undop
>= u
) {
883 if ((*undop
->pos
= x
= undop
->val
))
887 } while ((p
= *pp
++) != 0);
892 cc_output_phase(buffer
, output
, bufsize
)
900 for (i
= 0; i
< bufsize
;) {
901 if ((p
= output
[i
]) == 0) {
904 } else if (p
->code
>= 0) {
905 if (--p
->ccount
== 0)
907 (*tt
.tt_put_token
)(p
->code
, p
->string
, p
->length
);
909 wwntoksave
+= put_token_score(p
->length
);
911 } else if ((p1
= cc_q0a
.qback
) != &cc_q0a
) {
914 qinsert(p1
, &cc_q1a
);
915 if (--p
->ccount
== 0)
919 (*tt
.tt_set_token
)(p
->code
, p
->string
, p
->length
);
921 wwntoksave
-= tt
.tt_set_token_cost
;
925 ttwrite(p
->string
, p
->length
);
934 cc_token_compare(p1
, p2
)
937 const struct cc
**vp1
= (void *)p1
;
938 const struct cc
**vp2
= (void *)p2
;
939 return (*vp2
)->bcount
- (*vp1
)->bcount
;