I am developing a programming language, and in my programming language I store objects as hash tables. The hash function I use is Pearson Hashing , which depends on a 256-bit lookup table. Here's the function:
char* pearson(char* name, char* lookup)
{
char index = '\0';
while(*name)
{
index = lookup[index ^ *name];
name++;
}
return index;
}
My question is, with a fixed group of less than 256 member names, how can I define a table lookupto pearson()return unique characters in an adjacent range, starting at '\0'. In other words, I need an algorithm to create a lookup table for the perfect hash . This will allow me to have objects that take up no more space than the number of their members. This will be done at compile time, so speed is not a big concern, but faster will be better. It would be easy to overdo it, but I think (hopefully) there is a better way.
Here is an example: the given member variables "foo", "bar" and "baz" in the class, I want to define a lookupso that:
pearson('foo',lookup) == (char) 0
pearson('bar',lookup) == (char) 1
pearson('baz',lookup) == (char) 2
, , :
pearson('foo',lookup) == (char) 2
pearson('bar',lookup) == (char) 0
pearson('baz',lookup) == (char) 1
, , 2, , , , , , , , . , , , , ( , , , , , ).