This repository has been archived by the owner on Jul 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 559
/
MurmurHash.java
135 lines (116 loc) · 3.39 KB
/
MurmurHash.java
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package com.clearspring.analytics.hash;
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
/**
* This is a very fast, non-cryptographic hash suitable for general hash-based
* lookup. See http://murmurhash.googlepages.com/ for more details.
*
* <p>
* The C version of MurmurHash 2.0 found at that site was ported to Java by
* Andrzej Bialecki (ab at getopt org).
* </p>
*/
public class MurmurHash
{
public static int hash(Object o)
{
if (o == null)
return 0;
if(o instanceof Long)
return hashLong((Long)o);
if(o instanceof Integer)
return hashLong((Integer)o);
if(o instanceof Double)
return hashLong(Double.doubleToRawLongBits((Double)o));
if(o instanceof Float)
return hashLong(Float.floatToRawIntBits((Float)o));
if(o instanceof String)
return hash(((String)o).getBytes());
return hash(o.toString());
}
public static int hash(byte[] data)
{
return hash(data, data.length, -1);
}
public static int hash(byte[] data, int seed)
{
return hash(data, data.length, seed);
}
public static int hash(byte[] data, int length, int seed)
{
int m = 0x5bd1e995;
int r = 24;
int h = seed ^ length;
int len_4 = length >> 2;
for (int i = 0; i < len_4; i++)
{
int i_4 = i << 2;
int k = data[i_4 + 3];
k = k << 8;
k = k | (data[i_4 + 2] & 0xff);
k = k << 8;
k = k | (data[i_4 + 1] & 0xff);
k = k << 8;
k = k | (data[i_4 + 0] & 0xff);
k *= m;
k ^= k >>> r;
k *= m;
h *= m;
h ^= k;
}
// avoid calculating modulo
int len_m = len_4 << 2;
int left = length - len_m;
if (left != 0)
{
if (left >= 3)
{
h ^= (int) data[length - 3] << 16;
}
if (left >= 2)
{
h ^= (int) data[length - 2] << 8;
}
if (left >= 1)
{
h ^= (int) data[length - 1];
}
h *= m;
}
h ^= h >>> 13;
h *= m;
h ^= h >>> 15;
return h;
}
public static int hashLong(long data)
{
int m = 0x5bd1e995;
int r = 24;
int h = 0;
int k = (int)data * m;
k ^= k >>> r;
h ^= k*m;
k = (int)(data>>32) * m;
k ^= k >>> r;
h *= m;
h ^= k*m;
h ^= h >>> 13;
h *= m;
h ^= h >>> 15;
return h;
}
}