Created
March 19, 2012 15:46
-
-
Save ReaperUnreal/2116799 to your computer and use it in GitHub Desktop.
Bunch of hash functions
This file contains 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 int djb2(const char *text, int len) | |
{ | |
unsigned int hash = 5381; | |
int c; | |
while(c = *text++) | |
hash = ((hash << 5) + hash) + c; | |
return hash; | |
} | |
unsigned int sdbm(const char *text, int len) | |
{ | |
unsigned int hash = 0; | |
int c; | |
while(c = *text++) | |
hash = c + (hash << 6) + (hash << 16) - hash; | |
return hash; | |
} | |
unsigned int fnv1a(const char *text, int len) | |
{ | |
const int fnvOffsetBasis = 2166136261; | |
const int fnvPrime = 16777619; | |
unsigned int hash = fnvOffsetBasis; | |
for(int i = 0; i < len; ++i, ++text) | |
{ | |
hash = hash ^ (unsigned int)(*text); | |
hash *= fnvPrime; | |
} | |
return hash; | |
} | |
unsigned long long fnv1a64(const char *text, int len) | |
{ | |
const long long fnvOffsetBasis = 14695981039346656037ULL; | |
const long long fnvPrime = 1099511628211ULL; | |
unsigned long long hash = fnvOffsetBasis; | |
for(int i = 0; i < len; ++i, ++text) | |
{ | |
hash = hash ^ (unsigned long long)(*text); | |
hash *= fnvPrime; | |
} | |
return hash; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment