哈希解决冲突
1000以内的素数
一般的hash实现已经总结出一些比较重要的素数:
static unsigned int table_size[] = {7,13, 31, 61, 127, 251, 509, 1021, 2039, 4093, 8191, 16381, 32749, 65521, 131071, 262143, 524287, 1048575, 2097151, 4194303, 8388607, 16777211, 33554431, 67108863, 134217727, 268435455, 536870911, 1073741823, 2147483647, 0 };
--------------以上出自《算法精解 C语言描述》
下面这个hash函数是一个能够较好的处理字符串的哈希函数,它通过一系列的位操作将健强制转换为整数。此函数改编自《compilers principles techniques and tools》
也就是《编译原理》--龙书
int hashpjw(const void *key,size_t PRIME_TBLSIZ) { const char *ptr; int val; /***************************************************************************** * * * Hash the key by performing a number of bit operations on it. * * * *****************************************************************************/ val = 0; ptr = key; while (*ptr != '\0') { int tmp; val = (val << 4) + (*ptr); if (tmp = (val & 0xf0000000)) { val = val ^ (tmp >> 24); val = val ^ tmp; } ptr++; } /***************************************************************************** * * * In practice, replace PRIME_TBLSIZ with the actual table size. * * * *****************************************************************************/ return val % PRIME_TBLSIZ; }
《算法精解 C语言描述》源码地址:https://github.com/Caloni/MasteringCAlgorithms
http://technologyx.org/sourcecode/candcplusplus/c/hashpjw-c/
下面这个哈希函数来自MIT:http://web.mit.edu/majapw/MacData/afs/sipb/user/ssen/src/coreutils-5.2.1/lib/hash-pjw.c
#define SIZE_BITS (sizeof (size_t) * CHAR_BIT) /* A hash function for NUL-terminated char* strings using the method described by Bruno Haible. See http://www.haible.de/bruno/hashfunc.html. */ size_t hash_pjw (const void *x, size_t tablesize) { const char *s; size_t h = 0; for (s = x; *s; s++) h = *s + ((h << 9) | (h >> (SIZE_BITS - 9))); return h % tablesize; }
MIT那个项目还有一个hash函数的完整实现:http://web.mit.edu/majapw/MacData/afs/sipb/user/ssen/src/coreutils-5.2.1/lib/hash.c