Created
June 5, 2016 12:40
-
-
Save LiSheep/051372a7edcbda09347afa8f78ad8399 to your computer and use it in GitHub Desktop.
some hash function
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
// 一种简单快捷的hash算法 | |
unsigned int BKDRHash(char *str) { | |
register unsigned int hash = 0; | |
while (unsigned int ch = (unsigned int)*str++) | |
hash = hash * 131 + ch; // 也可以乘以31、131、1313、13131、131313.. | |
return hash; | |
} | |
// 开源项目SDBM使用 | |
unsigned int SDBMHash(char*str) { | |
register unsigned int hash = 0; | |
while (unsigned int ch = (unsigned int)*str++) | |
hash = 65599 * hash + ch; | |
return hash; | |
} | |
// Unix system系统中使用的一种著名hash算法,后来微软也在其hash_map中实现。 | |
unsigned int FNVHash(char* str) { | |
register unsigned int hash = 2166136261; | |
while (unsigned int ch = (unsigned int)*str++) { | |
hash *= 16777619; | |
hash ^= ch; | |
} | |
return hash; | |
} | |
unsigned int APHash(char *str) { | |
register unsigned int hash = 0; | |
int i; | |
for (i=0; *str; i++) { | |
if ((i & 1) == 0) { | |
hash ^= ((hash << 7) ^ (*str++) ^ (hash >> 3)); | |
} else { | |
hash ^= (~((hash << 11) ^ (*str++) ^ (hash >> 5))); | |
} | |
} | |
return (hash & 0x7FFFFFFF); | |
} | |
// RS Hash Function | |
unsigned int RSHash(char *str) { | |
register unsigned int b = 378551; | |
register unsigned int a = 63689; | |
register unsigned int hash = 0; | |
while (*str) { | |
hash = hash * a + (*str++); | |
a *= b; | |
} | |
return (hash & 0x7FFFFFFF); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment