Perfect HASH functions.....

Dave Jones djones at megatest.UUCP
Sat Sep 23 12:16:19 AEST 1989


>From article <9900014 at bradley>, by vijay at bradley.UUCP:
> 
> Hello,      
> 
>    I hope you can help me. Does anyone out there have a perfect
> hash function? The key is a string of characters (maximum length
> is 4, minimum is 2) ...

How about another idea?

For a similar application, I recently wrote a little program which
generates a big switch statement for recognizing strings. You give
it a list of strings, each paired with its enumeration value, and
it spits out the switch.  It's real fast. It helps if you optimize
away jumps to jumps, of course. It only took about an hour to
write the thing. Here's the first few lines of a switch it generated
for recognizing Pascal keywords in lower case:

switch(*str++) /*  */
{ default: break;
  case 'a': /* a */
   switch(*str++) /* a */
   { default: break;
     case 'n': /* an */
      switch(*str++) /* an */
      { default: break;
        case 'd': /* and */
         if(length==3)type = 307;
        break;
      }
     break;
     case 'r': /* ar */
      switch(*str++) /* ar */
      { default: break;
        case 'r': /* arr */
         switch(*str++) /* arr */
         { default: break;
           case 'a': /* arra */
            switch(*str++) /* arra */
            { default: break;
              case 'y': /* array */
               if(length==5)type = 257;
              break;
            }
           break;
         }
        break;
      }
     break;
   }
  break;


etc...



More information about the Comp.lang.c mailing list