APIdia Launch Partner
Top Description Fields Constructors Methods
com.badlogic.gdx.utils

public Class IdentitySet<T>

extends ObjectSet<T>
Class Inheritance

An unordered set that uses identity comparison for its keys. Null keys are not allowed. No allocation is done except when growing the table size.

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 OrderedSet and OrderedMap.

This implementation uses linear probing with the backward shift algorithm for removal. Linear probing continues to work even when all hashCodes collide, just more slowly.

Authors
Tommy Ettinger, Nathan Sweet

Field Summary

Inherited from com.badlogic.gdx.utils.ObjectSet:
keyTableloadFactormaskshiftsizethreshold

Constructor Summary

AccessConstructor and Description
public
IdentitySet()

Creates a new set with an initial capacity of 51 and a load factor of 0.8.

public
IdentitySet(int
The backing array size is initialCapacity / loadFactor, increased to the next power of two.
initialCapacity
)

Creates a new set with a load factor of 0.8.

public
IdentitySet(int
The backing array size is initialCapacity / loadFactor, increased to the next power of two.
initialCapacity
,
float loadFactor)

Creates a new set with the specified initial capacity and load factor.

public
IdentitySet(IdentitySet<? extends T> set)

Creates a new set identical to the specified set.

Method Summary

Modifier and TypeMethod and Description
public int
hashCode()

Overrides com.badlogic.gdx.utils.ObjectSet.hashCode.

Returns a hash code value for this object.

pack-priv int
locateKey(T key)

Overrides com.badlogic.gdx.utils.ObjectSet.locateKey.

Returns the index of the key if already present, else -(index + 1) for the next empty index.

protected int
place(T item)

Overrides com.badlogic.gdx.utils.ObjectSet.place.

Returns an index >= 0 and <= mask for the specified item.

public static <T> IdentitySet<T>
Inherited from com.badlogic.gdx.utils.ObjectSet:
addaddAlladdAlladdAlladdAlladdAllclearclearcontainsensureCapacityequalsfirstgetisEmptyiteratornotEmptyremoveshrinktableSizetoStringtoString

Constructor Detail

IdentitySetback to summary
public IdentitySet()

Creates a new set with an initial capacity of 51 and a load factor of 0.8.

IdentitySetback to summary
public IdentitySet(int initialCapacity)

Creates a new set with a load factor of 0.8.

Parameters
initialCapacity:int

The backing array size is initialCapacity / loadFactor, increased to the next power of two.

IdentitySetback to summary
public IdentitySet(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:int

The backing array size is initialCapacity / loadFactor, increased to the next power of two.

IdentitySetback to summary
public IdentitySet(IdentitySet<? extends T> set)

Creates a new set identical to the specified set.

Method Detail

hashCodeback to summary
public int hashCode()

Overrides com.badlogic.gdx.utils.ObjectSet.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for this object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
locateKeyback to summary
pack-priv int locateKey(T key)

Overrides com.badlogic.gdx.utils.ObjectSet.locateKey.

Doc from com.badlogic.gdx.utils.ObjectSet.locateKey.

Returns the index of the key if already present, else -(index + 1) for the next empty index. This can be overridden in this pacakge to compare for equality differently than Object#equals(Object).

placeback to summary
protected int place(T item)

Overrides com.badlogic.gdx.utils.ObjectSet.place.

Doc from com.badlogic.gdx.utils.ObjectSet.place.

Returns an index >= 0 and <= mask for the specified item.

The default implementation uses Fibonacci hashing on the item's Object#hashCode(): the hashcode is multiplied by a long constant (2 to the 64th, divided by the golden ratio) then the uppermost bits are shifted into the lowest positions to obtain an index in the desired range. Multiplication by a long may be slower than int (eg on GWT) but greatly improves rehashing, allowing even very poor hashcodes, such as those that only differ in their upper bits, to be used without high collision rates. Fibonacci hashing has increased collision rates when all or most hashcodes are multiples of larger Fibonacci numbers (see Malte Skarupke's blog post).

This method can be overridden to customizing hashing. This may be useful eg in the unlikely event that most hashcodes are Fibonacci numbers, if keys provide poor or incorrect hashcodes, or to simplify hashing if keys provide high quality hashcodes and don't need Fibonacci hashing: return item.hashCode() & mask;

withback to summary
public static <T> IdentitySet<T> with(T... array)

Hides com.badlogic.gdx.utils.ObjectSet.with.