/[Apache-SVN]/hadoop/hbase/trunk/src/java/org/apache/hadoop/hbase/util/JenkinsHash.java
ViewVC logotype

Contents of /hadoop/hbase/trunk/src/java/org/apache/hadoop/hbase/util/JenkinsHash.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 618357 - (show annotations)
Mon Feb 4 18:14:02 2008 UTC (21 months, 3 weeks ago) by omalley
File size: 10104 byte(s)
Making hbase a subproject
1 /**
2 * Copyright 2007 The Apache Software Foundation
3 *
4 * Licensed to the Apache Software Foundation (ASF) under one
5 * or more contributor license agreements. See the NOTICE file
6 * distributed with this work for additional information
7 * regarding copyright ownership. The ASF licenses this file
8 * to you under the Apache License, Version 2.0 (the
9 * "License"); you may not use this file except in compliance
10 * with the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21 package org.apache.hadoop.hbase.util;
22
23 /**
24 * lookup3.c, by Bob Jenkins, May 2006, Public Domain.
25 * <a href="http://burtleburtle.net/bob/c/lookup3.c">lookup3.c</a>
26 *
27 * You can use this free for any purpose. It's in the public domain.
28 * It has no warranty.
29 *
30 * Produces 32-bit hash for hash table lookup.
31 */
32 public class JenkinsHash {
33 private static long INT_MASK = 0x00000000ffffffffL;
34 private static long BYTE_MASK = 0x00000000000000ffL;
35
36 private static long rot(long val, int pos) {
37 return Long.valueOf(Integer.rotateLeft(
38 Long.valueOf(val & INT_MASK).intValue(), pos)).longValue() & INT_MASK;
39 }
40
41 /**
42 * Alternate form for hashing an entire byte array
43 *
44 * @param bytes
45 * @param initval
46 * @return hash value
47 */
48 public static int hash(byte[] bytes, int initval) {
49 return hash(bytes, bytes.length, initval);
50 }
51
52 /**
53 * taken from hashlittle() -- hash a variable-length key into a 32-bit value
54 *
55 * @param key the key (the unaligned variable-length array of bytes)
56 * @param nbytes number of bytes to include in hash
57 * @param initval can be any integer value
58 * @return a 32-bit value. Every bit of the key affects every bit of the
59 * return value. Two keys differing by one or two bits will have totally
60 * different hash values.
61 *
62 * The best hash table sizes are powers of 2. There is no need to do mod a
63 * prime (mod is sooo slow!). If you need less than 32 bits, use a bitmask.
64 * For example, if you need only 10 bits, do h = (h & hashmask(10));
65 * In which case, the hash table should have hashsize(10) elements.
66 *
67 * If you are hashing n strings byte[][] k, do it like this:
68 * for (int i = 0, h = 0; i < n; ++i) h = hash( k[i], h);
69 *
70 * By Bob Jenkins, 2006. bob_jenkins@burtleburtle.net. You may use this
71 * code any way you wish, private, educational, or commercial. It's free.
72 *
73 * Use for hash table lookup, or anything where one collision in 2^^32 is
74 * acceptable. Do NOT use for cryptographic purposes.
75 */
76 public static int hash(byte[] key, int nbytes, int initval) {
77 int length = nbytes;
78 long a, b, c; // We use longs because we don't have unsigned ints
79 a = b = c = (0x00000000deadbeefL + length + initval) & INT_MASK;
80 int offset = 0;
81 for (; length > 12; offset += 12, length -= 12) {
82 a = (a + (key[offset + 0] & BYTE_MASK)) & INT_MASK;
83 a = (a + (((key[offset + 1] & BYTE_MASK) << 8) & INT_MASK)) & INT_MASK;
84 a = (a + (((key[offset + 2] & BYTE_MASK) << 16) & INT_MASK)) & INT_MASK;
85 a = (a + (((key[offset + 3] & BYTE_MASK) << 24) & INT_MASK)) & INT_MASK;
86 b = (b + (key[offset + 4] & BYTE_MASK)) & INT_MASK;
87 b = (b + (((key[offset + 5] & BYTE_MASK) << 8) & INT_MASK)) & INT_MASK;
88 b = (b + (((key[offset + 6] & BYTE_MASK) << 16) & INT_MASK)) & INT_MASK;
89 b = (b + (((key[offset + 7] & BYTE_MASK) << 24) & INT_MASK)) & INT_MASK;
90 c = (c + (key[offset + 8] & BYTE_MASK)) & INT_MASK;
91 c = (c + (((key[offset + 9] & BYTE_MASK) << 8) & INT_MASK)) & INT_MASK;
92 c = (c + (((key[offset + 10] & BYTE_MASK) << 16) & INT_MASK)) & INT_MASK;
93 c = (c + (((key[offset + 11] & BYTE_MASK) << 24) & INT_MASK)) & INT_MASK;
94
95 /*
96 * mix -- mix 3 32-bit values reversibly.
97 * This is reversible, so any information in (a,b,c) before mix() is
98 * still in (a,b,c) after mix().
99 *
100 * If four pairs of (a,b,c) inputs are run through mix(), or through
101 * mix() in reverse, there are at least 32 bits of the output that
102 * are sometimes the same for one pair and different for another pair.
103 *
104 * This was tested for:
105 * - pairs that differed by one bit, by two bits, in any combination
106 * of top bits of (a,b,c), or in any combination of bottom bits of
107 * (a,b,c).
108 * - "differ" is defined as +, -, ^, or ~^. For + and -, I transformed
109 * the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
110 * is commonly produced by subtraction) look like a single 1-bit
111 * difference.
112 * - the base values were pseudorandom, all zero but one bit set, or
113 * all zero plus a counter that starts at zero.
114 *
115 * Some k values for my "a-=c; a^=rot(c,k); c+=b;" arrangement that
116 * satisfy this are
117 * 4 6 8 16 19 4
118 * 9 15 3 18 27 15
119 * 14 9 3 7 17 3
120 * Well, "9 15 3 18 27 15" didn't quite get 32 bits diffing for
121 * "differ" defined as + with a one-bit base and a two-bit delta. I
122 * used http://burtleburtle.net/bob/hash/avalanche.html to choose
123 * the operations, constants, and arrangements of the variables.
124 *
125 * This does not achieve avalanche. There are input bits of (a,b,c)
126 * that fail to affect some output bits of (a,b,c), especially of a.
127 * The most thoroughly mixed value is c, but it doesn't really even
128 * achieve avalanche in c.
129 *
130 * This allows some parallelism. Read-after-writes are good at doubling
131 * the number of bits affected, so the goal of mixing pulls in the
132 * opposite direction as the goal of parallelism. I did what I could.
133 * Rotates seem to cost as much as shifts on every machine I could lay
134 * my hands on, and rotates are much kinder to the top and bottom bits,
135 * so I used rotates.
136 *
137 * #define mix(a,b,c) \
138 * { \
139 * a -= c; a ^= rot(c, 4); c += b; \
140 * b -= a; b ^= rot(a, 6); a += c; \
141 * c -= b; c ^= rot(b, 8); b += a; \
142 * a -= c; a ^= rot(c,16); c += b; \
143 * b -= a; b ^= rot(a,19); a += c; \
144 * c -= b; c ^= rot(b, 4); b += a; \
145 * }
146 *
147 * mix(a,b,c);
148 */
149 a = (a - c) & INT_MASK; a ^= rot(c, 4); c = (c + b) & INT_MASK;
150 b = (b - a) & INT_MASK; b ^= rot(a, 6); a = (a + c) & INT_MASK;
151 c = (c - b) & INT_MASK; c ^= rot(b, 8); b = (b + a) & INT_MASK;
152 a = (a - c) & INT_MASK; a ^= rot(c,16); c = (c + b) & INT_MASK;
153 b = (b - a) & INT_MASK; b ^= rot(a,19); a = (a + c) & INT_MASK;
154 c = (c - b) & INT_MASK; c ^= rot(b, 4); b = (b + a) & INT_MASK;
155 }
156
157 //-------------------------------- last block: affect all 32 bits of (c)
158 switch (length) { // all the case statements fall through
159 case 12:
160 c = (c + (((key[offset + 11] & BYTE_MASK) << 24) & INT_MASK)) & INT_MASK;
161 case 11:
162 c = (c + (((key[offset + 10] & BYTE_MASK) << 16) & INT_MASK)) & INT_MASK;
163 case 10:
164 c = (c + (((key[offset + 9] & BYTE_MASK) << 8) & INT_MASK)) & INT_MASK;
165 case 9:
166 c = (c + (key[offset + 8] & BYTE_MASK)) & INT_MASK;
167 case 8:
168 b = (b + (((key[offset + 7] & BYTE_MASK) << 24) & INT_MASK)) & INT_MASK;
169 case 7:
170 b = (b + (((key[offset + 6] & BYTE_MASK) << 16) & INT_MASK)) & INT_MASK;
171 case 6:
172 b = (b + (((key[offset + 5] & BYTE_MASK) << 8) & INT_MASK)) & INT_MASK;
173 case 5:
174 b = (b + (key[offset + 4] & BYTE_MASK)) & INT_MASK;
175 case 4:
176 a = (a + (((key[offset + 3] & BYTE_MASK) << 24) & INT_MASK)) & INT_MASK;
177 case 3:
178 a = (a + (((key[offset + 2] & BYTE_MASK) << 16) & INT_MASK)) & INT_MASK;
179 case 2:
180 a = (a + (((key[offset + 1] & BYTE_MASK) << 8) & INT_MASK)) & INT_MASK;
181 case 1:
182 a = (a + (key[offset + 0] & BYTE_MASK)) & INT_MASK;
183 break;
184 case 0:
185 return Long.valueOf(c & INT_MASK).intValue();
186 }
187 /*
188 * final -- final mixing of 3 32-bit values (a,b,c) into c
189 *
190 * Pairs of (a,b,c) values differing in only a few bits will usually
191 * produce values of c that look totally different. This was tested for
192 * - pairs that differed by one bit, by two bits, in any combination
193 * of top bits of (a,b,c), or in any combination of bottom bits of
194 * (a,b,c).
195 *
196 * - "differ" is defined as +, -, ^, or ~^. For + and -, I transformed
197 * the output delta to a Gray code (a^(a>>1)) so a string of 1's (as
198 * is commonly produced by subtraction) look like a single 1-bit
199 * difference.
200 *
201 * - the base values were pseudorandom, all zero but one bit set, or
202 * all zero plus a counter that starts at zero.
203 *
204 * These constants passed:
205 * 14 11 25 16 4 14 24
206 * 12 14 25 16 4 14 24
207 * and these came close:
208 * 4 8 15 26 3 22 24
209 * 10 8 15 26 3 22 24
210 * 11 8 15 26 3 22 24
211 *
212 * #define final(a,b,c) \
213 * {
214 * c ^= b; c -= rot(b,14); \
215 * a ^= c; a -= rot(c,11); \
216 * b ^= a; b -= rot(a,25); \
217 * c ^= b; c -= rot(b,16); \
218 * a ^= c; a -= rot(c,4); \
219 * b ^= a; b -= rot(a,14); \
220 * c ^= b; c -= rot(b,24); \
221 * }
222 *
223 */
224 c ^= b; c = (c - rot(b,14)) & INT_MASK;
225 a ^= c; a = (a - rot(c,11)) & INT_MASK;
226 b ^= a; b = (b - rot(a,25)) & INT_MASK;
227 c ^= b; c = (c - rot(b,16)) & INT_MASK;
228 a ^= c; a = (a - rot(c,4)) & INT_MASK;
229 b ^= a; b = (b - rot(a,14)) & INT_MASK;
230 c ^= b; c = (c - rot(b,24)) & INT_MASK;
231
232 return Long.valueOf(c & INT_MASK).intValue();
233 }
234 }

apache@apache.org
ViewVC Help
Powered by ViewVC 1.1.2