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.
- Public long Rshash (String str)
- {
- int b = 378551;
- int a = 63689;
- long hash = 0;
- For (int i = 0; i < str.length (); i++)
- {
- hash = hash * A + str.charat (i);
- A = a * b;
- }
- return hash;
- }
2.JSJustin Sobel writes a hash function for a bit operation.
- Public long Jshash (String str)
- {
- long hash = 1315423911;
- For (int i = 0; i < str.length (); i++)
- {
- Hash ^= (Hash << 5) + Str.charat (i) + (hash >> 2));
- }
- return hash;
- }
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.
- Public long Pjwhash (String str)
- {
- Long Bitsinunsignedint = (long) (4 * 8);
- Long threequarters = (long) ((Bitsinunsignedint * 3)/ 4);
- Long oneeighth = (long) (Bitsinunsignedint/ 8);
- Long highbits = (long) (0xFFFFFFFF) << (bitsinunsignedint-oneeighth);
- long hash = 0;
- Long test = 0;
- For (int i = 0; i < str.length (); i++)
- {
- hash = (hash << oneeighth) + Str.charat (i);
- if (test = hash & highbits)! = 0)
- {
- hash = ((hash ^ (test >> threequarters)) & (~highbits));
- }
- }
- return hash;
- }
4.ELFsimilar to PJW, it is used more in UNIX systems.
- Public long Elfhash (String str)
- {
- long hash = 0;
- long x = 0;
- For (int i = 0; i < str.length (); i++)
- {
- hash = (hash << 4) + Str.charat (i);
- if ((x = hash & 0xf0000000l)! = 0)
- {
- Hash ^= (x >> 24);
- }
- Hash &= ~x;
- }
- return hash;
- }
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)
- Public long Bkdrhash (String str)
- {
- Long seed = 131; //131 1313 13131 131313 etc..
- long hash = 0;
- For (int i = 0; i < str.length (); i++)
- {
- hash = (hash * seed) + Str.charat (i);
- }
- return hash;
- }
6.SDBMThis algorithm is used in open-source sdbm, which seems to be well distributed for many different types of data.
- Public long Sdbmhash (String str)
- {
- long hash = 0;
- For (int i = 0; i < str.length (); i++)
- {
- hash = Str.charat (i) + (hash << 6) + (hash << + )-hash;
- }
- return hash;
- }
7.DJBThis algorithm was invented by Professor Daniel J.bernstein and is the most effective hash function currently published.
- Public long Djbhash (String str)
- {
- long hash = 5381;
- For (int i = 0; i < str.length (); i++)
- {
- hash = ((hash << 5) + hash) + Str.charat (i);
- }
- return hash;
- }
8.DEKby the great Knuth in the third volume of the art of programming, the sixth chapter is sorted and searched.
- Public long Dekhash (String str)
- {
- Long hash = Str.length ();
- For (int i = 0; i < str.length (); i++)
- {
- hash = ((hash << 5) ^ (hash >> )) ^ Str.charat (i);
- }
- return hash;
- }
9.APThis is a hash function that Arash Partow contributes, inheriting the above to rotate and add the operation. Algebraic Description:
- Public long Aphash (String str)
- {
- long hash = 0xAAAAAAAA;
- For (int i = 0; i < str.length (); i++)
- {
- if ((I & 1) = = 0)
- {
- Hash ^= (Hash << 7) ^ Str.charat (i) * (hash >> 3));
- }
- Else
- {
- Hash ^= ((hash << one ) + Str.charat (i) ^ (hash >> 5)) ;
- }
- }
- return hash;
- }
Commonly used hash functions