Last active
February 28, 2017 11:25
-
-
Save ChiHsiang/74d589f528d54c67552c922051afbaa2 to your computer and use it in GitHub Desktop.
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
| // BKDR Hash Function | |
| version-1 | |
| unsigned int BKDRHash(char *str) | |
| { | |
| unsigned int seed = 131; // 31 131 1313 13131 131313 etc.. | |
| unsigned int hash = 0; | |
| while (*str) | |
| { | |
| hash = hash * seed + (*str++); | |
| } | |
| return (hash & 0x7FFFFFFF); | |
| } | |
| ============================================================================================================= | |
| version-2 | |
| unsigned int BKDRHash(char* str, unsigned int len) | |
| { | |
| unsigned int seed = 131; /* 31 131 1313 13131 131313 etc.. */ | |
| unsigned int hash = 0; | |
| unsigned int i = 0; | |
| for(i = 0; i < len; str++, i++) | |
| { | |
| hash = (hash * seed) + (*str); | |
| } | |
| return hash; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment