I ran gperf on the list of LongNames that I maintain here in order to obtain a perfect hashing function for LongNames.

What is perfect hashing?

It is a hash function that maps elements to integers, with no collisions.

Here is a version that can be compiled. The original generated C code exploits fallthrough in switch statements.

hashFunc = Function[Typed[name, "String"],
   Module[{assoValues = {9826, 9826, 9826, 9826, 9826, 9826, 9826,
       9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826,
       9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826,
       9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826,
       9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826,
       9826, 9826, 9826, 10, 0, 9826, 9826, 9826, 9826, 9826, 9826,
       9826, 9826, 9826, 9826, 9826, 9826, 9826, 800, 1235, 0, 40,
       120, 10, 5, 400, 1280, 1015, 1155, 725, 1880, 845, 1875, 80,
       1510, 325, 15, 285, 900, 1595, 1325, 1270, 160, 395, 9826,
       9826, 9826, 9826, 9826, 9826, 0, 465, 45, 395, 115, 915, 1365,
       140, 5, 0, 1640, 190, 335, 5, 10, 405, 0, 325, 25, 30, 180,
       1060, 950, 510, 510, 720, 0, 9826, 9826, 9826, 9826, 9826,
       9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826,
       9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826,
       9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826,
       9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826,
       9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826,
       9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826,
       9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826,
       9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826,
       9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826,
       9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826,
       9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826,
       9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826,
       9826, 9826, 9826, 9826, 9826, 9826, 9826, 9826}, len, hval,
     fallthrough, points},
    len = StringLength[name];
    points = ToCharacterCode[name];
    hval = len;
    fallthrough = False;
    If[hval > 14,
     hval += assoValues[[points[[14 + 1]] + 1 + 1]];
     fallthrough = True
     ];
    If[hval == 14 || fallthrough,
     hval += assoValues[[points[[13 + 1]] + 1]];
     fallthrough = True
     ];
    If[hval == 9 || hval == 10 || hval == 11 || hval == 12 ||
      hval == 13 || fallthrough,
     hval += assoValues[[points[[8 + 1]] + 1 + 1]];
     fallthrough = True
     ];
    If[hval == 8 || fallthrough,
     hval += assoValues[[points[[7 + 1]] + 1]];
     fallthrough = True
     ];
    If[hval == 7 || fallthrough,
     hval += assoValues[[points[[6 + 1]] + 1]];
     fallthrough = True
     ];
    If[hval == 4 || hval == 5 || hval == 6 || fallthrough,
     hval += assoValues[[points[[3 + 1]] + 1 + 1]];
     fallthrough = True
     ];
    If[hval == 2 || hval == 3 || fallthrough,
     hval += assoValues[[points[[1 + 1]] + 1]];
     fallthrough = True
     ];
    If[hval == 1 || fallthrough,
     hval += assoValues[[points[[0 + 1]] + 1]];
     ];
    hval += assoValues[[points[[len - 1 + 1]] + 1]];
    hval
    ]
   ];


hashCompiledFunc = FunctionCompile[hashFunc];


In[5]:= hashCompiledFunc["Alpha"]

Out[5]= 1000

In[6]:= hashCompiledFunc["Beta"]

Out[6]= 1819

In[8]:= hashCompiledFunc["Or"]

Out[8]= 2527

In[9]:= hashCompiledFunc["CounterClockwiseContourIntegral"]

Out[9]= 936

In[10]:= hashCompiledFunc["DiscretionaryParagraphSeparator"]

Out[10]= 1781

Updated: