A few commonly used string hash functions are as follows:
Sdbmhash function
unsigned int sdbmhash (char *str) { unsigned int hash = 0; while (*STR) { //equivalent To:hash = 65599*hash + (*str++); hash = (*str++) + (hash << 6) + (hash << +)-hash; }
Rshash function
RS Hash functionunsigned int Rshash (char *str) { unsigned int b = 378551; unsigned int a = 63689; unsigned int hash = 0; while (*STR) { hash = hash * A + (*str++); a *= b; } Return (hash & 0x7FFFFFFF);}
Jshash function
JS Hash functionunsigned int Jshash (char *str) { unsigned int hash = 1315423911; while (*STR) { hash ^= (Hash << 5) + (*str++) + (hash >> 2)); } Return (hash & 0x7FFFFFFF);}
Pjwhash function
P. J. Weinberger Hash functionunsigned int Pjwhash (char *str) { unsigned int bitsinunignedint = (unsigned int) (Sizeo f (unsigned int) * 8); unsigned int threequarters = (unsigned int) ((bitsinunignedint * 3)/4); unsigned int oneeighth = (unsigned int) (BITSINUNIGNEDINT/8); unsigned int highbits = (unsigned int) (0xFFFFFFFF) << (bitsinunignedint-oneeighth); unsigned int hash = 0; unsigned int Test = 0; while (*STR) { hash = (hash << oneeighth) + (*str++); if (test = hash & highbits)! = 0) { hash = ((hash ^ (test >> threequarters)) & (~highbits));
} } return (hash & 0x7FFFFFFF);}
Elfhash function
ELF Hash functionunsigned int Elfhash (char *str) { unsigned int hash = 0; unsigned int x = 0; while (*STR) { hash = (Hash << 4) + (*str++); if ((x = hash & 0xf0000000l)! = 0) { hash ^= (x >>); Hash &= ~x; } } Return (hash & 0x7FFFFFFF);}
Bkdrhash function (recommended)
BKDR Hash functionunsigned int Bkdrhash (char *str) { unsigned int seed = 131;//131 1313 13131 131313 etc.. unsigned int hash = 0; while (*STR) { hash = hash * seed + (*str++); }
Djbhash function
DJB Hash functionunsigned int Djbhash (char *str) { unsigned int hash = 5381; while (*STR) { hash + = (hash << 5) + (*str++); } Return (hash & 0x7FFFFFFF);}
Aphash function
AP Hash functionunsigned int Aphash (char *str) { unsigned int hash = 0; int i; for (i=0; *str; i++) { if ((i & 1) = = 0) { hash ^= (Hash << 7) ^ (*str++) ^ (Hash >> 3 )); } else { hash ^= ((hash << one) ^ (*str++) ^ (hash >> 5));} } Return (hash & 0x7FFFFFFF);}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Common string Hash functions