Yesterday learned the use of the hash table, and did some problems, the hash more understanding. Today, let's do the hashing algorithm so that the puzzle can be done ... This is reproduced others, because the essence of the hash is to determine the address of each group of data, and how to determine the address will not repeat it. If you repeat, the query will be wrong, if you do not want to make a mistake, then the structure of the hash can also add a list, but in the face of some problems, it is not necessary. So there is the hash function, the largest unique to determine the address of each group of data ...
Commonly used string hash function and Elfhash,aphash, and so on, are very simple and effective method. These functions use the
Bitwise operations allow each character to have an effect on the last function value. There are also hash functions represented by MD5 and SHA1,
These functions are almost impossible to find collisions.
Commonly used string hash functions have Bkdrhash,aphash,djbhash,jshash,rshash,sdbmhash,
Pjwhash,elfhash and so on.
Bkdrhash both in the actual effect and coding implementation, the effect is the most prominent. Aphash is also an excellent algorithm. Djbhash,jshash,rshash and Sdbmhash have their own merits. Pjwhash and Elfhash have the worst effect, but the scores are similar and the algorithms are similar in nature.
C + + Language program code for various hash functions
//Simple hash, square fetch
for (i = 0; i < s; i++)
{
hash = Arr[i];
Hash *= Hash;
Hash/= 256;
Hash%= 65535;
Hashtable[hash] = Arr[i];
}
Sdbmhash
unsigned int sdbmhash (char *str)
{
unsigned int hash = 0;
while (*STR)
{
Equivalent To:hash = 65599*hash + (*str++);
hash = (*str++) + (hash << 6) + (hash << +)-hash;
}
Return (hash & 0x7FFFFFFF);
}
RS Hash
unsigned 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);
}
JS Hash
unsigned int jshash (char *str)
{
unsigned int hash = 1315423911;
while (*STR)
{
Hash ^= (Hash << 5) + (*str++) + (hash >> 2));
}
Return (hash & 0x7FFFFFFF);
}
P. J. Weinberger Hash
unsigned int pjwhash (char *str)
{
unsigned int bitsinunignedint = (unsigned int) (sizeof (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);
}
ELF Hash
unsigned 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 >> 24);
Hash &= ~x;
}
}
Return (hash & 0x7FFFFFFF);
}
Bkdr Hash effect is outstanding, but also the author said the effect is the most prominent
unsigned int bkdrhash (char *str)
{
unsigned int seed = 131; 131 1313 13131 131313 etc..
unsigned int hash = 0;
while (*STR)
{
hash = hash * seed + (*str++);
}
Return (hash & 0x7FFFFFFF);
}
DJB Hash
unsigned int djbhash (char *str)
{
unsigned int hash = 5381;
while (*STR)
{
Hash + = (hash << 5) + (*str++);
}
Return (hash & 0x7FFFFFFF);
}
AP Hash
unsigned 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