source: trunk/gsdl/src/mgpp/text/mgpp_compression_dict.cpp@ 2557

Last change on this file since 2557 was 2557, checked in by sjboddie, 23 years ago

getopt.h was renamed getopt_old.h

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