Package com.github.tommyettinger.ds
Class IdentityOrderedSet<T>
java.lang.Object
com.github.tommyettinger.ds.ObjectSet<T>
com.github.tommyettinger.ds.ObjectOrderedSet<T>
com.github.tommyettinger.ds.IdentityOrderedSet<T>
- All Implemented Interfaces:
Arrangeable,EnhancedCollection<T>,Ordered<T>,Iterable<T>,Collection<T>,Set<T>
A variant on
This can potentially be useful for tracking references to ensure they are all unique by identity, such as to avoid referential cycles. You might prefer this to
ObjectOrderedSet that compares items by identity (using ==) instead of equality (using equals()).
It also hashes with System.identityHashCode(Object) instead of calling the hashCode() of an item. This can be useful in
some cases where items may have invalid Object.equals(Object) and/or Object.hashCode() implementations, or if items
could be very large (making a hashCode() that uses all the parts of the item slow). Oddly, System.identityHashCode(Object) tends
to be slower than the hashCode() for most small items, because an explicitly-written hashCode() typically doesn't need to do anything
concurrently, but identityHashCode() needs to (concurrently) modify an internal JVM variable that ensures the results are unique, and
that requires the JVM to do lots of extra work whenever identityHashCode() is called. Despite that, identityHashCode() doesn't depend
on the quantity of variables in the item, so identityHashCode() gets relatively faster for larger items. The equals() method used by
ObjectOrderedSet also tends to slow down for large items, relative to the constant-time == this uses.
This can potentially be useful for tracking references to ensure they are all unique by identity, such as to avoid referential cycles. You might prefer this to
IdentitySet if you need to ensure fast iteration or want access by index.-
Nested Class Summary
Nested classes/interfaces inherited from class com.github.tommyettinger.ds.ObjectOrderedSet
ObjectOrderedSet.ObjectOrderedSetIterator<K>Nested classes/interfaces inherited from class com.github.tommyettinger.ds.ObjectSet
ObjectSet.ObjectSetIterator<T>Nested classes/interfaces inherited from interface com.github.tommyettinger.ds.Arrangeable
Arrangeable.ArrangeableList<T>Nested classes/interfaces inherited from interface com.github.tommyettinger.ds.Ordered
Ordered.OfBoolean, Ordered.OfByte, Ordered.OfChar, Ordered.OfDouble, Ordered.OfFloat, Ordered.OfInt, Ordered.OfLong, Ordered.OfShort -
Field Summary
Fields inherited from class com.github.tommyettinger.ds.ObjectOrderedSet
itemsFields inherited from class com.github.tommyettinger.ds.ObjectSet
hashMultiplier, iterator1, iterator2, keyTable, loadFactor, mask, shift, size, threshold -
Constructor Summary
ConstructorsConstructorDescriptionIdentityOrderedSet(int initialCapacity) IdentityOrderedSet(int initialCapacity, float loadFactor) IdentityOrderedSet(int initialCapacity, float loadFactor, OrderType type) IdentityOrderedSet(int initialCapacity, OrderType type) IdentityOrderedSet(ObjectOrderedSet<? extends T> set) IdentityOrderedSet(ObjectOrderedSet<? extends T> set, OrderType type) IdentityOrderedSet(Ordered<T> other, int offset, int count) IdentityOrderedSet(Ordered<T> other, int offset, int count, OrderType type) IdentityOrderedSet(OrderType type) IdentityOrderedSet(Collection<? extends T> coll) IdentityOrderedSet(Collection<? extends T> coll, OrderType type) IdentityOrderedSet(Iterator<? extends T> coll) Creates a new instance containing the items in the specified iterator.IdentityOrderedSet(Iterator<? extends T> coll, OrderType type) Creates a new instance containing the items in the specified iterator.IdentityOrderedSet(T[] array) IdentityOrderedSet(T[] array, int offset, int length) IdentityOrderedSet(T[] array, int offset, int length, OrderType type) IdentityOrderedSet(T[] array, OrderType type) -
Method Summary
Modifier and TypeMethodDescriptionprotected booleanCompares the objects left and right, which are usually keys, for equality, returning true if they are considered equal.intEffectively does nothing here because the hashMultiplier is not used by identity hashing.inthashCode()static <T> IdentityOrderedSet<T>parse(String str, String delimiter, PartialParser<T> parser) Callsparse(String, String, PartialParser, boolean)with brackets set to false.static <T> IdentityOrderedSet<T>parse(String str, String delimiter, PartialParser<T> parser, boolean brackets) Creates a new collection and fills it by callingEnhancedCollection.addLegible(String, String, PartialParser, int, int)on either all ofstr(ifbracketsis false) orstrwithout its first and last chars (ifbracketsis true).static <T> IdentityOrderedSet<T>parse(String str, String delimiter, PartialParser<T> parser, int offset, int length) Creates a new collection and fills it by callingEnhancedCollection.addLegible(String, String, PartialParser, int, int)with the given five parameters as-is.protected intReturns an index >= 0 and <=ObjectSet.maskfor the specifieditem, mixed.voidsetHashMultiplier(int hashMultiplier) Effectively does nothing here because the hashMultiplier is not used by identity hashing.static <T> IdentityOrderedSet<T>with()Constructs an empty set given the type as a generic type argument.static <T> IdentityOrderedSet<T>with(T item) Creates a new IdentityOrderedSet that holds only the given item, but can be resized.static <T> IdentityOrderedSet<T>with(T... varargs) Creates a new IdentityOrderedSet that holds only the given items, but can be resized.static <T> IdentityOrderedSet<T>with(T item0, T item1) Creates a new IdentityOrderedSet that holds only the given items, but can be resized.static <T> IdentityOrderedSet<T>with(T item0, T item1, T item2) Creates a new IdentityOrderedSet that holds only the given items, but can be resized.static <T> IdentityOrderedSet<T>with(T item0, T item1, T item2, T item3) Creates a new IdentityOrderedSet that holds only the given items, but can be resized.static <T> IdentityOrderedSet<T>with(T item0, T item1, T item2, T item3, T item4) Creates a new IdentityOrderedSet that holds only the given items, but can be resized.static <T> IdentityOrderedSet<T>with(T item0, T item1, T item2, T item3, T item4, T item5) Creates a new IdentityOrderedSet that holds only the given items, but can be resized.static <T> IdentityOrderedSet<T>with(T item0, T item1, T item2, T item3, T item4, T item5, T item6) Creates a new IdentityOrderedSet that holds only the given items, but can be resized.static <T> IdentityOrderedSet<T>with(T item0, T item1, T item2, T item3, T item4, T item5, T item6, T item7) Creates a new IdentityOrderedSet that holds only the given items, but can be resized.Methods inherited from class com.github.tommyettinger.ds.ObjectOrderedSet
add, add, addAll, addAll, alter, alterAt, clear, clear, ensureCapacity, first, getAt, iterator, order, remove, removeAt, removeRange, sort, sort, toString, toString, truncateMethods inherited from class com.github.tommyettinger.ds.ObjectSet
addAll, addAll, addAll, addAll, addResize, appendTo, contains, containsAll, containsAll, containsAll, containsAny, containsAny, containsAnyIterable, equals, get, getLoadFactor, getTableSize, isEmpty, locateKey, notEmpty, removeAll, removeAll, removeAll, resize, retainAll, setLoadFactor, shrink, size, toArray, toArrayMethods 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 java.util.Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface com.github.tommyettinger.ds.EnhancedCollection
add, add, add, addAll, addAllIterable, addLegible, addLegible, addVarargs, appendTo, appendTo, containsAll, containsAllIterable, containsAny, removeAll, removeAllIterable, removeEach, removeEach, removeEach, removeEachIterable, toString, toStringMethods inherited from interface com.github.tommyettinger.ds.Ordered
getOrderType, random, random, reverse, selectRanked, selectRankedIndex, shuffle, swapMethods inherited from interface java.util.Set
spliterator
-
Constructor Details
-
IdentityOrderedSet
-
IdentityOrderedSet
-
IdentityOrderedSet
-
IdentityOrderedSet
Creates a new instance containing the items in the specified iterator.- Parameters:
coll- an iterator that will have its remaining contents added to this
-
IdentityOrderedSet
-
IdentityOrderedSet
-
IdentityOrderedSet
-
IdentityOrderedSet
-
IdentityOrderedSet
-
IdentityOrderedSet
public IdentityOrderedSet() -
IdentityOrderedSet
public IdentityOrderedSet(int initialCapacity) -
IdentityOrderedSet
public IdentityOrderedSet(int initialCapacity, float loadFactor) -
IdentityOrderedSet
Creates a new instance containing the items in the specified iterator.- Parameters:
coll- an iterator that will have its remaining contents added to this
-
IdentityOrderedSet
-
IdentityOrderedSet
-
IdentityOrderedSet
-
IdentityOrderedSet
-
IdentityOrderedSet
-
-
Method Details
-
place
Description copied from class:ObjectSetReturns an index >= 0 and <=ObjectSet.maskfor the specifieditem, mixed.- Overrides:
placein classObjectSet<T>- Parameters:
item- a non-null Object; its hashCode() method should be used by most implementations- Returns:
- an index between 0 and
ObjectSet.mask(both inclusive)
-
equate
Description copied from class:ObjectSetCompares the objects left and right, which are usually keys, for equality, returning true if they are considered equal. This is used by the rest of this class to determine whether two keys are considered equal. Normally, this returnsleft.equals(right), but subclasses can override it to use reference equality, fuzzy equality, deep array equality, or any other custom definition of equality. Usually,ObjectSet.place(Object)is also overridden if this method is.- Overrides:
equatein classObjectSet<T>- Parameters:
left- must be non-null; typically a key being compared, but not necessarilyright- may be null; typically a key being compared, but can often be null for an empty key slot, or some other type- Returns:
- true if left and right are considered equal for the purposes of this class
-
hashCode
public int hashCode()- Specified by:
hashCodein interfaceCollection<T>- Specified by:
hashCodein interfaceSet<T>- Overrides:
hashCodein classObjectOrderedSet<T>
-
getHashMultiplier
public int getHashMultiplier()Effectively does nothing here because the hashMultiplier is not used by identity hashing.- Overrides:
getHashMultiplierin classObjectSet<T>- Returns:
- any int; the value isn't used internally, but may be used by subclasses to identify something
-
setHashMultiplier
public void setHashMultiplier(int hashMultiplier) Effectively does nothing here because the hashMultiplier is not used by identity hashing. Subclasses can use this to set some kind of identifier or user data, though. Unlike the superclass implementation, this does not alter the given int to make it negative or odd.- Overrides:
setHashMultiplierin classObjectSet<T>- Parameters:
hashMultiplier- any int; will not be used
-
with
Constructs an empty set given the type as a generic type argument. 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.- Type Parameters:
T- the type of items; must be given explicitly- Returns:
- a new set containing nothing
-
with
Creates a new IdentityOrderedSet that holds only the given item, but can be resized.- Type Parameters:
T- the type of item, typically inferred- Parameters:
item- one T item- Returns:
- a new IdentityOrderedSet that holds the given item
-
with
Creates a new IdentityOrderedSet that holds only the given items, but can be resized.- Type Parameters:
T- the type of item, typically inferred- Parameters:
item0- a T itemitem1- a T item- Returns:
- a new IdentityOrderedSet that holds the given items
-
with
Creates a new IdentityOrderedSet that holds only the given items, but can be resized.- Type Parameters:
T- the type of item, typically inferred- Parameters:
item0- a T itemitem1- a T itemitem2- a T item- Returns:
- a new IdentityOrderedSet that holds the given items
-
with
Creates a new IdentityOrderedSet that holds only the given items, but can be resized.- Type Parameters:
T- the type of item, typically inferred- Parameters:
item0- a T itemitem1- a T itemitem2- a T itemitem3- a T item- Returns:
- a new IdentityOrderedSet that holds the given items
-
with
Creates a new IdentityOrderedSet that holds only the given items, but can be resized.- Type Parameters:
T- the type of item, typically inferred- Parameters:
item0- a T itemitem1- a T itemitem2- a T itemitem3- a T itemitem4- a T item- Returns:
- a new IdentityOrderedSet that holds the given items
-
with
Creates a new IdentityOrderedSet that holds only the given items, but can be resized.- Type Parameters:
T- the type of item, typically inferred- Parameters:
item0- a T itemitem1- a T itemitem2- a T itemitem3- a T itemitem4- a T itemitem5- a T item- Returns:
- a new IdentityOrderedSet that holds the given items
-
with
public static <T> IdentityOrderedSet<T> with(T item0, T item1, T item2, T item3, T item4, T item5, T item6) Creates a new IdentityOrderedSet that holds only the given items, but can be resized.- Type Parameters:
T- the type of item, typically inferred- Parameters:
item0- a T itemitem1- a T itemitem2- a T itemitem3- a T itemitem4- a T itemitem5- a T itemitem6- a T item- Returns:
- a new IdentityOrderedSet that holds the given items
-
with
public static <T> IdentityOrderedSet<T> with(T item0, T item1, T item2, T item3, T item4, T item5, T item6, T item7) Creates a new IdentityOrderedSet that holds only the given items, but can be resized.- Type Parameters:
T- the type of item, typically inferred- Parameters:
item0- a T itemitem1- a T itemitem2- a T itemitem3- a T itemitem4- a T itemitem5- a T itemitem6- a T item- Returns:
- a new IdentityOrderedSet that holds the given items
-
with
Creates a new IdentityOrderedSet 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.- Type Parameters:
T- the type of item, typically inferred- Parameters:
varargs- a T varargs or T array; remember that varargs allocate- Returns:
- a new IdentityOrderedSet that holds the given items
-
parse
public static <T> IdentityOrderedSet<T> parse(String str, String delimiter, PartialParser<T> parser) Callsparse(String, String, PartialParser, boolean)with brackets set to false.- Parameters:
str- a String that will be parsed in fulldelimiter- the delimiter between items in strparser- a PartialParser that returns aTitem from a section ofstr- Returns:
- a new collection parsed from str
-
parse
public static <T> IdentityOrderedSet<T> parse(String str, String delimiter, PartialParser<T> parser, boolean brackets) Creates a new collection and fills it by callingEnhancedCollection.addLegible(String, String, PartialParser, 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 strparser- a PartialParser that returns aTitem from a section ofstrbrackets- if true, the first and last chars in str will be ignored- Returns:
- a new collection parsed from str
-
parse
public static <T> IdentityOrderedSet<T> parse(String str, String delimiter, PartialParser<T> parser, int offset, int length) Creates a new collection and fills it by callingEnhancedCollection.addLegible(String, String, PartialParser, int, int)with the given five parameters as-is.- Parameters:
str- a String that will have the given section parseddelimiter- the delimiter between items in strparser- a PartialParser that returns aTitem from a section ofstroffset- the first position to parse in str, inclusivelength- how many chars to parse, starting from offset- Returns:
- a new collection parsed from str
-