해시 함수의 디자인을 이해하지 못했습니다. 나는 예를 겪고 있었다. 함수 주석에서 볼 수 있듯이 곱셈 할 숫자로 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;
}
. [Apache Portable Runtime] (http://svn.apache.org/repos/asf/apr/apr/trunk/tables/apr_hash.c)에서 의견을 읽는 것이 좋습니다. – user7116