哈希函数原理及实现
哈希解決沖突
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 usingthe 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
總結
- 上一篇: 内存地址转换与分段
- 下一篇: Win32 环境下的堆栈