Commonly used hash functions

Source: Internet
Author: User

Common hash Functions  The generic hash function library has the following string hashing algorithms that mix addition and one operation. The following algorithms are different in terms of usage and functionality, but can be used as an example to learn the implementation of hashing algorithms.  1.RSobtained from Robert Sedgwicks's algorithms in C book. Some simple optimized algorithms have been added to expedite their hashing process.
  1. Public long Rshash (String str)
  2. {
  3. int b = 378551;
  4. int a = 63689;
  5. long hash = 0;
  6. For (int i = 0; i < str.length (); i++)
  7. {
  8. hash = hash * A + str.charat (i);
  9. A = a * b;
  10. }
  11. return hash;
  12. }
 2.JSJustin Sobel writes a hash function for a bit operation.
  1. Public long Jshash (String str)
  2. {
  3. long hash = 1315423911;
  4. For (int i = 0; i < str.length (); i++)
  5. {
  6. Hash ^= (Hash << 5) + Str.charat (i) + (hash >> 2));
  7. }
  8. return hash;
  9. }
 3.PJWThe hashing algorithm is based on the study of Peter J. Weinberg in Bell Labs . In the Compilers book (Principles, techniques and tools), it is recommended to use the Hashing method of this algorithm's hash function.
  1. Public long Pjwhash (String str)
  2. {
  3. Long Bitsinunsignedint = (long) (4 * 8);
  4. Long threequarters = (long) ((Bitsinunsignedint * 3)/ 4);
  5. Long oneeighth = (long) (Bitsinunsignedint/ 8);
  6. Long highbits = (long) (0xFFFFFFFF) << (bitsinunsignedint-oneeighth);
  7. long hash = 0;
  8. Long test = 0;
  9. For (int i = 0; i < str.length (); i++)
  10. {
  11. hash = (hash << oneeighth) + Str.charat (i);
  12. if (test = hash & highbits)! = 0)
  13. {
  14. hash = ((hash ^ (test >> threequarters)) & (~highbits));
  15. }
  16. }
  17. return hash;
  18. }
 4.ELFsimilar to PJW, it is used more in UNIX systems.
  1. Public long Elfhash (String str)
  2. {
  3. long hash = 0;
  4. long x = 0;
  5. For (int i = 0; i < str.length (); i++)
  6. {
  7. hash = (hash << 4) + Str.charat (i);
  8. if ((x = hash & 0xf0000000l)! = 0)
  9. {
  10. Hash ^= (x >> 24);
  11. }
  12. Hash &= ~x;
  13. }
  14. return hash;
  15. }
 5.BKDRThis algorithm comes from the C programming Language of Brian Kernighan and Dennis Ritchie . This is a very simple hashing algorithm that uses a series of strange numbers, such as 31,3131,31...31, that look similar to the DJB algorithm. (This is the string hash function of Java)
  1. Public long Bkdrhash (String str)
  2. {
  3. Long seed = 131;  //131 1313 13131 131313 etc..
  4. long hash = 0;
  5. For (int i = 0; i < str.length (); i++)
  6. {
  7. hash = (hash * seed) + Str.charat (i);
  8. }
  9. return hash;
  10. }
 6.SDBMThis algorithm is used in open-source sdbm, which seems to be well distributed for many different types of data.
  1. Public long Sdbmhash (String str)
  2. {
  3. long hash = 0;
  4. For (int i = 0; i < str.length (); i++)
  5. {
  6. hash = Str.charat (i) + (hash << 6) + (hash << + )-hash;
  7. }
  8. return hash;
  9. }
 7.DJBThis algorithm was invented by Professor Daniel J.bernstein and is the most effective hash function currently published.
  1. Public long Djbhash (String str)
  2. {
  3. long hash = 5381;
  4. For (int i = 0; i < str.length (); i++)
  5. {
  6. hash = ((hash << 5) + hash) + Str.charat (i);
  7. }
  8. return hash;
  9. }
8.DEKby the great Knuth in the third volume of the art of programming, the sixth chapter is sorted and searched.
  1. Public long Dekhash (String str)
  2. {
  3. Long hash = Str.length ();
  4. For (int i = 0; i < str.length (); i++)
  5. {
  6. hash = ((hash << 5) ^ (hash >> )) ^ Str.charat (i);
  7. }
  8. return hash;
  9. }
 9.APThis is a hash function that Arash Partow contributes, inheriting the above to rotate and add the operation. Algebraic Description:
  1. Public long Aphash (String str)
  2. {
  3. long hash = 0xAAAAAAAA;
  4. For (int i = 0; i < str.length (); i++)
  5. {
  6. if ((I & 1) = = 0)
  7. {
  8. Hash ^= (Hash << 7) ^ Str.charat (i) * (hash >> 3));
  9. }
  10. Else
  11. {
  12. Hash ^= ((hash << one ) + Str.charat (i) ^ (hash >> 5)) ;
  13. }
  14. }
  15. return hash;
  16. }

Commonly used 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.