Common string Hash functions

Source: Internet
Author: User

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

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.