• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.collect;
18 
19 import com.google.common.annotations.GwtCompatible;
20 import com.google.common.primitives.Ints;
21 import javax.annotation.CheckForNull;
22 
23 /**
24  * Static methods for implementing hash-based collections.
25  *
26  * @author Kevin Bourrillion
27  * @author Jesse Wilson
28  * @author Austin Appleby
29  */
30 @GwtCompatible
31 @ElementTypesAreNonnullByDefault
32 final class Hashing {
Hashing()33   private Hashing() {}
34 
35   /*
36    * These should be ints, but we need to use longs to force GWT to do the multiplications with
37    * enough precision.
38    */
39   private static final long C1 = 0xcc9e2d51;
40   private static final long C2 = 0x1b873593;
41 
42   /*
43    * This method was rewritten in Java from an intermediate step of the Murmur hash function in
44    * http://code.google.com/p/smhasher/source/browse/trunk/MurmurHash3.cpp, which contained the
45    * following header:
46    *
47    * MurmurHash3 was written by Austin Appleby, and is placed in the public domain. The author
48    * hereby disclaims copyright to this source code.
49    */
smear(int hashCode)50   static int smear(int hashCode) {
51     return (int) (C2 * Integer.rotateLeft((int) (hashCode * C1), 15));
52   }
53 
smearedHash(@heckForNull Object o)54   static int smearedHash(@CheckForNull Object o) {
55     return smear((o == null) ? 0 : o.hashCode());
56   }
57 
58   private static final int MAX_TABLE_SIZE = Ints.MAX_POWER_OF_TWO;
59 
closedTableSize(int expectedEntries, double loadFactor)60   static int closedTableSize(int expectedEntries, double loadFactor) {
61     // Get the recommended table size.
62     // Round down to the nearest power of 2.
63     expectedEntries = Math.max(expectedEntries, 2);
64     int tableSize = Integer.highestOneBit(expectedEntries);
65     // Check to make sure that we will not exceed the maximum load factor.
66     if (expectedEntries > (int) (loadFactor * tableSize)) {
67       tableSize <<= 1;
68       return (tableSize > 0) ? tableSize : MAX_TABLE_SIZE;
69     }
70     return tableSize;
71   }
72 
needsResizing(int size, int tableSize, double loadFactor)73   static boolean needsResizing(int size, int tableSize, double loadFactor) {
74     return size > loadFactor * tableSize && tableSize < MAX_TABLE_SIZE;
75   }
76 }
77