RegularImmutableMap(Entry<?, ?>... immutableEntries) { // each of our 6 callers carefully put only Entry<K, V>s into the array! @SuppressWarnings("unchecked") Entry<K, V>[] tmp = (Entry<K, V>[]) immutableEntries; this.entries = tmp; int tableSize = Hashing.chooseTableSize(immutableEntries.length); table = new Object[tableSize * 2]; mask = tableSize - 1; int keySetHashCodeMutable = 0; for (Entry<K, V> entry : this.entries) { K key = entry.getKey(); int keyHashCode = key.hashCode(); for (int i = Hashing.smear(keyHashCode); true; i++) { int index = (i & mask) * 2; Object existing = table[index]; if (existing == null) { V value = entry.getValue(); table[index] = key; table[index + 1] = value; keySetHashCodeMutable += keyHashCode; break; } else if (existing.equals(key)) { throw new IllegalArgumentException("duplicate key: " + key); } } } keySetHashCode = keySetHashCodeMutable; }
private RegularImmutableMap(Entry<?, ?>... entries) { // each of our 6 callers carefully put only Entry<K, V>s into the array! @SuppressWarnings("unchecked") Entry<K, V>[] tmp = (Entry<K, V>[]) entries; this.entries = tmp; int tableSize = Hashing.chooseTableSize(entries.length); table = new Object[tableSize * 2]; mask = tableSize - 1; int keySetHashCodeMutable = 0; for (Entry<K, V> entry : this.entries) { K key = entry.getKey(); int keyHashCode = key.hashCode(); for (int i = Hashing.smear(keyHashCode); true; i++) { int index = (i & mask) * 2; Object existing = table[index]; if (existing == null) { V value = entry.getValue(); table[index] = key; table[index + 1] = value; keySetHashCodeMutable += keyHashCode; break; } else if (existing.equals(key)) { throw new IllegalArgumentException("duplicate key: " + key); } } } keySetHashCode = keySetHashCodeMutable; }
RegularImmutableMap(Entry<?, ?>... immutableEntries) { // each of our 6 callers carefully put only Entry<K, V>s into the array! @SuppressWarnings("unchecked") Entry<K, V>[] tmp = (Entry<K, V>[]) immutableEntries; this.entries = tmp; int tableSize = Hashing.chooseTableSize(immutableEntries.length); table = new Object[tableSize * 2]; mask = tableSize - 1; int keySetHashCodeMutable = 0; for (Entry<K, V> entry : this.entries) { K key = entry.getKey(); int keyHashCode = key.hashCode(); for (int i = Hashing.smear(keyHashCode); true; i++) { int index = (i & mask) * 2; Object existing = table[index]; if (existing == null) { V value = entry.getValue(); table[index] = key; table[index + 1] = value; keySetHashCodeMutable += keyHashCode; break; } else if (existing.equals(key)) { throw new IllegalArgumentException("duplicate key: " + key); } } } keySetHashCode = keySetHashCodeMutable; }
RegularImmutableMap(Entry<?, ?>... immutableEntries) { // each of our 6 callers carefully put only Entry<K, V>s into the array! @SuppressWarnings("unchecked") Entry<K, V>[] tmp = (Entry<K, V>[]) immutableEntries; this.entries = tmp; int tableSize = Hashing.chooseTableSize(immutableEntries.length); table = new Object[tableSize * 2]; mask = tableSize - 1; int keySetHashCodeMutable = 0; for (Entry<K, V> entry : this.entries) { K key = entry.getKey(); int keyHashCode = key.hashCode(); for (int i = Hashing.smear(keyHashCode); true; i++) { int index = (i & mask) * 2; Object existing = table[index]; if (existing == null) { V value = entry.getValue(); table[index] = key; table[index + 1] = value; keySetHashCodeMutable += keyHashCode; break; } else if (existing.equals(key)) { throw new IllegalArgumentException("duplicate key: " + key); } } } keySetHashCode = keySetHashCodeMutable; }
private static <E> ImmutableSet<E> create( Iterable<? extends E> iterable, int count) { int tableSize = Hashing.chooseTableSize(count); Object[] table = new Object[tableSize]; int mask = tableSize - 1; } else if (tableSize > Hashing.chooseTableSize(elements.size())) {
private static <E> ImmutableSet<E> create( Iterable<? extends E> iterable, int count) { int tableSize = Hashing.chooseTableSize(count); Object[] table = new Object[tableSize]; int mask = tableSize - 1; } else if (tableSize > Hashing.chooseTableSize(elements.size())) {
private static <E> ImmutableSet<E> create( Iterable<? extends E> iterable, int count) { int tableSize = Hashing.chooseTableSize(count); Object[] table = new Object[tableSize]; int mask = tableSize - 1; } else if (tableSize > Hashing.chooseTableSize(elements.size())) {
private static <E> ImmutableSet<E> create( Iterable<? extends E> iterable, int count) { // count is always the (nonzero) number of elements in the iterable int tableSize = Hashing.chooseTableSize(count); Object[] table = new Object[tableSize]; int mask = tableSize - 1; List<E> elements = new ArrayList<E>(count); int hashCode = 0; for (E element : iterable) { int hash = element.hashCode(); for (int i = Hashing.smear(hash); true; i++) { int index = i & mask; Object value = table[index]; if (value == null) { // Came to an empty bucket. Put the element here. table[index] = element; elements.add(element); hashCode += hash; break; } else if (value.equals(element)) { break; // Found a duplicate. Nothing to do. } } } // The iterable might have contained only duplicates of the same element. return (elements.size() == 1) ? new SingletonImmutableSet<E>(elements.get(0), hashCode) : new RegularImmutableSet<E>(elements.toArray(), hashCode, table, mask); }