contains(x), add(x) and remove(x), are all (expected and amortized)
constant time operations. Expected in the hashtable sense (depends on the hash function doing a
good job of distributing the elements to the buckets to a distribution not far from uniform), and
amortized since some operations can trigger a hash table resize.
Unlike java.util.HashSet, iteration is only proportional to the actual size(),
which is optimal, and not the size of the internal hashtable, which could be much larger
than size(). Furthermore, this structure only depends on a fixed number of arrays; add(x) operations do not create objects for the garbage collector to deal with, and for
every element added, the garbage collector will have to traverse 1.5 references on
average, in the marking phase, not 5.0 as in java.util.HashSet.
If there are no removals, then iteration order is the same as insertion
order. Any removal invalidates any ordering guarantees.
This class should not be assumed to be universally superior to java.util.HashSet.
Generally speaking, this class reduces object allocation and memory consumption at the price of
moderately increased constant factors of CPU. Only use this class when there is a specific reason
to prioritize memory over CPU.
| Modifier and Type | Field and Description |
|---|---|
| pack-priv transient @Nullable Object[] | elements
The elements contained in the set, in the range of [0, size()). |
| private transient int[] | entries
Contains the logical entries, in the range of [0, size()). |
| pack-priv static final double | HASH_FLOODING_FPP
Maximum allowed false positive probability of detecting a hash flooding attack given random input. |
| private static final int | MAX_HASH_BUCKET_LENGTH
Maximum allowed length of a hash table bucket before falling back to a j.u.LinkedHashSet based implementation. |
| private transient int | metadata
Keeps track of metadata like the number of hash table bits and modifications of this data structure (to make it possible to throw ConcurrentModificationException in the iterator). |
| private transient int | size
The number of elements contained in the set. |
| private transient Object | table
The hashtable object. |
| Access | Constructor and Description |
|---|---|
| pack-priv | |
| pack-priv | CompactHashSet(int
the initial capacity of this expectedSize)CompactHashSet.Constructs a new instance of |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | add(E
element to be added to this set object)Overrides java. Implements java. |
| pack-priv int | adjustAfterRemove(int indexBeforeRemove, int indexRemoved)
Updates the index an iterator is pointing to after a call to remove: returns the index of the entry that should be looked at after a removal on indexRemoved, with indexBeforeRemove as the index that *was* the next entry that would be looked at. |
| pack-priv int | |
| public void | clear()
Overrides java. Implements java. |
| public boolean | contains(Object
element whose presence in this set is to be tested object)Overrides java. Implements java. true if this set contains the specified element.
|
| pack-priv Set | |
| public static <E extends @Nullable Object> CompactHashSet | |
| public static <E extends @Nullable Object> CompactHashSet | Returns: a newCompactHashSet containing those elements (minus duplicates)the elements that the set should contain collection)Creates a mutable |
| public static <E extends @Nullable Object> CompactHashSet | Returns: a newCompactHashSet containing those elements (minus duplicates)the elements that the set should contain elements)Creates a mutable |
| private Set | |
| public static <E extends @Nullable Object> CompactHashSet | Returns: a new, emptyCompactHashSet with enough capacity to hold expectedSize
elements without resizingthe number of elements you expect to add to the returned set expectedSize)Creates a |
| pack-priv Set | |
| private E | |
| private int | |
| pack-priv int | |
| public void | forEach(Consumer<? super E>
The action to be performed for each element action)Overrides default java. Iterable
until all elements have been processed or the action throws an
exception.
|
| pack-priv int | |
| private int | |
| pack-priv void | |
| pack-priv void | |
| pack-priv void | insertEntry(int entryIndex, E object, int hash, int mask)
Creates a fresh entry with the specified object at the specified position in the entry arrays. |
| public boolean | isEmpty()
Overrides java. Implements java. true if this set contains no elements.
|
| pack-priv boolean | |
| public Iterator | iterator()
Implements abstract java. Implements java. |
| pack-priv void | moveLastEntry(int dstIndex, int mask)
Moves the last entry in the entry array into |
| pack-priv boolean | |
| private void | |
| public boolean | remove(Object
object to be removed from this set, if present object)Overrides java. Implements java. |
| private @Nullable Object[] | |
| private int[] | |
| private Object | |
| pack-priv void | resizeEntries(int newCapacity)
Resizes the internal entries array to the specified capacity, which may be greater or less than the current capacity. |
| private void | |
| private int | |
| private void | |
| private void | |
| private void | setHashTableMask(int mask)
Stores the hash table mask as the number of bits needed to represent an index. |
| public int | size()
Implements abstract java. Implements java. |
| public Spliterator | spliterator()
Overrides default java. Spliterator over the elements in this set.
|
| public @Nullable Object[] | toArray()
Overrides java. Implements java. |
| public <T extends @Nullable Object> T[] | toArray(T[]
the array into which the elements of this set are to be
stored, if it is big enough; otherwise, a new array of the same
runtime type is allocated for this purpose. a)Overrides java. Implements java. |
| public void | trimToSize()
Ensures that this |
| private void |
| elements | back to summary |
|---|---|
| pack-priv transient @Nullable Object[] elements The elements contained in the set, in the range of [0, size()). The elements in [size(),
elements.length) are all | |
| entries | back to summary |
|---|---|
| private transient int[] entries Contains the logical entries, in the range of [0, size()). The high bits of each int are the part of the smeared hash of the element not covered by the hashtable mask, whereas the low bits are the "next" pointer (pointing to the next entry in the bucket chain), which will always be less than or equal to the hashtable mask. hash = aaaaaaaa mask = 00000fff next = 00000bbb entry = aaaaabbb The pointers in [size(), entries.length) are all "null" (UNSET). | |
| HASH_FLOODING_FPP | back to summary |
|---|---|
| pack-priv static final double HASH_FLOODING_FPP Maximum allowed false positive probability of detecting a hash flooding attack given random input. | |
| MAX_HASH_BUCKET_LENGTH | back to summary |
|---|---|
| private static final int MAX_HASH_BUCKET_LENGTH Maximum allowed length of a hash table bucket before falling back to a j.u.LinkedHashSet based implementation. Experimentally determined. | |
| metadata | back to summary |
|---|---|
| private transient int metadata Keeps track of metadata like the number of hash table bits and modifications of this data structure (to make it possible to throw ConcurrentModificationException in the iterator). Note that we choose not to make this volatile, so we do less of a "best effort" to track such errors, for better performance. | |
| size | back to summary |
|---|---|
| private transient int size The number of elements contained in the set. | |
| table | back to summary |
|---|---|
| private transient Object table The hashtable object. This can be either:
| |
| CompactHashSet | back to summary |
|---|---|
| pack-priv CompactHashSet() Constructs a new empty instance of | |
| CompactHashSet | back to summary |
|---|---|
| pack-priv CompactHashSet(int expectedSize) Constructs a new instance of
| |
| add | back to summary |
|---|---|
| public boolean add(E object) Overrides java. Implements java. Doc from java. Adds the specified element to this set if it is not already present
(optional operation). More formally, adds the specified element
The stipulation above does not imply that sets must accept all
elements; sets may refuse to add any particular element, including
| |
| adjustAfterRemove | back to summary |
|---|---|
| pack-priv int adjustAfterRemove(int indexBeforeRemove, int indexRemoved) Updates the index an iterator is pointing to after a call to remove: returns the index of the entry that should be looked at after a removal on indexRemoved, with indexBeforeRemove as the index that *was* the next entry that would be looked at. | |
| allocArrays | back to summary |
|---|---|
| pack-priv int allocArrays() Handle lazy allocation of arrays.
| |
| clear | back to summary |
|---|---|
| public void clear() Overrides java. Implements java. Doc from java. Removes all of the elements from this set (optional operation). The set will be empty after this call returns.
| |
| contains | back to summary |
|---|---|
| public boolean contains(Object object) Overrides java. Implements java. Doc from java. Returns | |
| convertToHashFloodingResistantImplementation | back to summary |
|---|---|
pack-priv Set
| |
| create | back to summary |
|---|---|
| public static <E extends @Nullable Object> CompactHashSet Creates an empty | |
| create | back to summary |
|---|---|
| public static <E extends @Nullable Object> CompactHashSet Creates a mutable
| |
| create | back to summary |
|---|---|
| public static <E extends @Nullable Object> CompactHashSet Creates a mutable
| |
| createHashFloodingResistantDelegate | back to summary |
|---|---|
| private Set | |
| createWithExpectedSize | back to summary |
|---|---|
| public static <E extends @Nullable Object> CompactHashSet Creates a
| |
| delegateOrNull | back to summary |
|---|---|
pack-priv Set
| |
| element | back to summary |
|---|---|
private E element(int i)
| |
| entry | back to summary |
|---|---|
| private int entry(int i) | |
| firstEntryIndex | back to summary |
|---|---|
| pack-priv int firstEntryIndex() | |
| forEach | back to summary |
|---|---|
| public void forEach(Consumer<? super E> action) Overrides default java. Doc from java. Performs the given action for each element of the The behavior of this method is unspecified if the action performs side-effects that modify the underlying source of elements, unless an overriding class has specified a concurrent modification policy. | |
| getSuccessor | back to summary |
|---|---|
| pack-priv int getSuccessor(int entryIndex) | |
| hashTableMask | back to summary |
|---|---|
| private int hashTableMask() Gets the hash table mask using the stored number of hash table bits. | |
| incrementModCount | back to summary |
|---|---|
| pack-priv void incrementModCount() | |
| init | back to summary |
|---|---|
| pack-priv void init(int expectedSize) Pseudoconstructor for serialization support. | |
| insertEntry | back to summary |
|---|---|
| pack-priv void insertEntry(int entryIndex, E object, int hash, int mask) Creates a fresh entry with the specified object at the specified position in the entry arrays. | |
| isEmpty | back to summary |
|---|---|
| public boolean isEmpty() Overrides java. Implements java. Doc from java. Returns
| |
| isUsingHashFloodingResistance | back to summary |
|---|---|
pack-priv boolean isUsingHashFloodingResistance()
| |
| iterator | back to summary |
|---|---|
| public Iterator Implements abstract java. Implements java. Doc from java. Returns an iterator over the elements in this set. The elements are returned in no particular order (unless this set is an instance of some class that provides a guarantee). | |
| moveLastEntry | back to summary |
|---|---|
| pack-priv void moveLastEntry(int dstIndex, int mask) Moves the last entry in the entry array into | |
| needsAllocArrays | back to summary |
|---|---|
| pack-priv boolean needsAllocArrays() Returns whether arrays need to be allocated.
| |
| readObject | back to summary |
|---|---|
private void readObject(ObjectInputStream stream) throws IOException, ClassNotFoundException
| |
| remove | back to summary |
|---|---|
| public boolean remove(Object object) Overrides java. Implements java. Doc from java. Removes the specified element from this set if it is present
(optional operation). More formally, removes an element
| |
| requireElements | back to summary |
|---|---|
| private @Nullable Object[] requireElements() | |
| requireEntries | back to summary |
|---|---|
| private int[] requireEntries() | |
| requireTable | back to summary |
|---|---|
| private Object requireTable() | |
| resizeEntries | back to summary |
|---|---|
| pack-priv void resizeEntries(int newCapacity) Resizes the internal entries array to the specified capacity, which may be greater or less than the current capacity. | |
| resizeMeMaybe | back to summary |
|---|---|
| private void resizeMeMaybe(int newSize) Resizes the entries storage if necessary. | |
| resizeTable | back to summary |
|---|---|
private int resizeTable(int oldMask, int newCapacity, int targetHash, int targetEntryIndex)
| |
| setElement | back to summary |
|---|---|
| private void setElement(int i, E value) | |
| setEntry | back to summary |
|---|---|
| private void setEntry(int i, int value) | |
| setHashTableMask | back to summary |
|---|---|
| private void setHashTableMask(int mask) Stores the hash table mask as the number of bits needed to represent an index. | |
| size | back to summary |
|---|---|
| public int size() Implements abstract java. Implements java. Doc from java. Returns the number of elements in this set (its cardinality). If this
set contains more than
| |
| spliterator | back to summary |
|---|---|
| public Spliterator Overrides default java. Doc from java. Creates a The
| |
| toArray | back to summary |
|---|---|
| public @Nullable Object[] toArray() Overrides java. Implements java. Doc from java. Returns an array containing all of the elements in this set. If this set makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order. The returned array will be "safe" in that no references to it are maintained by this set. (In other words, this method must allocate a new array even if this set is backed by an array). The caller is thus free to modify the returned array. This method acts as bridge between array-based and collection-based APIs. | |
| toArray | back to summary |
|---|---|
| public <T extends @Nullable Object> T[] toArray(T[] a) Overrides java. Implements java. Doc from java. Returns an array containing all of the elements in this set; the runtime type of the returned array is that of the specified array. If the set fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this set. If this set fits in the specified array with room to spare
(i.e., the array has more elements than this set), the element in
the array immediately following the end of the set is set to
If this set makes any guarantees as to what order its elements are returned by its iterator, this method must return the elements in the same order. Like the Suppose
String[] y = x.toArray(new String[0]);
Note that toArray(new Object[0]) is identical in function to
toArray().
| |
| trimToSize | back to summary |
|---|---|
| public void trimToSize() Ensures that this | |
| writeObject | back to summary |
|---|---|
private void writeObject(ObjectOutputStream stream) throws IOException
| |