| 24 |
import java.io.IOException; |
import java.io.IOException; |
| 25 |
|
|
| 26 |
/** |
/** |
| 27 |
* lookup3.c, by Bob Jenkins, May 2006, Public Domain. |
* Produces 32-bit hash for hash table lookup. |
| 28 |
* <a href="http://burtleburtle.net/bob/c/lookup3.c">lookup3.c</a> |
* |
| 29 |
|
* <pre>lookup3.c, by Bob Jenkins, May 2006, Public Domain. |
| 30 |
* |
* |
| 31 |
* You can use this free for any purpose. It's in the public domain. |
* You can use this free for any purpose. It's in the public domain. |
| 32 |
* It has no warranty. |
* It has no warranty. |
| 33 |
|
* </pre> |
| 34 |
* |
* |
| 35 |
* Produces 32-bit hash for hash table lookup. |
* @see <a href="http://burtleburtle.net/bob/c/lookup3.c">lookup3.c</a> |
| 36 |
|
* @see <a href="http://www.ddj.com/184410284">Hash Functions (and how this |
| 37 |
|
* function compares to others such as CRC, MD?, etc</a> |
| 38 |
|
* @see <a href="http://burtleburtle.net/bob/hash/doobs.html">Has update on the |
| 39 |
|
* Dr. Dobbs Article</a> |
| 40 |
*/ |
*/ |
| 41 |
public class JenkinsHash { |
public class JenkinsHash { |
| 42 |
private static long INT_MASK = 0x00000000ffffffffL; |
private static long INT_MASK = 0x00000000ffffffffL; |
| 51 |
* Alternate form for hashing an entire byte array |
* Alternate form for hashing an entire byte array |
| 52 |
* |
* |
| 53 |
* @param bytes |
* @param bytes |
| 54 |
|
* @return hash value |
| 55 |
|
*/ |
| 56 |
|
public static int hash(byte[] bytes) { |
| 57 |
|
return hash(bytes, bytes.length, -1); |
| 58 |
|
} |
| 59 |
|
|
| 60 |
|
/** |
| 61 |
|
* Alternate form for hashing an entire byte array |
| 62 |
|
* |
| 63 |
|
* @param bytes |
| 64 |
* @param initval |
* @param initval |
| 65 |
* @return hash value |
* @return hash value |
| 66 |
*/ |
*/ |
| 78 |
* return value. Two keys differing by one or two bits will have totally |
* return value. Two keys differing by one or two bits will have totally |
| 79 |
* different hash values. |
* different hash values. |
| 80 |
* |
* |
| 81 |
* The best hash table sizes are powers of 2. There is no need to do mod a |
* <p>The best hash table sizes are powers of 2. There is no need to do mod |
| 82 |
* prime (mod is sooo slow!). If you need less than 32 bits, use a bitmask. |
* a prime (mod is sooo slow!). If you need less than 32 bits, use a bitmask. |
| 83 |
* For example, if you need only 10 bits, do h = (h & hashmask(10)); |
* For example, if you need only 10 bits, do |
| 84 |
|
* <code>h = (h & hashmask(10));</code> |
| 85 |
* In which case, the hash table should have hashsize(10) elements. |
* In which case, the hash table should have hashsize(10) elements. |
| 86 |
* |
* |
| 87 |
* If you are hashing n strings byte[][] k, do it like this: |
* <p>If you are hashing n strings byte[][] k, do it like this: |
| 88 |
* for (int i = 0, h = 0; i < n; ++i) h = hash( k[i], h); |
* for (int i = 0, h = 0; i < n; ++i) h = hash( k[i], h); |
| 89 |
* |
* |
| 90 |
* By Bob Jenkins, 2006. bob_jenkins@burtleburtle.net. You may use this |
* <p>By Bob Jenkins, 2006. bob_jenkins@burtleburtle.net. You may use this |
| 91 |
* code any way you wish, private, educational, or commercial. It's free. |
* code any way you wish, private, educational, or commercial. It's free. |
| 92 |
* |
* |
| 93 |
* Use for hash table lookup, or anything where one collision in 2^^32 is |
* <p>Use for hash table lookup, or anything where one collision in 2^^32 is |
| 94 |
* acceptable. Do NOT use for cryptographic purposes. |
* acceptable. Do NOT use for cryptographic purposes. |
| 95 |
*/ |
*/ |
| 96 |
public static int hash(byte[] key, int nbytes, int initval) { |
public static int hash(byte[] key, int nbytes, int initval) { |