What is the best 32bit hash function for short strings (tag names)?

前端 未结 8 1461
傲寒
傲寒 2020-12-12 16:43

What is the best 32bit hash function for relatively short strings?

Strings are tag names that consist of English letters, numbers, spaces and some additional charact

8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-12 17:09

    I'm not sure if it's the best choice, but here is a hash function for strings:

    The Practice of Programming (HASH TABLES, pg. 57)

    /* hash: compute hash value of string */
    unsigned int hash(char *str)
    {
       unsigned int h;
       unsigned char *p;
    
       h = 0;
       for (p = (unsigned char*)str; *p != '\0'; p++)
          h = MULTIPLIER * h + *p;
       return h; // or, h % ARRAY_SIZE;
    }
    

    Empirically, the values 31 and 37 have proven to be good choices for the multiplier in a hash function for ASCII strings.

提交回复
热议问题