Murmur3_32HashFunction.java revision 7dd252788645e940eada959bdde927426e2531c9
1/* 2 * Copyright (C) 2011 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 15/* 16 * MurmurHash3 was written by Austin Appleby, and is placed in the public 17 * domain. The author hereby disclaims copyright to this source code. 18 */ 19 20/* 21 * Source: 22 * http://code.google.com/p/smhasher/source/browse/trunk/MurmurHash3.cpp 23 * (Modified to adapt to Guava coding conventions and to use the HashFunction interface) 24 */ 25 26package com.google.common.hash; 27 28import static com.google.common.primitives.UnsignedBytes.toInt; 29 30import com.google.common.primitives.Chars; 31import com.google.common.primitives.Ints; 32import com.google.common.primitives.Longs; 33 34import java.io.Serializable; 35import java.nio.ByteBuffer; 36 37/** 38 * See http://smhasher.googlecode.com/svn/trunk/MurmurHash3.cpp 39 * MurmurHash3_x86_32 40 * 41 * @author Austin Appleby 42 * @author Dimitris Andreou 43 * @author Kurt Alfred Kluever 44 */ 45final class Murmur3_32HashFunction extends AbstractStreamingHashFunction implements Serializable { 46 private static final int C1 = 0xcc9e2d51; 47 private static final int C2 = 0x1b873593; 48 49 private final int seed; 50 51 Murmur3_32HashFunction(int seed) { 52 this.seed = seed; 53 } 54 55 public int bits() { 56 return 32; 57 } 58 59 public Hasher newHasher() { 60 return new Murmur3_32Hasher(seed); 61 } 62 63 @Override 64 public String toString() { 65 return "Hashing.murmur3_32(" + seed + ")"; 66 } 67 68 @Override public HashCode hashInt(int input) { 69 int k1 = mixK1(input); 70 int h1 = mixH1(seed, k1); 71 72 return fmix(h1, Ints.BYTES); 73 } 74 75 @Override 76 public HashCode hashLong(long input) { 77 int low = (int) input; 78 int high = (int) (input >>> 32); 79 80 int k1 = mixK1(low); 81 int h1 = mixH1(seed, k1); 82 83 k1 = mixK1(high); 84 h1 = mixH1(h1, k1); 85 86 return fmix(h1, Longs.BYTES); 87 } 88 89 // TODO(user): Maybe implement #hashBytes instead? 90 @Override 91 public HashCode hashString(CharSequence input) { 92 int h1 = seed; 93 94 // step through the CharSequence 2 chars at a time 95 for (int i = 1; i < input.length(); i += 2) { 96 int k1 = input.charAt(i - 1) | (input.charAt(i) << 16); 97 k1 = mixK1(k1); 98 h1 = mixH1(h1, k1); 99 } 100 101 // deal with any remaining characters 102 if ((input.length() & 1) == 1) { 103 int k1 = input.charAt(input.length() - 1); 104 k1 = mixK1(k1); 105 h1 ^= k1; 106 } 107 108 return fmix(h1, Chars.BYTES * input.length()); 109 } 110 111 private static int mixK1(int k1) { 112 k1 *= C1; 113 k1 = Integer.rotateLeft(k1, 15); 114 k1 *= C2; 115 return k1; 116 } 117 118 private static int mixH1(int h1, int k1) { 119 h1 ^= k1; 120 h1 = Integer.rotateLeft(h1, 13); 121 h1 = h1 * 5 + 0xe6546b64; 122 return h1; 123 } 124 125 // Finalization mix - force all bits of a hash block to avalanche 126 private static HashCode fmix(int h1, int length) { 127 h1 ^= length; 128 h1 ^= h1 >>> 16; 129 h1 *= 0x85ebca6b; 130 h1 ^= h1 >>> 13; 131 h1 *= 0xc2b2ae35; 132 h1 ^= h1 >>> 16; 133 return HashCodes.fromInt(h1); 134 } 135 136 private static final class Murmur3_32Hasher extends AbstractStreamingHasher { 137 private static final int CHUNK_SIZE = 4; 138 private int h1; 139 private int length; 140 141 Murmur3_32Hasher(int seed) { 142 super(CHUNK_SIZE); 143 this.h1 = seed; 144 this.length = 0; 145 } 146 147 @Override 148 protected void process(ByteBuffer bb) { 149 int k1 = Murmur3_32HashFunction.mixK1(bb.getInt()); 150 h1 = Murmur3_32HashFunction.mixH1(h1, k1); 151 length += CHUNK_SIZE; 152 } 153 154 @Override 155 protected void processRemaining(ByteBuffer bb) { 156 length += bb.remaining(); 157 int k1 = 0; 158 for (int i = 0; bb.hasRemaining(); i += 8) { 159 k1 ^= toInt(bb.get()) << i; 160 } 161 h1 ^= Murmur3_32HashFunction.mixK1(k1); 162 } 163 164 @Override 165 public HashCode makeHash() { 166 return Murmur3_32HashFunction.fmix(h1, length); 167 } 168 } 169 170 private static final long serialVersionUID = 0L; 171} 172