Java Hash Calculate hashJenkins(int init, Object... vals)

Here you can find the source of hashJenkins(int init, Object... vals)

Description

hash Jenkins

License

Open Source License

Declaration

public static int hashJenkins(int init, Object... vals) 

Method Source Code

//package com.java2s;
/*//from   w ww  . jav a  2s. c  o  m
 * Copyright (C) 2012-2013 University of Freiburg
 *
 * This file is part of SMTInterpol.
 *
 * SMTInterpol is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * SMTInterpol is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with SMTInterpol.  If not, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    private final static int BASE = 0xdeadbeef;

    public static int hashJenkins(int init, Object... vals) {
        if (vals == null || vals.length == 0) {
            return init;
        }
        int a, b, c;
        a = b = c = BASE + (vals.length << 2) + init;
        int pos = 0;
        while (vals.length - pos > 3) {
            a += vals[pos].hashCode();
            b += vals[pos + 1].hashCode();
            c += vals[pos + 2].hashCode();
            // This is the mix function of Jenkins hash.
            // Note that we need >>> for unsigned shift.
            // rot(c,4) is ((c << 4) | (c >>> 28)).
            a -= c;
            a ^= ((c << 4) | (c >>> 28));
            c += b;
            b -= a;
            b ^= ((a << 6) | (a >>> 26));
            a += c;
            c -= b;
            c ^= ((b << 8) | (b >>> 24));
            b += a;
            a -= c;
            a ^= ((c << 16) | (c >>> 16));
            c += b;
            b -= a;
            b ^= ((a << 19) | (a >>> 13));
            a += c;
            c -= b;
            c ^= ((b << 4) | (b >>> 28));
            b += a;
            pos += 3;
        }
        switch (vals.length - pos) {
        case 3:
            c += vals[pos++].hashCode();
            // fallthrough
        case 2:
            b += vals[pos++].hashCode();
            // fallthrough
        case 1:
            a += vals[pos].hashCode();
            // This is the final mix function of Jenkins hash.
            c ^= b;
            c -= ((b << 14) | (b >>> 18));
            a ^= c;
            a -= ((c << 11) | (c >>> 21));
            b ^= a;
            b -= ((a << 25) | (a >>> 7));
            c ^= b;
            c -= ((b << 16) | (b >>> 16));
            a ^= c;
            a -= ((c << 4) | (c >>> 28));
            b ^= a;
            b -= ((a << 14) | (a >>> 18));
            c ^= b;
            c -= ((b << 24) | (b >>> 8));
            // fallthrough
        case 0:
            // fallthrough
        default:
            break;
        }
        return c;
    }

    public static int hashJenkins(int init, Object val) {
        int a, b, c;
        a = b = BASE + 4 + init;
        // slightly optimized version of hashJenkins(init, new Object[] {val})
        a += val.hashCode();
        c = -((b << 14) | (b >>> 18));
        a ^= c;
        a -= ((c << 11) | (c >>> 21));
        b ^= a;
        b -= ((a << 25) | (a >>> 7));
        c ^= b;
        c -= ((b << 16) | (b >>> 16));
        a ^= c;
        a -= ((c << 4) | (c >>> 28));
        b ^= a;
        b -= ((a << 14) | (a >>> 18));
        c ^= b;
        c -= ((b << 24) | (b >>> 8));
        return c;
    }
}

Related

  1. hashInt(final int v)
  2. hashIntArray(int seed, int[] data, int offset, int len)
  3. hashIt(Object o)
  4. hashJava32(byte[] byteList)
  5. hashJava64(byte[] byteList)
  6. hashLong(long id)
  7. hashLong(long l)
  8. hashMapCapacity(final int nElements)
  9. hashMore(final int hash, final int more)