001/* 002 * Copyright (C) 2007 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.google.common.collect; 018 019import static com.google.common.base.Preconditions.checkArgument; 020import static com.google.common.base.Preconditions.checkNotNull; 021import static com.google.common.collect.CollectPreconditions.checkNonnegative; 022import static java.util.Objects.requireNonNull; 023 024import com.google.common.annotations.GwtCompatible; 025import com.google.common.annotations.GwtIncompatible; 026import com.google.common.annotations.J2ktIncompatible; 027import com.google.common.annotations.VisibleForTesting; 028import com.google.common.math.IntMath; 029import com.google.common.primitives.Ints; 030import com.google.errorprone.annotations.CanIgnoreReturnValue; 031import com.google.errorprone.annotations.concurrent.LazyInit; 032import com.google.j2objc.annotations.RetainedWith; 033import java.io.InvalidObjectException; 034import java.io.ObjectInputStream; 035import java.io.Serializable; 036import java.math.RoundingMode; 037import java.util.Arrays; 038import java.util.Collection; 039import java.util.Collections; 040import java.util.EnumSet; 041import java.util.Iterator; 042import java.util.Set; 043import java.util.SortedSet; 044import java.util.Spliterator; 045import java.util.function.Consumer; 046import java.util.stream.Collector; 047import javax.annotation.CheckForNull; 048import org.checkerframework.checker.nullness.qual.Nullable; 049 050/** 051 * A {@link Set} whose contents will never change, with many other important properties detailed at 052 * {@link ImmutableCollection}. 053 * 054 * @since 2.0 055 */ 056@GwtCompatible(serializable = true, emulated = true) 057@SuppressWarnings("serial") // we're overriding default serialization 058@ElementTypesAreNonnullByDefault 059public abstract class ImmutableSet<E> extends ImmutableCollection<E> implements Set<E> { 060 static final int SPLITERATOR_CHARACTERISTICS = 061 ImmutableCollection.SPLITERATOR_CHARACTERISTICS | Spliterator.DISTINCT; 062 063 /** 064 * Returns a {@code Collector} that accumulates the input elements into a new {@code 065 * ImmutableSet}. Elements appear in the resulting set in the encounter order of the stream; if 066 * the stream contains duplicates (according to {@link Object#equals(Object)}), only the first 067 * duplicate in encounter order will appear in the result. 068 * 069 * @since 21.0 070 */ 071 public static <E> Collector<E, ?, ImmutableSet<E>> toImmutableSet() { 072 return CollectCollectors.toImmutableSet(); 073 } 074 075 /** 076 * Returns the empty immutable set. Preferred over {@link Collections#emptySet} for code 077 * consistency, and because the return type conveys the immutability guarantee. 078 * 079 * <p><b>Performance note:</b> the instance returned is a singleton. 080 */ 081 @SuppressWarnings({"unchecked"}) // fully variant implementation (never actually produces any Es) 082 public static <E> ImmutableSet<E> of() { 083 return (ImmutableSet<E>) RegularImmutableSet.EMPTY; 084 } 085 086 /** 087 * Returns an immutable set containing {@code element}. Preferred over {@link 088 * Collections#singleton} for code consistency, {@code null} rejection, and because the return 089 * type conveys the immutability guarantee. 090 */ 091 public static <E> ImmutableSet<E> of(E element) { 092 return new SingletonImmutableSet<>(element); 093 } 094 095 /* 096 * TODO: b/315526394 - Skip the Builder entirely for the of(...) methods, since we don't need to 097 * worry that we might trigger the fallback to the JDK-backed implementation? (The varargs one 098 * _could_, so we could keep it as it is. Or we could convince ourselves that hash flooding is 099 * unlikely in practice there, too.) 100 */ 101 102 /** 103 * Returns an immutable set containing the given elements, minus duplicates, in the order each was 104 * first specified. That is, if multiple elements are {@linkplain Object#equals equal}, all except 105 * the first are ignored. 106 */ 107 public static <E> ImmutableSet<E> of(E e1, E e2) { 108 return new RegularSetBuilderImpl<E>(2).add(e1).add(e2).review().build(); 109 } 110 111 /** 112 * Returns an immutable set containing the given elements, minus duplicates, in the order each was 113 * first specified. That is, if multiple elements are {@linkplain Object#equals equal}, all except 114 * the first are ignored. 115 */ 116 public static <E> ImmutableSet<E> of(E e1, E e2, E e3) { 117 return new RegularSetBuilderImpl<E>(3).add(e1).add(e2).add(e3).review().build(); 118 } 119 120 /** 121 * Returns an immutable set containing the given elements, minus duplicates, in the order each was 122 * first specified. That is, if multiple elements are {@linkplain Object#equals equal}, all except 123 * the first are ignored. 124 */ 125 public static <E> ImmutableSet<E> of(E e1, E e2, E e3, E e4) { 126 return new RegularSetBuilderImpl<E>(4).add(e1).add(e2).add(e3).add(e4).review().build(); 127 } 128 129 /** 130 * Returns an immutable set containing the given elements, minus duplicates, in the order each was 131 * first specified. That is, if multiple elements are {@linkplain Object#equals equal}, all except 132 * the first are ignored. 133 */ 134 public static <E> ImmutableSet<E> of(E e1, E e2, E e3, E e4, E e5) { 135 return new RegularSetBuilderImpl<E>(5).add(e1).add(e2).add(e3).add(e4).add(e5).review().build(); 136 } 137 138 /** 139 * Returns an immutable set containing the given elements, minus duplicates, in the order each was 140 * first specified. That is, if multiple elements are {@linkplain Object#equals equal}, all except 141 * the first are ignored. 142 * 143 * <p>The array {@code others} must not be longer than {@code Integer.MAX_VALUE - 6}. 144 * 145 * @since 3.0 (source-compatible since 2.0) 146 */ 147 @SafeVarargs // For Eclipse. For internal javac we have disabled this pointless type of warning. 148 public static <E> ImmutableSet<E> of(E e1, E e2, E e3, E e4, E e5, E e6, E... others) { 149 checkArgument( 150 others.length <= Integer.MAX_VALUE - 6, "the total number of elements must fit in an int"); 151 SetBuilderImpl<E> builder = new RegularSetBuilderImpl<>(6 + others.length); 152 builder = builder.add(e1).add(e2).add(e3).add(e4).add(e5).add(e6); 153 for (int i = 0; i < others.length; i++) { 154 builder = builder.add(others[i]); 155 } 156 return builder.review().build(); 157 } 158 159 /** 160 * Returns an immutable set containing each of {@code elements}, minus duplicates, in the order 161 * each appears first in the source collection. 162 * 163 * <p><b>Performance note:</b> This method will sometimes recognize that the actual copy operation 164 * is unnecessary; for example, {@code copyOf(copyOf(anArrayList))} will copy the data only once. 165 * This reduces the expense of habitually making defensive copies at API boundaries. However, the 166 * precise conditions for skipping the copy operation are undefined. 167 * 168 * @throws NullPointerException if any of {@code elements} is null 169 * @since 7.0 (source-compatible since 2.0) 170 */ 171 // This the best we could do to get copyOfEnumSet to compile in the mainline. 172 // The suppression also covers the cast to E[], discussed below. 173 // In the backport, we don't have those cases and thus don't need this suppression. 174 // We keep it to minimize diffs. 175 @SuppressWarnings("unchecked") 176 public static <E> ImmutableSet<E> copyOf(Collection<? extends E> elements) { 177 /* 178 * TODO(lowasser): consider checking for ImmutableAsList here 179 * TODO(lowasser): consider checking for Multiset here 180 */ 181 // Don't refer to ImmutableSortedSet by name so it won't pull in all that code 182 if (elements instanceof ImmutableSet && !(elements instanceof SortedSet)) { 183 @SuppressWarnings("unchecked") // all supported methods are covariant 184 ImmutableSet<E> set = (ImmutableSet<E>) elements; 185 if (!set.isPartialView()) { 186 return set; 187 } 188 } else if (elements instanceof EnumSet) { 189 return copyOfEnumSet((EnumSet<?>) elements); 190 } 191 192 if (elements.isEmpty()) { 193 // We avoid allocating anything. 194 return of(); 195 } 196 // Collection<E>.toArray() is required to contain only E instances, and all we do is read them. 197 // TODO(cpovirk): Consider using Object[] anyway. 198 E[] array = (E[]) elements.toArray(); 199 /* 200 * For a Set, we guess that it contains no duplicates. That's just a guess for purpose of 201 * sizing; if the Set uses different equality semantics, it might contain duplicates according 202 * to equals(), and we will deduplicate those properly, albeit at some cost in allocations. 203 */ 204 int expectedSize = 205 elements instanceof Set ? array.length : estimatedSizeForUnknownDuplication(array.length); 206 return fromArrayWithExpectedSize(array, expectedSize); 207 } 208 209 /** 210 * Returns an immutable set containing each of {@code elements}, minus duplicates, in the order 211 * each appears first in the source iterable. This method iterates over {@code elements} only 212 * once. 213 * 214 * <p><b>Performance note:</b> This method will sometimes recognize that the actual copy operation 215 * is unnecessary; for example, {@code copyOf(copyOf(anArrayList))} should copy the data only 216 * once. This reduces the expense of habitually making defensive copies at API boundaries. 217 * However, the precise conditions for skipping the copy operation are undefined. 218 * 219 * @throws NullPointerException if any of {@code elements} is null 220 */ 221 public static <E> ImmutableSet<E> copyOf(Iterable<? extends E> elements) { 222 return (elements instanceof Collection) 223 ? copyOf((Collection<? extends E>) elements) 224 : copyOf(elements.iterator()); 225 } 226 227 /** 228 * Returns an immutable set containing each of {@code elements}, minus duplicates, in the order 229 * each appears first in the source iterator. 230 * 231 * @throws NullPointerException if any of {@code elements} is null 232 */ 233 public static <E> ImmutableSet<E> copyOf(Iterator<? extends E> elements) { 234 // We special-case for 0 or 1 elements, but anything further is madness. 235 if (!elements.hasNext()) { 236 return of(); 237 } 238 E first = elements.next(); 239 if (!elements.hasNext()) { 240 return of(first); 241 } else { 242 return new ImmutableSet.Builder<E>().add(first).addAll(elements).build(); 243 } 244 } 245 246 /** 247 * Returns an immutable set containing each of {@code elements}, minus duplicates, in the order 248 * each appears first in the source array. 249 * 250 * @throws NullPointerException if any of {@code elements} is null 251 * @since 3.0 252 */ 253 public static <E> ImmutableSet<E> copyOf(E[] elements) { 254 return fromArrayWithExpectedSize(elements, estimatedSizeForUnknownDuplication(elements.length)); 255 } 256 257 private static <E> ImmutableSet<E> fromArrayWithExpectedSize(E[] elements, int expectedSize) { 258 switch (elements.length) { 259 case 0: 260 return of(); 261 case 1: 262 return of(elements[0]); 263 default: 264 SetBuilderImpl<E> builder = new RegularSetBuilderImpl<>(expectedSize); 265 for (int i = 0; i < elements.length; i++) { 266 builder = builder.add(elements[i]); 267 } 268 return builder.review().build(); 269 } 270 } 271 272 @SuppressWarnings({"rawtypes", "unchecked"}) // necessary to compile against Java 8 273 private static ImmutableSet copyOfEnumSet(EnumSet<?> enumSet) { 274 return ImmutableEnumSet.asImmutable(EnumSet.copyOf((EnumSet) enumSet)); 275 } 276 277 ImmutableSet() {} 278 279 /** Returns {@code true} if the {@code hashCode()} method runs quickly. */ 280 boolean isHashCodeFast() { 281 return false; 282 } 283 284 @Override 285 public boolean equals(@CheckForNull Object object) { 286 if (object == this) { 287 return true; 288 } 289 if (object instanceof ImmutableSet 290 && isHashCodeFast() 291 && ((ImmutableSet<?>) object).isHashCodeFast() 292 && hashCode() != object.hashCode()) { 293 return false; 294 } 295 return Sets.equalsImpl(this, object); 296 } 297 298 @Override 299 public int hashCode() { 300 return Sets.hashCodeImpl(this); 301 } 302 303 // This declaration is needed to make Set.iterator() and 304 // ImmutableCollection.iterator() consistent. 305 @Override 306 public abstract UnmodifiableIterator<E> iterator(); 307 308 @GwtCompatible 309 abstract static class CachingAsList<E> extends ImmutableSet<E> { 310 @LazyInit @RetainedWith @CheckForNull private transient ImmutableList<E> asList; 311 312 @Override 313 public ImmutableList<E> asList() { 314 ImmutableList<E> result = asList; 315 if (result == null) { 316 return asList = createAsList(); 317 } else { 318 return result; 319 } 320 } 321 322 ImmutableList<E> createAsList() { 323 return new RegularImmutableAsList<>(this, toArray()); 324 } 325 326 // redeclare to help optimizers with b/310253115 327 @SuppressWarnings("RedundantOverride") 328 @Override 329 @J2ktIncompatible // serialization 330 @GwtIncompatible // serialization 331 Object writeReplace() { 332 return super.writeReplace(); 333 } 334 } 335 336 abstract static class Indexed<E> extends CachingAsList<E> { 337 abstract E get(int index); 338 339 @Override 340 public UnmodifiableIterator<E> iterator() { 341 return asList().iterator(); 342 } 343 344 @Override 345 public Spliterator<E> spliterator() { 346 return CollectSpliterators.indexed(size(), SPLITERATOR_CHARACTERISTICS, this::get); 347 } 348 349 @Override 350 public void forEach(Consumer<? super E> consumer) { 351 checkNotNull(consumer); 352 int n = size(); 353 for (int i = 0; i < n; i++) { 354 consumer.accept(get(i)); 355 } 356 } 357 358 @Override 359 int copyIntoArray(@Nullable Object[] dst, int offset) { 360 return asList().copyIntoArray(dst, offset); 361 } 362 363 @Override 364 ImmutableList<E> createAsList() { 365 return new ImmutableAsList<E>() { 366 @Override 367 public E get(int index) { 368 return Indexed.this.get(index); 369 } 370 371 @Override 372 Indexed<E> delegateCollection() { 373 return Indexed.this; 374 } 375 376 // redeclare to help optimizers with b/310253115 377 @SuppressWarnings("RedundantOverride") 378 @Override 379 @J2ktIncompatible // serialization 380 @GwtIncompatible // serialization 381 Object writeReplace() { 382 return super.writeReplace(); 383 } 384 }; 385 } 386 387 // redeclare to help optimizers with b/310253115 388 @SuppressWarnings("RedundantOverride") 389 @Override 390 @J2ktIncompatible // serialization 391 @GwtIncompatible // serialization 392 Object writeReplace() { 393 return super.writeReplace(); 394 } 395 } 396 397 /* 398 * This class is used to serialize all ImmutableSet instances, except for 399 * ImmutableEnumSet/ImmutableSortedSet, regardless of implementation type. It 400 * captures their "logical contents" and they are reconstructed using public 401 * static factories. This is necessary to ensure that the existence of a 402 * particular implementation type is an implementation detail. 403 */ 404 @J2ktIncompatible // serialization 405 private static class SerializedForm implements Serializable { 406 final Object[] elements; 407 408 SerializedForm(Object[] elements) { 409 this.elements = elements; 410 } 411 412 Object readResolve() { 413 return copyOf(elements); 414 } 415 416 private static final long serialVersionUID = 0; 417 } 418 419 @Override 420 @J2ktIncompatible // serialization 421 Object writeReplace() { 422 return new SerializedForm(toArray()); 423 } 424 425 @J2ktIncompatible // serialization 426 private void readObject(ObjectInputStream stream) throws InvalidObjectException { 427 throw new InvalidObjectException("Use SerializedForm"); 428 } 429 430 /** 431 * Returns a new builder. The generated builder is equivalent to the builder created by the {@link 432 * Builder} constructor. 433 */ 434 public static <E> Builder<E> builder() { 435 return new Builder<>(); 436 } 437 438 /** 439 * Returns a new builder, expecting the specified number of distinct elements to be added. 440 * 441 * <p>If {@code expectedSize} is exactly the number of distinct elements added to the builder 442 * before {@link Builder#build} is called, the builder is likely to perform better than an unsized 443 * {@link #builder()} would have. 444 * 445 * <p>It is not specified if any performance benefits apply if {@code expectedSize} is close to, 446 * but not exactly, the number of distinct elements added to the builder. 447 * 448 * @since 23.1 449 */ 450 public static <E> Builder<E> builderWithExpectedSize(int expectedSize) { 451 checkNonnegative(expectedSize, "expectedSize"); 452 return new Builder<>(expectedSize); 453 } 454 455 /** 456 * A builder for creating {@code ImmutableSet} instances. Example: 457 * 458 * <pre>{@code 459 * static final ImmutableSet<Color> GOOGLE_COLORS = 460 * ImmutableSet.<Color>builder() 461 * .addAll(WEBSAFE_COLORS) 462 * .add(new Color(0, 191, 255)) 463 * .build(); 464 * }</pre> 465 * 466 * <p>Elements appear in the resulting set in the same order they were first added to the builder. 467 * 468 * <p>Building does not change the state of the builder, so it is still possible to add more 469 * elements and to build again. 470 * 471 * @since 2.0 472 */ 473 public static class Builder<E> extends ImmutableCollection.Builder<E> { 474 /* 475 * `impl` is null only for instances of the subclass, ImmutableSortedSet.Builder. That subclass 476 * overrides all the methods that access it here. Thus, all the methods here can safely assume 477 * that this field is non-null. 478 */ 479 @CheckForNull private SetBuilderImpl<E> impl; 480 boolean forceCopy; 481 482 public Builder() { 483 this(0); 484 } 485 486 Builder(int capacity) { 487 if (capacity > 0) { 488 impl = new RegularSetBuilderImpl<>(capacity); 489 } else { 490 impl = EmptySetBuilderImpl.instance(); 491 } 492 } 493 494 Builder(@SuppressWarnings("unused") boolean subclass) { 495 this.impl = null; // unused 496 } 497 498 @VisibleForTesting 499 void forceJdk() { 500 requireNonNull(impl); // see the comment on the field 501 this.impl = new JdkBackedSetBuilderImpl<>(impl); 502 } 503 504 final void copyIfNecessary() { 505 if (forceCopy) { 506 copy(); 507 forceCopy = false; 508 } 509 } 510 511 void copy() { 512 requireNonNull(impl); // see the comment on the field 513 impl = impl.copy(); 514 } 515 516 @Override 517 @CanIgnoreReturnValue 518 public Builder<E> add(E element) { 519 requireNonNull(impl); // see the comment on the field 520 checkNotNull(element); 521 copyIfNecessary(); 522 impl = impl.add(element); 523 return this; 524 } 525 526 @Override 527 @CanIgnoreReturnValue 528 public Builder<E> add(E... elements) { 529 super.add(elements); 530 return this; 531 } 532 533 /** 534 * Adds each element of {@code elements} to the {@code ImmutableSet}, ignoring duplicate 535 * elements (only the first duplicate element is added). 536 * 537 * @param elements the elements to add 538 * @return this {@code Builder} object 539 * @throws NullPointerException if {@code elements} is null or contains a null element 540 */ 541 @Override 542 @CanIgnoreReturnValue 543 public Builder<E> addAll(Iterable<? extends E> elements) { 544 super.addAll(elements); 545 return this; 546 } 547 548 @Override 549 @CanIgnoreReturnValue 550 public Builder<E> addAll(Iterator<? extends E> elements) { 551 super.addAll(elements); 552 return this; 553 } 554 555 @CanIgnoreReturnValue 556 Builder<E> combine(Builder<E> other) { 557 requireNonNull(impl); 558 requireNonNull(other.impl); 559 /* 560 * For discussion of requireNonNull, see the comment on the field. 561 * 562 * (And I don't believe there's any situation in which we call x.combine(y) when x is a plain 563 * ImmutableSet.Builder but y is an ImmutableSortedSet.Builder (or vice versa). Certainly 564 * ImmutableSortedSet.Builder.combine() is written as if its argument will never be a plain 565 * ImmutableSet.Builder: It casts immediately to ImmutableSortedSet.Builder.) 566 */ 567 copyIfNecessary(); 568 this.impl = this.impl.combine(other.impl); 569 return this; 570 } 571 572 @Override 573 public ImmutableSet<E> build() { 574 requireNonNull(impl); // see the comment on the field 575 forceCopy = true; 576 impl = impl.review(); 577 return impl.build(); 578 } 579 } 580 581 /** Swappable internal implementation of an ImmutableSet.Builder. */ 582 private abstract static class SetBuilderImpl<E> { 583 // The first `distinct` elements are non-null. 584 // Since we can never access null elements, we don't mark this nullable. 585 E[] dedupedElements; 586 int distinct; 587 588 @SuppressWarnings("unchecked") 589 SetBuilderImpl(int expectedCapacity) { 590 this.dedupedElements = (E[]) new Object[expectedCapacity]; 591 this.distinct = 0; 592 } 593 594 /** Initializes this SetBuilderImpl with a copy of the deduped elements array from toCopy. */ 595 SetBuilderImpl(SetBuilderImpl<E> toCopy) { 596 this.dedupedElements = Arrays.copyOf(toCopy.dedupedElements, toCopy.dedupedElements.length); 597 this.distinct = toCopy.distinct; 598 } 599 600 /** 601 * Resizes internal data structures if necessary to store the specified number of distinct 602 * elements. 603 */ 604 private void ensureCapacity(int minCapacity) { 605 if (minCapacity > dedupedElements.length) { 606 int newCapacity = 607 ImmutableCollection.Builder.expandedCapacity(dedupedElements.length, minCapacity); 608 dedupedElements = Arrays.copyOf(dedupedElements, newCapacity); 609 } 610 } 611 612 /** Adds e to the insertion-order array of deduplicated elements. Calls ensureCapacity. */ 613 final void addDedupedElement(E e) { 614 ensureCapacity(distinct + 1); 615 dedupedElements[distinct++] = e; 616 } 617 618 /** 619 * Adds e to this SetBuilderImpl, returning the updated result. Only use the returned 620 * SetBuilderImpl, since we may switch implementations if e.g. hash flooding is detected. 621 */ 622 abstract SetBuilderImpl<E> add(E e); 623 624 /** Adds all the elements from the specified SetBuilderImpl to this SetBuilderImpl. */ 625 final SetBuilderImpl<E> combine(SetBuilderImpl<E> other) { 626 SetBuilderImpl<E> result = this; 627 for (int i = 0; i < other.distinct; i++) { 628 /* 629 * requireNonNull is safe because we ensure that the first `distinct` elements have been 630 * populated. 631 */ 632 result = result.add(requireNonNull(other.dedupedElements[i])); 633 } 634 return result; 635 } 636 637 /** 638 * Creates a new copy of this SetBuilderImpl. Modifications to that SetBuilderImpl will not 639 * affect this SetBuilderImpl or sets constructed from this SetBuilderImpl via build(). 640 */ 641 abstract SetBuilderImpl<E> copy(); 642 643 /** 644 * Call this before build(). Does a final check on the internal data structures, e.g. shrinking 645 * unnecessarily large structures or detecting previously unnoticed hash flooding. 646 */ 647 SetBuilderImpl<E> review() { 648 return this; 649 } 650 651 abstract ImmutableSet<E> build(); 652 } 653 654 private static final class EmptySetBuilderImpl<E> extends SetBuilderImpl<E> { 655 private static final EmptySetBuilderImpl<Object> INSTANCE = new EmptySetBuilderImpl<>(); 656 657 @SuppressWarnings("unchecked") 658 static <E> SetBuilderImpl<E> instance() { 659 return (SetBuilderImpl<E>) INSTANCE; 660 } 661 662 private EmptySetBuilderImpl() { 663 super(0); 664 } 665 666 @Override 667 SetBuilderImpl<E> add(E e) { 668 return new RegularSetBuilderImpl<E>(Builder.DEFAULT_INITIAL_CAPACITY).add(e); 669 } 670 671 @Override 672 SetBuilderImpl<E> copy() { 673 return this; 674 } 675 676 @Override 677 ImmutableSet<E> build() { 678 return ImmutableSet.of(); 679 } 680 } 681 682 // We use power-of-2 tables, and this is the highest int that's a power of 2 683 static final int MAX_TABLE_SIZE = Ints.MAX_POWER_OF_TWO; 684 685 // Represents how tightly we can pack things, as a maximum. 686 private static final double DESIRED_LOAD_FACTOR = 0.7; 687 688 // If the set has this many elements, it will "max out" the table size 689 private static final int CUTOFF = (int) (MAX_TABLE_SIZE * DESIRED_LOAD_FACTOR); 690 691 /** 692 * Returns an array size suitable for the backing array of a hash table that uses open addressing 693 * with linear probing in its implementation. The returned size is the smallest power of two that 694 * can hold setSize elements with the desired load factor. Always returns at least setSize + 2. 695 */ 696 // TODO(cpovirk): Move to Hashing or something, since it's used elsewhere in the Android version. 697 static int chooseTableSize(int setSize) { 698 setSize = Math.max(setSize, 2); 699 // Correct the size for open addressing to match desired load factor. 700 if (setSize < CUTOFF) { 701 // Round up to the next highest power of 2. 702 int tableSize = Integer.highestOneBit(setSize - 1) << 1; 703 while (tableSize * DESIRED_LOAD_FACTOR < setSize) { 704 tableSize <<= 1; 705 } 706 return tableSize; 707 } 708 709 // The table can't be completely full or we'll get infinite reprobes 710 checkArgument(setSize < MAX_TABLE_SIZE, "collection too large"); 711 return MAX_TABLE_SIZE; 712 } 713 714 /** 715 * Default implementation of the guts of ImmutableSet.Builder, creating an open-addressed hash 716 * table and deduplicating elements as they come, so it only allocates O(max(distinct, 717 * expectedCapacity)) rather than O(calls to add). 718 * 719 * <p>This implementation attempts to detect hash flooding, and if it's identified, falls back to 720 * JdkBackedSetBuilderImpl. 721 */ 722 private static final class RegularSetBuilderImpl<E> extends SetBuilderImpl<E> { 723 // null until at least two elements are present 724 @CheckForNull private @Nullable Object[] hashTable; 725 private int maxRunBeforeFallback; 726 private int expandTableThreshold; 727 private int hashCode; 728 729 RegularSetBuilderImpl(int expectedCapacity) { 730 super(expectedCapacity); 731 this.hashTable = null; 732 this.maxRunBeforeFallback = 0; 733 this.expandTableThreshold = 0; 734 } 735 736 RegularSetBuilderImpl(RegularSetBuilderImpl<E> toCopy) { 737 super(toCopy); 738 this.hashTable = (toCopy.hashTable == null) ? null : toCopy.hashTable.clone(); 739 this.maxRunBeforeFallback = toCopy.maxRunBeforeFallback; 740 this.expandTableThreshold = toCopy.expandTableThreshold; 741 this.hashCode = toCopy.hashCode; 742 } 743 744 @Override 745 SetBuilderImpl<E> add(E e) { 746 checkNotNull(e); 747 if (hashTable == null) { 748 if (distinct == 0) { 749 addDedupedElement(e); 750 return this; 751 } else { 752 ensureTableCapacity(dedupedElements.length); 753 E elem = dedupedElements[0]; 754 distinct--; 755 return insertInHashTable(elem).add(e); 756 } 757 } 758 return insertInHashTable(e); 759 } 760 761 private SetBuilderImpl<E> insertInHashTable(E e) { 762 requireNonNull(hashTable); 763 int eHash = e.hashCode(); 764 int i0 = Hashing.smear(eHash); 765 int mask = hashTable.length - 1; 766 for (int i = i0; i - i0 < maxRunBeforeFallback; i++) { 767 int index = i & mask; 768 Object tableEntry = hashTable[index]; 769 if (tableEntry == null) { 770 addDedupedElement(e); 771 hashTable[index] = e; 772 hashCode += eHash; 773 ensureTableCapacity(distinct); // rebuilds table if necessary 774 return this; 775 } else if (tableEntry.equals(e)) { // not a new element, ignore 776 return this; 777 } 778 } 779 // we fell out of the loop due to a long run; fall back to JDK impl 780 return new JdkBackedSetBuilderImpl<E>(this).add(e); 781 } 782 783 @Override 784 SetBuilderImpl<E> copy() { 785 return new RegularSetBuilderImpl<>(this); 786 } 787 788 @Override 789 SetBuilderImpl<E> review() { 790 if (hashTable == null) { 791 return this; 792 } 793 int targetTableSize = chooseTableSize(distinct); 794 if (targetTableSize * 2 < hashTable.length) { 795 hashTable = rebuildHashTable(targetTableSize, dedupedElements, distinct); 796 maxRunBeforeFallback = maxRunBeforeFallback(targetTableSize); 797 expandTableThreshold = (int) (DESIRED_LOAD_FACTOR * targetTableSize); 798 } 799 return hashFloodingDetected(hashTable) ? new JdkBackedSetBuilderImpl<E>(this) : this; 800 } 801 802 @Override 803 ImmutableSet<E> build() { 804 switch (distinct) { 805 case 0: 806 return of(); 807 case 1: 808 /* 809 * requireNonNull is safe because we ensure that the first `distinct` elements have been 810 * populated. 811 */ 812 return of(requireNonNull(dedupedElements[0])); 813 default: 814 /* 815 * The suppression is safe because we ensure that the first `distinct` elements have been 816 * populated. 817 */ 818 @SuppressWarnings("nullness") 819 Object[] elements = 820 (distinct == dedupedElements.length) 821 ? dedupedElements 822 : Arrays.copyOf(dedupedElements, distinct); 823 return new RegularImmutableSet<>( 824 elements, hashCode, requireNonNull(hashTable), hashTable.length - 1); 825 } 826 } 827 828 /** Builds a new open-addressed hash table from the first n objects in elements. */ 829 static @Nullable Object[] rebuildHashTable(int newTableSize, Object[] elements, int n) { 830 @Nullable Object[] hashTable = new @Nullable Object[newTableSize]; 831 int mask = hashTable.length - 1; 832 for (int i = 0; i < n; i++) { 833 // requireNonNull is safe because we ensure that the first n elements have been populated. 834 Object e = requireNonNull(elements[i]); 835 int j0 = Hashing.smear(e.hashCode()); 836 for (int j = j0; ; j++) { 837 int index = j & mask; 838 if (hashTable[index] == null) { 839 hashTable[index] = e; 840 break; 841 } 842 } 843 } 844 return hashTable; 845 } 846 847 void ensureTableCapacity(int minCapacity) { 848 int newTableSize; 849 if (hashTable == null) { 850 newTableSize = chooseTableSize(minCapacity); 851 hashTable = new Object[newTableSize]; 852 } else if (minCapacity > expandTableThreshold && hashTable.length < MAX_TABLE_SIZE) { 853 newTableSize = hashTable.length * 2; 854 hashTable = rebuildHashTable(newTableSize, dedupedElements, distinct); 855 } else { 856 return; 857 } 858 maxRunBeforeFallback = maxRunBeforeFallback(newTableSize); 859 expandTableThreshold = (int) (DESIRED_LOAD_FACTOR * newTableSize); 860 } 861 862 /** 863 * We attempt to detect deliberate hash flooding attempts. If one is detected, we fall back to a 864 * wrapper around j.u.HashSet, which has built-in flooding protection. MAX_RUN_MULTIPLIER was 865 * determined experimentally to match our desired probability of false positives. 866 */ 867 // NB: yes, this is surprisingly high, but that's what the experiments said was necessary 868 // Raising this number slows the worst-case contains behavior, speeds up hashFloodingDetected, 869 // and reduces the false-positive probability. 870 static final int MAX_RUN_MULTIPLIER = 13; 871 872 /** 873 * Checks the whole hash table for poor hash distribution. Takes O(n) in the worst case, O(n / 874 * log n) on average. 875 * 876 * <p>The online hash flooding detecting in RegularSetBuilderImpl.add can detect e.g. many 877 * exactly matching hash codes, which would cause construction to take O(n^2), but can't detect 878 * e.g. hash codes adversarially designed to go into ascending table locations, which keeps 879 * construction O(n) (as desired) but then can have O(n) queries later. 880 * 881 * <p>If this returns false, then no query can take more than O(log n). 882 * 883 * <p>Note that for a RegularImmutableSet with elements with truly random hash codes, contains 884 * operations take expected O(1) time but with high probability take O(log n) for at least some 885 * element. (https://en.wikipedia.org/wiki/Linear_probing#Analysis) 886 * 887 * <p>This method may return {@code true} even on truly random input, but {@code 888 * ImmutableSetTest} tests that the probability of that is low. 889 */ 890 static boolean hashFloodingDetected(@Nullable Object[] hashTable) { 891 int maxRunBeforeFallback = maxRunBeforeFallback(hashTable.length); 892 int mask = hashTable.length - 1; 893 894 // Invariant: all elements at indices in [knownRunStart, knownRunEnd) are nonnull. 895 // If knownRunStart == knownRunEnd, this is vacuously true. 896 // When knownRunEnd exceeds hashTable.length, it "wraps", detecting runs around the end 897 // of the table. 898 int knownRunStart = 0; 899 int knownRunEnd = 0; 900 901 outerLoop: 902 while (knownRunStart < hashTable.length) { 903 if (knownRunStart == knownRunEnd && hashTable[knownRunStart] == null) { 904 if (hashTable[(knownRunStart + maxRunBeforeFallback - 1) & mask] == null) { 905 // There are only maxRunBeforeFallback - 1 elements between here and there, 906 // so even if they were all nonnull, we wouldn't detect a hash flood. Therefore, 907 // we can skip them all. 908 knownRunStart += maxRunBeforeFallback; 909 } else { 910 knownRunStart++; // the only case in which maxRunEnd doesn't increase by mRBF 911 // happens about f * (1-f) for f = DESIRED_LOAD_FACTOR, so around 21% of the time 912 } 913 knownRunEnd = knownRunStart; 914 } else { 915 for (int j = knownRunStart + maxRunBeforeFallback - 1; j >= knownRunEnd; j--) { 916 if (hashTable[j & mask] == null) { 917 knownRunEnd = knownRunStart + maxRunBeforeFallback; 918 knownRunStart = j + 1; 919 continue outerLoop; 920 } 921 } 922 return true; 923 } 924 } 925 return false; 926 } 927 928 /** 929 * If more than this many consecutive positions are filled in a table of the specified size, 930 * report probable hash flooding. ({@link #hashFloodingDetected} may also report hash flooding 931 * if fewer consecutive positions are filled; see that method for details.) 932 */ 933 static int maxRunBeforeFallback(int tableSize) { 934 return MAX_RUN_MULTIPLIER * IntMath.log2(tableSize, RoundingMode.UNNECESSARY); 935 } 936 } 937 938 /** 939 * SetBuilderImpl version that uses a JDK HashSet, which has built in hash flooding protection. 940 */ 941 private static final class JdkBackedSetBuilderImpl<E> extends SetBuilderImpl<E> { 942 private final Set<Object> delegate; 943 944 JdkBackedSetBuilderImpl(SetBuilderImpl<E> toCopy) { 945 super(toCopy); // initializes dedupedElements and distinct 946 delegate = Sets.newHashSetWithExpectedSize(distinct); 947 for (int i = 0; i < distinct; i++) { 948 /* 949 * requireNonNull is safe because we ensure that the first `distinct` elements have been 950 * populated. 951 */ 952 delegate.add(requireNonNull(dedupedElements[i])); 953 } 954 } 955 956 @Override 957 SetBuilderImpl<E> add(E e) { 958 checkNotNull(e); 959 if (delegate.add(e)) { 960 addDedupedElement(e); 961 } 962 return this; 963 } 964 965 @Override 966 SetBuilderImpl<E> copy() { 967 return new JdkBackedSetBuilderImpl<>(this); 968 } 969 970 @Override 971 ImmutableSet<E> build() { 972 switch (distinct) { 973 case 0: 974 return of(); 975 case 1: 976 /* 977 * requireNonNull is safe because we ensure that the first `distinct` elements have been 978 * populated. 979 */ 980 return of(requireNonNull(dedupedElements[0])); 981 default: 982 return new JdkBackedImmutableSet<>( 983 delegate, ImmutableList.asImmutableList(dedupedElements, distinct)); 984 } 985 } 986 } 987 988 private static int estimatedSizeForUnknownDuplication(int inputElementsIncludingAnyDuplicates) { 989 if (inputElementsIncludingAnyDuplicates 990 < ImmutableCollection.Builder.DEFAULT_INITIAL_CAPACITY) { 991 return inputElementsIncludingAnyDuplicates; 992 } 993 // Guess the size is "halfway between" all duplicates and no duplicates, on a log scale. 994 return Math.max( 995 ImmutableCollection.Builder.DEFAULT_INITIAL_CAPACITY, 996 IntMath.sqrt(inputElementsIncludingAnyDuplicates, RoundingMode.CEILING)); 997 } 998 999 private static final long serialVersionUID = 0xcafebabe; 1000}