|
36 | 36 | */ |
37 | 37 | public final class MurmurHash2 { |
38 | 38 |
|
39 | | - // all methods static; private constructor. |
40 | | - private MurmurHash2() { |
41 | | - } |
42 | | - |
43 | | - /** |
44 | | - * Generates 32 bit hash from byte array of the given length and seed. |
45 | | - * |
46 | | - * @param data byte array to hash |
47 | | - * @param length length of the array to hash |
48 | | - * @param seed initial seed value |
49 | | - * @return 32 bit hash of the given array |
50 | | - */ |
51 | | - public static int hash32(final byte[] data, final int length, final int seed) { |
52 | | - // 'm' and 'r' are mixing constants generated offline. |
53 | | - // They're not really 'magic', they just happen to work well. |
54 | | - final int m = 0x5bd1e995; |
55 | | - final int r = 24; |
56 | | - |
57 | | - // Initialize the hash to a random value |
58 | | - int h = seed ^ length; |
59 | | - final int length4 = length / 4; |
60 | | - |
61 | | - for (int i = 0; i < length4; i++) { |
62 | | - final int i4 = i * 4; |
63 | | - int k = (data[i4 + 0] & 0xff) + ((data[i4 + 1] & 0xff) << 8) + ((data[i4 + 2] & 0xff) << 16) |
64 | | - + ((data[i4 + 3] & 0xff) << 24); |
65 | | - k *= m; |
66 | | - k ^= k >>> r; |
67 | | - k *= m; |
68 | | - h *= m; |
69 | | - h ^= k; |
70 | | - } |
71 | | - |
72 | | - // Handle the last few bytes of the input array |
73 | | - switch (length % 4) { |
74 | | - case 3: |
75 | | - h ^= (data[(length & ~3) + 2] & 0xff) << 16; |
76 | | - case 2: |
77 | | - h ^= (data[(length & ~3) + 1] & 0xff) << 8; |
78 | | - case 1: |
79 | | - h ^= (data[length & ~3] & 0xff); |
80 | | - h *= m; |
81 | | - } |
82 | | - |
83 | | - h ^= h >>> 13; |
84 | | - h *= m; |
85 | | - h ^= h >>> 15; |
86 | | - |
87 | | - return h; |
88 | | - } |
89 | | - |
90 | | - /** |
91 | | - * Generates 32 bit hash from byte array with default seed value. |
92 | | - * |
93 | | - * @param data byte array to hash |
94 | | - * @param length length of the array to hash |
95 | | - * @return 32 bit hash of the given array |
96 | | - */ |
97 | | - public static int hash32(final byte[] data, final int length) { |
98 | | - return hash32(data, length, 0x9747b28c); |
99 | | - } |
100 | | - |
101 | | - /** |
102 | | - * Generates 32 bit hash from a string. |
103 | | - * |
104 | | - * @param text string to hash |
105 | | - * @return 32 bit hash of the given string |
106 | | - */ |
107 | | - public static int hash32(final String text) { |
108 | | - final byte[] bytes = text.getBytes(); |
109 | | - return hash32(bytes, bytes.length); |
110 | | - } |
111 | | - |
112 | | - /** |
113 | | - * Generates 32 bit hash from a substring. |
114 | | - * |
115 | | - * @param text string to hash |
116 | | - * @param from starting index |
117 | | - * @param length length of the substring to hash |
118 | | - * @return 32 bit hash of the given string |
119 | | - */ |
120 | | - public static int hash32(final String text, final int from, final int length) { |
121 | | - return hash32(text.substring(from, from + length)); |
122 | | - } |
123 | | - |
124 | | - /** |
125 | | - * Generates 64 bit hash from byte array of the given length and seed. |
126 | | - * |
127 | | - * @param data byte array to hash |
128 | | - * @param length length of the array to hash |
129 | | - * @param seed initial seed value |
130 | | - * @return 64 bit hash of the given array |
131 | | - */ |
132 | | - public static long hash64(final byte[] data, final int length, final int seed) { |
133 | | - final long m = 0xc6a4a7935bd1e995L; |
134 | | - final int r = 47; |
135 | | - |
136 | | - long h = (seed & 0xffffffffl) ^ (length * m); |
137 | | - |
138 | | - final int length8 = length / 8; |
139 | | - |
140 | | - for (int i = 0; i < length8; i++) { |
141 | | - final int i8 = i * 8; |
142 | | - long k = ((long) data[i8 + 0] & 0xff) + (((long) data[i8 + 1] & 0xff) << 8) |
143 | | - + (((long) data[i8 + 2] & 0xff) << 16) + (((long) data[i8 + 3] & 0xff) << 24) |
144 | | - + (((long) data[i8 + 4] & 0xff) << 32) + (((long) data[i8 + 5] & 0xff) << 40) |
145 | | - + (((long) data[i8 + 6] & 0xff) << 48) + (((long) data[i8 + 7] & 0xff) << 56); |
146 | | - |
147 | | - k *= m; |
148 | | - k ^= k >>> r; |
149 | | - k *= m; |
150 | | - |
151 | | - h ^= k; |
152 | | - h *= m; |
153 | | - } |
154 | | - |
155 | | - switch (length % 8) { |
156 | | - case 7: |
157 | | - h ^= (long) (data[(length & ~7) + 6] & 0xff) << 48; |
158 | | - case 6: |
159 | | - h ^= (long) (data[(length & ~7) + 5] & 0xff) << 40; |
160 | | - case 5: |
161 | | - h ^= (long) (data[(length & ~7) + 4] & 0xff) << 32; |
162 | | - case 4: |
163 | | - h ^= (long) (data[(length & ~7) + 3] & 0xff) << 24; |
164 | | - case 3: |
165 | | - h ^= (long) (data[(length & ~7) + 2] & 0xff) << 16; |
166 | | - case 2: |
167 | | - h ^= (long) (data[(length & ~7) + 1] & 0xff) << 8; |
168 | | - case 1: |
169 | | - h ^= data[length & ~7] & 0xff; |
170 | | - h *= m; |
171 | | - } |
172 | | - |
173 | | - h ^= h >>> r; |
174 | | - h *= m; |
175 | | - h ^= h >>> r; |
176 | | - |
177 | | - return h; |
178 | | - } |
179 | | - |
180 | | - /** |
181 | | - * Generates 64 bit hash from byte array with default seed value. |
182 | | - * |
183 | | - * @param data byte array to hash |
184 | | - * @param length length of the array to hash |
185 | | - * @return 64 bit hash of the given string |
186 | | - */ |
187 | | - public static long hash64(final byte[] data, final int length) { |
188 | | - return hash64(data, length, 0xe17a1465); |
189 | | - } |
190 | | - |
191 | | - /** |
192 | | - * Generates 64 bit hash from a string. |
193 | | - * |
194 | | - * @param text string to hash |
195 | | - * @return 64 bit hash of the given string |
196 | | - */ |
197 | | - public static long hash64(final String text) { |
198 | | - final byte[] bytes = text.getBytes(); |
199 | | - return hash64(bytes, bytes.length); |
200 | | - } |
201 | | - |
202 | | - /** |
203 | | - * Generates 64 bit hash from a substring. |
204 | | - * |
205 | | - * @param text string to hash |
206 | | - * @param from starting index |
207 | | - * @param length length of the substring to hash |
208 | | - * @return 64 bit hash of the given array |
209 | | - */ |
210 | | - public static long hash64(final String text, final int from, final int length) { |
211 | | - return hash64(text.substring(from, from + length)); |
212 | | - } |
| 39 | + // all methods static; private constructor. |
| 40 | + private MurmurHash2() { |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Generates 32 bit hash from byte array of the given length and seed. |
| 45 | + * |
| 46 | + * @param data byte array to hash |
| 47 | + * @param length length of the array to hash |
| 48 | + * @param seed initial seed value |
| 49 | + * @return 32 bit hash of the given array |
| 50 | + */ |
| 51 | + public static int hash32(final byte[] data, final int length, final int seed) { |
| 52 | + // 'm' and 'r' are mixing constants generated offline. |
| 53 | + // They're not really 'magic', they just happen to work well. |
| 54 | + final int m = 0x5bd1e995; |
| 55 | + final int r = 24; |
| 56 | + |
| 57 | + // Initialize the hash to a random value |
| 58 | + int h = seed ^ length; |
| 59 | + final int length4 = length / 4; |
| 60 | + |
| 61 | + for (int i = 0; i < length4; i++) { |
| 62 | + final int i4 = i * 4; |
| 63 | + int k = (data[i4 + 0] & 0xff) + ((data[i4 + 1] & 0xff) << 8) + ((data[i4 + 2] & 0xff) << 16) |
| 64 | + + ((data[i4 + 3] & 0xff) << 24); |
| 65 | + k *= m; |
| 66 | + k ^= k >>> r; |
| 67 | + k *= m; |
| 68 | + h *= m; |
| 69 | + h ^= k; |
| 70 | + } |
| 71 | + |
| 72 | + // Handle the last few bytes of the input array |
| 73 | + switch (length % 4) { |
| 74 | + case 3: |
| 75 | + h ^= (data[(length & ~3) + 2] & 0xff) << 16; |
| 76 | + case 2: |
| 77 | + h ^= (data[(length & ~3) + 1] & 0xff) << 8; |
| 78 | + case 1: |
| 79 | + h ^= (data[length & ~3] & 0xff); |
| 80 | + h *= m; |
| 81 | + } |
| 82 | + |
| 83 | + h ^= h >>> 13; |
| 84 | + h *= m; |
| 85 | + h ^= h >>> 15; |
| 86 | + |
| 87 | + return h; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Generates 32 bit hash from byte array with default seed value. |
| 92 | + * |
| 93 | + * @param data byte array to hash |
| 94 | + * @param length length of the array to hash |
| 95 | + * @return 32 bit hash of the given array |
| 96 | + */ |
| 97 | + public static int hash32(final byte[] data, final int length) { |
| 98 | + return hash32(data, length, 0x9747b28c); |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Generates 32 bit hash from a string. |
| 103 | + * |
| 104 | + * @param text string to hash |
| 105 | + * @return 32 bit hash of the given string |
| 106 | + */ |
| 107 | + public static int hash32(final String text) { |
| 108 | + final byte[] bytes = text.getBytes(); |
| 109 | + return hash32(bytes, bytes.length); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Generates 32 bit hash from a substring. |
| 114 | + * |
| 115 | + * @param text string to hash |
| 116 | + * @param from starting index |
| 117 | + * @param length length of the substring to hash |
| 118 | + * @return 32 bit hash of the given string |
| 119 | + */ |
| 120 | + public static int hash32(final String text, final int from, final int length) { |
| 121 | + return hash32(text.substring(from, from + length)); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Generates 64 bit hash from byte array of the given length and seed. |
| 126 | + * |
| 127 | + * @param data byte array to hash |
| 128 | + * @param length length of the array to hash |
| 129 | + * @param seed initial seed value |
| 130 | + * @return 64 bit hash of the given array |
| 131 | + */ |
| 132 | + public static long hash64(final byte[] data, final int length, final int seed) { |
| 133 | + final long m = 0xc6a4a7935bd1e995L; |
| 134 | + final int r = 47; |
| 135 | + |
| 136 | + long h = (seed & 0xffffffffl) ^ (length * m); |
| 137 | + |
| 138 | + final int length8 = length / 8; |
| 139 | + |
| 140 | + for (int i = 0; i < length8; i++) { |
| 141 | + final int i8 = i * 8; |
| 142 | + long k = ((long) data[i8 + 0] & 0xff) + (((long) data[i8 + 1] & 0xff) << 8) |
| 143 | + + (((long) data[i8 + 2] & 0xff) << 16) + (((long) data[i8 + 3] & 0xff) << 24) |
| 144 | + + (((long) data[i8 + 4] & 0xff) << 32) + (((long) data[i8 + 5] & 0xff) << 40) |
| 145 | + + (((long) data[i8 + 6] & 0xff) << 48) + (((long) data[i8 + 7] & 0xff) << 56); |
| 146 | + |
| 147 | + k *= m; |
| 148 | + k ^= k >>> r; |
| 149 | + k *= m; |
| 150 | + |
| 151 | + h ^= k; |
| 152 | + h *= m; |
| 153 | + } |
| 154 | + |
| 155 | + switch (length % 8) { |
| 156 | + case 7: |
| 157 | + h ^= (long) (data[(length & ~7) + 6] & 0xff) << 48; |
| 158 | + case 6: |
| 159 | + h ^= (long) (data[(length & ~7) + 5] & 0xff) << 40; |
| 160 | + case 5: |
| 161 | + h ^= (long) (data[(length & ~7) + 4] & 0xff) << 32; |
| 162 | + case 4: |
| 163 | + h ^= (long) (data[(length & ~7) + 3] & 0xff) << 24; |
| 164 | + case 3: |
| 165 | + h ^= (long) (data[(length & ~7) + 2] & 0xff) << 16; |
| 166 | + case 2: |
| 167 | + h ^= (long) (data[(length & ~7) + 1] & 0xff) << 8; |
| 168 | + case 1: |
| 169 | + h ^= data[length & ~7] & 0xff; |
| 170 | + h *= m; |
| 171 | + } |
| 172 | + |
| 173 | + h ^= h >>> r; |
| 174 | + h *= m; |
| 175 | + h ^= h >>> r; |
| 176 | + |
| 177 | + return h; |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Generates 64 bit hash from byte array with default seed value. |
| 182 | + * |
| 183 | + * @param data byte array to hash |
| 184 | + * @param length length of the array to hash |
| 185 | + * @return 64 bit hash of the given string |
| 186 | + */ |
| 187 | + public static long hash64(final byte[] data, final int length) { |
| 188 | + return hash64(data, length, 0xe17a1465); |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * Generates 64 bit hash from a string. |
| 193 | + * |
| 194 | + * @param text string to hash |
| 195 | + * @return 64 bit hash of the given string |
| 196 | + */ |
| 197 | + public static long hash64(final String text) { |
| 198 | + final byte[] bytes = text.getBytes(); |
| 199 | + return hash64(bytes, bytes.length); |
| 200 | + } |
| 201 | + |
| 202 | + /** |
| 203 | + * Generates 64 bit hash from a substring. |
| 204 | + * |
| 205 | + * @param text string to hash |
| 206 | + * @param from starting index |
| 207 | + * @param length length of the substring to hash |
| 208 | + * @return 64 bit hash of the given array |
| 209 | + */ |
| 210 | + public static long hash64(final String text, final int from, final int length) { |
| 211 | + return hash64(text.substring(from, from + length)); |
| 212 | + } |
213 | 213 | } |
0 commit comments