Class IdentityObjectIntMap<K>
- java.lang.Object
-
- com.esotericsoftware.kryo.util.ObjectIntMap<K>
-
- com.esotericsoftware.kryo.util.IdentityObjectIntMap<K>
-
- All Implemented Interfaces:
java.lang.Iterable<ObjectIntMap.Entry<K>>
public class IdentityObjectIntMap<K> extends ObjectIntMap<K>
An unordered map where identity comparison is used for the objects keys and the values are unboxed ints. 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.
This implementation uses linear probing with the backward shift algorithm for removal. Hashcodes are rehashed using Fibonacci hashing, instead of the more common power-of-two mask, to better distribute poor hashCodes (see Malte Skarupke's blog post). Linear probing continues to work even when all hashCodes collide, just more slowly.
- Author:
- Nathan Sweet, Tommy Ettinger
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.esotericsoftware.kryo.util.ObjectIntMap
ObjectIntMap.Entries<K>, ObjectIntMap.Entry<K>, ObjectIntMap.Keys<K>, ObjectIntMap.Values
-
-
Field Summary
-
Fields inherited from class com.esotericsoftware.kryo.util.ObjectIntMap
mask, shift, size
-
-
Constructor Summary
Constructors Constructor Description IdentityObjectIntMap()
Creates a new map with an initial capacity of 51 and a load factor of 0.8.IdentityObjectIntMap(int initialCapacity)
Creates a new map with a load factor of 0.8.IdentityObjectIntMap(int initialCapacity, float loadFactor)
Creates a new map with the specified initial capacity and load factor.IdentityObjectIntMap(IdentityObjectIntMap<K> map)
Creates a new map identical to the specified map.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
hashCode()
protected int
place(K item)
Returns an index >= 0 and <=ObjectIntMap.mask
for the specifieditem
.-
Methods inherited from class com.esotericsoftware.kryo.util.ObjectIntMap
clear, clear, containsKey, containsValue, ensureCapacity, entries, equals, findKey, get, getAndIncrement, isEmpty, iterator, keys, notEmpty, put, putAll, remove, shrink, toString, toString, values
-
-
-
-
Constructor Detail
-
IdentityObjectIntMap
public IdentityObjectIntMap()
Creates a new map with an initial capacity of 51 and a load factor of 0.8.
-
IdentityObjectIntMap
public IdentityObjectIntMap(int initialCapacity)
Creates a new map with a load factor of 0.8.- Parameters:
initialCapacity
- If not a power of two, it is increased to the next nearest power of two.
-
IdentityObjectIntMap
public IdentityObjectIntMap(int initialCapacity, float loadFactor)
Creates a new map with the specified initial capacity and load factor. This map 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.
-
IdentityObjectIntMap
public IdentityObjectIntMap(IdentityObjectIntMap<K> map)
Creates a new map identical to the specified map.
-
-
Method Detail
-
place
protected int place(K item)
Description copied from class:ObjectIntMap
Returns an index >= 0 and <=ObjectIntMap.mask
for the specifieditem
.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 overriden 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;
- Overrides:
place
in classObjectIntMap<K>
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classObjectIntMap<K>
-
-