我一直不理解散列函数的设计。我正在通过一个例子。正如你在函数注释中看到的,为什么你应该选择31作为乘数。你如何决定?这是随机的还是意味着什么?好的散列函数
unsigned int hash(hash_table_t *hashtable, char *str)
{
unsigned int hashval;
/* we start our hash out at 0 */
hashval = 0;
/* for each character, we multiply the old hash by 31 and add the current
* character. Remember that shifting a number left is equivalent to
* multiplying it by 2 raised to the number of places shifted. So we
* are in effect multiplying hashval by 32 and then subtracting hashval.
* Why do we do this? Because shifting and subtraction are much more
* efficient operations than multiplication.
*/
for(; *str != '\0'; str++) hashval = *str + (hashval << 5) - hashval;
/* we then return the hash value mod the hashtable size so that it will
* fit into the necessary range
*/
return hashval % hashtable->size;
}
这就是被称为伯恩斯坦散,散托雷克,或简称为 “次33哈希”。我建议阅读[Apache Portable Runtime]中的注释(http://svn.apache.org/repos/asf/apr/apr/trunk/tables/apr_hash.c)。 – user7116