| 1 |
/* |
| 2 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
* you may not use this file except in compliance with the License. |
| 4 |
* You may obtain a copy of the License at |
| 5 |
* |
| 6 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
* |
| 8 |
* Unless required by applicable law or agreed to in writing, software |
| 9 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
* See the License for the specific language governing permissions and |
| 12 |
* limitations under the License. |
| 13 |
*/ |
| 14 |
package io.airlift.slice; |
| 15 |
|
| 16 |
public final class Murmur3Hash32 |
| 17 |
{ |
| 18 |
private static final int C1 = 0xcc9e2d51; |
| 19 |
private static final int C2 = 0x1b873593; |
| 20 |
|
| 21 |
private final static int DEFAULT_SEED = 0; |
| 22 |
|
| 23 |
private Murmur3Hash32() {} |
| 24 |
|
| 25 |
public static int hash(Slice data) |
| 26 |
{ |
| 27 |
return hash(data, 0, data.length()); |
| 28 |
} |
| 29 |
|
| 30 |
public static int hash(Slice data, int offset, int length) |
| 31 |
{ |
| 32 |
return hash(DEFAULT_SEED, data, offset, length); |
| 33 |
} |
| 34 |
|
| 35 |
public static int hash(int seed, Slice data, int offset, int length) |
| 36 |
{ |
| 37 |
final int fastLimit = offset + length - SizeOf.SIZE_OF_INT + 1; |
| 38 |
|
| 39 |
int h1 = seed; |
| 40 |
|
| 41 |
int current = offset; |
| 42 |
while (current < fastLimit) { |
| 43 |
int k1 = mixK1(data.getInt(current)); |
| 44 |
current += SizeOf.SIZE_OF_INT; |
| 45 |
h1 = mixH1(h1, k1); |
| 46 |
} |
| 47 |
|
| 48 |
int k1 = 0; |
| 49 |
|
| 50 |
switch (length & 3) { |
| 51 |
case 3: |
| 52 |
k1 ^= ((int) data.getUnsignedByte(current + 2)) << 16; |
| 53 |
case 2: |
| 54 |
k1 ^= ((int) data.getUnsignedByte(current + 1)) << 8; |
| 55 |
case 1: |
| 56 |
k1 ^= ((int) data.getUnsignedByte(current + 0)) << 0; |
| 57 |
} |
| 58 |
|
| 59 |
h1 ^= mixK1(k1); |
| 60 |
|
| 61 |
return fmix(h1, length); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Special-purpose version for hashing a single int value. Value is treated as little-endian |
| 66 |
*/ |
| 67 |
public static int hash(int input) |
| 68 |
{ |
| 69 |
int k1 = mixK1(input); |
| 70 |
int h1 = mixH1(DEFAULT_SEED, k1); |
| 71 |
|
| 72 |
return fmix(h1, SizeOf.SIZE_OF_INT); |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Special-purpose version for hashing a single long value. Value is treated as little-endian |
| 77 |
*/ |
| 78 |
public static int hash(long input) |
| 79 |
{ |
| 80 |
int low = (int) input; |
| 81 |
int high = (int) (input >>> 32); |
| 82 |
|
| 83 |
int k1 = mixK1(low); |
| 84 |
int h1 = mixH1(DEFAULT_SEED, k1); |
| 85 |
|
| 86 |
k1 = mixK1(high); |
| 87 |
h1 = mixH1(h1, k1); |
| 88 |
|
| 89 |
return fmix(h1, SizeOf.SIZE_OF_LONG); |
| 90 |
} |
| 91 |
|
| 92 |
private static int mixK1(int k1) |
| 93 |
{ |
| 94 |
k1 *= C1; |
| 95 |
k1 = Integer.rotateLeft(k1, 15); |
| 96 |
k1 *= C2; |
| 97 |
return k1; |
| 98 |
} |
| 99 |
|
| 100 |
private static int mixH1(int h1, int k1) |
| 101 |
{ |
| 102 |
h1 ^= k1; |
| 103 |
h1 = Integer.rotateLeft(h1, 13); |
| 104 |
h1 = h1 * 5 + 0xe6546b64; |
| 105 |
return h1; |
| 106 |
} |
| 107 |
|
| 108 |
private static int fmix(int h1, int length) |
| 109 |
{ |
| 110 |
h1 ^= length; |
| 111 |
h1 ^= h1 >>> 16; |
| 112 |
h1 *= 0x85ebca6b; |
| 113 |
h1 ^= h1 >>> 13; |
| 114 |
h1 *= 0xc2b2ae35; |
| 115 |
h1 ^= h1 >>> 16; |
| 116 |
return h1; |
| 117 |
} |
| 118 |
} |