1 | /**************************************************************************
|
---|
2 | *
|
---|
3 | * locallib.c -- Misc functions
|
---|
4 | * Copyright (C) 1994 Gary Eddy, Alistair Moffat and Neil Sharman
|
---|
5 | *
|
---|
6 | * This program is free software; you can redistribute it and/or modify
|
---|
7 | * it under the terms of the GNU General Public License as published by
|
---|
8 | * the Free Software Foundation; either version 2 of the License, or
|
---|
9 | * (at your option) any later version.
|
---|
10 | *
|
---|
11 | * This program is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
14 | * GNU General Public License for more details.
|
---|
15 | *
|
---|
16 | * You should have received a copy of the GNU General Public License
|
---|
17 | * along with this program; if not, write to the Free Software
|
---|
18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
19 | *
|
---|
20 | * $Id: locallib.c 439 1999-08-10 21:23:37Z sjboddie $
|
---|
21 | *
|
---|
22 | **************************************************************************/
|
---|
23 |
|
---|
24 | #include "sysfuncs.h"
|
---|
25 |
|
---|
26 | #include "memlib.h"
|
---|
27 | #include "bitio_gen.h"
|
---|
28 | #include "huffman.h"
|
---|
29 | #include "netorder.h" /* [RPAP - Jan 97: Endian Ordering] */
|
---|
30 |
|
---|
31 | #include "locallib.h"
|
---|
32 | #include "text.h"
|
---|
33 |
|
---|
34 | /*
|
---|
35 | $Log$
|
---|
36 | Revision 1.1 1999/08/10 21:18:02 sjboddie
|
---|
37 | renamed mg-1.3d directory mg
|
---|
38 |
|
---|
39 | Revision 1.1 1998/11/17 09:34:49 rjmcnab
|
---|
40 | *** empty log message ***
|
---|
41 |
|
---|
42 | * Revision 1.3 1994/10/20 03:56:51 tes
|
---|
43 | * I have rewritten the boolean query optimiser and abstracted out the
|
---|
44 | * components of the boolean query.
|
---|
45 | *
|
---|
46 | * Revision 1.2 1994/09/20 04:41:38 tes
|
---|
47 | * For version 1.1
|
---|
48 | *
|
---|
49 | */
|
---|
50 |
|
---|
51 | static char *RCSID = "$Id: locallib.c 439 1999-08-10 21:23:37Z sjboddie $";
|
---|
52 |
|
---|
53 |
|
---|
54 | int
|
---|
55 | vecentropy (int *A, int n)
|
---|
56 | {
|
---|
57 | int total = 0, i;
|
---|
58 | double bits = 0.0;
|
---|
59 |
|
---|
60 | for (i = 0; i < n; i++)
|
---|
61 | total += A[i];
|
---|
62 | for (i = 0; i < n; i++)
|
---|
63 | if (A[i])
|
---|
64 | bits += A[i] * log2 (1.0 * total / A[i]);
|
---|
65 | return ((int) (bits + 0.9999));
|
---|
66 |
|
---|
67 | }
|
---|
68 |
|
---|
69 | static void
|
---|
70 | siftdown (int *A, int i, int n)
|
---|
71 | {
|
---|
72 | int ms, t;
|
---|
73 |
|
---|
74 | while (2 * i + 1 <= n - 1)
|
---|
75 | {
|
---|
76 | if (2 * i + 1 == n - 1 || A[2 * i + 1] < A[2 * i + 2])
|
---|
77 | ms = 2 * i + 1;
|
---|
78 | else
|
---|
79 | ms = 2 * i + 2;
|
---|
80 | if (A[i] > A[ms])
|
---|
81 | {
|
---|
82 | t = A[i];
|
---|
83 | A[i] = A[ms];
|
---|
84 | A[ms] = t;
|
---|
85 | }
|
---|
86 | i = ms;
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | static void
|
---|
91 | buildheap (int *A, int n)
|
---|
92 | {
|
---|
93 | int i;
|
---|
94 | for (i = n / 2 - 1; i >= 0; i--)
|
---|
95 | siftdown (A, i, n);
|
---|
96 | }
|
---|
97 |
|
---|
98 | int
|
---|
99 | huffcodebits (unsigned long *A, int n)
|
---|
100 | {
|
---|
101 | int i, j, tot = 0, bits = 0, v1, v2, *B;
|
---|
102 |
|
---|
103 | B = (int *) Xmalloc (n * sizeof (int));
|
---|
104 | j = 0;
|
---|
105 | for (i = 0; i < n; i++)
|
---|
106 | {
|
---|
107 | if (A[i])
|
---|
108 | {
|
---|
109 | tot += (B[j++] = A[i]);
|
---|
110 | }
|
---|
111 | }
|
---|
112 | n = j;
|
---|
113 |
|
---|
114 | if (n == 0 || tot == 0)
|
---|
115 | return (0);
|
---|
116 |
|
---|
117 | buildheap (B, n);
|
---|
118 | while (n > 1)
|
---|
119 | {
|
---|
120 | v1 = B[0];
|
---|
121 | B[0] = B[n - 1];
|
---|
122 | n--;
|
---|
123 | siftdown (B, 0, n);
|
---|
124 | v2 = B[0];
|
---|
125 | B[0] = v1 + v2;
|
---|
126 | siftdown (B, 0, n);
|
---|
127 | bits += v1 + v2;
|
---|
128 | }
|
---|
129 | Xfree (B);
|
---|
130 | return (bits);
|
---|
131 | }
|
---|
132 |
|
---|
133 |
|
---|
134 | int
|
---|
135 | modelbits (unsigned long *A, int n)
|
---|
136 | {
|
---|
137 | int i, bits = 0, last, N = 0;
|
---|
138 |
|
---|
139 | last = -1;
|
---|
140 | for (i = 0; i < n; i++)
|
---|
141 | {
|
---|
142 | if (A[i])
|
---|
143 | {
|
---|
144 | bits += BIO_Gamma_Length (i - last) + BIO_Gamma_Length (A[i]);
|
---|
145 | last = i;
|
---|
146 | N++;
|
---|
147 | }
|
---|
148 | }
|
---|
149 | if (N == 0)
|
---|
150 | return (0);
|
---|
151 | bits += BIO_Gamma_Length (N);
|
---|
152 | return (bits);
|
---|
153 | }
|
---|
154 |
|
---|
155 |
|
---|
156 |
|
---|
157 |
|
---|
158 |
|
---|
159 | /* Find the next prime number larger than p */
|
---|
160 | int
|
---|
161 | prime (int p)
|
---|
162 | {
|
---|
163 | int limit;
|
---|
164 | p += (p & 1) + 1;
|
---|
165 | limit = (int) sqrt ((double) p) + 1;
|
---|
166 | do
|
---|
167 | {
|
---|
168 | int j;
|
---|
169 | while (limit * limit < p)
|
---|
170 | limit++;
|
---|
171 | for (j = 3; j <= limit && p % j; j += 2)
|
---|
172 | ; /* NULLBODY */
|
---|
173 | if (j > limit)
|
---|
174 | break;
|
---|
175 | p += 2;
|
---|
176 | }
|
---|
177 | while (1);
|
---|
178 | return (p);
|
---|
179 | }
|
---|
180 |
|
---|
181 |
|
---|
182 |
|
---|
183 | int
|
---|
184 | Read_cdh (FILE * f, compression_dict_header * cdh, u_long * mem,
|
---|
185 | u_long * disk)
|
---|
186 | {
|
---|
187 | if (disk)
|
---|
188 | (*disk) += sizeof (*cdh);
|
---|
189 |
|
---|
190 | /* [RPAP - Jan 97: Endian Ordering] */
|
---|
191 | if (fread (cdh, sizeof (*cdh), 1, f) == 1)
|
---|
192 | {
|
---|
193 | int i;
|
---|
194 | NTOHUL(cdh->dict_type);
|
---|
195 | NTOHUL(cdh->novel_method);
|
---|
196 | for (i = 0; i < TEXT_PARAMS; i++)
|
---|
197 | NTOHUL(cdh->params[i]);
|
---|
198 | NTOHUL(cdh->num_words[0]);
|
---|
199 | NTOHUL(cdh->num_words[1]);
|
---|
200 | NTOHUL(cdh->num_word_chars[0]);
|
---|
201 | NTOHUL(cdh->num_word_chars[1]);
|
---|
202 | NTOHUL(cdh->lookback);
|
---|
203 | return 0;
|
---|
204 | }
|
---|
205 | else
|
---|
206 | return -1;
|
---|
207 | }
|
---|
208 |
|
---|
209 |
|
---|
210 | int
|
---|
211 | F_Read_cdh (File * f, compression_dict_header * cdh, u_long * mem,
|
---|
212 | u_long * disk)
|
---|
213 | {
|
---|
214 | if (disk)
|
---|
215 | (*disk) += sizeof (*cdh);
|
---|
216 |
|
---|
217 | /* [RPAP - Jan 97: Endian Ordering] */
|
---|
218 | if (Fread (cdh, sizeof (*cdh), 1, f) == 1)
|
---|
219 | {
|
---|
220 | int i;
|
---|
221 | NTOHUL(cdh->dict_type);
|
---|
222 | NTOHUL(cdh->novel_method);
|
---|
223 | for (i = 0; i < TEXT_PARAMS; i++)
|
---|
224 | NTOHUL(cdh->params[i]);
|
---|
225 | NTOHUL(cdh->num_words[0]);
|
---|
226 | NTOHUL(cdh->num_words[1]);
|
---|
227 | NTOHUL(cdh->num_word_chars[0]);
|
---|
228 | NTOHUL(cdh->num_word_chars[1]);
|
---|
229 | NTOHUL(cdh->lookback);
|
---|
230 | return 0;
|
---|
231 | }
|
---|
232 | else
|
---|
233 | return -1;
|
---|
234 | }
|
---|
235 |
|
---|
236 |
|
---|
237 | int
|
---|
238 | Read_cfh (FILE * f, comp_frags_header * cfh, u_long * mem, u_long * disk)
|
---|
239 | {
|
---|
240 | int i; /* [RPAP - Jan 97: Endian Ordering] */
|
---|
241 |
|
---|
242 | if (Read_Huffman_Data (f, &cfh->hd, mem, disk) == -1)
|
---|
243 | return -1;
|
---|
244 | if (fread (&cfh->uncompressed_size,
|
---|
245 | sizeof (cfh->uncompressed_size), 1, f) != 1)
|
---|
246 | return -1;
|
---|
247 | NTOHUL(cfh->uncompressed_size); /* [RPAP - Jan 97: Endian Ordering] */
|
---|
248 |
|
---|
249 | if (disk)
|
---|
250 | (*disk) += sizeof (cfh->uncompressed_size);
|
---|
251 |
|
---|
252 | bzero ((char *) cfh->huff_words_size, sizeof (cfh->huff_words_size));
|
---|
253 |
|
---|
254 | if (fread (cfh->huff_words_size + cfh->hd.mincodelen,
|
---|
255 | sizeof (*cfh->huff_words_size),
|
---|
256 | cfh->hd.maxcodelen - cfh->hd.mincodelen + 1, f) !=
|
---|
257 | cfh->hd.maxcodelen - cfh->hd.mincodelen + 1)
|
---|
258 | return -1;
|
---|
259 |
|
---|
260 | /* [RPAP - Jan 97: Endian Ordering] */
|
---|
261 | for (i = cfh->hd.mincodelen; i < cfh->hd.maxcodelen + 1; i++)
|
---|
262 | NTOHUL(cfh->huff_words_size[i]);
|
---|
263 |
|
---|
264 | if (disk)
|
---|
265 | (*disk) += sizeof (*cfh->huff_words_size) *
|
---|
266 | (cfh->hd.maxcodelen - cfh->hd.mincodelen + 1);
|
---|
267 | return 0;
|
---|
268 | }
|
---|
269 |
|
---|
270 |
|
---|
271 |
|
---|
272 | int
|
---|
273 | F_Read_cfh (File * f, comp_frags_header * cfh, u_long * mem, u_long * disk)
|
---|
274 | {
|
---|
275 | int i; /* [RPAP - Jan 97: Endian Ordering] */
|
---|
276 |
|
---|
277 | if (F_Read_Huffman_Data (f, &cfh->hd, mem, disk) == -1)
|
---|
278 | return -1;
|
---|
279 | if (Fread (&cfh->uncompressed_size,
|
---|
280 | sizeof (cfh->uncompressed_size), 1, f) != 1)
|
---|
281 | return -1;
|
---|
282 | NTOHUL(cfh->uncompressed_size); /* [RPAP - Jan 97: Endian Ordering] */
|
---|
283 |
|
---|
284 | if (disk)
|
---|
285 | (*disk) += sizeof (cfh->uncompressed_size);
|
---|
286 |
|
---|
287 | bzero ((char *) cfh->huff_words_size, sizeof (cfh->huff_words_size));
|
---|
288 |
|
---|
289 | if (Fread (cfh->huff_words_size + cfh->hd.mincodelen,
|
---|
290 | sizeof (*cfh->huff_words_size),
|
---|
291 | cfh->hd.maxcodelen - cfh->hd.mincodelen + 1, f) !=
|
---|
292 | cfh->hd.maxcodelen - cfh->hd.mincodelen + 1)
|
---|
293 | return -1;
|
---|
294 |
|
---|
295 | /* [RPAP - Jan 97: Endian Ordering] */
|
---|
296 | for (i = cfh->hd.mincodelen; i < cfh->hd.maxcodelen + 1; i++)
|
---|
297 | NTOHUL(cfh->huff_words_size[i]);
|
---|
298 |
|
---|
299 | if (disk)
|
---|
300 | (*disk) += sizeof (*cfh->huff_words_size) *
|
---|
301 | (cfh->hd.maxcodelen - cfh->hd.mincodelen + 1);
|
---|
302 |
|
---|
303 | return 0;
|
---|
304 | }
|
---|