1
/* Copyright (C) 2000-2006 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
21
The idea of presented algorithm see in
22
"The Art of Computer Programming" by Donald E. Knuth
23
Volume 3 "Sorting and searching"
24
(chapter 6.3 "Digital searching" - name and number of chapter
25
is back translation from Russian edition :))
27
as illustration of data structures, imagine next table:
29
static SYMBOL symbols[] = {
30
{ "ADD", SYM(ADD),0,0},
31
{ "AND", SYM(AND),0,0},
32
{ "DAY", SYM(DAY_SYM),0,0},
35
for this structure, presented program generate next searching-structure:
53
| symbols[2] ( "DAY" )
55
+----------+--+-+-+-+-+-+-+-+-+-+--+
56
| 2 char|d |e|f|j|h|i|j|k|l|m|n |
57
+----------+--+-+-+-+-+-+-+-+-+-+--+
58
|first_char|0 |0|0|0|0|0|0|0|0|0|0 |
59
|last_char |-1|0|0|0|0|0|0|0|0|0|-1|
60
|link |+ |0|0|0|0|0|0|0|0|0|+ |
63
symbols[0] ( "ADD" ) symbols[1] ( "AND" )
65
for optimization, link is the 16-bit index in 'symbols' or 'sql_functions'
68
So, we can read full search-structure as 32-bit word
72
use instead to_upper_lex, special array
73
(substitute chars) without skip codes..
75
try use reverse order of comparing..
79
#define NO_YACC_SYMBOLS
80
#include "my_global.h"
83
#ifndef __GNU_LIBRARY__
84
#define __GNU_LIBRARY__ // Skip warnings in getopt.h
86
#include <my_getopt.h>
87
#include "mysql_version.h"
90
const char *default_dbug_option="d:t:o,/tmp/gen_lex_hash.trace";
92
struct my_option my_long_options[] =
95
{"debug", '#', "This is a non-debug version. Catch this and exit",
96
0,0, 0, GET_DISABLED, OPT_ARG, 0, 0, 0, 0, 0, 0},
98
{"debug", '#', "Output debug log", (uchar**) &default_dbug_option,
99
(uchar**) &default_dbug_option, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
101
{"help", '?', "Display help and exit",
102
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
103
{"version", 'V', "Output version information and exit",
104
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
105
{0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
108
struct hash_lex_struct
113
hash_lex_struct *char_tails;
119
hash_lex_struct *get_hash_struct_by_len(hash_lex_struct **root_by_len,
120
int len, int *max_len)
123
*root_by_len= (hash_lex_struct *)realloc((char*)*root_by_len,
124
sizeof(hash_lex_struct)*len);
125
hash_lex_struct *cur, *end= *root_by_len + len;
126
for (cur= *root_by_len + *max_len; cur<end; cur++)
130
return (*root_by_len)+(len-1);
133
void insert_into_hash(hash_lex_struct *root, const char *name,
134
int len_from_begin, int index, int function)
136
hash_lex_struct *end, *cur, *tails;
138
if (!root->first_char)
140
root->first_char= -1;
141
root->iresult= index;
145
if (root->first_char == -1)
147
int index2= root->iresult;
148
const char *name2= (index2 < 0 ? sql_functions[-index2-1] :
149
symbols[index2]).name + len_from_begin;
150
root->first_char= (int) (uchar) name2[0];
151
root->last_char= (char) root->first_char;
152
tails= (hash_lex_struct*)malloc(sizeof(hash_lex_struct));
153
root->char_tails= tails;
154
tails->first_char= -1;
155
tails->iresult= index2;
158
size_t real_size= (root->last_char-root->first_char+1);
160
if (root->first_char>(*name))
162
size_t new_size= root->last_char-(*name)+1;
163
if (new_size<real_size) printf("error!!!!\n");
164
tails= root->char_tails;
165
tails= (hash_lex_struct*)realloc((char*)tails,
166
sizeof(hash_lex_struct)*new_size);
167
root->char_tails= tails;
168
memmove(tails+(new_size-real_size),tails,real_size*sizeof(hash_lex_struct));
169
end= tails + new_size - real_size;
170
for (cur= tails; cur<end; cur++)
172
root->first_char= (int) (uchar) *name;
175
if (root->last_char<(*name))
177
size_t new_size= (*name)-root->first_char+1;
178
if (new_size<real_size) printf("error!!!!\n");
179
tails= root->char_tails;
180
tails= (hash_lex_struct*)realloc((char*)tails,
181
sizeof(hash_lex_struct)*new_size);
182
root->char_tails= tails;
183
end= tails + new_size;
184
for (cur= tails+real_size; cur<end; cur++)
186
root->last_char= (*name);
189
insert_into_hash(root->char_tails+(*name)-root->first_char,
190
name+1,len_from_begin+1,index,function);
194
hash_lex_struct *root_by_len= 0;
197
hash_lex_struct *root_by_len2= 0;
200
void insert_symbols()
204
for (cur= symbols; i<array_elements(symbols); cur++, i++){
205
hash_lex_struct *root=
206
get_hash_struct_by_len(&root_by_len,cur->length,&max_len);
207
insert_into_hash(root,cur->name,0,i,0);
211
void insert_sql_functions()
215
for (cur= sql_functions; i < (int) array_elements(sql_functions); cur++, i++)
217
hash_lex_struct *root=
218
get_hash_struct_by_len(&root_by_len,cur->length,&max_len);
219
insert_into_hash(root,cur->name,0,-i-1,1);
225
SYMBOL *cur, *end= symbols + array_elements(symbols);
226
for (cur= symbols; cur < end; cur++)
227
cur->length=(uchar) strlen(cur->name);
228
end= sql_functions + array_elements(sql_functions);
229
for (cur= sql_functions; cur<end; cur++)
230
cur->length=(uchar) strlen(cur->name);
233
void generate_find_structs()
240
root_by_len2= root_by_len;
247
insert_sql_functions();
251
int size_hash_map= 0;
253
void add_struct_to_map(hash_lex_struct *st)
255
st->ithis= size_hash_map/4;
257
hash_map= (char*)realloc((char*)hash_map,size_hash_map);
258
hash_map[size_hash_map-4]= (char) (st->first_char == -1 ? 0 :
260
hash_map[size_hash_map-3]= (char) (st->first_char == -1 ||
261
st->first_char == 0 ? 0 : st->last_char);
262
if (st->first_char == -1)
264
hash_map[size_hash_map-2]= ((unsigned int)(int16)st->iresult)&255;
265
hash_map[size_hash_map-1]= ((unsigned int)(int16)st->iresult)>>8;
267
else if (st->first_char == 0)
269
hash_map[size_hash_map-2]= ((unsigned int)(int16)array_elements(symbols))&255;
270
hash_map[size_hash_map-1]= ((unsigned int)(int16)array_elements(symbols))>>8;
275
void add_structs_to_map(hash_lex_struct *st, int len)
277
hash_lex_struct *cur, *end= st+len;
278
for (cur= st; cur<end; cur++)
279
add_struct_to_map(cur);
280
for (cur= st; cur<end; cur++)
282
if (cur->first_char && cur->first_char != -1)
283
add_structs_to_map(cur->char_tails,cur->last_char-cur->first_char+1);
287
void set_links(hash_lex_struct *st, int len)
289
hash_lex_struct *cur, *end= st+len;
290
for (cur= st; cur<end; cur++)
292
if (cur->first_char != 0 && cur->first_char != -1)
294
int ilink= cur->char_tails->ithis;
295
hash_map[cur->ithis*4+2]= ilink%256;
296
hash_map[cur->ithis*4+3]= ilink/256;
297
set_links(cur->char_tails,cur->last_char-cur->first_char+1);
303
void print_hash_map(const char *name)
308
printf("static uchar %s[%d]= {\n",name,size_hash_map);
309
for (i=0, cur= hash_map; i<size_hash_map; i++, cur++)
316
printf("\'%c\', ",*cur);
318
case 2: printf("%u, ",(uint)(uchar)*cur); break;
319
case 3: printf("%u,\n",(uint)(uchar)*cur); break;
326
void print_find_structs()
328
add_structs_to_map(root_by_len,max_len);
329
set_links(root_by_len,max_len);
330
print_hash_map("sql_functions_map");
337
add_structs_to_map(root_by_len2,max_len2);
338
set_links(root_by_len2,max_len2);
339
print_hash_map("symbols_map");
343
static void usage(int version)
345
printf("%s Ver 3.6 Distrib %s, for %s (%s)\n",
346
my_progname, MYSQL_SERVER_VERSION, SYSTEM_TYPE, MACHINE_TYPE);
349
puts("Copyright (C) 2001 MySQL AB, by VVA and Monty");
350
puts("This software comes with ABSOLUTELY NO WARRANTY. This is free software,\n\
351
and you are welcome to modify and redistribute it under the GPL license\n");
352
puts("This program generates a perfect hashing function for the sql_lex.cc");
353
printf("Usage: %s [OPTIONS]\n\n", my_progname);
354
my_print_help(my_long_options);
359
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
360
char *argument __attribute__((unused)))
371
DBUG_PUSH(argument ? argument : default_dbug_option);
378
static int get_options(int argc, char **argv)
382
if ((ho_error= handle_options(&argc, &argv, my_long_options, get_one_option)))
394
int check_dup_symbols(SYMBOL *s1, SYMBOL *s2)
396
if (s1->length!=s2->length || strncmp(s1->name,s2->name,s1->length))
399
const char *err_tmpl= "\ngen_lex_hash fatal error : \
400
Unfortunately gen_lex_hash can not generate a hash,\n since \
401
your lex.h has duplicate definition for a symbol \"%s\"\n\n";
402
printf (err_tmpl,s1->name);
403
fprintf (stderr,err_tmpl,s1->name);
409
int check_duplicates()
411
SYMBOL *cur1, *cur2, *s_end, *f_end;
413
s_end= symbols + array_elements(symbols);
414
f_end= sql_functions + array_elements(sql_functions);
416
for (cur1= symbols; cur1<s_end; cur1++)
418
for (cur2= cur1+1; cur2<s_end; cur2++)
420
if (check_dup_symbols(cur1,cur2))
423
for (cur2= sql_functions; cur2<f_end; cur2++)
425
if (check_dup_symbols(cur1,cur2))
430
for (cur1= sql_functions; cur1<f_end; cur1++)
432
for (cur2= cur1+1; cur2< f_end; cur2++)
434
if (check_dup_symbols(cur1,cur2))
442
int main(int argc,char **argv)
445
DBUG_PROCESS(argv[0]);
447
if (get_options(argc,(char **) argv))
450
/* Broken up to indicate that it's not advice to you, gentle reader. */
451
printf("/*\n\n Do " "not " "edit " "this " "file " "directly!\n\n*/\n");
454
/* Copyright (C) 2001-2004 MySQL AB\n\
456
This program is free software; you can redistribute it and/or modify\n\
457
it under the terms of the GNU General Public License as published by\n\
458
the Free Software Foundation; version 2 of the License.\n\
460
This program is distributed in the hope that it will be useful,\n\
461
but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
462
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\
463
GNU General Public License for more details.\n\
465
You should have received a copy of the GNU General Public License\n\
466
along with this program; see the file COPYING. If not, write to the\n\
467
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston\n\
468
MA 02110-1301 USA. */\n\
472
/* Broken up to indicate that it's not advice to you, gentle reader. */
473
printf("/* Do " "not " "edit " "this " "file! This is generated by "
474
"gen_lex_hash.cc\nthat seeks for a perfect hash function */\n\n");
475
printf("#include \"lex.h\"\n\n");
479
if (check_duplicates())
482
generate_find_structs();
483
print_find_structs();
485
printf("\nstatic unsigned int sql_functions_max_len=%d;\n", max_len);
486
printf("\nstatic unsigned int symbols_max_len=%d;\n\n", max_len2);
489
static SYMBOL *get_hash_symbol(const char *s,\n\
490
unsigned int len,bool function)\n\
492
register uchar *hash_map;\n\
493
register const char *cur_str= s;\n\
496
DBUG_PRINT(\"warning\", (\"get_hash_symbol() received a request for a zero-length symbol, which is probably a mistake.\"));\
503
if (len>sql_functions_max_len) return 0;\n\
504
hash_map= sql_functions_map;\n\
505
register uint32 cur_struct= uint4korr(hash_map+((len-1)*4));\n\
508
register uchar first_char= (uchar)cur_struct;\n\
510
if (first_char == 0)\n\
512
register int16 ires= (int16)(cur_struct>>16);\n\
513
if (ires==array_elements(symbols)) return 0;\n\
514
register SYMBOL *res;\n\
516
res= symbols+ires;\n\
518
res= sql_functions-ires-1;\n\
519
register uint count= cur_str-s;\n\
520
return lex_casecmp(cur_str,res->name+count,len-count) ? 0 : res;\n\
523
register uchar cur_char= (uchar)to_upper_lex[(uchar)*cur_str];\n\
524
if (cur_char<first_char) return 0;\n\
526
if (cur_char>(uchar)cur_struct) return 0;\n\
529
cur_struct= uint4korr(hash_map+\n\
530
(((uint16)cur_struct + cur_char - first_char)*4));\n\
537
if (len>symbols_max_len) return 0;\n\
538
hash_map= symbols_map;\n\
539
register uint32 cur_struct= uint4korr(hash_map+((len-1)*4));\n\
542
register uchar first_char= (uchar)cur_struct;\n\
544
if (first_char==0){\n\
545
register int16 ires= (int16)(cur_struct>>16);\n\
546
if (ires==array_elements(symbols)) return 0;\n\
547
register SYMBOL *res= symbols+ires;\n\
548
register uint count= cur_str-s;\n\
549
return lex_casecmp(cur_str,res->name+count,len-count)!=0 ? 0 : res;\n\
552
register uchar cur_char= (uchar)to_upper_lex[(uchar)*cur_str];\n\
553
if (cur_char<first_char) return 0;\n\
555
if (cur_char>(uchar)cur_struct) return 0;\n\
558
cur_struct= uint4korr(hash_map+\n\
559
(((uint16)cur_struct + cur_char - first_char)*4));\n\