Various hash functions and code __ functions

Source: Internet
Author: User
Tags hash

Yesterday learned the use of hash table, and did some problems, to the hash more understanding. Let's do the hash algorithm today so that the puzzle can be done ... This is reproduced by 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 go wrong, then the hash structure can also add a list, but in the face of some problems, do not need to use. So there's the hash function, the biggest one to determine the address of each set of data ...

Commonly used string hash functions and Elfhash,aphash and so on, are very simple and effective methods. These functions use the

Bitwise operations make each character 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.

Common string hash functions have Bkdrhash,aphash,djbhash,jshash,rshash,sdbmhash,

Pjwhash,elfhash and so on.

Bkdrhash, both in the actual effect and the coding implementation, the effect is the most prominent. Aphash is also a more excellent algorithm. Djbhash,jshash,rshash and Sdbmhash. Pjwhash and Elfhash effect is the worst, but the score is similar, its algorithm essence is similar.

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.