Class IntMap<V>
- java.lang.Object
-
- com.esotericsoftware.kryo.util.IntMap<V>
-
- All Implemented Interfaces:
java.lang.Iterable<IntMap.Entry<V>>
public class IntMap<V> extends java.lang.Object implements java.lang.Iterable<IntMap.Entry<V>>
An unordered map where the keys are unboxed ints and values are objects. 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 Modifier and Type Class Description static class
IntMap.Entries<V>
static class
IntMap.Entry<V>
static class
IntMap.Keys
static class
IntMap.Values<V>
-
Constructor Summary
Constructors Constructor Description IntMap()
Creates a new map with an initial capacity of 51 and a load factor of 0.8.IntMap(int initialCapacity)
Creates a new map with a load factor of 0.8.IntMap(int initialCapacity, float loadFactor)
Creates a new map with the specified initial capacity and load factor.IntMap(IntMap<? extends V> map)
Creates a new map identical to the specified map.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear()
void
clear(int maximumCapacity)
Clears the map and reduces the size of the backing arrays to be the specified capacity / loadFactor, if they are larger.boolean
containsKey(int key)
boolean
containsValue(java.lang.Object value, boolean identity)
Returns true if the specified value is in the map.void
ensureCapacity(int additionalCapacity)
Increases the size of the backing array to accommodate the specified number of additional items / loadFactor.IntMap.Entries<V>
entries()
Returns an iterator for the entries in the map.boolean
equals(java.lang.Object obj)
boolean
equalsIdentity(java.lang.Object obj)
Uses == for comparison of each value.int
findKey(java.lang.Object value, boolean identity, int notFound)
Returns the key for the specified value, or notFound if it is not in the map.V
get(int key)
V
get(int key, V defaultValue)
int
hashCode()
boolean
isEmpty()
Returns true if the map is empty.java.util.Iterator<IntMap.Entry<V>>
iterator()
IntMap.Keys
keys()
Returns an iterator for the keys in the map.boolean
notEmpty()
Returns true if the map has one or more items.protected int
place(int item)
Returns an index >= 0 and <=mask
for the specifieditem
.V
put(int key, V value)
void
putAll(IntMap<? extends V> map)
V
remove(int key)
void
shrink(int maximumCapacity)
Reduces the size of the backing arrays to be the specified capacity / loadFactor, or less.java.lang.String
toString()
IntMap.Values<V>
values()
Returns an iterator for the values in the map.
-
-
-
Field Detail
-
size
public int size
-
shift
protected int shift
Used byplace(int)
to bit shift the upper bits of along
into 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 if 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.mask
can also be used to mask the low bits of a number, which may be faster for some hashcodes, ifplace(int)
is overridden.
-
mask
protected int mask
A 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(int)
is overriden, this can be used instead ofshift
to isolate usable bits of a hash.
-
-
Constructor Detail
-
IntMap
public IntMap()
Creates a new map with an initial capacity of 51 and a load factor of 0.8.
-
IntMap
public IntMap(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.
-
IntMap
public IntMap(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.
-
-
Method Detail
-
place
protected int place(int item)
Returns an index >= 0 and <=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;
-
get
public V get(int key)
-
notEmpty
public boolean notEmpty()
Returns true if the map has one or more items.
-
isEmpty
public boolean isEmpty()
Returns true if the map is empty.
-
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 map 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 map and reduces the size of the backing arrays to be the specified capacity / loadFactor, if they are larger.
-
clear
public void clear()
-
containsValue
public boolean containsValue(@Null java.lang.Object value, boolean identity)
Returns true if the specified value is in the map. Note this traverses the entire map and compares every value, which may be an expensive operation.- Parameters:
identity
- If true, uses == to compare the specified value with values in the map. If false, usesequals(Object)
.
-
containsKey
public boolean containsKey(int key)
-
findKey
public int findKey(@Null java.lang.Object value, boolean identity, int notFound)
Returns the key for the specified value, or notFound if it is not in the map. Note this traverses the entire map and compares every value, which may be an expensive operation.- Parameters:
identity
- If true, uses == to compare the specified value with values in the map. If false, usesequals(Object)
.
-
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.
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equals
in classjava.lang.Object
-
equalsIdentity
public boolean equalsIdentity(@Null java.lang.Object obj)
Uses == for comparison of each value.
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
iterator
public java.util.Iterator<IntMap.Entry<V>> iterator()
- Specified by:
iterator
in interfacejava.lang.Iterable<V>
-
entries
public IntMap.Entries<V> entries()
Returns an iterator for the entries in the map. Remove is supported.
-
values
public IntMap.Values<V> values()
Returns an iterator for the values in the map. Remove is supported.
-
keys
public IntMap.Keys keys()
Returns an iterator for the keys in the map. Remove is supported.
-
-