Package arc.struct
Class IntSet
java.lang.Object
arc.struct.IntSet
An unordered set that uses int keys. This implementation uses cuckoo hashing using 3 hashes, random walking, and a small stash
for problematic keys. No allocation is done except when growing the table size.
This set performs very fast contains and remove (typically O(1), worst case O(log(n))). Add may be a bit slower, depending on hash collisions. Load factors greater than 0.91 greatly increase the chances the set will have to rehash to the next higher POT size.
This set performs very fast contains and remove (typically O(1), worst case O(log(n))). Add may be a bit slower, depending on hash collisions. Load factors greater than 0.91 greatly increase the chances the set will have to rehash to the next higher POT size.
-
Nested Class Summary
-
Field Summary
-
Constructor Summary
ConstructorDescriptionIntSet()
Creates a new set with an initial capacity of 51 and a load factor of 0.8.IntSet
(int initialCapacity) Creates a new set with a load factor of 0.8.IntSet
(int initialCapacity, float loadFactor) Creates a new set with the specified initial capacity and load factor.Creates a new set identical to the specified set. -
Method Summary
Modifier and TypeMethodDescriptionboolean
add
(int key) Returns true if the key was not already in the set.void
addAll
(int... array) void
addAll
(int[] array, int offset, int length) void
void
void
void
clear()
void
clear
(int maximumCapacity) Clears the set and reduces the size of the backing arrays to be the specified capacity if they are larger.boolean
contains
(int key) void
void
ensureCapacity
(int additionalCapacity) Increases the size of the backing array to accommodate the specified number of additional items.boolean
int
first()
int
hashCode()
boolean
isEmpty()
Returns true if the set is empty.iterator()
Returns an iterator for the keys in the set.boolean
remove
(int key) Returns true if the key was removed.void
shrink
(int maximumCapacity) Reduces the size of the backing arrays to be the specified capacity or less.toString()
static IntSet
with
(int... array)
-
Field Details
-
size
public int size
-
-
Constructor Details
-
IntSet
public IntSet()Creates a new set with an initial capacity of 51 and a load factor of 0.8. -
IntSet
public IntSet(int initialCapacity) Creates a new set with a load factor of 0.8.- Parameters:
initialCapacity
- If not a power of two, it is increased to the next nearest power of two.
-
IntSet
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.- Parameters:
initialCapacity
- If not a power of two, it is increased to the next nearest power of two.
-
IntSet
Creates a new set identical to the specified set.
-
-
Method Details
-
with
-
each
-
add
public boolean add(int key) Returns true if the key was not already in the set. -
addAll
-
addAll
-
addAll
public void addAll(int... array) -
addAll
public void addAll(int[] array, int offset, int length) -
addAll
-
remove
public boolean remove(int key) Returns true if the key was removed. -
isEmpty
public boolean isEmpty()Returns true if the set is empty. -
shrink
public void shrink(int maximumCapacity) Reduces the size of the backing arrays to be the specified capacity 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. -
clear
public void clear(int maximumCapacity) Clears the set and reduces the size of the backing arrays to be the specified capacity if they are larger. -
clear
public void clear() -
contains
public boolean contains(int key) -
first
public int first() -
ensureCapacity
public void ensureCapacity(int additionalCapacity) Increases the size of the backing array to accommodate the specified number of additional items. Useful before adding many items to avoid multiple backing array resizes. -
hashCode
public int hashCode() -
equals
-
toString
-
iterator
Returns an iterator for the keys in the set. Remove is supported. Note that the same iterator instance is returned each time this method is called. Use theIntSet.IntSetIterator
constructor for nested or multithreaded iteration.
-