static <K, V> RegularImmutableBiMap<K, V> fromEntryArray(int n, Entry<K, V>[] entryArray) { checkPositionIndex(n, entryArray.length); int tableSize = Hashing.closedTableSize(n, MAX_LOAD_FACTOR); int mask = tableSize - 1; ImmutableMapEntry<K, V>[] keyTable = createEntryArray(tableSize); ImmutableMapEntry<K, V>[] valueTable = createEntryArray(tableSize); Entry<K, V>[] entries; if (n == entryArray.length) { entries = entryArray; } else { entries = createEntryArray(n); } int hashCode = 0; for (int i = 0; i < n; i++) { @SuppressWarnings("unchecked") Entry<K, V> entry = entryArray[i]; K key = entry.getKey(); V value = entry.getValue(); checkEntryNotNull(key, value); int keyHash = key.hashCode(); int valueHash = value.hashCode(); int keyBucket = Hashing.smear(keyHash) & mask; int valueBucket = Hashing.smear(valueHash) & mask; ImmutableMapEntry<K, V> nextInKeyBucket = keyTable[keyBucket]; checkNoConflictInKeyBucket(key, entry, nextInKeyBucket); ImmutableMapEntry<K, V> nextInValueBucket = valueTable[valueBucket]; checkNoConflictInValueBucket(value, entry, nextInValueBucket); ImmutableMapEntry<K, V> newEntry; if (nextInValueBucket == null && nextInKeyBucket == null) { /* * TODO(user): consider using a NonTerminalImmutableMapEntry when nextInKeyBucket is * nonnull but nextInValueBucket is null. This may save a few bytes on some platforms, but * 2-morphic call sites are often optimized much better than 3-morphic, so it'd require * benchmarking. */ boolean reusable = entry instanceof ImmutableMapEntry && ((ImmutableMapEntry<K, V>) entry).isReusable(); newEntry = reusable ? (ImmutableMapEntry<K, V>) entry : new ImmutableMapEntry<K, V>(key, value); } else { newEntry = new NonTerminalImmutableBiMapEntry<K, V>( key, value, nextInKeyBucket, nextInValueBucket); } keyTable[keyBucket] = newEntry; valueTable[valueBucket] = newEntry; entries[i] = newEntry; hashCode += keyHash ^ valueHash; } return new RegularImmutableBiMap<K, V>(keyTable, valueTable, entries, mask, hashCode); }
RegularImmutableBiMap(Map.Entry<?, ?>[] entriesToAdd) { int n = entriesToAdd.length; int tableSize = Hashing.closedTableSize(n, 1.2D); this.mask = (tableSize - 1); ImmutableMapEntry<K, V>[] keyTable = createEntryArray(tableSize); ImmutableMapEntry<K, V>[] valueTable = createEntryArray(tableSize); ImmutableMapEntry<K, V>[] entries = createEntryArray(n); int hashCode = 0; for (int i = 0; i < n; i++) { Map.Entry<K, V> entry = entriesToAdd[i]; K key = entry.getKey(); V value = entry.getValue(); CollectPreconditions.checkEntryNotNull(key, value); int keyHash = key.hashCode(); int valueHash = value.hashCode(); int keyBucket = Hashing.smear(keyHash) & this.mask; int valueBucket = Hashing.smear(valueHash) & this.mask; ImmutableMapEntry<K, V> nextInKeyBucket = keyTable[keyBucket]; for (ImmutableMapEntry<K, V> keyEntry = nextInKeyBucket; keyEntry != null; keyEntry = keyEntry.getNextInKeyBucket()) { checkNoConflict(!key.equals(keyEntry.getKey()), "key", entry, keyEntry); } ImmutableMapEntry<K, V> nextInValueBucket = valueTable[valueBucket]; for (ImmutableMapEntry<K, V> valueEntry = nextInValueBucket; valueEntry != null; valueEntry = valueEntry.getNextInValueBucket()) { checkNoConflict(!value.equals(valueEntry.getValue()), "value", entry, valueEntry); } ImmutableMapEntry<K, V> newEntry = (nextInKeyBucket == null) && (nextInValueBucket == null) ? new ImmutableMapEntry.TerminalEntry(key, value) : new NonTerminalBiMapEntry(key, value, nextInKeyBucket, nextInValueBucket); keyTable[keyBucket] = newEntry; valueTable[valueBucket] = newEntry; entries[i] = newEntry; hashCode += (keyHash ^ valueHash); } this.keyTable = keyTable; this.valueTable = valueTable; this.entries = entries; this.hashCode = hashCode; }
public K get(@Nullable Object value) { if (value == null) { return null; } int bucket = Hashing.smear(value.hashCode()) & RegularImmutableBiMap.this.mask; for (ImmutableMapEntry<K, V> entry = RegularImmutableBiMap.this.valueTable[bucket]; entry != null; entry = entry.getNextInValueBucket()) { if (value.equals(entry.getValue())) { return (K) entry.getKey(); } } return null; }
@Nullable public V get(@Nullable Object key) { if (key == null) { return null; } int bucket = Hashing.smear(key.hashCode()) & this.mask; for (ImmutableMapEntry<K, V> entry = this.keyTable[bucket]; entry != null; entry = entry.getNextInKeyBucket()) { if (key.equals(entry.getKey())) { return (V) entry.getValue(); } } return null; }
@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; } } }
@Override public K get(@Nullable Object value) { if (value == null || valueTable == null) { return null; } int bucket = Hashing.smear(value.hashCode()) & mask; for (ImmutableMapEntry<K, V> entry = valueTable[bucket]; entry != null; entry = entry.getNextInValueBucket()) { if (value.equals(entry.getValue())) { return entry.getKey(); } } return null; }
private static int hash(@Nullable Object paramObject) { if (paramObject == null); for (int i = 0; ; i = paramObject.hashCode()) return Hashing.smear(i); }