source: main/trunk/greenstone2/common-src/indexers/mgpp/text/mgpp_compression_dict.cpp@ 26294

Last change on this file since 26294 was 25147, checked in by kjdon, 12 years ago

merged 64_bit_Greenstone branch into trunk, rev 25139

  • Property svn:keywords set to Author Date Id Revision
File size: 25.6 KB
Line 
1/**************************************************************************
2 *
3 * mgpp_compression_dict.cpp -- Routines for creating compression dictionary
4 * Copyright (C) 1994 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 **************************************************************************/
21
22#define _XOPEN_SOURCE 1
23// This was added for Solaris, but it makes things worse on Solaris for me...
24// #define _XOPEN_SOURCE_EXTENDED 1
25
26// need this to avoid bizarre compiler problems under VC++ 6.0
27#if defined (__WIN32__) && !defined (GSDL_USE_IOS_H)
28# include <iostream>
29#endif
30
31/* getopt is in posix.2, so cygwin should have it in unistd, but doesn't */
32#if defined (__WIN32__) || defined (__CYGWIN__)
33# include "getopt_old.h"
34#else
35# include <unistd.h>
36#endif
37
38#include "sysfuncs.h"
39#include "memlib.h"
40#include "messages.h"
41#include "local_strings.h"
42#include "bitio_gen.h"
43#include "bitio_m_stdio.h"
44#include "mgheap.h"
45#include "netorder.h" /* [RPAP - Jan 97: Endian Ordering] */
46
47#include "mg_files.h"
48#include "locallib.h"
49#include "invf.h"
50#include "text.h"
51#include "words.h"
52#include "mg.h"
53#include "WordData.h"
54
55#define MAXBITS (sizeof(mg_u_long) * 8)
56
57#define is_power_of_two(a) ((a) != 0 && (((a) & ((a)-1)) == 0))
58
59#define MAX_RECALCULATIONS 100
60
61typedef struct DictWordData : public WordData
62 {
63 float saving;
64 float char_bit_cost;
65 mg_u_long num_trans;
66 u_char *word;
67 }
68DictWordData;
69
70static DictWordData *Words[2];
71static mg_u_long Num[2];
72static mg_u_long chars[2];
73
74#define KIND(p) (((p) >= Words[0] && (p) < Words[0] + Num[0]) ? 0 : 1)
75#define IsWord(p) (((p) >= Words[1] && (p) < Words[1] + Num[1]) ? 1 : 0)
76#define IsNonWord(p) (((p) >= Words[0] && (p) < Words[0] + Num[0]) ? 1 : 0)
77
78
79typedef struct DictData
80 {
81 DictWordData **wd;
82 mg_u_long num_wds;
83 mg_u_long chars;
84 }
85DictData;
86
87typedef DictData DictInfo[2];
88
89static DictInfo keep, discard, all;
90static compression_stats_header csh;
91
92static char *file_name = (char*)"";
93static mg_u_long novel_method = MG_NOVEL_HUFFMAN_CHARS;
94
95
96static void ReadInWords (char *);
97static void Select_on (int k, heap_comp hc);
98static void Method3 (int k);
99static void Select_all (void);
100static mg_u_long WriteOutWords (char *, mg_u_long, int);
101static int DecFreqIncWL (void *a, void *b);
102static int OccuranceOrder (void *a, void *b);
103
104
105
106int main (int argc, char **argv)
107{
108 int ch;
109 char type = 'C';
110 char mode = '0';
111 int lookback = 2;
112 double k = 0;
113 mg_u_long mem_reqd;
114 opterr = 0;
115 msg_prefix = argv[0];
116 while ((ch = getopt (argc, argv, "0123CPSf:d:l:hk:HDY")) != -1)
117 switch (ch)
118 {
119 case 'H':
120 novel_method = MG_NOVEL_HUFFMAN_CHARS;
121 break;
122 case 'D':
123 novel_method = MG_NOVEL_DELTA;
124 break;
125 case 'Y':
126 novel_method = MG_NOVEL_HYBRID;
127 break;
128 case 'f': /* input file */
129 file_name = optarg;
130 break;
131 case 'd':
132 set_basepath (optarg);
133 break;
134 case 'C':
135 case 'P':
136 case 'S':
137 type = ch;
138 break;
139 case '0':
140 case '1':
141 case '2':
142 case '3':
143 mode = ch;
144 break;
145 case 'l':
146 lookback = atoi (optarg);
147 if (!is_power_of_two (lookback))
148 FatalError (1, "The lookback value must be a power of 2");
149 lookback = floorlog_2 (lookback);
150 break;
151 case 'k':
152 k = atof (optarg) * 1024;
153 break;
154 case 'h':
155 case '?':
156 fprintf (stderr, "usage: %s [-l lookback] [-f input_file]"
157 "[-d data directory] [-h] [-0|-1|-2|-3] [-C|-P|-S] [-k mem (Kb)]\n",
158 argv[0]);
159 exit (1);
160 }
161
162 ReadInWords (file_name);
163
164 if (type == 'C')
165 {
166 Select_all ();
167 mem_reqd = WriteOutWords (file_name, MG_COMPLETE_DICTIONARY, lookback);
168 }
169 else
170 {
171 switch (mode)
172 {
173 case '0':
174 Select_all ();
175 break;
176 case '1':
177#ifndef SILENT
178 Message ("Dictionary limit of %.2f Kb", k / 1024);
179#endif
180 Select_on ((int) k, OccuranceOrder);
181 break;
182 case '2':
183#ifndef SILENT
184 Message ("Dictionary limit of %.2f Kb", k / 1024);
185#endif
186 Select_on ((int) k, DecFreqIncWL);
187 break;
188 case '3':
189#ifndef SILENT
190 Message ("Dictionary limit of %.2f Kb", k / 1024);
191#endif
192 Method3 ((int) k);
193 break;
194 }
195 if (type == 'P')
196 {
197 mem_reqd = WriteOutWords (file_name, MG_PARTIAL_DICTIONARY, lookback);
198 }
199 else
200 {
201 mem_reqd = WriteOutWords (file_name, MG_SEED_DICTIONARY, lookback);
202 }
203 }
204
205#ifndef SILENT
206 Message ("Num words : %8u -> %8u\n", Num[1], keep[1].num_wds);
207 Message ("Num non-words : %8u -> %8u\n", Num[0], keep[0].num_wds);
208 Message ("Chars of words : %8u -> %8u\n", chars[1], keep[1].chars);
209 Message ("Chars of non-words : %8u -> %8u\n", chars[0], keep[0].chars);
210 Message ("Mem usage : %8u -> %8u\n",
211 (Num[0] + Num[1]) * sizeof (char *) + chars[0] + chars[1],
212 (keep[0].num_wds + keep[1].num_wds) * sizeof (char *) +
213 keep[0].chars + keep[1].chars);
214 Message ("Actual mem required : %8u\n", mem_reqd);
215#endif
216 return (0);
217}
218
219
220
221
222static void
223ReadInWords (char *filename)
224{
225 FILE *f;
226 mg_u_long i;
227 f = open_file (filename, TEXT_STATS_DICT_SUFFIX, "rb",
228 MAGIC_STATS_DICT, MG_ABORT); /* [RPAP - Feb 97: WIN32 Port] */
229
230 fread (&csh, sizeof (csh), 1, f);
231
232 /* [RPAP - Jan 97: Endian Ordering] */
233 NTOHUL(csh.num_docs);
234 NTOHD(csh.num_bytes);
235
236 for (i = 0; i < 2; ++i)
237 {
238 frags_stats_header fsh;
239 char *buf;
240 DictWordData *wd;
241 chars[i] = 0;
242
243 fread (&fsh, sizeof (fsh), 1, f);
244 /* [RPAP - Jan 97: Endian Ordering] */
245 NTOHUL(fsh.num_frags);
246 NTOHUL(fsh.mem_for_frags);
247
248 Num[i] = all[i].num_wds = fsh.num_frags;
249
250 /* The +1 on the following line is to leave room for the esc code. */
251 all[i].wd = (DictWordData **) Xmalloc (sizeof (DictWordData *) * Num[i] + 1);
252
253 buf = (char *) Xmalloc (fsh.mem_for_frags);
254 wd = Words[i] = (DictWordData *) Xmalloc (sizeof (DictWordData) * Num[i]);
255 unsigned int j;
256 for (j = 0; j < Num[i]; ++j, ++wd)
257 {
258 int len;
259
260 // read docCount and wordCount
261 wd->read(f);
262
263 // read an mgString
264 len = fgetc (f);
265 wd->word = (u_char *) buf;
266 *buf++ = len;
267 fread (buf, len, 1, f);
268 buf += len;
269 all[i].wd[j] = wd;
270 }
271 chars[i] = fsh.mem_for_frags - fsh.num_frags;
272 }
273 fclose (f);
274}
275
276
277static void
278Alloc_keep_discard (void)
279{
280 keep[0].num_wds = 0;
281 keep[0].wd = (DictWordData **) Xmalloc ((Num[0] + 1) * sizeof (DictWordData *));
282 keep[1].num_wds = 0;
283 keep[1].wd = (DictWordData **) Xmalloc ((Num[1] + 1) * sizeof (DictWordData *));
284 discard[0].num_wds = 0;
285 discard[0].wd = (DictWordData **) Xmalloc ((Num[0] + 1) * sizeof (DictWordData *));
286 discard[1].num_wds = 0;
287 discard[1].wd = (DictWordData **) Xmalloc ((Num[1] + 1) * sizeof (DictWordData *));
288}
289
290
291static int
292sort_comp (const void *a, const void *b)
293{
294 DictWordData *aa = *(DictWordData **) a;
295 DictWordData *bb = *(DictWordData **) b;
296 return casecompare (aa->word, bb->word); /* [RPAP - Jan 97: Stem Index Change] */
297}
298
299
300
301static void
302SortAndCount_DictData (DictData * dd)
303{
304 unsigned int i;
305 DictWordData **wd;
306 qsort (dd->wd, dd->num_wds, sizeof (DictWordData *), sort_comp);
307 dd->chars = 0;
308 wd = dd->wd;
309 for (i = 0; i < dd->num_wds; ++i, ++wd)
310 dd->chars += (*wd)->word[0];
311}
312
313
314static void
315Select_all (void)
316{
317 unsigned int i;
318 Alloc_keep_discard ();
319 keep[0].num_wds = Num[0];
320 for (i = 0; i < Num[0]; ++i)
321 keep[0].wd[i] = Words[0] + i;
322 keep[1].num_wds = Num[1];
323 for (i = 0; i < Num[1]; ++i)
324 keep[1].wd[i] = Words[1] + i;
325 SortAndCount_DictData (&keep[0]);
326 SortAndCount_DictData (&keep[1]);
327}
328
329
330
331
332static int
333DecFreqIncWL (void *a, void *b)
334{
335 DictWordData *aa = *(DictWordData **) a;
336 DictWordData *bb = *(DictWordData **) b;
337 if (aa->docCount > bb->docCount)
338 return -1;
339 else if (aa->docCount < bb->docCount)
340 return 1;
341 else
342 return bb->word[0] - aa->word[0];
343}
344
345
346static int
347OccuranceOrder (void *a, void *b)
348{
349 DictWordData *aa = *(DictWordData **) a;
350 DictWordData *bb = *(DictWordData **) b;
351 if (aa->words() > bb->words())
352 return 1;
353 else if (aa->words() < bb->words())
354 return -1;
355 else
356 return 0;
357}
358
359
360static void
361Select_on (int k, heap_comp hc)
362{
363 int i, num, mem;
364 DictWordData **wd;
365
366 Alloc_keep_discard ();
367
368 num = Num[0] + Num[1];
369 wd = (DictWordData **) Xmalloc (num * sizeof (DictWordData *));
370 for (i = 0; (unsigned int)i < Num[0]; ++i)
371 wd[i] = Words[0] + i;
372 for (i = 0; (unsigned int)i < Num[1]; ++i)
373 wd[i + Num[0]] = Words[1] + i;
374
375 heap_build (wd, sizeof (*wd), num, hc);
376
377 mem = 0;
378 while (k > mem && num)
379 {
380 int idx;
381 DictWordData *word = wd[0];
382#ifdef DEBUG
383 fprintf (stderr, "%4d:%4d:%8d :%8d :%8d : \"%s\"\n",
384 keep[0].num_wds, keep[1].num_wds,
385 mem, word->documents(), word->word, word2str (word->word));
386#endif
387 mem += sizeof (u_char *) + word->word[0];
388 heap_deletehead (wd, sizeof (*wd), &num, hc);
389 idx = KIND (word);
390 keep[idx].wd[keep[idx].num_wds++] = word;
391 }
392
393 for (i = 0; i < num; ++i)
394 {
395 DictWordData *word = wd[i];
396 int idx = KIND (word);
397 discard[idx].wd[discard[idx].num_wds++] = word;
398 }
399 SortAndCount_DictData (&keep[0]);
400 SortAndCount_DictData (&keep[1]);
401 SortAndCount_DictData (&discard[0]);
402 SortAndCount_DictData (&discard[1]);
403
404 assert (keep[0].num_wds + discard[0].num_wds == Num[0]);
405 assert (keep[1].num_wds + discard[1].num_wds == Num[1]);
406
407}
408
409
410
411static int
412BigSaving (void *a, void *b)
413{
414 DictWordData *aa = *(DictWordData **) a;
415 DictWordData *bb = *(DictWordData **) b;
416 return (aa->saving > bb->saving) ? -1 : (aa->saving < bb->saving);
417}
418
419static int
420SmallSaving (void *a, void *b)
421{
422 DictWordData *aa = *(DictWordData **) a;
423 DictWordData *bb = *(DictWordData **) b;
424 return (aa->saving < bb->saving) ? -1 : (aa->saving > bb->saving);
425}
426
427
428static void
429CalcCharCounts (DictWordData ** wd, int num,
430 char *char_lens[2], char *len_lens[2],
431 mg_u_long escape[2])
432{
433 mg_s_long char_freqs[2][256];
434 mg_s_long len_freqs[2][16];
435 huff_data hd;
436 int i;
437 escape[0] = 0;
438 escape[1] = 0;
439 memset (char_freqs, '\0', sizeof (char_freqs));
440 memset (len_freqs, '\0', sizeof (len_freqs));
441 for (i = 0; i < num; ++i, ++wd)
442 {
443 mg_u_long freq = (*wd)->documents();
444 u_char *buf = (*wd)->word;
445 int len = *buf++;
446 int idx = KIND (*wd);
447 len_freqs[idx][len] += freq;
448 escape[idx] += freq;
449 for (; len; --len, ++buf)
450 char_freqs[idx][(mg_u_long) (*buf)] += freq;
451 }
452 Generate_Huffman_Data (256, char_freqs[0], &hd, NULL);
453 char_lens[0] = hd.clens;
454 Generate_Huffman_Data (256, char_freqs[1], &hd, NULL);
455 char_lens[1] = hd.clens;
456
457 Generate_Huffman_Data (16, len_freqs[0], &hd, NULL);
458 len_lens[0] = hd.clens;
459 Generate_Huffman_Data (16, len_freqs[1], &hd, NULL);
460 len_lens[1] = hd.clens;
461}
462
463
464
465
466
467inline void CalcBitCostForWordData(DictWordData **word, int num,
468 double freqs_trans[2], mg_u_long escape[2],
469 char * char_lens[2], char *len_lens[2],
470 double esc[2], int num_trans)
471{
472 int j;
473
474 for (j = 0; j < num; ++j, ++word)
475 {
476 float cbc, wbc;
477 u_char *buf = (*word)->word;
478 int len = *buf++;
479 mg_u_long freq = (*word)->documents();
480 int idx = KIND (*word);
481
482 cbc = len_lens[idx][len];
483 for (; len; --len, ++buf)
484 cbc += char_lens[idx][(mg_u_long) (*buf)];
485
486 (*word)->char_bit_cost = (cbc + esc[idx]) * freq;
487
488 wbc = -log2 (freq / (freqs_trans[idx] + escape[idx])) * freq;
489
490 (*word)->saving = ((*word)->char_bit_cost - wbc) /
491 (sizeof (char *) + (*word)->word[0]);
492
493 (*word)->num_trans = num_trans;
494 }
495}
496
497
498
499void
500CalcBitCost (DictWordData ** discard_word, int discard_num,
501 DictWordData ** keep_word, int keep_num, double freqs_trans[2],
502 mg_u_long escape[2], int num_trans)
503{
504 char *char_lens[2];
505 char *len_lens[2];
506 double esc[2];
507 CalcCharCounts (discard_word, discard_num, char_lens, len_lens, escape);
508 esc[0] = -log2 (escape[0] / (freqs_trans[0] + escape[0]));
509 esc[1] = -log2 (escape[1] / (freqs_trans[1] + escape[1]));
510 CalcBitCostForWordData(discard_word, discard_num, freqs_trans, escape,
511 char_lens, len_lens, esc, num_trans);
512 CalcBitCostForWordData(keep_word, keep_num, freqs_trans, escape,
513 char_lens, len_lens, esc, num_trans);
514 Xfree (char_lens[0]);
515 Xfree (char_lens[1]);
516 Xfree (len_lens[0]);
517 Xfree (len_lens[1]);
518}
519
520inline void m3_transferWord(DictWordData **toHeap, int *toNum, heap_comp toSaving,
521 DictWordData **fromHeap, int *fromNum, heap_comp fromSaving,
522 int *num_trans, double freqs_trans[], int *mem, double *total,
523 int *count)
524{
525 /* Transfer the word at the top of the keep heap to the
526 discard heap. */
527 DictWordData *word = fromHeap[0];
528 int idx = KIND (word);
529 heap_deletehead (fromHeap, sizeof (word), fromNum, fromSaving);
530 toHeap[*toNum] = word;
531 heap_additem (toHeap, sizeof (word), toNum, toSaving);
532 freqs_trans[idx] -= word->documents();
533 *mem = (*mem) - sizeof (u_char *) + word->word[0];
534 *num_trans += 1;
535 *total += word->saving;
536 *count += 1;
537#ifdef DEBUG
538 fprintf (stderr,
539 "KEEP -> DISCARD %8d :%8d :%8.0f :%8.0f : \"%s\"\n",
540 *mem, word->documents(), word->char_bit_cost,
541 word->saving, word2str (word->word));
542#endif
543}
544
545inline void m3_storeWord(DictWordData **wordHeap, int wordNum, int num_trans,
546 double freqs_trans[], mg_u_long escape[], heap_comp hc)
547{
548 DictWordData *word = wordHeap[0];
549 int idx = KIND (word);
550 float wbc;
551#ifdef DEBUG1
552 fprintf (stderr, "KEEP \"%s\" %.2f ->", word2str (word->word),
553 word->saving);
554#endif
555 wbc = -log2 (word->documents() / (freqs_trans[idx] + escape[idx])) *
556 word->documents();
557 word->saving = (word->char_bit_cost - wbc) /
558 (sizeof (char *) + word->word[0]);
559#ifdef DEBUG1
560 fprintf (stderr, " %.2f\n", word->saving);
561#endif
562 word->num_trans = num_trans;
563 heap_changedhead (wordHeap, sizeof (word), wordNum, hc);
564}
565
566
567static void
568Method3 (int k)
569{
570 int i, keep_num, discard_num, mem, num_trans, recalc_reqd;
571 int keep_to_discard = 0;
572 int discard_to_keep = 0;
573 int recalcs = 0;
574 double freqs_trans[2], total;
575 mg_u_long escape[2];
576 DictWordData **keep_heap, **discard_heap;
577
578 freqs_trans[0] = freqs_trans[1] = 0;
579 num_trans = 0;
580
581 discard_num = Num[0] + Num[1];
582 discard_heap = (DictWordData **) Xmalloc (discard_num * sizeof (DictWordData *));
583
584 keep_num = 0;
585 keep_heap = (DictWordData **) Xmalloc (discard_num * sizeof (DictWordData *));
586
587
588 for (i = 0; (unsigned int)i < Num[0]; ++i)
589 discard_heap[i] = Words[0] + i;
590 for (i = 0; (unsigned int)i < Num[1]; ++i)
591 discard_heap[i + Num[0]] = Words[1] + i;
592
593 heap_build (discard_heap, sizeof (*discard_heap), discard_num, DecFreqIncWL);
594
595 mem = 0;
596 while (k > mem && discard_num)
597 {
598 DictWordData *word = discard_heap[0];
599 mem += sizeof (u_char *) + word->word[0];
600 heap_deletehead (discard_heap, sizeof (word), &discard_num, DecFreqIncWL);
601 keep_heap[keep_num++] = word;
602 freqs_trans[KIND (word)] += word->documents();
603 ++num_trans;
604 }
605
606 CalcBitCost (discard_heap, discard_num, keep_heap, keep_num,
607 freqs_trans, escape, num_trans);
608 heap_build (discard_heap, sizeof (*discard_heap), discard_num, BigSaving);
609 heap_build (keep_heap, sizeof (*keep_heap), keep_num, SmallSaving);
610
611 total = 0;
612 recalc_reqd = 0;
613 while (keep_num && discard_num)
614 {
615 if ((keep_num && keep_heap[0]->num_trans < (unsigned)num_trans) ||
616 (discard_num && discard_heap[0]->num_trans < (unsigned)num_trans))
617 {
618 if (keep_num && keep_heap[0]->num_trans < (unsigned)num_trans)
619 {
620 m3_storeWord(keep_heap, keep_num, num_trans, freqs_trans, escape, SmallSaving);
621 }
622
623 if (discard_num && discard_heap[0]->num_trans < (unsigned)num_trans)
624 {
625 m3_storeWord(discard_heap, discard_num, num_trans, freqs_trans, escape, BigSaving);
626 }
627 }
628 else if (keep_heap[0]->saving < discard_heap[0]->saving)
629 {
630 assert (keep_num && discard_num);
631 if (keep_num && mem + sizeof (char *) + discard_heap[0]->word[0] > (unsigned)k)
632 {
633 /* Transfer the word at the top of the keep heap to the
634 discard heap. */
635 m3_transferWord(discard_heap, &discard_num, BigSaving,
636 keep_heap, &keep_num, SmallSaving,
637 &num_trans, freqs_trans, &mem, &total, &keep_to_discard);
638 }
639 else
640 {
641 /* Transfer the word at the top of the discard heap to the
642 keep heap. */
643 m3_transferWord(keep_heap, &keep_num, SmallSaving,
644 discard_heap, &discard_num, BigSaving,
645 &num_trans, freqs_trans, &mem, &total, &discard_to_keep);
646 }
647
648 recalc_reqd = 1;
649
650 }
651 else
652 {
653 if (recalc_reqd == 0)
654 break;
655#ifdef DEBUG1
656 fprintf (stderr, "--------------\n");
657#endif
658 if (recalcs == MAX_RECALCULATIONS)
659 break;
660 CalcBitCost (discard_heap, discard_num, keep_heap, keep_num,
661 freqs_trans, escape, num_trans);
662 heap_build (discard_heap, sizeof (*discard_heap),
663 discard_num, BigSaving);
664 heap_build (keep_heap, sizeof (keep_heap), keep_num, SmallSaving);
665 recalc_reqd = 0;
666 ++recalcs;
667 }
668 }
669
670 Alloc_keep_discard ();
671
672 for (i = 0; i < discard_num; ++i)
673 {
674 DictWordData *word = discard_heap[i];
675 int idx = KIND (word);
676 assert (IsWord (word) || IsNonWord (word));
677 discard[idx].wd[discard[idx].num_wds++] = word;
678 }
679 for (i = 0; i < keep_num; ++i)
680 {
681 DictWordData *word = keep_heap[i];
682 int idx = KIND (word);
683 assert (IsWord (word) || IsNonWord (word));
684 keep[idx].wd[keep[idx].num_wds++] = word;
685 }
686 SortAndCount_DictData (&keep[0]);
687 SortAndCount_DictData (&keep[1]);
688 SortAndCount_DictData (&discard[0]);
689 SortAndCount_DictData (&discard[1]);
690
691 assert (keep[0].num_wds + discard[0].num_wds == Num[0]);
692 assert (keep[1].num_wds + discard[1].num_wds == Num[1]);
693#ifndef SILENT
694 Message ("Keep -> Discard : %8d", keep_to_discard);
695 Message ("Discard -> Keep : %8d", discard_to_keep);
696 Message ("Huffman Recalculations : %8d", recalcs);
697 if (recalcs == MAX_RECALCULATIONS)
698 Message ("WARNING: The number of recalculations == %d", MAX_RECALCULATIONS);
699#endif
700}
701
702
703
704
705
706
707
708
709
710
711/****************************************************************************
712 ***** *****
713 ***** Dictionary saving code *****
714 ***** *****
715 ****************************************************************************/
716
717
718
719static void
720Write_cdh (FILE * f, compression_dict_header * cdh)
721{
722 /* [RPAP - Jan 97: Endian Ordering] */
723 int i;
724 compression_dict_header tmp = *cdh;
725 HTONUL(tmp.dict_type);
726 HTONUL(tmp.novel_method);
727 for (i = 0; i < TEXT_PARAMS; ++i)
728 HTONUL(tmp.params[i]);
729 HTONUL(tmp.num_words[0]);
730 HTONUL(tmp.num_words[1]);
731 HTONUL(tmp.num_word_chars[0]);
732 HTONUL(tmp.num_word_chars[1]);
733 HTONUL(tmp.lookback);
734
735 fwrite (&tmp, sizeof (tmp), 1, f);
736}
737
738
739static void
740Write_words (FILE * f, DictData * dd)
741{
742 unsigned int i;
743 u_char *curr, *prev = NULL;
744 for (i = 0; i < dd->num_wds; ++i)
745 {
746 int len;
747 curr = dd->wd[i]->word;
748 if (prev)
749 /* look for prefix match with prev string */
750 len = prefixlen (prev, curr);
751 else
752 len = 0;
753 fputc ((len << 4) + (curr[0] - len), f);
754 fwrite (curr + len + 1, sizeof (u_char), curr[0] - len, f);
755 prev = curr;
756 }
757
758}
759
760
761static int
762Uncompressed_size (DictData * dd)
763{
764 unsigned int i, us;
765 for (us = i = 0; i < dd->num_wds; ++i)
766 us += dd->wd[i]->word[0];
767 return us;
768}
769
770
771static mg_u_long
772Write_data (FILE * f, DictData * dd, int lookback)
773{
774 mg_u_long mem_reqd;
775 huff_data *hd;
776 int i;
777 mg_u_long us = dd->num_wds;
778 mg_s_long *freqs;
779 mg_u_long huff_words_size[MAX_HUFFCODE_LEN + 1];
780 mg_u_long lencounts[MAX_HUFFCODE_LEN + 1];
781 u_char *lastword[MAX_HUFFCODE_LEN + 1];
782
783 if (!(freqs = new mg_s_long [dd->num_wds]))
784 FatalError (1, "Unable to allocate memory for freqs");
785
786 for (i = 0; (unsigned)i < dd->num_wds; ++i)
787 {
788 freqs[i] = dd->wd[i]->documents();
789 us += dd->wd[i]->word[0];
790 }
791
792 if (!(hd = Generate_Huffman_Data (dd->num_wds, freqs, NULL, NULL)))
793 FatalError (1, "Unable to allocate memory for huffman data");
794
795 delete []freqs;
796 freqs = NULL;
797
798 if (Write_Huffman_Data (f, hd) == -1)
799 FatalError (1, "Unable to write huffman data");
800
801 HTONUL(us); /* [RPAP - Jan 97: Endian Ordering] */
802 fwrite (&us, sizeof (us), 1, f);
803 NTOHUL(us); /* [RPAP - Jan 97: Endian Ordering] */
804
805
806/* Calculate the amount of memory that will be required to store the text for
807 each different huffman code len. Every 1<<lookback words for each different
808 codelen length will not be prefixed by previous strings. */
809
810
811 memset (&huff_words_size, '\0', sizeof (huff_words_size));
812 memset (&lencounts, '\0', sizeof (lencounts));
813
814 mem_reqd = 0;
815
816 for (i = 0; (unsigned)i < dd->num_wds; ++i)
817 {
818 int codelen = hd->clens[i];
819 u_char *word = dd->wd[i]->word;
820
821 if (!codelen)
822 FatalError (1, "The length of a code for a word was zero");
823
824 huff_words_size[codelen] += word[0] + 1;
825 mem_reqd += word[0] + (lookback != 0);
826#if 0
827 if ((lencounts[codelen] & ((1 << lookback) - 1)) == 0)
828 lastword[codelen] = word;
829 else
830 huff_words_size[codelen] -= prefixlen (lastword[codelen], word);
831#else
832 if ((lencounts[codelen] & ((1 << lookback) - 1)) != 0)
833 {
834 int save = prefixlen (lastword[codelen], word);
835 mem_reqd -= save;
836 huff_words_size[codelen] -= save;
837 }
838 else
839 {
840 mem_reqd += sizeof (u_char *);
841 }
842 lastword[codelen] = word;
843#endif
844 ++lencounts[codelen];
845 }
846
847 /* [RPAP - Jan 97: Endian Ordering] */
848 for (i = hd->mincodelen; i < hd->maxcodelen + 1; ++i)
849 HTONUL(huff_words_size[i]);
850
851 fwrite (huff_words_size + hd->mincodelen, sizeof (*huff_words_size),
852 hd->maxcodelen - hd->mincodelen + 1, f);
853
854 /* [RPAP - Jan 97: Endian Ordering] */
855 for (i = hd->mincodelen; i < hd->maxcodelen + 1; ++i)
856 NTOHUL(huff_words_size[i]);
857
858 Write_words (f, dd);
859
860 delete []hd->clens;
861 delete hd;
862
863 return mem_reqd;
864}
865
866
867
868static void
869Write_charfreqs (FILE * f, DictData * dd, int words,
870 int zero_freq_permitted)
871{
872 unsigned int j;
873 mg_s_long freqs[256];
874 DictWordData **wd = dd->wd;
875 huff_data *hd;
876
877 memset (freqs, '\0', sizeof (freqs));
878
879 for (j = 0; j < dd->num_wds; ++j, ++wd)
880 {
881 u_char *buf = (*wd)->word;
882 int len = *buf++;
883 for (; len; --len, ++buf)
884 freqs[(mg_u_long) (*buf)] += (*wd)->documents();
885 }
886
887 if (!zero_freq_permitted)
888 for (j = 0; j < 256; ++j)
889 if (!freqs[j] && PESINAWORD (j) == words)
890 freqs[j] = 1;
891
892 if (!(hd = Generate_Huffman_Data (256, freqs, NULL, NULL)))
893 FatalError (1, "Unable to allocate memory for huffman data");
894
895 if (Write_Huffman_Data (f, hd) == -1)
896 FatalError (1, "Unable to write huffman data");
897
898 delete []hd->clens;
899 delete hd;
900}
901
902
903
904
905static void
906Write_wordlens (FILE * f, DictData * dd, int zero_freq_permitted)
907{
908 unsigned int j;
909 mg_s_long freqs[16];
910 DictWordData **wd = dd->wd;
911 huff_data *hd;
912
913 memset (freqs, '\0', sizeof (freqs));
914
915 for (j = 0; j < dd->num_wds; ++j, ++wd)
916 freqs[(*wd)->word[0]] += (*wd)->documents();
917
918 if (!zero_freq_permitted)
919 for (j = 0; j < 16; ++j)
920 if (!freqs[j])
921 freqs[j] = 1;
922
923 if (!(hd = Generate_Huffman_Data (16, freqs, NULL, NULL)))
924 FatalError (1, "Unable to allocate memory for huffman data");
925
926 if (Write_Huffman_Data (f, hd) == -1)
927 FatalError (1, "Unable to write huffman data");
928
929
930 delete []hd->clens;
931 delete hd;
932}
933
934
935
936static mg_u_long
937WriteOutWords (char *file_name, mg_u_long type, int lookback)
938{
939 FILE *f;
940 int i;
941 mg_u_long mem_reqd = 0;
942
943 compression_dict_header cdh;
944 f = create_file (file_name, TEXT_DICT_SUFFIX, "w+b",
945 MAGIC_DICT, MG_ABORT); /* [RPAP - Feb 97: WIN32 Port] */
946
947 memset (&cdh, '\0', sizeof(cdh));
948
949 cdh.dict_type = type;
950 cdh.novel_method = (type == MG_SEED_DICTIONARY) ? novel_method :
951 MG_NOVEL_HUFFMAN_CHARS;
952
953 cdh.num_words[1] = keep[1].num_wds;
954 cdh.num_words[0] = keep[0].num_wds;
955 cdh.num_word_chars[1] = Uncompressed_size (&keep[1]);
956 cdh.num_word_chars[0] = Uncompressed_size (&keep[0]);
957 cdh.lookback = lookback;
958
959 Write_cdh (f, &cdh);
960
961 for (i = 0; i < 2; ++i)
962 switch (type)
963 {
964 case MG_COMPLETE_DICTIONARY:
965 {
966 mem_reqd += Write_data (f, &keep[i], lookback);
967 }
968 break;
969 case MG_PARTIAL_DICTIONARY:
970 {
971 if (keep[i].num_wds)
972 {
973 int j;
974 DictWordData esc;
975 esc.docCount = 0;
976 esc.word = (u_char *) "";
977 keep[i].wd[keep[i].num_wds++] = &esc;
978 for (j = 0; (unsigned)j < discard[i].num_wds; ++j)
979 esc.docCount += discard[i].wd[j]->documents();
980 if (!esc.docCount)
981 ++esc.docCount;
982 mem_reqd += Write_data (f, &keep[i], lookback);
983 }
984 Write_charfreqs (f, &discard[i], i, 1);
985 Write_wordlens (f, &discard[i], 1);
986 }
987 break;
988 case MG_SEED_DICTIONARY:
989 {
990 if (keep[i].num_wds)
991 {
992 int j;
993 DictWordData esc;
994 esc.docCount = 0;
995 esc.word = (u_char *) "";
996 keep[i].wd[keep[i].num_wds++] = &esc;
997 for (j = 0; (unsigned)j < all[i].num_wds; ++j)
998 if (all[i].wd[j]->documents() == 1)
999 ++esc.docCount;
1000 if (!esc.docCount)
1001 ++esc.docCount;
1002 mem_reqd += Write_data (f, &keep[i], lookback);
1003 }
1004 switch (novel_method)
1005 {
1006 case MG_NOVEL_HUFFMAN_CHARS:
1007 Write_charfreqs (f, &all[i], i, 0);
1008 Write_wordlens (f, &all[i], 0);
1009 break;
1010 case MG_NOVEL_DELTA:
1011 break;
1012 case MG_NOVEL_HYBRID:
1013 break;
1014 default:
1015 FatalError (1, "Bad novel method");
1016 }
1017 }
1018 break;
1019 }
1020 fclose (f);
1021 return mem_reqd;
1022}
Note: See TracBrowser for help on using the repository browser.