1 | /**************************************************************************
|
---|
2 | *
|
---|
3 | * locallib.cpp -- 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.cpp 856 2000-01-14 02:26:25Z 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 2000/01/14 02:26:08 sjboddie
|
---|
37 | Rodgers new C++ mg
|
---|
38 |
|
---|
39 | Revision 1.1 1999/10/11 02:57:43 cs025
|
---|
40 | Base install of MG-PP
|
---|
41 |
|
---|
42 | Revision 1.1 1999/08/10 21:18:02 sjboddie
|
---|
43 | renamed mg-1.3d directory mg
|
---|
44 |
|
---|
45 | Revision 1.1 1998/11/17 09:34:49 rjmcnab
|
---|
46 | *** empty log message ***
|
---|
47 |
|
---|
48 | * Revision 1.3 1994/10/20 03:56:51 tes
|
---|
49 | * I have rewritten the boolean query optimiser and abstracted out the
|
---|
50 | * components of the boolean query.
|
---|
51 | *
|
---|
52 | * Revision 1.2 1994/09/20 04:41:38 tes
|
---|
53 | * For version 1.1
|
---|
54 | *
|
---|
55 | */
|
---|
56 |
|
---|
57 |
|
---|
58 | int
|
---|
59 | vecentropy (int *A, int n)
|
---|
60 | {
|
---|
61 | int total = 0, i;
|
---|
62 | double bits = 0.0;
|
---|
63 |
|
---|
64 | for (i = 0; i < n; i++)
|
---|
65 | total += A[i];
|
---|
66 | for (i = 0; i < n; i++)
|
---|
67 | if (A[i])
|
---|
68 | bits += A[i] * log2 (1.0 * total / A[i]);
|
---|
69 | return ((int) (bits + 0.9999));
|
---|
70 |
|
---|
71 | }
|
---|
72 |
|
---|
73 | static void
|
---|
74 | siftdown (int *A, int i, int n)
|
---|
75 | {
|
---|
76 | int ms, t;
|
---|
77 |
|
---|
78 | while (2 * i + 1 <= n - 1)
|
---|
79 | {
|
---|
80 | if (2 * i + 1 == n - 1 || A[2 * i + 1] < A[2 * i + 2])
|
---|
81 | ms = 2 * i + 1;
|
---|
82 | else
|
---|
83 | ms = 2 * i + 2;
|
---|
84 | if (A[i] > A[ms])
|
---|
85 | {
|
---|
86 | t = A[i];
|
---|
87 | A[i] = A[ms];
|
---|
88 | A[ms] = t;
|
---|
89 | }
|
---|
90 | i = ms;
|
---|
91 | }
|
---|
92 | }
|
---|
93 |
|
---|
94 | static void
|
---|
95 | buildheap (int *A, int n)
|
---|
96 | {
|
---|
97 | int i;
|
---|
98 | for (i = n / 2 - 1; i >= 0; i--)
|
---|
99 | siftdown (A, i, n);
|
---|
100 | }
|
---|
101 |
|
---|
102 | int
|
---|
103 | huffcodebits (unsigned long *A, int n)
|
---|
104 | {
|
---|
105 | int i, j, tot = 0, bits = 0, v1, v2, *B;
|
---|
106 |
|
---|
107 | B = (int *) Xmalloc (n * sizeof (int));
|
---|
108 | j = 0;
|
---|
109 | for (i = 0; i < n; i++)
|
---|
110 | {
|
---|
111 | if (A[i])
|
---|
112 | {
|
---|
113 | tot += (B[j++] = A[i]);
|
---|
114 | }
|
---|
115 | }
|
---|
116 | n = j;
|
---|
117 |
|
---|
118 | if (n == 0 || tot == 0)
|
---|
119 | return (0);
|
---|
120 |
|
---|
121 | buildheap (B, n);
|
---|
122 | while (n > 1)
|
---|
123 | {
|
---|
124 | v1 = B[0];
|
---|
125 | B[0] = B[n - 1];
|
---|
126 | n--;
|
---|
127 | siftdown (B, 0, n);
|
---|
128 | v2 = B[0];
|
---|
129 | B[0] = v1 + v2;
|
---|
130 | siftdown (B, 0, n);
|
---|
131 | bits += v1 + v2;
|
---|
132 | }
|
---|
133 | Xfree (B);
|
---|
134 | return (bits);
|
---|
135 | }
|
---|
136 |
|
---|
137 |
|
---|
138 | int
|
---|
139 | modelbits (unsigned long *A, int n)
|
---|
140 | {
|
---|
141 | int i, bits = 0, last, N = 0;
|
---|
142 |
|
---|
143 | last = -1;
|
---|
144 | for (i = 0; i < n; i++)
|
---|
145 | {
|
---|
146 | if (A[i])
|
---|
147 | {
|
---|
148 | bits += BIO_Gamma_Length (i - last) + BIO_Gamma_Length (A[i]);
|
---|
149 | last = i;
|
---|
150 | N++;
|
---|
151 | }
|
---|
152 | }
|
---|
153 | if (N == 0)
|
---|
154 | return (0);
|
---|
155 | bits += BIO_Gamma_Length (N);
|
---|
156 | return (bits);
|
---|
157 | }
|
---|
158 |
|
---|
159 |
|
---|
160 |
|
---|
161 |
|
---|
162 |
|
---|
163 | /* Find the next prime number larger than p */
|
---|
164 | int
|
---|
165 | prime (int p)
|
---|
166 | {
|
---|
167 | int limit;
|
---|
168 | p += (p & 1) + 1;
|
---|
169 | limit = (int) sqrt ((double) p) + 1;
|
---|
170 | do
|
---|
171 | {
|
---|
172 | int j;
|
---|
173 | while (limit * limit < p)
|
---|
174 | limit++;
|
---|
175 | for (j = 3; j <= limit && p % j; j += 2)
|
---|
176 | ; /* NULLBODY */
|
---|
177 | if (j > limit)
|
---|
178 | break;
|
---|
179 | p += 2;
|
---|
180 | }
|
---|
181 | while (1);
|
---|
182 | return (p);
|
---|
183 | }
|
---|
184 |
|
---|
185 |
|
---|
186 |
|
---|
187 | int Read_cdh (FILE * f, compression_dict_header * cdh, u_long * /*mem*/,
|
---|
188 | u_long * disk) {
|
---|
189 | if (disk)
|
---|
190 | (*disk) += sizeof (*cdh);
|
---|
191 |
|
---|
192 | /* [RPAP - Jan 97: Endian Ordering] */
|
---|
193 | if (fread (cdh, sizeof (*cdh), 1, f) == 1)
|
---|
194 | {
|
---|
195 | int i;
|
---|
196 | NTOHUL(cdh->dict_type);
|
---|
197 | NTOHUL(cdh->novel_method);
|
---|
198 | for (i = 0; i < TEXT_PARAMS; i++)
|
---|
199 | NTOHUL(cdh->params[i]);
|
---|
200 | NTOHUL(cdh->num_words[0]);
|
---|
201 | NTOHUL(cdh->num_words[1]);
|
---|
202 | NTOHUL(cdh->num_word_chars[0]);
|
---|
203 | NTOHUL(cdh->num_word_chars[1]);
|
---|
204 | NTOHUL(cdh->lookback);
|
---|
205 | return 0;
|
---|
206 | }
|
---|
207 | else
|
---|
208 | return -1;
|
---|
209 | }
|
---|
210 |
|
---|
211 |
|
---|
212 | int
|
---|
213 | Read_cfh (FILE * f, comp_frags_header * cfh, u_long * mem, u_long * disk)
|
---|
214 | {
|
---|
215 | int i; /* [RPAP - Jan 97: Endian Ordering] */
|
---|
216 |
|
---|
217 | if (Read_Huffman_Data (f, &cfh->hd, mem, disk) == -1)
|
---|
218 | return -1;
|
---|
219 | if (fread (&cfh->uncompressed_size,
|
---|
220 | sizeof (cfh->uncompressed_size), 1, f) != 1)
|
---|
221 | return -1;
|
---|
222 | NTOHUL(cfh->uncompressed_size); /* [RPAP - Jan 97: Endian Ordering] */
|
---|
223 |
|
---|
224 | if (disk)
|
---|
225 | (*disk) += sizeof (cfh->uncompressed_size);
|
---|
226 |
|
---|
227 | bzero ((char *) cfh->huff_words_size, sizeof (cfh->huff_words_size));
|
---|
228 |
|
---|
229 | if ((int)fread (cfh->huff_words_size + cfh->hd.mincodelen,
|
---|
230 | sizeof (*cfh->huff_words_size),
|
---|
231 | cfh->hd.maxcodelen - cfh->hd.mincodelen + 1, f) !=
|
---|
232 | cfh->hd.maxcodelen - cfh->hd.mincodelen + 1)
|
---|
233 | return -1;
|
---|
234 |
|
---|
235 | /* [RPAP - Jan 97: Endian Ordering] */
|
---|
236 | for (i = cfh->hd.mincodelen; i < cfh->hd.maxcodelen + 1; i++)
|
---|
237 | NTOHUL(cfh->huff_words_size[i]);
|
---|
238 |
|
---|
239 | if (disk)
|
---|
240 | (*disk) += sizeof (*cfh->huff_words_size) *
|
---|
241 | (cfh->hd.maxcodelen - cfh->hd.mincodelen + 1);
|
---|
242 | return 0;
|
---|
243 | }
|
---|
244 |
|
---|