source: main/tags/2.80/indexers/mgpp/text/mgpp_perf_hash_build.cpp@ 24540

Last change on this file since 24540 was 9613, checked in by kjdon, 19 years ago

added in x++ -> ++x changes submitted by Emanuel Dejanu

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