public class CaseInsensitiveIntMap extends java.lang.Object implements java.lang.Iterable<CaseInsensitiveIntMap.Entry>
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.
This implementation uses linear probing with the backward shift algorithm for removal. Hashcodes are rehashed using
a form of random hashing; a multiplier changes every time resize(int) gets called, which means if resize()
has to be called early due to frequent collisions, the hashes will change when the multiplier does, and that may help
alleviate the collisions. Linear probing continues to work even when all hashCodes collide, just more slowly.
This implementation is closely based on ObjectIntMap from libGDX, but also uses ideas
from jdkgdxds, such as the randomized hashing (and the case-insensitive matching in general).
| Modifier and Type | Class and Description |
|---|---|
static class |
CaseInsensitiveIntMap.Entries |
static class |
CaseInsensitiveIntMap.Entry |
static class |
CaseInsensitiveIntMap.Keys |
static class |
CaseInsensitiveIntMap.Values |
| Modifier and Type | Field and Description |
|---|---|
protected CaseInsensitiveIntMap.Entries |
entries1 |
protected CaseInsensitiveIntMap.Entries |
entries2 |
protected long |
hashMultiplier
Used by
place(String) to mix hashCode() results. |
protected CaseInsensitiveIntMap.Keys |
keys1 |
protected CaseInsensitiveIntMap.Keys |
keys2 |
protected java.lang.String[] |
keyTable |
protected float |
loadFactor |
protected int |
mask
A bitmask used to confine hashcodes to the size of the table.
|
protected int |
shift
|
int |
size |
protected int |
threshold |
protected CaseInsensitiveIntMap.Values |
values1 |
protected CaseInsensitiveIntMap.Values |
values2 |
protected int[] |
valueTable |
| Constructor and Description |
|---|
CaseInsensitiveIntMap()
Creates a new map with an initial capacity of 51 and a load factor of 0.8.
|
CaseInsensitiveIntMap(CaseInsensitiveIntMap map)
Creates a new map identical to the specified map.
|
CaseInsensitiveIntMap(int initialCapacity)
Creates a new map with a load factor of 0.8.
|
CaseInsensitiveIntMap(int initialCapacity,
float loadFactor)
Creates a new map with the specified initial capacity and load factor.
|
| Modifier and Type | Method and 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(java.lang.String key) |
boolean |
containsValue(int value)
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.
|
CaseInsensitiveIntMap.Entries |
entries()
Returns an iterator for the entries in the map.
|
boolean |
equals(java.lang.Object obj) |
java.lang.String |
findKey(int value)
Returns the key for the specified value, or null if it is not in the map.
|
int |
get(java.lang.String key,
int defaultValue)
Returns the value for the specified key, or the default value if the key is not in the map.
|
int |
getAndIncrement(java.lang.String key,
int defaultValue,
int increment)
Returns the key's current value and increments the stored value.
|
int |
hashCode() |
boolean |
isEmpty()
Returns true if the map is empty.
|
CaseInsensitiveIntMap.Entries |
iterator() |
CaseInsensitiveIntMap.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(java.lang.String item)
Returns an index >= 0 and <=
mask for the specified item. |
void |
put(java.lang.String key,
int value) |
int |
put(java.lang.String key,
int value,
int defaultValue)
Returns the old value associated with the specified key, or the specified default value.
|
void |
putAll(CaseInsensitiveIntMap map) |
int |
remove(java.lang.String key,
int defaultValue)
Returns the value for the removed key, or the default value if the key is not in the map.
|
void |
shrink(int maximumCapacity)
Reduces the size of the backing arrays to be the specified capacity / loadFactor, or less.
|
static int |
tableSize(int capacity,
float loadFactor)
Used to establish the size of a hash table.
|
java.lang.String |
toString() |
java.lang.String |
toString(java.lang.String separator) |
CaseInsensitiveIntMap.Values |
values()
Returns an iterator for the values in the map.
|
public int size
protected java.lang.String[] keyTable
protected int[] valueTable
protected float loadFactor
protected int threshold
protected int shift
place(String) to bit shift the upper bits of a long 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, if
place(String) is overridden.
protected int mask
place(String) is overriden, this can be used instead of shift to isolate usable bits of a
hash.protected long hashMultiplier
place(String) to mix hashCode() results. Changes on every call to resize(int) by default.
This only needs to be serialized if the full key and value tables are serialized, or if the iteration order should be
the same before and after serialization.protected transient CaseInsensitiveIntMap.Entries entries1
protected transient CaseInsensitiveIntMap.Entries entries2
protected transient CaseInsensitiveIntMap.Values values1
protected transient CaseInsensitiveIntMap.Values values2
protected transient CaseInsensitiveIntMap.Keys keys1
protected transient CaseInsensitiveIntMap.Keys keys2
public CaseInsensitiveIntMap()
public CaseInsensitiveIntMap(int initialCapacity)
initialCapacity - The backing array size is initialCapacity / loadFactor, increased to the next power of two.public CaseInsensitiveIntMap(int initialCapacity,
float loadFactor)
initialCapacity - The backing array size is initialCapacity / loadFactor, increased to the next power of two.public CaseInsensitiveIntMap(CaseInsensitiveIntMap map)
public static int tableSize(int capacity,
float loadFactor)
capacity / loadFactor.capacity - the amount of items the hash table should be able to holdloadFactor - between 0.0 (exclusive) and 1.0 (inclusive); the fraction of how much of the table can be filledprotected int place(java.lang.String item)
mask for the specified item.public void put(java.lang.String key,
int value)
public int put(java.lang.String key,
int value,
int defaultValue)
public void putAll(CaseInsensitiveIntMap map)
public int get(java.lang.String key,
int defaultValue)
public int getAndIncrement(java.lang.String key,
int defaultValue,
int increment)
public int remove(java.lang.String key,
int defaultValue)
public boolean notEmpty()
public boolean isEmpty()
public void shrink(int maximumCapacity)
public void clear(int maximumCapacity)
public void clear()
public boolean containsValue(int value)
public boolean containsKey(java.lang.String key)
public java.lang.String findKey(int value)
public void ensureCapacity(int additionalCapacity)
public int hashCode()
hashCode in class java.lang.Objectpublic boolean equals(java.lang.Object obj)
equals in class java.lang.Objectpublic java.lang.String toString(java.lang.String separator)
public java.lang.String toString()
toString in class java.lang.Objectpublic CaseInsensitiveIntMap.Entries iterator()
iterator in interface java.lang.Iterable<CaseInsensitiveIntMap.Entry>public CaseInsensitiveIntMap.Entries entries()
CaseInsensitiveIntMap.Entries constructor for nested or multithreaded iteration.public CaseInsensitiveIntMap.Values values()
public CaseInsensitiveIntMap.Keys keys()