Class LongSet
- All Implemented Interfaces:
PrimitiveCollection<Long>,PrimitiveCollection.OfLong,PrimitiveSet<Long>,PrimitiveSet.SetOfLong
- Direct Known Subclasses:
LongOrderedSet
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(long)to mix hashCode() results.protected booleanprotected LongSet.LongSetIteratorprotected LongSet.LongSetIteratorprotected long[]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 hashcodes to the size of the table.protected intprotected intprotected intPrecalculated value of(int)(keyTable.length * loadFactor), used to determine when to resize. -
Constructor Summary
ConstructorsConstructorDescriptionLongSet()Creates a new set with an initial capacity ofUtilities.getDefaultTableCapacity()and a load factor ofUtilities.getDefaultLoadFactor().LongSet(int initialCapacity) Creates a new set with a load factor ofUtilities.getDefaultLoadFactor().LongSet(int initialCapacity, float loadFactor) Creates a new set with the specified initial capacity and load factor.LongSet(long[] array) Creates a new set containing all of the items in the given array.LongSet(long[] array, int offset, int length) Creates a new set usinglengthitems from the givenarray, starting at offset (inclusive).Creates a new set identical to the specified set.Creates a new set using all distinct items in the given PrimitiveCollection, such as aLongListorLongObjectMap.Keys.LongSet(LongIterator coll) Creates a new instance containing the items in the specified iterator. -
Method Summary
Modifier and TypeMethodDescriptionbooleanadd(long key) Returns true if the key was not already in the set.booleanaddAll(long... array) booleanaddAll(long[] array, int offset, int length) booleanbooleanbooleanprotected voidaddResize(long 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(long key) voidensureCapacity(int additionalCapacity) Increases the size of the backing array to accommodate the specified number of additional items / loadFactor.booleanlongfirst()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(long)to mix hash codes.floatintGets the length of the internal array used to store all items, as well as empty space awaiting more items to be entered.inthashCode()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 LongSetCallsparse(String, String, boolean)with brackets set to false.static LongSetCreates a new collection and fills it by callingPrimitiveCollection.OfLong.addLegible(String, String, int, int)on either all ofstr(ifbracketsis false) orstrwithout its first and last chars (ifbracketsis true).static LongSetCreates a new collection and fills it by callingPrimitiveCollection.OfLong.addLegible(String, String, int, int)with the given four parameters as-is.protected intplace(long item) Returns an index >= 0 and <=maskfor the specifieditem.booleanremove(long 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 LongSetwith()Constructs an empty set.static LongSetwith(long item) Creates a new LongSet that holds only the given item, but can be resized.static LongSetwith(long... varargs) Creates a new LongSet that holds only the given items, but can be resized.static LongSetwith(long item0, long item1) Creates a new LongSet that holds only the given items, but can be resized.static LongSetwith(long item0, long item1, long item2) Creates a new LongSet that holds only the given items, but can be resized.static LongSetwith(long item0, long item1, long item2, long item3) Creates a new LongSet that holds only the given items, but can be resized.static LongSetwith(long item0, long item1, long item2, long item3, long item4) Creates a new LongSet that holds only the given items, but can be resized.static LongSetwith(long item0, long item1, long item2, long item3, long item4, long item5) Creates a new LongSet that holds only the given items, but can be resized.static LongSetwith(long item0, long item1, long item2, long item3, long item4, long item5, long item6) Creates a new LongSet that holds only the given items, but can be resized.static LongSetwith(long item0, long item1, long item2, long item3, long item4, long item5, long item6, long item7) Creates a new LongSet 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.OfLong
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.SetOfLong
equalContents
-
Field Details
-
size
protected int size -
keyTable
protected long[] 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(long)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(long)is overridden. -
mask
protected int maskA 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. Ifplace(long)is overridden, this can be used instead ofshiftto isolate usable bits of a hash. -
hashMultiplier
protected int hashMultiplierUsed byplace(long)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 usingLongOrderedSet. -
iterator1
-
iterator2
-
-
Constructor Details
-
LongSet
public LongSet()Creates a new set with an initial capacity ofUtilities.getDefaultTableCapacity()and a load factor ofUtilities.getDefaultLoadFactor(). -
LongSet
public LongSet(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.
-
LongSet
public LongSet(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
-
LongSet
Creates a new instance containing the items in the specified iterator.- Parameters:
coll- an iterator that will have its remaining contents added to this
-
LongSet
Creates a new set identical to the specified set. -
LongSet
Creates a new set using all distinct items in the given PrimitiveCollection, such as aLongListorLongObjectMap.Keys.- Parameters:
coll- a PrimitiveCollection that will be used in full, except for duplicate items
-
LongSet
public LongSet(long[] 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
-
LongSet
public LongSet(long[] array) Creates a new set containing all of 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(long item) Returns an index >= 0 and <=maskfor the specifieditem.- Parameters:
item- any long; it is usually mixed or masked here- Returns:
- an index between 0 and
mask(both inclusive)
-
add
public boolean add(long key) Returns true if the key was not already in the set.- Specified by:
addin interfacePrimitiveCollection.OfLong
-
addAll
-
addAll
-
addAll
public boolean addAll(long... array) - Specified by:
addAllin interfacePrimitiveCollection.OfLong
-
addAll
public boolean addAll(long[] array, int offset, int length) - Specified by:
addAllin interfacePrimitiveCollection.OfLong
-
addAll
-
addResize
protected void addResize(long key) Skips checks for existing keys, doesn't increment size, doesn't need to handle key 0. -
remove
public boolean remove(long key) Returns true if the key was removed.- Specified by:
removein interfacePrimitiveCollection.OfLong
-
notEmpty
public boolean notEmpty()Returns true if the set has one or more items.- Specified by:
notEmptyin interfacePrimitiveCollection<Long>
-
isEmpty
public boolean isEmpty()Returns true if the set is empty.- Specified by:
isEmptyin interfacePrimitiveCollection<Long>
-
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<Long>
-
contains
public boolean contains(long key) - Specified by:
containsin interfacePrimitiveCollection.OfLong
-
first
public long first()Description copied from interface:PrimitiveCollection.OfLongAttempts 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.OfLong.iterator(), tries to get the first item, or throws an IllegalStateException if this is empty.- Specified by:
firstin interfacePrimitiveCollection.OfLong- Returns:
- the first item in this PrimitiveCollection, as produced by
PrimitiveCollection.OfLong.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. -
size
public int size()- Specified by:
sizein interfacePrimitiveCollection<Long>
-
resize
protected void resize(int newSize) -
getHashMultiplier
public int getHashMultiplier()Gets the current hashMultiplier, used inplace(long)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.
-
getTableSize
public int getTableSize()Gets the length of the internal array used to store all items, as well as empty space awaiting more items to be entered. This is also called the capacity.- Returns:
- the length of the internal array that holds all items
-
getLoadFactor
public float getLoadFactor() -
setLoadFactor
public void setLoadFactor(float loadFactor) -
hashCode
public int hashCode()- Specified by:
hashCodein interfacePrimitiveCollection<Long>- Specified by:
hashCodein interfacePrimitiveSet<Long>- Overrides:
hashCodein classObject
-
equals
- Specified by:
equalsin interfacePrimitiveCollection<Long>- Specified by:
equalsin interfacePrimitiveSet<Long>- 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
LongSet.LongSetIteratorconstructor for nested or multithreaded iteration.- Specified by:
iteratorin interfacePrimitiveCollection<Long>- Specified by:
iteratorin interfacePrimitiveCollection.OfLong
-
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 LongSet that holds only the given item, but can be resized.- Parameters:
item- a long item- Returns:
- a new LongSet that holds the given item
-
with
Creates a new LongSet that holds only the given items, but can be resized.- Parameters:
item0- a long itemitem1- a long item- Returns:
- a new LongSet that holds the given items
-
with
Creates a new LongSet that holds only the given items, but can be resized.- Parameters:
item0- a long itemitem1- a long itemitem2- a long item- Returns:
- a new LongSet that holds the given items
-
with
Creates a new LongSet that holds only the given items, but can be resized.- Parameters:
item0- a long itemitem1- a long itemitem2- a long itemitem3- a long item- Returns:
- a new LongSet that holds the given items
-
with
Creates a new LongSet that holds only the given items, but can be resized.- Parameters:
item0- a long itemitem1- a long itemitem2- a long itemitem3- a long itemitem4- a long item- Returns:
- a new LongSet that holds the given items
-
with
Creates a new LongSet that holds only the given items, but can be resized.- Parameters:
item0- a long itemitem1- a long itemitem2- a long itemitem3- a long itemitem4- a long itemitem5- a long item- Returns:
- a new LongSet that holds the given items
-
with
public static LongSet with(long item0, long item1, long item2, long item3, long item4, long item5, long item6) Creates a new LongSet that holds only the given items, but can be resized.- Parameters:
item0- a long itemitem1- a long itemitem2- a long itemitem3- a long itemitem4- a long itemitem5- a long itemitem6- a long item- Returns:
- a new LongSet that holds the given items
-
with
public static LongSet with(long item0, long item1, long item2, long item3, long item4, long item5, long item6, long item7) Creates a new LongSet that holds only the given items, but can be resized.- Parameters:
item0- a long itemitem1- a long itemitem2- a long itemitem3- a long itemitem4- a long itemitem5- a long itemitem6- a long item- Returns:
- a new LongSet that holds the given items
-
with
Creates a new LongSet 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- a long varargs or long array; remember that varargs allocate- Returns:
- a new LongSet 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.OfLong.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.OfLong.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
-