Basic Hash Functions

Source: Internet
Author: User
// SDBM Hashunsigned int SDBMHash(char *str){    unsigned int hash = 0;     while (*str)    {        // equivalent to: hash = 65599*hash + (*str++);        hash = (*str++) + (hash << 6) + (hash << 16) - 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 unsigned int SDBMHash(char *str) - 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 unsigned int BKDRHash(char *str){    unsigned int seed = 131; // 31 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         {              hash ^= (~((hash << 11) ^ (*str++) ^ (hash >> 5)));         }     }      return (hash & 0x7FFFFFFF);}

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.