80 #define NO_YACC_SYMBOLS
81 #include <my_global.h>
82 #include "mysql_version.h"
88 #include <welcome_copyright_notice.h>
102 int len,
int *max_len)
108 for (cur= *root_by_len + *max_len; cur<end; cur++)
112 return (*root_by_len)+(len-1);
116 int len_from_begin,
int index,
int function)
120 if (!root->first_char)
122 root->first_char= -1;
123 root->iresult=
index;
127 if (root->first_char == -1)
129 int index2= root->iresult;
130 const char *name2= (index2 < 0 ? sql_functions[-index2-1] :
131 symbols[index2]).name + len_from_begin;
132 root->first_char= (int) (uchar) name2[0];
133 root->last_char= (char) root->first_char;
135 root->char_tails= tails;
136 tails->first_char= -1;
137 tails->iresult= index2;
140 size_t real_size= (root->last_char-root->first_char+1);
142 if (root->first_char>(*name))
144 size_t new_size= root->last_char-(*name)+1;
145 if (new_size<real_size) printf(
"error!!!!\n");
146 tails= root->char_tails;
149 root->char_tails= tails;
150 memmove(tails+(new_size-real_size),tails,real_size*
sizeof(
hash_lex_struct));
151 end= tails + new_size - real_size;
152 for (cur= tails; cur<end; cur++)
154 root->first_char= (
int) (uchar) *name;
157 if (root->last_char<(*name))
159 size_t new_size= (*name)-root->first_char+1;
160 if (new_size<real_size) printf(
"error!!!!\n");
161 tails= root->char_tails;
164 root->char_tails= tails;
165 end= tails + new_size;
166 for (cur= tails+real_size; cur<end; cur++)
168 root->last_char= (*name);
171 insert_into_hash(root->char_tails+(*name)-root->first_char,
172 name+1,len_from_begin+1,index,
function);
182 void insert_symbols()
186 for (cur= symbols; i<array_elements(symbols); cur++, i++){
188 get_hash_struct_by_len(&root_by_len,cur->length,&max_len);
189 insert_into_hash(root,cur->name,0,(uint) i,0);
193 void insert_sql_functions()
197 for (cur= sql_functions; i < (int) array_elements(sql_functions); cur++, i++)
200 get_hash_struct_by_len(&root_by_len,cur->length,&max_len);
201 insert_into_hash(root,cur->name,0,-i-1,1);
207 SYMBOL *cur, *end= symbols + array_elements(symbols);
208 for (cur= symbols; cur < end; cur++)
209 cur->length=(uchar) strlen(cur->name);
210 end= sql_functions + array_elements(sql_functions);
211 for (cur= sql_functions; cur<end; cur++)
212 cur->length=(uchar) strlen(cur->name);
215 void generate_find_structs()
222 root_by_len2= root_by_len;
229 insert_sql_functions();
233 int size_hash_map= 0;
237 st->ithis= size_hash_map/4;
239 hash_map= (
char*)realloc((
char*)hash_map,size_hash_map);
240 hash_map[size_hash_map-4]= (char) (st->first_char == -1 ? 0 :
242 hash_map[size_hash_map-3]= (char) (st->first_char == -1 ||
243 st->first_char == 0 ? 0 : st->last_char);
244 if (st->first_char == -1)
246 hash_map[size_hash_map-2]= ((
unsigned int)(int16)st->iresult)&255;
247 hash_map[size_hash_map-1]= ((
unsigned int)(int16)st->iresult)>>8;
249 else if (st->first_char == 0)
251 hash_map[size_hash_map-2]= ((
unsigned int)(int16)array_elements(symbols))&255;
252 hash_map[size_hash_map-1]= ((
unsigned int)(int16)array_elements(symbols))>>8;
260 for (cur= st; cur<end; cur++)
261 add_struct_to_map(cur);
262 for (cur= st; cur<end; cur++)
264 if (cur->first_char && cur->first_char != -1)
265 add_structs_to_map(cur->char_tails,cur->last_char-cur->first_char+1);
272 for (cur= st; cur<end; cur++)
274 if (cur->first_char != 0 && cur->first_char != -1)
276 int ilink= cur->char_tails->ithis;
277 hash_map[cur->ithis*4+2]= ilink%256;
278 hash_map[cur->ithis*4+3]= ilink/256;
279 set_links(cur->char_tails,cur->last_char-cur->first_char+1);
285 void print_hash_map(
const char *name)
290 printf(
"static uchar %s[%d]= {\n",name,size_hash_map);
291 for (i=0, cur= hash_map; i<size_hash_map; i++, cur++)
298 printf(
"\'%c\', ",*cur);
300 case 2: printf(
"%u, ",(uint)(uchar)*cur);
break;
301 case 3: printf(
"%u,\n",(uint)(uchar)*cur);
break;
308 void print_find_structs()
310 add_structs_to_map(root_by_len,max_len);
311 set_links(root_by_len,max_len);
312 print_hash_map(
"sql_functions_map");
319 add_structs_to_map(root_by_len2,max_len2);
320 set_links(root_by_len2,max_len2);
321 print_hash_map(
"symbols_map");
326 if (s1->length!=s2->length || strncmp(s1->name,s2->name,s1->length))
329 const char *err_tmpl=
"\ngen_lex_hash fatal error : \
330 Unfortunately gen_lex_hash can not generate a hash,\n since \
331 your lex.h has duplicate definition for a symbol \"%s\"\n\n";
332 printf (err_tmpl,s1->name);
333 fprintf (stderr,err_tmpl,s1->name);
339 int check_duplicates()
341 SYMBOL *cur1, *cur2, *s_end, *f_end;
343 s_end= symbols + array_elements(symbols);
344 f_end= sql_functions + array_elements(sql_functions);
346 for (cur1= symbols; cur1<s_end; cur1++)
348 for (cur2= cur1+1; cur2<s_end; cur2++)
350 if (check_dup_symbols(cur1,cur2))
353 for (cur2= sql_functions; cur2<f_end; cur2++)
355 if (check_dup_symbols(cur1,cur2))
360 for (cur1= sql_functions; cur1<f_end; cur1++)
362 for (cur2= cur1+1; cur2< f_end; cur2++)
364 if (check_dup_symbols(cur1,cur2))
372 int main(
int argc,
char **argv)
377 printf(
"/*\n\n Do " "not " "edit " "this " "file " "directly!\n\n*/\n");
380 puts(ORACLE_WELCOME_COPYRIGHT_NOTICE(
"2000"));
384 printf(
"/* Do " "not " "edit " "this " "file! This is generated by "
385 "gen_lex_hash.cc\nthat seeks for a perfect hash function */\n\n");
386 printf(
"#include \"lex.h\"\n\n");
390 if (check_duplicates())
393 generate_find_structs();
394 print_find_structs();
396 printf(
"\nstatic unsigned int sql_functions_max_len=%d;\n", max_len);
397 printf(
"\nstatic unsigned int symbols_max_len=%d;\n\n", max_len2);
400 static SYMBOL *get_hash_symbol(const char *s,\n\
401 unsigned int len,bool function)\n\
403 register uchar *hash_map;\n\
404 register const char *cur_str= s;\n\
407 DBUG_PRINT(\"warning\", (\"get_hash_symbol() received a request for a zero-length symbol, which is probably a mistake.\"));\
414 if (len>sql_functions_max_len) return 0;\n\
415 hash_map= sql_functions_map;\n\
416 register uint32 cur_struct= uint4korr(hash_map+((len-1)*4));\n\
419 register uchar first_char= (uchar)cur_struct;\n\
421 if (first_char == 0)\n\
423 register int16 ires= (int16)(cur_struct>>16);\n\
424 if (ires==array_elements(symbols)) return 0;\n\
425 register SYMBOL *res;\n\
427 res= symbols+ires;\n\
429 res= sql_functions-ires-1;\n\
430 register uint count= (uint) (cur_str - s);\n\
431 return lex_casecmp(cur_str,res->name+count,len-count) ? 0 : res;\n\
434 register uchar cur_char= (uchar)to_upper_lex[(uchar)*cur_str];\n\
435 if (cur_char<first_char) return 0;\n\
437 if (cur_char>(uchar)cur_struct) return 0;\n\
440 cur_struct= uint4korr(hash_map+\n\
441 (((uint16)cur_struct + cur_char - first_char)*4));\n\
448 if (len>symbols_max_len) return 0;\n\
449 hash_map= symbols_map;\n\
450 register uint32 cur_struct= uint4korr(hash_map+((len-1)*4));\n\
453 register uchar first_char= (uchar)cur_struct;\n\
455 if (first_char==0){\n\
456 register int16 ires= (int16)(cur_struct>>16);\n\
457 if (ires==array_elements(symbols)) return 0;\n\
458 register SYMBOL *res= symbols+ires;\n\
459 register uint count= (uint) (cur_str - s);\n\
460 return lex_casecmp(cur_str,res->name+count,len-count)!=0 ? 0 : res;\n\
463 register uchar cur_char= (uchar)to_upper_lex[(uchar)*cur_str];\n\
464 if (cur_char<first_char) return 0;\n\
466 if (cur_char>(uchar)cur_struct) return 0;\n\
469 cur_struct= uint4korr(hash_map+\n\
470 (((uint16)cur_struct + cur_char - first_char)*4));\n\