Hi there!
I have to do a B-tree, which can use strings or ints as keys. Anyway, keys can be repeated, and I have to use a has function in order to converto from string to int...
My teacher said he doesn't care about the function we use, and that he wouldn't care if we used an already done one. He even told us we didn't loose time in implementing one ourselves.
The thing is, I haven't find one... COuld you help me?
It just has to work, no need of it to be efficient.
THANKS!! :D :D
Good hash functions are written by alchemists, not computer scientists ;-)
It's a dark branch where creativity is badly needed. I prefer a simple hash
function that does fairly well. Have a look at this one (I assume C code):
-
int hashCode(char* c) {
-
int hash= 0;
-
if (!c) return 0;
-
for (; *c; c++)
-
hash= (hash<<1)^*c^((hash>>21)&0x3ff);
-
return hash;
-
}
This function slowly rotates the 'hash' value while 'xoring' every character to it.
The 11 most significant bits are also 'xored' in therefore all characters are
significant in the resulting hash value.
kind regards,
Jos