Class IntSet
- All Implemented Interfaces:
PrimitiveCollection<Integer>,PrimitiveCollection.OfInt,PrimitiveSet<Integer>,PrimitiveSet.SetOfInt
- Direct Known Subclasses:
IntOrderedSet
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 Ordered types like
ObjectOrderedSet and ObjectObjectOrderedMap.
This implementation uses linear probing with the backward shift algorithm for removal. Linear probing continues to work even when all hashCodes collide; it just works more slowly in that case.
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from interface com.github.tommyettinger.ds.PrimitiveCollection
PrimitiveCollection.OfBoolean, PrimitiveCollection.OfByte, PrimitiveCollection.OfChar, PrimitiveCollection.OfDouble, PrimitiveCollection.OfFloat, PrimitiveCollection.OfInt, PrimitiveCollection.OfLong, PrimitiveCollection.OfShortNested classes/interfaces inherited from interface com.github.tommyettinger.ds.PrimitiveSet
PrimitiveSet.SetOfChar, PrimitiveSet.SetOfInt, PrimitiveSet.SetOfLong -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected intUsed byplace(int)to mix hashCode() results.protected booleanprotected IntSet.IntSetIteratorprotected IntSet.IntSetIteratorprotected int[]protected floatBetween 0f (exclusive) and 1f (inclusive, if you're careful), this determines how full the backing table can get before this increases their size.protected intA bitmask used to confine hash codes to the size of the table.protected intprotected intprotected intPrecalculated value of(int)(keyTable.length * loadFactor), used to determine when to resize. -
Constructor Summary
ConstructorsConstructorDescriptionIntSet()Creates a new set with an initial capacity ofUtilities.getDefaultTableCapacity()and a load factor ofUtilities.getDefaultLoadFactor().IntSet(int initialCapacity) Creates a new set with a load factor ofUtilities.getDefaultLoadFactor().IntSet(int[] array) Creates a new set containing all the items in the given array.IntSet(int[] array, int offset, int length) Creates a new set usinglengthitems from the givenarray, starting at offset (inclusive).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.Creates a new set using all distinct items in the given PrimitiveCollection, such as aIntListorIntObjectMap.Keys.IntSet(IntIterator coll) Creates a new instance containing the items in the specified iterator. -
Method Summary
Modifier and TypeMethodDescriptionbooleanadd(int key) Returns true if the key was not already in the set.booleanaddAll(int... array) booleanaddAll(int[] array, int offset, int length) booleanbooleanbooleanprotected voidaddResize(int key) Skips checks for existing keys, doesn't increment size, doesn't need to handle key 0.appendTo(StringBuilder builder) voidclear()voidclear(int maximumCapacity) Clears the set and reduces the size of the backing arrays to be the specified capacity / loadFactor, if they are larger.booleancontains(int key) voidensureCapacity(int additionalCapacity) Increases the size of the backing array to accommodate the specified number of additional items / loadFactor.booleanintfirst()Attempts to get the first item in this PrimitiveCollection, where "first" is only defined meaningfully if this type is ordered.intGets the current hashMultiplier, used inplace(int)to mix hash codes.floatinthashCode()booleanisEmpty()Returns true if the set is empty.iterator()Returns an iterator for the keys in the set.booleannotEmpty()Returns true if the set has one or more items.static IntSetCallsparse(String, String, boolean)with brackets set to false.static IntSetCreates a new collection and fills it by callingPrimitiveCollection.OfInt.addLegible(String, String, int, int)on either all ofstr(ifbracketsis false) orstrwithout its first and last chars (ifbracketsis true).static IntSetCreates a new collection and fills it by callingPrimitiveCollection.OfInt.addLegible(String, String, int, int)with the given four parameters as-is.protected intplace(int item) Returns an index >= 0 and <=maskfor the specifieditem.booleanremove(int key) Returns true if the key was removed.protected voidresize(int newSize) voidsetHashMultiplier(int hashMultiplier) Sets the hashMultiplier to the given int, which will be made odd if even and always negative (by OR-ing with 0x80000001).voidsetLoadFactor(float loadFactor) voidshrink(int maximumCapacity) Reduces the size of the backing arrays to be the specified capacity / loadFactor, or less.intsize()toString()voidtruncate(int newSize) Reduces the size of the set to the specified size.static IntSetwith()Constructs an empty set.static IntSetwith(int item) Creates a new IntSet that holds only the given item, but can be resized.static IntSetwith(int... varargs) Creates a new IntSet that holds only the given items, but can be resized.static IntSetwith(int item0, int item1) Creates a new IntSet that holds only the given items, but can be resized.static IntSetwith(int item0, int item1, int item2) Creates a new IntSet that holds only the given items, but can be resized.static IntSetwith(int item0, int item1, int item2, int item3) Creates a new IntSet that holds only the given items, but can be resized.static IntSetwith(int item0, int item1, int item2, int item3, int item4) Creates a new IntSet that holds only the given items, but can be resized.static IntSetwith(int item0, int item1, int item2, int item3, int item4, int item5) Creates a new IntSet that holds only the given items, but can be resized.static IntSetwith(int item0, int item1, int item2, int item3, int item4, int item5, int item6) Creates a new IntSet that holds only the given items, but can be resized.static IntSetwith(int item0, int item1, int item2, int item3, int item4, int item5, int item6, int item7) Creates a new IntSet that holds only the given items, but can be resized.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface com.github.tommyettinger.ds.PrimitiveCollection.OfInt
addAll, addAll, addDense, addDense, addLegible, addLegible, addVarargs, appendTo, appendTo, containsAll, containsAll, containsAll, containsAll, containsAny, containsAny, containsAny, containsAny, denseAppendTo, forEach, removeAll, removeAll, removeAll, removeAll, removeEach, removeEach, removeEach, removeEach, removeIf, retainAll, retainAll, retainAll, toArray, toArray, toDenseString, toDenseString, toString, toString, toStringMethods inherited from interface com.github.tommyettinger.ds.PrimitiveSet.SetOfInt
equalContents
-
Field Details
-
size
protected int size -
keyTable
protected int[] keyTable -
hasZeroValue
protected boolean hasZeroValue -
loadFactor
protected float loadFactorBetween 0f (exclusive) and 1f (inclusive, if you're careful), this determines how full the backing table can get before this increases their size. Larger values use less memory but make the data structure slower. -
threshold
protected int thresholdPrecalculated value of(int)(keyTable.length * loadFactor), used to determine when to resize. -
shift
protected int shiftUsed byplace(int)to bit shift the upper bits of anintinto a usable range (>= 0 and <=mask). The shift can be negative, which is convenient to match the number of bits in mask: if mask is a 7-bit number, a shift of -7 shifts the upper 7 bits into the lowest 7 positions. This class sets the shift > 32 and < 64, which when used with an int will still move the upper bits of an int to the lower bits due to Java's implicit modulus on shifts.maskcan also be used to mask the low bits of a number, which may be faster for some hashcodes, ifplace(int)is overridden. -
mask
protected int maskA bitmask used to confine hash codes to the size of the table. Must be all 1-bits in its low positions, ie a power of two minus 1. Ifplace(int)is overridden, this can be used instead ofshiftto isolate usable bits of a hash. -
hashMultiplier
protected int hashMultiplierUsed byplace(int)to mix hashCode() results. Changes on every call toresize(int)by default. This should always change whenshiftchanges, meaning, when the backing table resizes. This only needs to be serialized if the full key and value tables are serialized, or if the iteration order should be the same before and after serialization. Iteration order is better handled by usingIntOrderedSet. -
iterator1
-
iterator2
-
-
Constructor Details
-
IntSet
public IntSet()Creates a new set with an initial capacity ofUtilities.getDefaultTableCapacity()and a load factor ofUtilities.getDefaultLoadFactor(). -
IntSet
public IntSet(int initialCapacity) Creates a new set with a load factor ofUtilities.getDefaultLoadFactor().- 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.loadFactor- what fraction of the capacity can be filled before this has to resize; 0 < loadFactor <= 1
-
IntSet
Creates a new instance containing the items in the specified iterator.- Parameters:
coll- an iterator that will have its remaining contents added to this
-
IntSet
Creates a new set identical to the specified set. -
IntSet
Creates a new set using all distinct items in the given PrimitiveCollection, such as aIntListorIntObjectMap.Keys.- Parameters:
coll- a PrimitiveCollection that will be used in full, except for duplicate items
-
IntSet
public IntSet(int[] array, int offset, int length) Creates a new set usinglengthitems from the givenarray, starting at offset (inclusive).- Parameters:
array- an array to draw items fromoffset- the first index in array to draw an item fromlength- how many items to take from array; bounds-checking is the responsibility of the using code
-
IntSet
public IntSet(int[] array) Creates a new set containing all the items in the given array.- Parameters:
array- an array that will be used in full, except for duplicate items
-
-
Method Details
-
place
protected int place(int item) Returns an index >= 0 and <=maskfor the specifieditem.- Parameters:
item- any int; it is usually mixed and shifted or masked here- Returns:
- an index between 0 and
mask(both inclusive)
-
add
public boolean add(int key) Returns true if the key was not already in the set.- Specified by:
addin interfacePrimitiveCollection.OfInt
-
addAll
-
addAll
-
addAll
public boolean addAll(int... array) - Specified by:
addAllin interfacePrimitiveCollection.OfInt
-
addAll
public boolean addAll(int[] array, int offset, int length) - Specified by:
addAllin interfacePrimitiveCollection.OfInt
-
addAll
-
addResize
protected void addResize(int key) Skips checks for existing keys, doesn't increment size, doesn't need to handle key 0. -
remove
public boolean remove(int key) Returns true if the key was removed.- Specified by:
removein interfacePrimitiveCollection.OfInt
-
notEmpty
public boolean notEmpty()Returns true if the set has one or more items.- Specified by:
notEmptyin interfacePrimitiveCollection<Integer>
-
isEmpty
public boolean isEmpty()Returns true if the set is empty.- Specified by:
isEmptyin interfacePrimitiveCollection<Integer>
-
shrink
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. -
clear
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
public void clear()- Specified by:
clearin interfacePrimitiveCollection<Integer>
-
contains
public boolean contains(int key) - Specified by:
containsin interfacePrimitiveCollection.OfInt
-
first
public int first()Description copied from interface:PrimitiveCollection.OfIntAttempts to get the first item in this PrimitiveCollection, where "first" is only defined meaningfully if this type is ordered. Many times, this applies to a class that is not ordered, and in those cases it can get an arbitrary item, and that item is permitted to be different for different calls to first().
This is useful for cases where you would normally be able to call something likeList.get(int)to get an item, any item, from a collection, but whatever class you're using doesn't necessarily provide a get(), first(), peek(), or similar method.
The default implementation usesPrimitiveCollection.OfInt.iterator(), tries to get the first item, or throws an IllegalStateException if this is empty.- Specified by:
firstin interfacePrimitiveCollection.OfInt- Returns:
- the first item in this PrimitiveCollection, as produced by
PrimitiveCollection.OfInt.iterator()
-
ensureCapacity
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. -
resize
protected void resize(int newSize) -
getHashMultiplier
public int getHashMultiplier()Gets the current hashMultiplier, used inplace(int)to mix hash codes. IfsetHashMultiplier(int)is never called, the hashMultiplier will always be drawn fromUtilities.GOOD_MULTIPLIERS, with the index equal to64 - shift.- Returns:
- the current hashMultiplier
-
setHashMultiplier
public void setHashMultiplier(int hashMultiplier) Sets the hashMultiplier to the given int, which will be made odd if even and always negative (by OR-ing with 0x80000001). This can be any negative, odd int, but should almost always be drawn fromUtilities.GOOD_MULTIPLIERSor something like it.- Parameters:
hashMultiplier- any int; will be made odd if even.
-
getLoadFactor
public float getLoadFactor() -
setLoadFactor
public void setLoadFactor(float loadFactor) -
hashCode
public int hashCode()- Specified by:
hashCodein interfacePrimitiveCollection<Integer>- Specified by:
hashCodein interfacePrimitiveSet<Integer>- Overrides:
hashCodein classObject
-
equals
- Specified by:
equalsin interfacePrimitiveCollection<Integer>- Specified by:
equalsin interfacePrimitiveSet<Integer>- Overrides:
equalsin classObject
-
appendTo
-
toString
-
truncate
public void truncate(int newSize) Reduces the size of the set to the specified size. If the set is already smaller than the specified size, no action is taken. This indiscriminately removes items from the backing array until the requested newSize is reached, or until the full backing array has had its elements removed.
This tries to remove from the end of the iteration order, but because the iteration order is not guaranteed by an unordered set, this can remove essentially any item(s) from the set if it is larger than newSize.- Parameters:
newSize- the target size to try to reach by removing items, if smaller than the current size
-
iterator
Returns an iterator for the keys in the set. Remove is supported.Use the
IntSet.IntSetIteratorconstructor for nested or multithreaded iteration.- Specified by:
iteratorin interfacePrimitiveCollection<Integer>- Specified by:
iteratorin interfacePrimitiveCollection.OfInt
-
size
public int size()- Specified by:
sizein interfacePrimitiveCollection<Integer>
-
with
Constructs an empty set. This is usually less useful than just using the constructor, but can be handy in some code-generation scenarios when you don't know how many arguments you will have.- Returns:
- a new set containing nothing
-
with
Creates a new IntSet that holds only the given item, but can be resized.- Parameters:
item- an int item- Returns:
- a new IntSet that holds the given item
-
with
Creates a new IntSet that holds only the given items, but can be resized.- Parameters:
item0- an int itemitem1- an int item- Returns:
- a new IntSet that holds the given items
-
with
Creates a new IntSet that holds only the given items, but can be resized.- Parameters:
item0- an int itemitem1- an int itemitem2- an int item- Returns:
- a new IntSet that holds the given items
-
with
Creates a new IntSet that holds only the given items, but can be resized.- Parameters:
item0- an int itemitem1- an int itemitem2- an int itemitem3- an int item- Returns:
- a new IntSet that holds the given items
-
with
Creates a new IntSet that holds only the given items, but can be resized.- Parameters:
item0- an int itemitem1- an int itemitem2- an int itemitem3- an int itemitem4- an int item- Returns:
- a new IntSet that holds the given items
-
with
Creates a new IntSet that holds only the given items, but can be resized.- Parameters:
item0- an int itemitem1- an int itemitem2- an int itemitem3- an int itemitem4- an int itemitem5- an int item- Returns:
- a new IntSet that holds the given items
-
with
public static IntSet with(int item0, int item1, int item2, int item3, int item4, int item5, int item6) Creates a new IntSet that holds only the given items, but can be resized.- Parameters:
item0- an int itemitem1- an int itemitem2- an int itemitem3- an int itemitem4- an int itemitem5- an int itemitem6- an int item- Returns:
- a new IntSet that holds the given items
-
with
public static IntSet with(int item0, int item1, int item2, int item3, int item4, int item5, int item6, int item7) Creates a new IntSet that holds only the given items, but can be resized.- Parameters:
item0- an int itemitem1- an int itemitem2- an int itemitem3- an int itemitem4- an int itemitem5- an int itemitem6- an int item- Returns:
- a new IntSet that holds the given items
-
with
Creates a new IntSet that holds only the given items, but can be resized. This overload will only be used when an array is supplied and the type of the items requested is the component type of the array, or if varargs are used and there are 9 or more arguments.- Parameters:
varargs- an int varargs or int array; remember that varargs allocate- Returns:
- a new IntSet that holds the given items
-
parse
Callsparse(String, String, boolean)with brackets set to false.- Parameters:
str- a String that will be parsed in fulldelimiter- the delimiter between items in str- Returns:
- a new collection parsed from str
-
parse
Creates a new collection and fills it by callingPrimitiveCollection.OfInt.addLegible(String, String, int, int)on either all ofstr(ifbracketsis false) orstrwithout its first and last chars (ifbracketsis true). Each item is expected to be separated bydelimiter.- Parameters:
str- a String that will be parsed in full (depending on brackets)delimiter- the delimiter between items in strbrackets- if true, the first and last chars in str will be ignored- Returns:
- a new collection parsed from str
-
parse
Creates a new collection and fills it by callingPrimitiveCollection.OfInt.addLegible(String, String, int, int)with the given four parameters as-is.- Parameters:
str- a String that will have the given section parseddelimiter- the delimiter between items in stroffset- the first position to parse in str, inclusivelength- how many chars to parse, starting from offset- Returns:
- a new collection parsed from str
-