SmearHashFunction

From CSE231 Wiki
Jump to navigation Jump to search
class: hash.core.java DEMO: Java.png
methods: applyAsInt
package: SmearHashFunction
source folder: src/main/java
public final class SmearHashFunction<K> implements ToIntFunction<K> {
    private final ToIntFunction<K> hashFunction;

    public SmearHashFunction(ToIntFunction<K> hashFunction) {
        this.hashFunction = hashFunction;
    }
    public SmearHashFunction() {
        this(Objects::hashCode);
    }

    @Override
    public int applyAsInt(K key) {
        int hashCode = hashFunction.applyAsInt(key);
        /*
         * This method was written by Doug Lea with assistance from members of JCP
         * JSR-166 Expert Group and released to the public domain, as explained at
         * http://creativecommons.org/licenses/publicdomain
         *
         * As of 2010/06/11, this method is identical to the (package private) hash
         * method in OpenJDK 7's java.util.HashMap class.
         */
        hashCode ^= (hashCode >>> 20) ^ (hashCode >>> 12);
        return hashCode ^ (hashCode >>> 7) ^ (hashCode >>> 4);
    }
}