这个哈希函数是如何实现映射的?
在某开源软件上扣下来的:
/* Hashing of numeric values, such as pointers and integers.
This implementation is the Robert Jenkins ' 32 bit Mix Function,
with a simple adaptation for 64-bit values. It offers excellent
spreading of values and doesn 't need to know the hash table size to
work (unlike the very popular Knuth 's multiplication hash). */
/*哈希函数*/
unsigned long
hash_pointer (const void *ptr)
{
unsigned long key = (unsigned long)ptr;
key += (key < < 12);
key ^= (key > > 22);
key += (key < < 4);
key ^= (key > > 9);
key += (key < < 10);
key ^= (key > > 2);
key += (key < < 7);
key ^= (key > > 12);
#if SIZEOF_LONG > 4
key += (key < < 44);
key ^= (key > > 54);
key += (key < < 36);
key ^= (key > > 41);
key += (key < < 42);
key ^= (key > > 34);
key += (key < < 39);
key ^= (key > > 44);
#endif
return key;
}
[解决办法]
把一个字符串的地址当作哈希表的初始值