source: trunk/gsdl/src/mgpp/text/mg_perf_hash_build.cpp@ 2468

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

Fiddled about with mgpp to get it compiling on Windows under VC++ 6.0. I
still can't get it to compile under VC++ 4.2 because of some weird
behaviour in STLport.

Also tidied up a little and removed some of the old log information
that was scattered about in some of the files.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 4.0 KB
Line 
1/**************************************************************************
2 *
3 * mg_perf_hash_build.cpp -- Program to build a perfect hash function
4 * Copyright (C) 1999 Rodger McNab
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.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 "perf_hash.h"
41#include "netorder.h"
42
43#include "mg_files.h"
44#include "invf.h"
45#include "locallib.h"
46#include "words.h"
47#include "mg.h"
48
49#define POOL_SIZE 1024*1024
50
51static void ProcessFiles (char *filename, int r) {
52 FILE *dictFile, *hashFile;
53 unsigned long i;
54 invf_dict_header idh;
55 perf_hash_data *phd;
56 u_char *pool;
57 unsigned long pool_left;
58 u_char **starts;
59
60 // read in the dictionary
61 dictFile = open_file (filename, INVF_DICT_SUFFIX, "rb",
62 MAGIC_STEM_BUILD, MG_ABORT);
63 if (dictFile==NULL) {
64 FatalError(1, "unable to open file");
65 }
66 idh.Read (dictFile);
67
68 //cerr << idh.lookback<<" "<<idh.word_dict_start<<endl;
69 // go to the start of the word dictionary
70 fseek (dictFile, idh.word_dict_start, SEEK_SET);
71
72 if (!(pool = (u_char *) Xmalloc (POOL_SIZE)))
73 FatalError (1, "Out of memory");
74 pool_left = POOL_SIZE;
75
76 if (!(starts = (u_char **) Xmalloc (sizeof (u_char *) * idh.word_dict_size)))
77 FatalError (1, "Out of memory");
78 //cerr << "size= "<< idh.word_dict_size<<endl;
79 word_dict_el wordEl;
80 wordEl.SetNumLevels (idh.num_levels);
81 for (i = 0; i < idh.word_dict_size; i++) {
82 // read the next word and associated information
83 wordEl.Read (dictFile, idh.num_levels);
84
85 // push string onto pool data
86 register unsigned long l;
87 l = wordEl.el.size() + 1;
88 if (pool_left < l) {
89 pool = (u_char *) Xmalloc (POOL_SIZE);
90 pool_left = POOL_SIZE;
91 }
92 starts[i] = pool;
93
94 *pool++ = wordEl.el.size();
95 memcpy ((char *) pool, (const char *) wordEl.el.begin(), wordEl.el.size());
96 //cerr << pool<<" " <<starts[i]<<endl;
97 pool += wordEl.el.size();
98 pool_left -= l;
99
100 }
101 fclose (dictFile);
102 //cerr << pool<<" " <<starts[i-1]<<endl;
103 //cerr<<"starts "<<starts[113529]<<endl;
104 //cerr << starts[17][1] << " "<<starts[25][4]<<endl;
105 // create perfect hash file
106 hashFile = create_file (filename, INVF_DICT_HASH_SUFFIX, "wb",
107 MAGIC_HASH, MG_ABORT);
108 if (!(phd = gen_hash_func (idh.word_dict_size, starts, r)))
109 FatalError (1, "Unable to generate hash function");
110 if (write_perf_hash_data (hashFile, phd) == -1)
111 FatalError (1, "Unable to write hash function");
112 fclose (hashFile);
113}
114
115
116
117int main (int argc, char **argv) {
118 int r = -1;
119 char *filename = "";
120 int ch;
121 msg_prefix = argv[0];
122 opterr = 0;
123
124 while ((ch = getopt (argc, argv, "f:d:r:h")) != -1) {
125 switch (ch) {
126 case 'f': // input file
127 filename = optarg;
128 break;
129 case 'd':
130 set_basepath (optarg);
131 break;
132 case 'r':
133 r = atoi (optarg);
134 break;
135 case 'h':
136 case '?':
137 fprintf (stderr, "usage: %s [-f input_file]"
138 "[-d data directory] [-r random seed] [-h]\n", argv[0]);
139 exit (1);
140 }
141 }
142
143 ProcessFiles (filename, r);
144 return 0;
145}
Note: See TracBrowser for help on using the repository browser.