Java Hash Code Calculate hashCode(byte[] data, int offset, int len, int seed)

Here you can find the source of hashCode(byte[] data, int offset, int len, int seed)

Description

hash Code

License

Open Source License

Declaration

public static int hashCode(byte[] data, int offset, int len, int seed) 

Method Source Code

//package com.java2s;

public class Main {
    public static int hashCode(byte[] data, int offset, int len, int seed) {
        final int c1 = 0xcc9e2d51;
        final int c2 = 0x1b873593;
        int h1 = seed;
        int roundedEnd = offset + (len & 0xfffffffc); // round down to 4 byte block
        for (int i = offset; i < roundedEnd; i += 4) {
            // little endian load order
            int k1 = (data[i] & 0xff) | ((data[i + 1] & 0xff) << 8) | ((data[i + 2] & 0xff) << 16)
                    | (data[i + 3] << 24);
            k1 *= c1;//www.  java 2  s .  c o  m
            k1 = (k1 << 15) | (k1 >>> 17); // ROTL32(k1,15);
            k1 *= c2;
            h1 ^= k1;
            h1 = (h1 << 13) | (h1 >>> 19); // ROTL32(h1,13);
            h1 = h1 * 5 + 0xe6546b64;
        }
        // tail
        int k1 = 0;
        switch (len & 0x03) {
        case 3:
            k1 = (data[roundedEnd + 2] & 0xff) << 16;
            // fallthrough
        case 2:
            k1 |= (data[roundedEnd + 1] & 0xff) << 8;
            // fallthrough
        case 1:
            k1 |= (data[roundedEnd] & 0xff);
            k1 *= c1;
            k1 = (k1 << 15) | (k1 >>> 17); // ROTL32(k1,15);
            k1 *= c2;
            h1 ^= k1;
        }
        // finalization
        h1 ^= len;
        // fmix(h1);
        h1 ^= h1 >>> 16;
        h1 *= 0x85ebca6b;
        h1 ^= h1 >>> 13;
        h1 *= 0xc2b2ae35;
        h1 ^= h1 >>> 16;
        return h1;
    }

    public static int hashCode(String str) {
        byte[] data = str.getBytes();
        return hashCode(data, 0, data.length, 2342);
    }
}

Related

  1. hashCode(byte a[], int offset, int length)
  2. hashCode(byte[] array)
  3. hashCode(byte[] array, int size)
  4. hashCode(byte[] bytes, int offset, int length)
  5. hashCode(byte[] bytes, int size)
  6. hashCode(char[] array)
  7. hashCode(char[] array, int start, int end)
  8. hashCode(char[] array, int start, int end)
  9. hashCode(CharSequence seq)