Class LongOrderedSet
- All Implemented Interfaces:
Arrangeable,Ordered.OfLong,PrimitiveCollection<Long>,PrimitiveCollection.OfLong,PrimitiveSet<Long>,PrimitiveSet.SetOfLong
LongSet that also stores keys in a LongList using the insertion order. No
allocation is done except when growing the table size.
Iteration is ordered and faster than an unordered set. Keys can also be accessed and the order changed
using order(). There is some additional overhead for put and remove.
This class performs fast contains (typically O(1), worst case O(n) but that is rare in practice). Remove is somewhat slower due
to order(). 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.
You can customize most behavior of this set by extending it. LongSet.place(long) can be overridden to change how hashCodes
are calculated (which can be useful for types like StringBuilder that don't implement hashCode()).
This implementation uses linear probing with the backward shift algorithm for removal. It tries different hashes from a simple family, with the hash changing on resize. Linear probing continues to work even when all hashCodes collide, just more slowly.
-
Nested Class Summary
Nested ClassesNested classes/interfaces inherited from class com.github.tommyettinger.ds.LongSet
LongSet.LongSetIteratorNested classes/interfaces inherited from interface com.github.tommyettinger.ds.Arrangeable
Arrangeable.ArrangeableList<T>Nested 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
FieldsFields inherited from class com.github.tommyettinger.ds.LongSet
hashMultiplier, hasZeroValue, iterator1, iterator2, keyTable, loadFactor, mask, shift, size, threshold -
Constructor Summary
ConstructorsConstructorDescriptionLongOrderedSet(int initialCapacity) LongOrderedSet(int initialCapacity, float loadFactor) LongOrderedSet(int initialCapacity, float loadFactor, OrderType ordering) LongOrderedSet(int initialCapacity, OrderType ordering) LongOrderedSet(long[] items) Creates a new set that contains all distinct elements initems.LongOrderedSet(long[] array, int offset, int length) Creates a new set usinglengthitems from the givenarray, starting at offset (inclusive).LongOrderedSet(long[] array, int offset, int length, OrderType ordering) Creates a new set usinglengthitems from the givenarray, starting at offset (inclusive).LongOrderedSet(long[] items, OrderType ordering) Creates a new set that contains all distinct elements initems.LongOrderedSet(LongOrderedSet set, OrderType ordering) LongOrderedSet(LongSet set) Creates a new set that contains all distinct elements inset.LongOrderedSet(LongSet set, OrderType ordering) Creates a new set that contains all distinct elements inset.LongOrderedSet(Ordered.OfLong other, int offset, int count) Creates a new set by copyingcountitems from the given Ordered, starting atoffsetin that Ordered, into this.LongOrderedSet(Ordered.OfLong other, int offset, int count, OrderType ordering) Creates a new set by copyingcountitems from the given Ordered, starting atoffsetin that Ordered, into this.LongOrderedSet(OrderType ordering) Creates an LongOrderedSet with the option to use an LongDeque or LongBag for keeping order.Creates a new set that contains all distinct elements incoll.LongOrderedSet(PrimitiveCollection.OfLong coll, OrderType ordering) Creates a new set that contains all distinct elements incoll.LongOrderedSet(LongIterator coll) Creates a new instance containing the items in the specified iterator. -
Method Summary
Modifier and TypeMethodDescriptionbooleanadd(int index, long key) Sets the key at the specified index.booleanadd(long key) Returns true if the key was not already in the set.booleanaddAll(int insertionIndex, Ordered.OfLong other, int offset, int count) Adds up tocountitems, starting fromoffset, in the Orderedotherto this set, inserting starting atinsertionIndexin the iteration order.booleanaddAll(Ordered.OfLong other, int offset, int count) Adds up tocountitems, starting fromoffset, in the Orderedotherto this set, inserting at the end of the iteration order.booleanalter(long before, long after) Changes the itembeforetoafterwithout changing its position in the order.booleanalterAt(int index, long after) Changes the item at the givenindexin the order toafter, without changing the ordering of other items.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.voidensureCapacity(int additionalCapacity) Increases the size of the backing array to accommodate the specified number of additional items / loadFactor.longfirst()Attempts to get the first item in this PrimitiveCollection, where "first" is only defined meaningfully if this type is ordered.longgetAt(int index) Gets the long item at the givenindexin the insertion order.inthashCode()iterator()Iterates through items in the same order asorder().order()Gets the ObjectList of items in the order this class will iterate through them.static LongOrderedSetCallsparse(String, String, boolean)with brackets set to false.static LongOrderedSetCreates 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 LongOrderedSetCreates a new collection and fills it by callingPrimitiveCollection.OfLong.addLegible(String, String, int, int)with the given four parameters as-is.booleanremove(long key) Returns true if the key was removed.longremoveAt(int index) Removes and returns the item at the given index in this set's order.voidremoveRange(int start, int end) Removes the items between the specified start index, inclusive, and end index, exclusive.voidsort()Sorts this ObjectOrderedSet in-place by the keys' natural ordering;Tmust implementComparable.Delegates toPrimitiveCollection.OfLong.toString(String, boolean)with the given separator and without brackets.voidtruncate(int newSize) Reduces the size of the set to the specified size.static LongOrderedSetwith()Constructs an empty set.static LongOrderedSetwith(long item) Creates a new LongOrderedSet that holds only the given item, but can be resized.static LongOrderedSetwith(long... varargs) Creates a new LongOrderedSet that holds only the given items, but can be resized.static LongOrderedSetwith(long item0, long item1) Creates a new LongOrderedSet that holds only the given items, but can be resized.static LongOrderedSetwith(long item0, long item1, long item2) Creates a new LongOrderedSet that holds only the given items, but can be resized.static LongOrderedSetwith(long item0, long item1, long item2, long item3) Creates a new LongOrderedSet that holds only the given items, but can be resized.static LongOrderedSetwith(long item0, long item1, long item2, long item3, long item4) Creates a new LongOrderedSet that holds only the given items, but can be resized.static LongOrderedSetwith(long item0, long item1, long item2, long item3, long item4, long item5) Creates a new LongOrderedSet that holds only the given items, but can be resized.static LongOrderedSetwith(long item0, long item1, long item2, long item3, long item4, long item5, long item6) Creates a new LongOrderedSet that holds only the given items, but can be resized.static LongOrderedSetwith(long item0, long item1, long item2, long item3, long item4, long item5, long item6, long item7) Creates a new LongOrderedSet that holds only the given items, but can be resized.Methods inherited from class com.github.tommyettinger.ds.LongSet
addAll, addAll, addAll, addAll, addAll, addResize, appendTo, contains, equals, getHashMultiplier, getLoadFactor, getTableSize, isEmpty, notEmpty, place, resize, setHashMultiplier, setLoadFactor, shrink, size, toStringMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface com.github.tommyettinger.ds.Arrangeable
rearrange, shuffle, sizeMethods inherited from interface com.github.tommyettinger.ds.Ordered.OfLong
getOrderType, random, random, reverse, selectRanked, selectRankedIndex, shuffle, sort, swapMethods 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, toStringMethods inherited from interface com.github.tommyettinger.ds.PrimitiveSet.SetOfLong
equalContents
-
Field Details
-
items
-
-
Constructor Details
-
LongOrderedSet
public LongOrderedSet() -
LongOrderedSet
Creates an LongOrderedSet with the option to use an LongDeque or LongBag for keeping order.- Parameters:
ordering- determines what implementationorder()will use
-
LongOrderedSet
public LongOrderedSet(int initialCapacity, float loadFactor) -
LongOrderedSet
-
LongOrderedSet
-
LongOrderedSet
public LongOrderedSet(int initialCapacity) -
LongOrderedSet
Creates a new instance containing the items in the specified iterator.- Parameters:
coll- an iterator that will have its remaining contents added to this
-
LongOrderedSet
-
LongOrderedSet
-
LongOrderedSet
Creates a new set that contains all distinct elements inset.- Parameters:
set- a LongSet without an order
-
LongOrderedSet
Creates a new set that contains all distinct elements inset.- Parameters:
set- a LongSet without an orderordering- determines what implementationorder()will use
-
LongOrderedSet
Creates a new set that contains all distinct elements incoll. -
LongOrderedSet
Creates a new set that contains all distinct elements incoll.- Parameters:
coll- anyPrimitiveCollection.OfLongordering- determines what implementationorder()will use
-
LongOrderedSet
Creates a new set by copyingcountitems from the given Ordered, starting atoffsetin that Ordered, into this.- Parameters:
other- another Ordered.OfLongoffset- the first index in other's ordering to draw an item fromcount- how many items to copy from other
-
LongOrderedSet
Creates a new set by copyingcountitems from the given Ordered, starting atoffsetin that Ordered, into this.- Parameters:
other- another Ordered.OfLongoffset- the first index in other's ordering to draw an item fromcount- how many items to copy from otherordering- determines what implementationorder()will use
-
LongOrderedSet
public LongOrderedSet(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
-
LongOrderedSet
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 codeordering- determines what implementationorder()will use
-
LongOrderedSet
public LongOrderedSet(long[] items) Creates a new set that contains all distinct elements initems.- Parameters:
items- an array that will be used in full, except for duplicate items
-
LongOrderedSet
Creates a new set that contains all distinct elements initems.- Parameters:
items- an array that will be used in full, except for duplicate itemsordering- determines what implementationorder()will use
-
-
Method Details
-
add
public boolean add(long key) Description copied from class:LongSetReturns true if the key was not already in the set.- Specified by:
addin interfacePrimitiveCollection.OfLong- Overrides:
addin classLongSet
-
add
public boolean add(int index, long key) Sets the key at the specified index. Returns true if the key was not already in the set. If this set already contains the key, the existing key's index is changed if needed and false is returned. Note, the order of the parameters matches the order inObjectListand the rest of the JDK, not OrderedSet in libGDX.- Parameters:
index- where in the iteration order to add the given key, or to move it if already presentkey- what long item to try to add, if not already present- Returns:
- true if the key was added for the first time, or false if the key was already present (even if moved)
-
addAll
Adds up tocountitems, starting fromoffset, in the Orderedotherto this set, inserting at the end of the iteration order.- Parameters:
other- a non-nullOrdered.OfLongofToffset- the first index inotherto usecount- how many indices inotherto use- Returns:
- true if this is modified by this call, as
LongSet.addAll(LongSet)does
-
addAll
Adds up tocountitems, starting fromoffset, in the Orderedotherto this set, inserting starting atinsertionIndexin the iteration order.- Parameters:
insertionIndex- where to insert into the iteration orderother- a non-nullOrdered.OfLongoffset- the first index inotherto usecount- how many indices inotherto use- Returns:
- true if this is modified by this call, as
LongSet.addAll(LongSet)does
-
remove
public boolean remove(long key) Description copied from class:LongSetReturns true if the key was removed.- Specified by:
removein interfacePrimitiveCollection.OfLong- Overrides:
removein classLongSet
-
removeAt
public long removeAt(int index) Removes and returns the item at the given index in this set's order.- Parameters:
index- the index of the item to remove- Returns:
- the removed item
-
removeRange
public void removeRange(int start, int end) Removes the items between the specified start index, inclusive, and end index, exclusive. Note that this takes different arguments than some other range-related methods; this needs a start index and an end index, rather than a count of items. This matches the behavior in the JDK collections.- Specified by:
removeRangein interfaceOrdered.OfLong- Parameters:
start- the first index to remove, inclusiveend- the last index (after what should be removed), exclusive
-
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- Overrides:
firstin classLongSet- 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.- Overrides:
ensureCapacityin classLongSet- Parameters:
additionalCapacity- how many additional items this should be able to hold without resizing (probably)
-
alter
public boolean alter(long before, long after) Changes the itembeforetoafterwithout changing its position in the order. Returns true ifafterhas been added to the ObjectOrderedSet andbeforehas been removed; returns false ifafteris already present orbeforeis not present. If you are iterating over an ObjectOrderedSet and have an index, you should preferalterAt(int, long), which doesn't need to search for an index like this does and so can be faster.- Parameters:
before- an item that must be present for this to succeedafter- an item that must not be in this set for this to succeed- Returns:
- true if
beforewas removed andafterwas added, false otherwise
-
alterAt
public boolean alterAt(int index, long after) Changes the item at the givenindexin the order toafter, without changing the ordering of other items. Ifafteris already present, this returns false; it will also return false ifindexis invalid for the size of this set. Otherwise, it returns true. Unlikealter(long, long), this operates in constant time.- Parameters:
index- the index in the order of the item to change; must be non-negative and less thanLongSet.sizeafter- the item that will replace the contents atindex; this item must not be present for this to succeed- Returns:
- true if
aftersuccessfully replaced the contents atindex, false otherwise
-
getAt
public long getAt(int index) Gets the long item at the givenindexin the insertion order. The index should be between 0 (inclusive) andLongSet.size()(exclusive).- Parameters:
index- an index in the insertion order, between 0 (inclusive) andLongSet.size()(exclusive)- Returns:
- the item at the given index
-
clear
public void clear(int maximumCapacity) Description copied from class:LongSetClears 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>- Overrides:
clearin classLongSet
-
order
Gets the ObjectList of items in the order this class will iterate through them. Returns a direct reference to the same ObjectList this uses, so changes to the returned list will also change the iteration order here.- Specified by:
orderin interfaceOrdered.OfLong- Returns:
- the ObjectList of items, in iteration order (usually insertion-order), that this uses
-
sort
public void sort()Sorts this ObjectOrderedSet in-place by the keys' natural ordering;Tmust implementComparable. -
iterator
Iterates through items in the same order asorder(). Reuses one of two iterators, and does not permit nested iteration; useLongOrderedSetIterator(LongOrderedSet)to nest iterators.- Specified by:
iteratorin interfacePrimitiveCollection<Long>- Specified by:
iteratorin interfacePrimitiveCollection.OfLong- Overrides:
iteratorin classLongSet- Returns:
- an
Iteratorover the T items in this, in order
-
hashCode
public int hashCode()- Specified by:
hashCodein interfacePrimitiveCollection<Long>- Specified by:
hashCodein interfacePrimitiveSet<Long>- Overrides:
hashCodein classLongSet
-
toString
Description copied from interface:PrimitiveCollection.OfLongDelegates toPrimitiveCollection.OfLong.toString(String, boolean)with the given separator and without brackets.- Specified by:
toStringin interfacePrimitiveCollection.OfLong- Parameters:
separator- how to separate items, such as", "- Returns:
- a new String representing this PrimitiveCollection
-
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. -
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 LongOrderedSet that holds only the given item, but can be resized.- Parameters:
item- a long item- Returns:
- a new LongOrderedSet that holds the given item
-
with
Creates a new LongOrderedSet that holds only the given items, but can be resized.- Parameters:
item0- a long itemitem1- a long item- Returns:
- a new LongOrderedSet that holds the given items
-
with
Creates a new LongOrderedSet that holds only the given items, but can be resized.- Parameters:
item0- a long itemitem1- a long itemitem2- a long item- Returns:
- a new LongOrderedSet that holds the given items
-
with
Creates a new LongOrderedSet 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 LongOrderedSet that holds the given items
-
with
Creates a new LongOrderedSet 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 LongOrderedSet that holds the given items
-
with
public static LongOrderedSet with(long item0, long item1, long item2, long item3, long item4, long item5) Creates a new LongOrderedSet 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 LongOrderedSet that holds the given items
-
with
public static LongOrderedSet with(long item0, long item1, long item2, long item3, long item4, long item5, long item6) Creates a new LongOrderedSet 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 LongOrderedSet that holds the given items
-
with
public static LongOrderedSet with(long item0, long item1, long item2, long item3, long item4, long item5, long item6, long item7) Creates a new LongOrderedSet 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 LongOrderedSet that holds the given items
-
with
Creates a new LongOrderedSet 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 LongOrderedSet 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
-