1
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems
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; version 2 of the License.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
The idea of presented algorithm see in
26
"The Art of Computer Programming" by Donald E. Knuth
27
Volume 3 "Sorting and searching"
28
(chapter 6.3 "Digital searching" - name and number of chapter
29
is back translation from Russian edition :))
31
as illustration of data structures, imagine next table:
33
static SYMBOL symbols[] = {
34
{ "ADD", SYM(ADD),0,0},
35
{ "AND", SYM(AND),0,0},
36
{ "DAY", SYM(DAY_SYM),0,0},
39
for this structure, presented program generate next searching-structure:
57
| symbols[2] ( "DAY" )
59
+----------+--+-+-+-+-+-+-+-+-+-+--+
60
| 2 char|d |e|f|j|h|i|j|k|l|m|n |
61
+----------+--+-+-+-+-+-+-+-+-+-+--+
62
|first_char|0 |0|0|0|0|0|0|0|0|0|0 |
63
|last_char |-1|0|0|0|0|0|0|0|0|0|-1|
64
|link |+ |0|0|0|0|0|0|0|0|0|+ |
67
symbols[0] ( "ADD" ) symbols[1] ( "AND" )
69
for optimization, link is the 16-bit index in 'symbols' or 'sql_functions'
72
So, we can read full search-structure as 32-bit word
76
use instead to_upper_lex, special array
77
(substitute chars) without skip codes..
79
try use reverse order of comparing..
83
#define NO_YACC_SYMBOLS
84
#include <drizzled/global.h>
85
#include <mysys/my_sys.h>
86
#include <mystrings/m_string.h>
87
#include <mysys/my_getopt.h>
89
#include <drizzled/lex.h>
93
const char *default_dbug_option="d:t:o,/tmp/gen_lex_hash.trace";
95
struct my_option my_long_options[] =
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) (unsigned char) 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) (unsigned char) *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=(unsigned char) strlen(cur->name);
224
end= sql_functions + array_elements(sql_functions);
225
for (cur= sql_functions; cur<end; cur++)
226
cur->length=(unsigned char) 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
/* Ok. I honestly don't know why this has no problem and
250
* array_elements macro doesn't. But it works.
252
static inline uint32_t array_elements_func(SYMBOL *symbol_arr) {
253
return sizeof(symbol_arr)/sizeof(symbol_arr[0]);
256
void add_struct_to_map(hash_lex_struct *st)
258
st->ithis= size_hash_map/4;
260
hash_map= (char*)realloc((char*)hash_map,size_hash_map);
261
hash_map[size_hash_map-4]= (char) (st->first_char == -1 ? 0 :
263
hash_map[size_hash_map-3]= (char) (st->first_char == -1 ||
264
st->first_char == 0 ? 0 : st->last_char);
265
if (st->first_char == -1)
267
hash_map[size_hash_map-2]= ((unsigned int)(int16_t)st->iresult)&255;
268
hash_map[size_hash_map-1]= ((unsigned int)(int16_t)st->iresult)>>8;
270
else if (st->first_char == 0)
272
hash_map[size_hash_map-2]= ((unsigned int)(int16_t)array_elements_func(symbols))&255;
273
hash_map[size_hash_map-1]= ((unsigned int)(int16_t)array_elements(symbols))>>8;
278
void add_structs_to_map(hash_lex_struct *st, int len)
280
hash_lex_struct *cur, *end= st+len;
281
for (cur= st; cur<end; cur++)
282
add_struct_to_map(cur);
283
for (cur= st; cur<end; cur++)
285
if (cur->first_char && cur->first_char != -1)
286
add_structs_to_map(cur->char_tails,cur->last_char-cur->first_char+1);
290
void set_links(hash_lex_struct *st, int len)
292
hash_lex_struct *cur, *end= st+len;
293
for (cur= st; cur<end; cur++)
295
if (cur->first_char != 0 && cur->first_char != -1)
297
int ilink= cur->char_tails->ithis;
298
hash_map[cur->ithis*4+2]= ilink%256;
299
hash_map[cur->ithis*4+3]= ilink/256;
300
set_links(cur->char_tails,cur->last_char-cur->first_char+1);
306
void print_hash_map(const char *name)
311
printf("static unsigned char %s[%d]= {\n",name,size_hash_map);
312
for (i=0, cur= hash_map; i<size_hash_map; i++, cur++)
319
printf("\'%c\', ",*cur);
321
case 2: printf("%u, ",(uint)(unsigned char)*cur); break;
322
case 3: printf("%u,\n",(uint)(unsigned char)*cur); break;
329
void print_find_structs()
331
add_structs_to_map(root_by_len,max_len);
332
set_links(root_by_len,max_len);
333
print_hash_map("sql_functions_map");
340
add_structs_to_map(root_by_len2,max_len2);
341
set_links(root_by_len2,max_len2);
342
print_hash_map("symbols_map");
346
static void usage(int version)
348
printf("%s Ver 3.6 Distrib %s, for %s (%s)\n",
349
my_progname, VERSION, SYSTEM_TYPE, MACHINE_TYPE);
352
puts("Copyright (C) 2008 Sun Microsystems, Inc.");
353
puts("This software comes with ABSOLUTELY NO WARRANTY. This is free software,\n\
354
and you are welcome to modify and redistribute it under the GPL license\n");
355
puts("This program generates a perfect hashing function for the sql_lex.cc");
356
printf("Usage: %s [OPTIONS]\n\n", my_progname);
357
my_print_help(my_long_options);
362
get_one_option(int optid, const struct my_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)
446
if (get_options(argc,(char **) argv))
449
/* Broken up to indicate that it's not advice to you, gentle reader. */
450
printf("/*\n\n Do " "not " "edit " "this " "file " "directly!\n\n*/\n");
452
printf("/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*- \n"
453
" * vim:expandtab:shiftwidth=2:tabstop=2:smarttab: \n"
455
" * Copyright (C) 2008 Sun Microsystems \n"
457
" * This program is free software; you can redistribute it and/or modify \n"
458
" * it under the terms of the GNU General Public License as published by \n"
459
" * the Free Software Foundation; version 2 of the License. \n"
461
" * This program is distributed in the hope that it will be useful, \n"
462
" * but WITHOUT ANY WARRANTY; without even the implied warranty of \n"
463
" * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the \n"
464
" * GNU General Public License for more details. \n"
466
" * You should have received a copy of the GNU General Public License \n"
467
" * along with this program; if not, write to the Free Software \n"
468
" * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA \n"
471
/* Broken up to indicate that it's not advice to you, gentle reader. */
472
printf("/* Do " "not " "edit " "this " "file! This is generated by "
473
"gen_lex_hash.cc\nthat seeks for a perfect hash function */\n\n");
474
printf("#include <drizzled/lex.h>\n\n");
478
if (check_duplicates())
481
generate_find_structs();
482
print_find_structs();
484
printf("\nstatic unsigned int sql_functions_max_len=%d;\n", max_len);
485
printf("\nstatic unsigned int symbols_max_len=%d;\n\n", max_len2);
488
static SYMBOL *get_hash_symbol(const char *s,\n\
489
unsigned int len,bool function)\n\
491
register unsigned char *hash_map;\n\
492
register const char *cur_str= s;\n\
501
if (len>sql_functions_max_len) return 0;\n\
502
hash_map= sql_functions_map;\n\
503
register uint32_t cur_struct= uint4korr(hash_map+((len-1)*4));\n\
506
register unsigned char first_char= (unsigned char)cur_struct;\n\
508
if (first_char == 0)\n\
510
register int16_t ires= (int16_t)(cur_struct>>16);\n\
511
if (ires==array_elements(symbols)) return 0;\n\
512
register SYMBOL *res;\n\
514
res= symbols+ires;\n\
516
res= sql_functions-ires-1;\n\
517
register uint32_t count= cur_str-s;\n\
518
return lex_casecmp(cur_str,res->name+count,len-count) ? 0 : res;\n\
521
register unsigned char cur_char= (unsigned char)to_upper_lex[(unsigned char)*cur_str];\n\
522
if (cur_char<first_char) return 0;\n\
524
if (cur_char>(unsigned char)cur_struct) return 0;\n\
527
cur_struct= uint4korr(hash_map+\n\
528
(((uint16_t)cur_struct + cur_char - first_char)*4));\n\
535
if (len>symbols_max_len) return 0;\n\
536
hash_map= symbols_map;\n\
537
register uint32_t cur_struct= uint4korr(hash_map+((len-1)*4));\n\
540
register unsigned char first_char= (unsigned char)cur_struct;\n\
542
if (first_char==0){\n\
543
register int16_t ires= (int16_t)(cur_struct>>16);\n\
544
if (ires==array_elements(symbols)) return 0;\n\
545
register SYMBOL *res= symbols+ires;\n\
546
register uint32_t count= cur_str-s;\n\
547
return lex_casecmp(cur_str,res->name+count,len-count)!=0 ? 0 : res;\n\
550
register unsigned char cur_char= (unsigned char)to_upper_lex[(unsigned char)*cur_str];\n\
551
if (cur_char<first_char) return 0;\n\
553
if (cur_char>(unsigned char)cur_struct) return 0;\n\
556
cur_struct= uint4korr(hash_map+\n\
557
(((uint16_t)cur_struct + cur_char - first_char)*4));\n\