forked from jruby/jruby
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMurmurHash.java
More file actions
62 lines (51 loc) · 1.87 KB
/
Copy pathMurmurHash.java
File metadata and controls
62 lines (51 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package org.jruby.util;
public class MurmurHash {
// Based on Murmurhash 2.0 Java port at http://dmy999.com/article/50/murmurhash-2-java-port
// 2011-12-05: Modified by Hiroshi Nakamura <nahi@ruby-lang.org>
// - signature change to use offset
// hash(byte[] data, int seed) to hash(byte[] src, int offset, int length, int seed)
// - extract 'm' and 'r' as murmurhash2.0 constants
// Ported by Derek Young from the C version (specifically the endian-neutral
// version) from:
// http://murmurhash.googlepages.com/
//
// released to the public domain - dmy999@gmail.com
// 'm' and 'r' are mixing constants generated offline.
// They're not really 'magic', they just happen to work well.
private static final int MURMUR2_MAGIC = 0x5bd1e995;
// CRuby 1.9 uses 16 but original C++ implementation uses 24 with above Magic.
private static final int MURMUR2_R = 24;
@SuppressWarnings("fallthrough")
public static int hash32(byte[] src, int offset, int length, int seed) {
// Initialize the hash to a 'random' value
int h = seed ^ length;
int i = offset;
int len = length;
while (len >= 4) {
int k = src[i + 0] & 0xFF;
k |= (src[i + 1] & 0xFF) << 8;
k |= (src[i + 2] & 0xFF) << 16;
k |= (src[i + 3] & 0xFF) << 24;
k *= MURMUR2_MAGIC;
k ^= k >>> MURMUR2_R;
k *= MURMUR2_MAGIC;
h *= MURMUR2_MAGIC;
h ^= k;
i += 4;
len -= 4;
}
switch (len) {
case 3:
h ^= (src[i + 2] & 0xFF) << 16;
case 2:
h ^= (src[i + 1] & 0xFF) << 8;
case 1:
h ^= (src[i + 0] & 0xFF);
h *= MURMUR2_MAGIC;
}
h ^= h >>> 13;
h *= MURMUR2_MAGIC;
h ^= h >>> 15;
return h;
}
}