Class BobHash


  • public class BobHash
    extends java.lang.Object

    A Java port of Michael Susag's BobHash in FastLib. This version is specifically done to be bit compatible with the one in FastLib, as it is used in decoding packets from FastServer.

    Hash function based on http://burtleburtle.net/bob/hash/index.html by Bob Jenkins, 1996. [email protected]. You may use this code any way you wish, private, educational, or commercial. It's free.

    Author:
    Michael Susag, Steinar Knutsen
    • Constructor Summary

      Constructors 
      Constructor Description
      BobHash()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static int hash​(byte[] k, int initval)
      The hash function
      static int hash​(java.lang.String key)
      Hashes a string, by calling hash(byte[] key,int initval) with the utf-8 bytes of the string as key and 0 as initval.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • BobHash

        public BobHash()
    • Method Detail

      • hash

        public static int hash​(java.lang.String key)
        Hashes a string, by calling hash(byte[] key,int initval) with the utf-8 bytes of the string as key and 0 as initval. Note: This is copying the string content, change implementation to use efficiently on large strings. bratseth
      • hash

        public static int hash​(byte[] k,
                               int initval)
        The hash function

        hash() -- hash a variable-length key into a 32-bit value
        k : the key (the unaligned variable-length array of bytes)
        len : the length of the key, counting by bytes
        initval : can be any 4-byte value

        Returns a 32-bit value. Every bit of the key affects every bit of the return value. Every 1-bit and 2-bit delta achieves avalanche. About 6*len+35 instructions.

        The best hash table sizes are powers of 2. There is no need to do mod a prime (mod is sooo slow!). If you need less than 32 bits, use a bitmask. For example, if you need only 10 bits, do h = (h & hashmask(10)); In which case, the hash table should have hashsize(10) elements. If you are hashing n strings (ub1 **)k, do it like this: for (i=0, h=0; i<n; ++i) h = hash( k[i], len[i], h);

        By Bob Jenkins, 1996. [email protected]. You may use this code any way you wish, private, educational, or commercial. It's free.

        See http://burtleburtle.net/bob/hash/evahash.html Use for hash table lookup, or anything where one collision in 2^^32 is acceptable. Do NOT use for cryptographic purposes.

        Parameters:
        k - the key
        initval - the previous hash, or an arbitrary value
        Returns:
        A 32 bit hash value