1
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult 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; either version 2 of the License, or
6
(at your option) any later version.
8
This program is distributed in the hope that it will be useful,
9
but WITHOUT ANY WARRANTY; without even the implied warranty of
10
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
GNU General Public License for more details.
13
You should have received a copy of the GNU General Public License
14
along with this program; if not, write to the Free Software
15
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
19
The idea of presented algorithm see in
20
"The Art of Computer Programming" by Donald E. Knuth
21
Volume 3 "Sorting and searching"
22
(chapter 6.3 "Digital searching" - name and number of chapter
23
is back translation from Russian edition :))
25
as illustration of data structures, imagine next table:
27
static SYMBOL symbols[] = {
28
{ "ADD", SYM(ADD),0,0},
29
{ "AND", SYM(AND),0,0},
30
{ "DAY", SYM(DAY_SYM),0,0},
33
for this structure, presented program generate next searching-structure:
51
| symbols[2] ( "DAY" )
53
+----------+--+-+-+-+-+-+-+-+-+-+--+
54
| 2 char|d |e|f|j|h|i|j|k|l|m|n |
55
+----------+--+-+-+-+-+-+-+-+-+-+--+
56
|first_char|0 |0|0|0|0|0|0|0|0|0|0 |
57
|last_char |-1|0|0|0|0|0|0|0|0|0|-1|
58
|link |+ |0|0|0|0|0|0|0|0|0|+ |
61
symbols[0] ( "ADD" ) symbols[1] ( "AND" )
63
for optimization, link is the 16-bit index in 'symbols' or 'sql_functions'
66
So, we can read full search-structure as 32-bit word
69
1. use instead to_upper_lex, special array
70
(substitute chars) without skip codes..
71
2. try use reverse order of comparing..
75
#define NO_YACC_SYMBOLS
76
#include "my_global.h"
79
#ifndef __GNU_LIBRARY__
80
#define __GNU_LIBRARY__ // Skip warnings in getopt.h
82
#include <my_getopt.h>
83
#include "mysql_version.h"
86
const char *default_dbug_option="d:t:o,/tmp/gen_lex_hash.trace";
88
struct my_option my_long_options[] =
91
{"debug", '#', "This is a non-debug version. Catch this and exit",
92
0,0, 0, GET_DISABLED, OPT_ARG, 0, 0, 0, 0, 0, 0},
94
{"debug", '#', "Output debug log", (gptr*) &default_dbug_option,
95
(gptr*) &default_dbug_option, 0, GET_STR, OPT_ARG, 0, 0, 0, 0, 0, 0},
97
{"help", '?', "Display help and exit",
98
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
99
{"version", 'V', "Output version information and exit",
100
0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0},
101
{0, 0, 0, 0, 0, 0, GET_NO_ARG, NO_ARG, 0, 0, 0, 0, 0, 0}
104
struct hash_lex_struct
109
hash_lex_struct *char_tails;
115
hash_lex_struct *get_hash_struct_by_len(hash_lex_struct **root_by_len,
116
int len, int *max_len)
119
*root_by_len= (hash_lex_struct *)realloc((char*)*root_by_len,
120
sizeof(hash_lex_struct)*len);
121
hash_lex_struct *cur, *end= *root_by_len + len;
122
for (cur= *root_by_len + *max_len; cur<end; cur++)
126
return (*root_by_len)+(len-1);
129
void insert_into_hash(hash_lex_struct *root, const char *name,
130
int len_from_begin, int index, int function)
132
hash_lex_struct *end, *cur, *tails;
134
if (!root->first_char)
136
root->first_char= -1;
137
root->iresult= index;
141
if (root->first_char == -1)
143
int index2= root->iresult;
144
const char *name2= (index2 < 0 ? sql_functions[-index2-1] :
145
symbols[index2]).name + len_from_begin;
146
root->first_char= (int) (uchar) name2[0];
147
root->last_char= (char) root->first_char;
148
tails= (hash_lex_struct*)malloc(sizeof(hash_lex_struct));
149
root->char_tails= tails;
150
tails->first_char= -1;
151
tails->iresult= index2;
154
size_t real_size= (root->last_char-root->first_char+1);
156
if (root->first_char>(*name))
158
size_t new_size= root->last_char-(*name)+1;
159
if (new_size<real_size) printf("error!!!!\n");
160
tails= root->char_tails;
161
tails= (hash_lex_struct*)realloc((char*)tails,
162
sizeof(hash_lex_struct)*new_size);
163
root->char_tails= tails;
164
memmove(tails+(new_size-real_size),tails,real_size*sizeof(hash_lex_struct));
165
end= tails + new_size - real_size;
166
for (cur= tails; cur<end; cur++)
168
root->first_char= (int) (uchar) *name;
171
if (root->last_char<(*name))
173
size_t new_size= (*name)-root->first_char+1;
174
if (new_size<real_size) printf("error!!!!\n");
175
tails= root->char_tails;
176
tails= (hash_lex_struct*)realloc((char*)tails,
177
sizeof(hash_lex_struct)*new_size);
178
root->char_tails= tails;
179
end= tails + new_size;
180
for (cur= tails+real_size; cur<end; cur++)
182
root->last_char= (*name);
185
insert_into_hash(root->char_tails+(*name)-root->first_char,
186
name+1,len_from_begin+1,index,function);
190
hash_lex_struct *root_by_len= 0;
193
hash_lex_struct *root_by_len2= 0;
196
void insert_symbols()
200
for (cur= symbols; i<array_elements(symbols); cur++, i++){
201
hash_lex_struct *root=
202
get_hash_struct_by_len(&root_by_len,cur->length,&max_len);
203
insert_into_hash(root,cur->name,0,i,0);
207
void insert_sql_functions()
211
for (cur= sql_functions; i < (int) array_elements(sql_functions); cur++, i++)
213
hash_lex_struct *root=
214
get_hash_struct_by_len(&root_by_len,cur->length,&max_len);
215
insert_into_hash(root,cur->name,0,-i-1,1);
221
SYMBOL *cur, *end= symbols + array_elements(symbols);
222
for (cur= symbols; cur < end; cur++)
223
cur->length=(uchar) strlen(cur->name);
224
end= sql_functions + array_elements(sql_functions);
225
for (cur= sql_functions; cur<end; cur++)
226
cur->length=(uchar) strlen(cur->name);
229
void generate_find_structs()
236
root_by_len2= root_by_len;
243
insert_sql_functions();
247
int size_hash_map= 0;
249
void add_struct_to_map(hash_lex_struct *st)
251
st->ithis= size_hash_map/4;
253
hash_map= (char*)realloc((char*)hash_map,size_hash_map);
254
hash_map[size_hash_map-4]= (char) (st->first_char == -1 ? 0 :
256
hash_map[size_hash_map-3]= (char) (st->first_char == -1 ||
257
st->first_char == 0 ? 0 : st->last_char);
258
if (st->first_char == -1)
260
hash_map[size_hash_map-2]= ((unsigned int)(int16)st->iresult)&255;
261
hash_map[size_hash_map-1]= ((unsigned int)(int16)st->iresult)>>8;
263
else if (st->first_char == 0)
265
hash_map[size_hash_map-2]= ((unsigned int)(int16)array_elements(symbols))&255;
266
hash_map[size_hash_map-1]= ((unsigned int)(int16)array_elements(symbols))>>8;
271
void add_structs_to_map(hash_lex_struct *st, int len)
273
hash_lex_struct *cur, *end= st+len;
274
for (cur= st; cur<end; cur++)
275
add_struct_to_map(cur);
276
for (cur= st; cur<end; cur++)
278
if (cur->first_char && cur->first_char != -1)
279
add_structs_to_map(cur->char_tails,cur->last_char-cur->first_char+1);
283
void set_links(hash_lex_struct *st, int len)
285
hash_lex_struct *cur, *end= st+len;
286
for (cur= st; cur<end; cur++)
288
if (cur->first_char != 0 && cur->first_char != -1)
290
int ilink= cur->char_tails->ithis;
291
hash_map[cur->ithis*4+2]= ilink%256;
292
hash_map[cur->ithis*4+3]= ilink/256;
293
set_links(cur->char_tails,cur->last_char-cur->first_char+1);
299
void print_hash_map(const char *name)
304
printf("static uchar %s[%d]= {\n",name,size_hash_map);
305
for (i=0, cur= hash_map; i<size_hash_map; i++, cur++)
312
printf("\'%c\', ",*cur);
314
case 2: printf("%u, ",(uint)(uchar)*cur); break;
315
case 3: printf("%u,\n",(uint)(uchar)*cur); break;
322
void print_find_structs()
324
add_structs_to_map(root_by_len,max_len);
325
set_links(root_by_len,max_len);
326
print_hash_map("sql_functions_map");
333
add_structs_to_map(root_by_len2,max_len2);
334
set_links(root_by_len2,max_len2);
335
print_hash_map("symbols_map");
339
static void usage(int version)
341
printf("%s Ver 3.6 Distrib %s, for %s (%s)\n",
342
my_progname, MYSQL_SERVER_VERSION, SYSTEM_TYPE, MACHINE_TYPE);
345
puts("Copyright (C) 2001 MySQL AB, by VVA and Monty");
346
puts("This software comes with ABSOLUTELY NO WARRANTY. This is free software,\n\
347
and you are welcome to modify and redistribute it under the GPL license\n");
348
puts("This program generates a perfect hashing function for the sql_lex.cc");
349
printf("Usage: %s [OPTIONS]\n\n", my_progname);
350
my_print_help(my_long_options);
355
get_one_option(int optid, const struct my_option *opt __attribute__((unused)),
356
char *argument __attribute__((unused)))
367
DBUG_PUSH(argument ? argument : default_dbug_option);
374
static int get_options(int argc, char **argv)
378
if ((ho_error= handle_options(&argc, &argv, my_long_options, get_one_option)))
390
int check_dup_symbols(SYMBOL *s1, SYMBOL *s2)
392
if (s1->length!=s2->length || strncmp(s1->name,s2->name,s1->length))
395
const char *err_tmpl= "\ngen_lex_hash fatal error : \
396
Unfortunately gen_lex_hash can not generate a hash,\n since \
397
your lex.h has duplicate definition for a symbol \"%s\"\n\n";
398
printf (err_tmpl,s1->name);
399
fprintf (stderr,err_tmpl,s1->name);
405
int check_duplicates()
407
SYMBOL *cur1, *cur2, *s_end, *f_end;
409
s_end= symbols + array_elements(symbols);
410
f_end= sql_functions + array_elements(sql_functions);
412
for (cur1= symbols; cur1<s_end; cur1++)
414
for (cur2= cur1+1; cur2<s_end; cur2++)
416
if (check_dup_symbols(cur1,cur2))
419
for (cur2= sql_functions; cur2<f_end; cur2++)
421
if (check_dup_symbols(cur1,cur2))
426
for (cur1= sql_functions; cur1<f_end; cur1++)
428
for (cur2= cur1+1; cur2< f_end; cur2++)
430
if (check_dup_symbols(cur1,cur2))
438
int main(int argc,char **argv)
441
DBUG_PROCESS(argv[0]);
443
if (get_options(argc,(char **) argv))
446
/* Broken up to indicate that it's not advice to you, gentle reader. */
447
printf("/*\n\n Do " "not " "edit " "this " "file " "directly!\n\n*/\n");
449
printf("/* Copyright (C) 2001-2004 MySQL AB\n\
450
This software comes with ABSOLUTELY NO WARRANTY. This is free software,\n\
451
and you are welcome to modify and redistribute it under the GPL license\n\
454
/* Broken up to indicate that it's not advice to you, gentle reader. */
455
printf("/* Do " "not " "edit " "this " "file! This is generated by "
456
"gen_lex_hash.cc\nthat seeks for a perfect hash function */\n\n");
457
printf("#include \"lex.h\"\n\n");
461
if (check_duplicates())
464
generate_find_structs();
465
print_find_structs();
467
printf("\nstatic unsigned int sql_functions_max_len=%d;\n", max_len);
468
printf("\nstatic unsigned int symbols_max_len=%d;\n\n", max_len2);
471
static inline SYMBOL *get_hash_symbol(const char *s,\n\
472
unsigned int len,bool function)\n\
474
register uchar *hash_map;\n\
475
register const char *cur_str= s;\n\
478
DBUG_PRINT(\"warning\", (\"get_hash_symbol() received a request for a zero-length symbol, which is probably a mistake.\"));\
485
if (len>sql_functions_max_len) return 0;\n\
486
hash_map= sql_functions_map;\n\
487
register uint32 cur_struct= uint4korr(hash_map+((len-1)*4));\n\
490
register uchar first_char= (uchar)cur_struct;\n\
492
if (first_char == 0)\n\
494
register int16 ires= (int16)(cur_struct>>16);\n\
495
if (ires==array_elements(symbols)) return 0;\n\
496
register SYMBOL *res;\n\
498
res= symbols+ires;\n\
500
res= sql_functions-ires-1;\n\
501
register uint count= cur_str-s;\n\
502
return lex_casecmp(cur_str,res->name+count,len-count) ? 0 : res;\n\
505
register uchar cur_char= (uchar)to_upper_lex[(uchar)*cur_str];\n\
506
if (cur_char<first_char) return 0;\n\
508
if (cur_char>(uchar)cur_struct) return 0;\n\
511
cur_struct= uint4korr(hash_map+\n\
512
(((uint16)cur_struct + cur_char - first_char)*4));\n\
519
if (len>symbols_max_len) return 0;\n\
520
hash_map= symbols_map;\n\
521
register uint32 cur_struct= uint4korr(hash_map+((len-1)*4));\n\
524
register uchar first_char= (uchar)cur_struct;\n\
526
if (first_char==0){\n\
527
register int16 ires= (int16)(cur_struct>>16);\n\
528
if (ires==array_elements(symbols)) return 0;\n\
529
register SYMBOL *res= symbols+ires;\n\
530
register uint count= cur_str-s;\n\
531
return lex_casecmp(cur_str,res->name+count,len-count)!=0 ? 0 : res;\n\
534
register uchar cur_char= (uchar)to_upper_lex[(uchar)*cur_str];\n\
535
if (cur_char<first_char) return 0;\n\
537
if (cur_char>(uchar)cur_struct) return 0;\n\
540
cur_struct= uint4korr(hash_map+\n\
541
(((uint16)cur_struct + cur_char - first_char)*4));\n\