All Downloads are FREE. Search and download functionalities are using the official Maven repository.

eu.monnetproject.translation.fidel.MurmurHash Maven / Gradle / Ivy

Go to download

com.github.monnetproject.translation.fidel OSGi Bundle from the Monnet Project's translation.project project.

The newest version!
/*********************************************************************************
 * Copyright (c) 2011, Monnet Project All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met: *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer. * Redistributions in binary
 * form must reproduce the above copyright notice, this list of conditions and
 * the following disclaimer in the documentation and/or other materials provided
 * with the distribution. * Neither the name of the Monnet Project nor the names
 * of its contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE MONNET PROJECT BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * *******************************************************************************
 */
package eu.monnetproject.translation.fidel;

/**
 *
 * @author John McCrae
 */
public class MurmurHash {

    // Murmur Hash implementation from http://d3s.mff.cuni.cz/~holub/sw/javamurmurhash/MurmurHash.java
    public static int hash32(final int[] data) {
        final int offset = 0;
        final int length = data.length;
        return hash32(data, offset, length);
    }

    public static int hash32(final int[] data, final int offset, final int length) {
        final int seed = 0x9747b28c;
        // 'm' and 'r' are mixing constants generated offline.
        // They're not really 'magic', they just happen to work well.
        final int m = 0x5bd1e995;
        final int r = 24;
        // Initialize the hash to a random value
        int h = seed ^ length;
        //int length4 = length / 4;

        for (int i = 0; i < length; i++) {
            int k = data[offset + i];
            k *= m;
            k ^= k >>> r;
            k *= m;
            h *= m;
            h ^= k;
        }

        h ^= h >>> 13;
        h *= m;
        h ^= h >>> 15;

        return h;
    }

    public static int hash32(final double[] data) {
        final int length = data.length * 2;
        final int seed = 0x9747b28c;
        // 'm' and 'r' are mixing constants generated offline.
        // They're not really 'magic', they just happen to work well.
        final int m = 0x5bd1e995;
        final int r = 24;
        // Initialize the hash to a random value
        int h = seed ^ length;
        //int length4 = length / 4;

        for (int i = 0; i < length / 2; i++) {
            final long raw = Double.doubleToRawLongBits(data[i]);
            int k1 = (int) ((raw & 0xffffffff00000000l) >>> 32);
            k1 *= m;
            k1 ^= k1 >>> r;
            k1 *= m;
            h *= m;
            h ^= k1;
            int k2 = (int) ((raw & 0x00000000ffffffffl));
            k2 *= m;
            k2 ^= k2 >>> r;
            k2 *= m;
            h *= m;
            h ^= k2;
        }

        h ^= h >>> 13;
        h *= m;
        h ^= h >>> 15;

        return h;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy