Created
May 3, 2011 10:00
-
-
Save JulesWang/953097 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ELF Hash Function | |
unsigned int ELFHash(char *str) | |
{ | |
unsigned int hash = 0; | |
unsigned int x = 0; | |
while (*str) | |
{ | |
hash = (hash << 4) + (*str++);//hash左移4位,当前字符ASCII存入hash低四位。 | |
if ((x = hash & 0xF0000000L) != 0) | |
{ | |
//如果最高的四位不为0,则说明字符多余7个,如果不处理,再加第九个字符时,第一个字符会被移出,因此要有如下处理。 | |
//该处理,如果对于字符串(a-z 或者A-Z)就会仅仅影响5-8位,否则会影响5-31位,因为C语言使用的算数移位 | |
hash ^= (x >> 24); | |
//清空28-31位。 | |
hash &= ~x; | |
} | |
} | |
//返回一个符号位为0的数,即丢弃最高位,以免函数外产生影响。(我们可以考虑,如果只有字符,符号位不可能为负) | |
return (hash & 0x7FFFFFFF); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
unsigned long hashpjw(register CONST char *str, | |
register unsigned long int size) | |
{ | |
register CONST char *p; | |
register unsigned long h=0,g; | |
asc_assert((NULL != str) && (size > 0)); | |
for(p = str; *p != '\0'; p++) { | |
h = (h << 4) + (*p); | |
if (0 != (g = h&0xf0000000)) { | |
h = h ^ (g >> 24); | |
h = h ^ g; | |
} | |
} | |
return h % size; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment