/** Builds a new open-addressed hash table from the first n objects in elements. */ static Object[] rebuildHashTable(int newTableSize, Object[] elements, int n) { Object[] hashTable = new Object[newTableSize]; int mask = hashTable.length - 1; for (int i = 0; i < n; i++) { Object e = elements[i]; int j0 = Hashing.smear(e.hashCode()); for (int j = j0; ; j++) { int index = j & mask; if (hashTable[index] == null) { hashTable[index] = e; break; } } } return hashTable; }
static int smearedHash(@Nullable Object o) { return smear((o == null) ? 0 : o.hashCode()); }
/** Builds a new open-addressed hash table from the first n objects in elements. */ static Object[] rebuildHashTable(int newTableSize, Object[] elements, int n) { Object[] hashTable = new Object[newTableSize]; int mask = hashTable.length - 1; for (int i = 0; i < n; i++) { Object e = elements[i]; int j0 = Hashing.smear(e.hashCode()); for (int j = j0; ; j++) { int index = j & mask; if (hashTable[index] == null) { hashTable[index] = e; break; } } } return hashTable; }
static int smearedHash(@NullableDecl Object o) { return smear((o == null) ? 0 : o.hashCode()); }
static int smearedHash(@NullableDecl Object o) { return smear((o == null) ? 0 : o.hashCode()); }
@Override SetBuilderImpl<E> add(E e) { checkNotNull(e); int eHash = e.hashCode(); int i0 = Hashing.smear(eHash); int mask = hashTable.length - 1; for (int i = i0; i - i0 < maxRunBeforeFallback; i++) { int index = i & mask; Object tableEntry = hashTable[index]; if (tableEntry == null) { addDedupedElement(e); hashTable[index] = e; hashCode += eHash; ensureTableCapacity(distinct); // rebuilds table if necessary return this; } else if (tableEntry.equals(e)) { // not a new element, ignore return this; } } // we fell out of the loop due to a long run; fall back to JDK impl return new JdkBackedSetBuilderImpl<E>(this).add(e); }
Object element = checkElementNotNull(elements[i], i); int hash = element.hashCode(); for (int j = Hashing.smear(hash); ; j++) { int index = j & mask; Object value = table[index];
static <V> @Nullable V get( @Nullable Object key, ImmutableMapEntry<?, V> @Nullable [] keyTable, int mask) { if (key == null || keyTable == null) { return null; } int index = Hashing.smear(key.hashCode()) & mask; for (ImmutableMapEntry<?, V> entry = keyTable[index]; entry != null; entry = entry.getNextInKeyBucket()) { Object candidateKey = entry.getKey(); /* * Assume that equals uses the == optimization when appropriate, and that * it would check hash codes as an optimization when appropriate. If we * did these things, it would just make things worse for the most * performance-conscious users. */ if (key.equals(candidateKey)) { return entry.getValue(); } } return null; }
@Override SetBuilderImpl<E> add(E e) { checkNotNull(e); int eHash = e.hashCode(); int i0 = Hashing.smear(eHash); int mask = hashTable.length - 1; for (int i = i0; i - i0 < maxRunBeforeFallback; i++) { int index = i & mask; Object tableEntry = hashTable[index]; if (tableEntry == null) { addDedupedElement(e); hashTable[index] = e; hashCode += eHash; ensureTableCapacity(distinct); // rebuilds table if necessary return this; } else if (tableEntry.equals(e)) { // not a new element, ignore return this; } } // we fell out of the loop due to a long run; fall back to JDK impl return new JdkBackedSetBuilderImpl<E>(this).add(e); }
public void testSmear() { assertEquals(1459320713, smear(754102528)); assertEquals(-160560296, smear(1234567890)); assertEquals(-1017931171, smear(1)); assertEquals(-1350072884, smear(-2000000000)); assertEquals(-809843551, smear(2000000000)); assertEquals(-309370926, smear(-1155484576)); assertEquals(-1645495900, smear(-723955400)); assertEquals(766424523, smear(1033096058)); assertEquals(-757003149, smear(-1690734402)); assertEquals(-245078984, smear(-1557280266)); assertEquals(-1401957971, smear(1327362106)); assertEquals(1398564061, smear(-1930858313)); assertEquals(799903031, smear(502539523)); assertEquals(587718043, smear(-1728529858)); assertEquals(1757836855, smear(-938301587)); assertEquals(1002498708, smear(1431162155)); assertEquals(52905316, smear(1085665355)); assertEquals(-1590037357, smear(1654374947)); assertEquals(-100883544, smear(-1661998771)); assertEquals(1312247346, smear(-65105105)); assertEquals(-79641824, smear(-73789608)); assertEquals(1739416943, smear(-518907128)); assertEquals(483849880, smear(99135751)); assertEquals(1797032732, smear(-252332814)); assertEquals(329701497, smear(755814641)); assertEquals(-1411506712, smear(1180918287)); assertEquals(-132448996, smear(1344049776)); assertEquals(51088191, smear(553609048)); assertEquals(-322136643, smear(1580443894));
int count = entry.getCount(); int hash = element.hashCode(); int bucket = Hashing.smear(hash) & mask; Multisets.ImmutableEntry<E> bucketHead = hashTable[bucket]; Multisets.ImmutableEntry<E> newEntry;
V value = entry.getValue(); checkEntryNotNull(key, value); int tableIndex = Hashing.smear(key.hashCode()) & mask; @Nullable ImmutableMapEntry<K, V> existing = table[tableIndex];
@NullableDecl static <V> V get( @NullableDecl Object key, @NullableDecl ImmutableMapEntry<?, V>[] keyTable, int mask) { if (key == null || keyTable == null) { return null; } int index = Hashing.smear(key.hashCode()) & mask; for (ImmutableMapEntry<?, V> entry = keyTable[index]; entry != null; entry = entry.getNextInKeyBucket()) { Object candidateKey = entry.getKey(); /* * Assume that equals uses the == optimization when appropriate, and that * it would check hash codes as an optimization when appropriate. If we * did these things, it would just make things worse for the most * performance-conscious users. */ if (key.equals(candidateKey)) { return entry.getValue(); } } return null; }
int count = entry.getCount(); int hash = element.hashCode(); int bucket = Hashing.smear(hash) & mask; Multisets.ImmutableEntry<E> bucketHead = hashTable[bucket]; Multisets.ImmutableEntry<E> newEntry;
V value = entry.getValue(); checkEntryNotNull(key, value); int tableIndex = Hashing.smear(key.hashCode()) & mask; @NullableDecl ImmutableMapEntry<K, V> existing = table[tableIndex];
Object value = alternatingKeysAndValues[2 * i + (keyOffset ^ 1)]; checkEntryNotNull(key, value); for (int h = Hashing.smear(key.hashCode()); ; h++) { h &= mask; int previous = hashTable[h];
@Override public boolean contains(Object target) { if (target == null) { return false; } for (int i = Hashing.smear(target.hashCode()); true; i++) { Object candidate = table[i & mask]; if (candidate == null) { return false; } if (candidate.equals(target)) { return true; } } }
private static int hash(@Nullable Object o) { return Hashing.smear((o == null) ? 0 : o.hashCode()); }