This class performs fast contains and remove (typically O(1), worst case O(n) but that is rare in practice). Add may be slightly slower, depending on hash collisions. Hashcodes are rehashed to reduce collisions and the need to resize. Load factors greater than 0.91 greatly increase the chances to resize to the next higher POT size.
Unordered sets and maps are not designed to provide especially fast iteration. Iteration is faster with OrderedSet and OrderedMap.
This implementation uses linear probing with the backward shift algorithm for removal. Hashcodes are rehashed using Fibonacci hashing, instead of the more common power-of-two mask, to better distribute poor hashCodes (see Malte Skarupke's blog post). Linear probing continues to work even when all hashCodes collide, just more slowly.
| Modifier and Type | Class and Description |
|---|---|
| public static class |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv boolean | |
| private transient IntSet. | |
| private transient IntSet. | |
| pack-priv int[] | |
| private final float | |
| protected int | mask
A bitmask used to confine hashcodes to the size of the table. |
| protected int | shift
Used by |
| public int | |
| private int |
| Access | Constructor and Description |
|---|---|
| public | |
| public | IntSet(int
The backing array size is initialCapacity / loadFactor, increased to the next power of two. initialCapacity)Creates a new set with a load factor of 0.8. |
| public | IntSet(int
The backing array size is initialCapacity / loadFactor, increased to the next power of two. initialCapacity, float loadFactor)Creates a new set with the specified initial capacity and load factor. |
| public |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | |
| public void | |
| public void | |
| public void | |
| public void | |
| public void | |
| private void | addResize(int key)
Skips checks for existing keys, doesn't increment size, doesn't need to handle key 0. |
| public void | clear(int maximumCapacity)
Clears the set and reduces the size of the backing arrays to be the specified capacity / loadFactor, if they are larger. |
| public void | |
| public boolean | |
| public void | ensureCapacity(int additionalCapacity)
Increases the size of the backing array to accommodate the specified number of additional items / loadFactor. |
| public boolean | equals(Object obj)
Overrides java. Indicates whether some other object is "equal to" this one. |
| public int | |
| public int | |
| public boolean | |
| public IntSet. | |
| private int | locateKey(int key)
Returns the index of the key if already present, else -(index + 1) for the next empty index. |
| public boolean | |
| protected int | |
| public boolean | |
| private void | |
| public void | shrink(int maximumCapacity)
Reduces the size of the backing arrays to be the specified capacity / loadFactor, or less. |
| public String | |
| public static IntSet |
| hasZeroValue | back to summary |
|---|---|
| pack-priv boolean hasZeroValue | |
| iterator1 | back to summary |
|---|---|
| private transient IntSet. | |
| iterator2 | back to summary |
|---|---|
| private transient IntSet. | |
| keyTable | back to summary |
|---|---|
| pack-priv int[] keyTable | |
| loadFactor | back to summary |
|---|---|
| private final float loadFactor | |
| mask | back to summary |
|---|---|
| protected int mask A bitmask used to confine hashcodes to the size of the table. Must be all 1 bits in its low positions, ie a power of two
minus 1. If | |
| shift | back to summary |
|---|---|
| protected int shift Used by
| |
| size | back to summary |
|---|---|
| public int size | |
| threshold | back to summary |
|---|---|
| private int threshold | |
| IntSet | back to summary |
|---|---|
| public IntSet() Creates a new set with an initial capacity of 51 and a load factor of 0.8. | |
| IntSet | back to summary |
|---|---|
| public IntSet(int initialCapacity) Creates a new set with a load factor of 0.8.
| |
| IntSet | back to summary |
|---|---|
| public IntSet(int initialCapacity, float loadFactor) Creates a new set with the specified initial capacity and load factor. This set will hold initialCapacity items before growing the backing table.
| |
| IntSet | back to summary |
|---|---|
| public IntSet(IntSet set) Creates a new set identical to the specified set. | |
| add | back to summary |
|---|---|
| public boolean add(int key) Returns true if the key was added to the set or false if it was already in the set. | |
| addAll | back to summary |
|---|---|
| public void addAll(IntArray array) | |
| addAll | back to summary |
|---|---|
| public void addAll(IntArray array, int offset, int length) | |
| addAll | back to summary |
|---|---|
| public void addAll(int... array) | |
| addAll | back to summary |
|---|---|
| public void addAll(int[] array, int offset, int length) | |
| addAll | back to summary |
|---|---|
| public void addAll(IntSet set) | |
| addResize | back to summary |
|---|---|
| private void addResize(int key) Skips checks for existing keys, doesn't increment size, doesn't need to handle key 0. | |
| clear | back to summary |
|---|---|
| public void clear(int maximumCapacity) Clears the set and reduces the size of the backing arrays to be the specified capacity / loadFactor, if they are larger. | |
| clear | back to summary |
|---|---|
| public void clear() | |
| contains | back to summary |
|---|---|
| public boolean contains(int key) | |
| ensureCapacity | back to summary |
|---|---|
| public void ensureCapacity(int additionalCapacity) Increases the size of the backing array to accommodate the specified number of additional items / loadFactor. Useful before adding many items to avoid multiple backing array resizes. | |
| equals | back to summary |
|---|---|
| public boolean equals(Object obj) Overrides java. Doc from java. Indicates whether some other object is "equal to" this one.
The
An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes. | |
| first | back to summary |
|---|---|
| public int first() | |
| hashCode | back to summary |
|---|---|
| public int hashCode() Overrides java. Doc from java. Returns a hash code value for this object. This method is
supported for the benefit of hash tables such as those provided by
The general contract of
| |
| isEmpty | back to summary |
|---|---|
| public boolean isEmpty() Returns true if the set is empty. | |
| iterator | back to summary |
|---|---|
| public IntSet. Returns an iterator for the keys in the set. Remove is supported.
If | |
| locateKey | back to summary |
|---|---|
| private int locateKey(int key) Returns the index of the key if already present, else -(index + 1) for the next empty index. | |
| notEmpty | back to summary |
|---|---|
| public boolean notEmpty() Returns true if the set has one or more items. | |
| place | back to summary |
|---|---|
| protected int place(int item) Returns an index >= 0 and <=
The default implementation uses Fibonacci hashing on the item's This method can be overridden to customizing hashing. This may be useful eg in the unlikely event that most hashcodes are Fibonacci numbers, if keys provide poor or incorrect hashcodes, or to simplify hashing if keys provide high quality hashcodes and don't need Fibonacci hashing. | |
| remove | back to summary |
|---|---|
| public boolean remove(int key) Returns true if the key was removed. | |
| resize | back to summary |
|---|---|
| private void resize(int newSize) | |
| shrink | back to summary |
|---|---|
| public void shrink(int maximumCapacity) Reduces the size of the backing arrays to be the specified capacity / loadFactor, or less. If the capacity is already less, nothing is done. If the set contains more items than the specified capacity, the next highest power of two capacity is used instead. | |
| toString | back to summary |
|---|---|
| public String toString() Overrides java. Doc from java. Returns a string representation of the object.
Satisfying this method's contract implies a non- | |
| with | back to summary |
|---|---|
| public static IntSet with(int... array) | |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv int | |
| public boolean | |
| private static final int | |
| private static final int | |
| pack-priv int | |
| pack-priv final IntSet | |
| pack-priv boolean |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv void | |
| public int | |
| public void | |
| public void | |
| public IntArray |
| currentIndex | back to summary |
|---|---|
| pack-priv int currentIndex | |
| hasNext | back to summary |
|---|---|
| public boolean hasNext | |
| INDEX_ILLEGAL | back to summary |
|---|---|
| private static final int INDEX_ILLEGAL | |
| INDEX_ZERO | back to summary |
|---|---|
| private static final int INDEX_ZERO | |
| nextIndex | back to summary |
|---|---|
| pack-priv int nextIndex | |
| set | back to summary |
|---|---|
| pack-priv final IntSet set | |
| valid | back to summary |
|---|---|
| pack-priv boolean valid | |
| IntSetIterator | back to summary |
|---|---|
| public IntSetIterator(IntSet set) | |
| findNextIndex | back to summary |
|---|---|
| pack-priv void findNextIndex() | |
| next | back to summary |
|---|---|
| public int next() | |
| remove | back to summary |
|---|---|
| public void remove() | |
| reset | back to summary |
|---|---|
| public void reset() | |
| toArray | back to summary |
|---|---|
| public IntArray toArray() Returns a new array containing the remaining keys. | |