字符串哈希函数 【ZZ http://www.byvoid.com/blog/string-hash-compare/】

 1 unsigned int BKDRHash(char *str)

 2 {

 3     unsigned int seed = 131; // 31 131 1313 13131 131313 etc..

 4     unsigned int hash = 0;

 5  

 6     while (*str)

 7     {

 8         hash = hash * seed + (*str++);

 9     }

10  

11     return (hash & 0x7FFFFFFF);

12 }

 

 1 unsigned int APHash(char *str)

 2 {

 3     unsigned int hash = 0;

 4     int i;

 5  

 6     for (i=0; *str; i++)

 7     {

 8         if ((i & 1) == 0)

 9         {

10             hash ^= ((hash << 7) ^ (*str++) ^ (hash >> 3));

11         }

12         else

13         {

14             hash ^= (~((hash << 11) ^ (*str++) ^ (hash >> 5)));

15         }

16     }

17  

18     return (hash & 0x7FFFFFFF);

19 }

你可能感兴趣的:(compare)