source: trunk/mgpp/text/mgpp_compression_dict.cpp@ 3365

Last change on this file since 3365 was 3365, checked in by kjdon, 22 years ago

Initial revision

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