source: trunk/gsdl/packages/mg-1.3d/src/text/locallib.c@ 30

Last change on this file since 30 was 13, checked in by rjmcnab, 26 years ago

* empty log message *

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 6.4 KB
Line 
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 13 1998-11-17 09:36:00Z rjmcnab $
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 1998/11/17 09:34:49 rjmcnab
37 *** empty log message ***
38
39 * Revision 1.3 1994/10/20 03:56:51 tes
40 * I have rewritten the boolean query optimiser and abstracted out the
41 * components of the boolean query.
42 *
43 * Revision 1.2 1994/09/20 04:41:38 tes
44 * For version 1.1
45 *
46 */
47
48static char *RCSID = "$Id: locallib.c 13 1998-11-17 09:36:00Z rjmcnab $";
49
50
51int
52vecentropy (int *A, int n)
53{
54 int total = 0, i;
55 double bits = 0.0;
56
57 for (i = 0; i < n; i++)
58 total += A[i];
59 for (i = 0; i < n; i++)
60 if (A[i])
61 bits += A[i] * log2 (1.0 * total / A[i]);
62 return ((int) (bits + 0.9999));
63
64}
65
66static void
67siftdown (int *A, int i, int n)
68{
69 int ms, t;
70
71 while (2 * i + 1 <= n - 1)
72 {
73 if (2 * i + 1 == n - 1 || A[2 * i + 1] < A[2 * i + 2])
74 ms = 2 * i + 1;
75 else
76 ms = 2 * i + 2;
77 if (A[i] > A[ms])
78 {
79 t = A[i];
80 A[i] = A[ms];
81 A[ms] = t;
82 }
83 i = ms;
84 }
85}
86
87static void
88buildheap (int *A, int n)
89{
90 int i;
91 for (i = n / 2 - 1; i >= 0; i--)
92 siftdown (A, i, n);
93}
94
95int
96huffcodebits (unsigned long *A, int n)
97{
98 int i, j, tot = 0, bits = 0, v1, v2, *B;
99
100 B = (int *) Xmalloc (n * sizeof (int));
101 j = 0;
102 for (i = 0; i < n; i++)
103 {
104 if (A[i])
105 {
106 tot += (B[j++] = A[i]);
107 }
108 }
109 n = j;
110
111 if (n == 0 || tot == 0)
112 return (0);
113
114 buildheap (B, n);
115 while (n > 1)
116 {
117 v1 = B[0];
118 B[0] = B[n - 1];
119 n--;
120 siftdown (B, 0, n);
121 v2 = B[0];
122 B[0] = v1 + v2;
123 siftdown (B, 0, n);
124 bits += v1 + v2;
125 }
126 Xfree (B);
127 return (bits);
128}
129
130
131int
132modelbits (unsigned long *A, int n)
133{
134 int i, bits = 0, last, N = 0;
135
136 last = -1;
137 for (i = 0; i < n; i++)
138 {
139 if (A[i])
140 {
141 bits += BIO_Gamma_Length (i - last) + BIO_Gamma_Length (A[i]);
142 last = i;
143 N++;
144 }
145 }
146 if (N == 0)
147 return (0);
148 bits += BIO_Gamma_Length (N);
149 return (bits);
150}
151
152
153
154
155
156/* Find the next prime number larger than p */
157int
158prime (int p)
159{
160 int limit;
161 p += (p & 1) + 1;
162 limit = (int) sqrt ((double) p) + 1;
163 do
164 {
165 int j;
166 while (limit * limit < p)
167 limit++;
168 for (j = 3; j <= limit && p % j; j += 2)
169 ; /* NULLBODY */
170 if (j > limit)
171 break;
172 p += 2;
173 }
174 while (1);
175 return (p);
176}
177
178
179
180int
181Read_cdh (FILE * f, compression_dict_header * cdh, u_long * mem,
182 u_long * disk)
183{
184 if (disk)
185 (*disk) += sizeof (*cdh);
186
187 /* [RPAP - Jan 97: Endian Ordering] */
188 if (fread (cdh, sizeof (*cdh), 1, f) == 1)
189 {
190 int i;
191 NTOHUL(cdh->dict_type);
192 NTOHUL(cdh->novel_method);
193 for (i = 0; i < TEXT_PARAMS; i++)
194 NTOHUL(cdh->params[i]);
195 NTOHUL(cdh->num_words[0]);
196 NTOHUL(cdh->num_words[1]);
197 NTOHUL(cdh->num_word_chars[0]);
198 NTOHUL(cdh->num_word_chars[1]);
199 NTOHUL(cdh->lookback);
200 return 0;
201 }
202 else
203 return -1;
204}
205
206
207int
208F_Read_cdh (File * f, compression_dict_header * cdh, u_long * mem,
209 u_long * disk)
210{
211 if (disk)
212 (*disk) += sizeof (*cdh);
213
214 /* [RPAP - Jan 97: Endian Ordering] */
215 if (Fread (cdh, sizeof (*cdh), 1, f) == 1)
216 {
217 int i;
218 NTOHUL(cdh->dict_type);
219 NTOHUL(cdh->novel_method);
220 for (i = 0; i < TEXT_PARAMS; i++)
221 NTOHUL(cdh->params[i]);
222 NTOHUL(cdh->num_words[0]);
223 NTOHUL(cdh->num_words[1]);
224 NTOHUL(cdh->num_word_chars[0]);
225 NTOHUL(cdh->num_word_chars[1]);
226 NTOHUL(cdh->lookback);
227 return 0;
228 }
229 else
230 return -1;
231}
232
233
234int
235Read_cfh (FILE * f, comp_frags_header * cfh, u_long * mem, u_long * disk)
236{
237 int i; /* [RPAP - Jan 97: Endian Ordering] */
238
239 if (Read_Huffman_Data (f, &cfh->hd, mem, disk) == -1)
240 return -1;
241 if (fread (&cfh->uncompressed_size,
242 sizeof (cfh->uncompressed_size), 1, f) != 1)
243 return -1;
244 NTOHUL(cfh->uncompressed_size); /* [RPAP - Jan 97: Endian Ordering] */
245
246 if (disk)
247 (*disk) += sizeof (cfh->uncompressed_size);
248
249 bzero ((char *) cfh->huff_words_size, sizeof (cfh->huff_words_size));
250
251 if (fread (cfh->huff_words_size + cfh->hd.mincodelen,
252 sizeof (*cfh->huff_words_size),
253 cfh->hd.maxcodelen - cfh->hd.mincodelen + 1, f) !=
254 cfh->hd.maxcodelen - cfh->hd.mincodelen + 1)
255 return -1;
256
257 /* [RPAP - Jan 97: Endian Ordering] */
258 for (i = cfh->hd.mincodelen; i < cfh->hd.maxcodelen + 1; i++)
259 NTOHUL(cfh->huff_words_size[i]);
260
261 if (disk)
262 (*disk) += sizeof (*cfh->huff_words_size) *
263 (cfh->hd.maxcodelen - cfh->hd.mincodelen + 1);
264 return 0;
265}
266
267
268
269int
270F_Read_cfh (File * f, comp_frags_header * cfh, u_long * mem, u_long * disk)
271{
272 int i; /* [RPAP - Jan 97: Endian Ordering] */
273
274 if (F_Read_Huffman_Data (f, &cfh->hd, mem, disk) == -1)
275 return -1;
276 if (Fread (&cfh->uncompressed_size,
277 sizeof (cfh->uncompressed_size), 1, f) != 1)
278 return -1;
279 NTOHUL(cfh->uncompressed_size); /* [RPAP - Jan 97: Endian Ordering] */
280
281 if (disk)
282 (*disk) += sizeof (cfh->uncompressed_size);
283
284 bzero ((char *) cfh->huff_words_size, sizeof (cfh->huff_words_size));
285
286 if (Fread (cfh->huff_words_size + cfh->hd.mincodelen,
287 sizeof (*cfh->huff_words_size),
288 cfh->hd.maxcodelen - cfh->hd.mincodelen + 1, f) !=
289 cfh->hd.maxcodelen - cfh->hd.mincodelen + 1)
290 return -1;
291
292 /* [RPAP - Jan 97: Endian Ordering] */
293 for (i = cfh->hd.mincodelen; i < cfh->hd.maxcodelen + 1; i++)
294 NTOHUL(cfh->huff_words_size[i]);
295
296 if (disk)
297 (*disk) += sizeof (*cfh->huff_words_size) *
298 (cfh->hd.maxcodelen - cfh->hd.mincodelen + 1);
299
300 return 0;
301}
Note: See TracBrowser for help on using the repository browser.