Class CaseInsensitiveIntMap
- All Implemented Interfaces:
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).
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic classstatic classstatic class -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected CaseInsensitiveIntMap.Entriesprotected CaseInsensitiveIntMap.Entriesprotected intUsed byplace(String)to modifyhashCodeIgnoreCase(CharSequence, int)results.protected CaseInsensitiveIntMap.Keysprotected CaseInsensitiveIntMap.Keysprotected String[]protected floatprotected intA bitmask used to confine hashcodes to the size of the table.protected intintprotected intprotected CaseInsensitiveIntMap.Valuesprotected CaseInsensitiveIntMap.Valuesprotected int[] -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new map with an initial capacity of 51 and a load factor of 0.6.CaseInsensitiveIntMap(int initialCapacity) Creates a new map with a load factor of 0.6 .CaseInsensitiveIntMap(int initialCapacity, float loadFactor) Creates a new map with the specified initial capacity and load factor.Creates a new map identical to the specified map.CaseInsensitiveIntMap(String[] keys, int[] values) Creates a new map and puts key-value pairs sequentially from the two given arrays until either array is exhausted. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()voidclear(int maximumCapacity) Clears the map and reduces the size of the backing arrays to be the specified capacity / loadFactor, if they are larger.booleancontainsKey(String key) booleancontainsValue(int value) Returns true if the specified value is in the map.voidensureCapacity(int additionalCapacity) Increases the size of the backing array to accommodate the specified number of additional items / loadFactor.entries()Returns an iterator for the entries in the map.booleanfindKey(int value) Returns the key for the specified value, or null if it is not in the map.intReturns the value for the specified key, or the default value if the key is not in the map.intgetAndIncrement(String key, int defaultValue, int increment) Returns the key's current value and increments the stored value.inthashCode()static intGets a 32-bit thoroughly-random hashCode from the given CharSequence, ignoring the case of any cased letters.static inthashCodeIgnoreCase(CharSequence data, int seed) Gets a 32-bit thoroughly-random hashCode from the given CharSequence, ignoring the case of any cased letters.booleanisEmpty()Returns true if the map is empty.iterator()keys()Returns an iterator for the keys in the map.booleannotEmpty()Returns true if the map has one or more items.protected intReturns an index >= 0 and <=maskfor the specifieditem.voidintReturns the old value associated with the specified key, or the specified default value.voidvoidPuts keys with values in sequential pairs from the two arrays given, until either array is exhausted.intReturns the value for the removed key, or the default value if the key is not in the map.voidshrink(int maximumCapacity) Reduces the size of the backing arrays to be the specified capacity / loadFactor, or less.static inttableSize(int capacity, float loadFactor) Used to establish the size of a hash table.toString()values()Returns an iterator for the values in the map.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
size
public int size -
keyTable
-
valueTable
protected int[] valueTable -
loadFactor
protected float loadFactor -
threshold
protected int threshold -
shift
protected int shiftUsed byplace(String)to bit shift the upper bits of alonginto a usable range (>= 0 and <=mask). This class expects the shift to be > 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.Currently, shift isn't used to move bits in hashes, but it is updated and used to select different values for
hashSeed, with the value changing when the map resizes. -
mask
protected int maskA 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. Inplace(String), this is used to get the relevant low bits of a hash. -
hashSeed
protected int hashSeedUsed byplace(String)to modifyhashCodeIgnoreCase(CharSequence, int)results. Changes on every call toresize(int)by default. This only needs to be serialized if the full key and value tables are serialized. Unless this is changed by some other code (which would need to be a subclass), hashSeed is fully determined by theshiftwhen the map was constructed or last resized. -
entries1
-
entries2
-
values1
-
values2
-
keys1
-
keys2
-
-
Constructor Details
-
CaseInsensitiveIntMap
public CaseInsensitiveIntMap()Creates a new map with an initial capacity of 51 and a load factor of 0.6. -
CaseInsensitiveIntMap
public CaseInsensitiveIntMap(int initialCapacity) Creates a new map with a load factor of 0.6 .- Parameters:
initialCapacity- The backing array size is initialCapacity / loadFactor, increased to the next power of two.
-
CaseInsensitiveIntMap
public CaseInsensitiveIntMap(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- The backing array size is initialCapacity / loadFactor, increased to the next power of two.
-
CaseInsensitiveIntMap
Creates a new map and puts key-value pairs sequentially from the two given arrays until either array is exhausted. The initial capacity will be the length of the shorter of the two arrays, and the load factor will be 0.6 . -
CaseInsensitiveIntMap
Creates a new map identical to the specified map.
-
-
Method Details
-
tableSize
public static int tableSize(int capacity, float loadFactor) Used to establish the size of a hash table. The table size will always be a power of two, and should be the next power of two that is at least equal tocapacity / loadFactor.- Parameters:
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 filled- Returns:
- the size of a hash table that can handle the specified capacity with the given loadFactor
-
place
Returns an index >= 0 and <=maskfor the specifieditem. -
put
-
put
Returns the old value associated with the specified key, or the specified default value. -
putAll
Puts keys with values in sequential pairs from the two arrays given, until either array is exhausted. -
putAll
-
get
Returns the value for the specified key, or the default value if the key is not in the map. -
getAndIncrement
Returns the key's current value and increments the stored value. If the key is not in the map, defaultValue + increment is put into the map and defaultValue is returned. -
remove
Returns the value for the removed key, or the default value if the key is not in the map. -
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(int value) 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. -
containsKey
-
findKey
Returns the key for the specified value, or null if it is not in the map. Note this traverses the entire map and compares every value, which may be an expensive operation. -
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() -
equals
-
toString
-
toString
-
iterator
- Specified by:
iteratorin interfaceIterable<CaseInsensitiveIntMap.Entry>
-
entries
Returns an iterator for the entries in the map. Remove is supported. Use theCaseInsensitiveIntMap.Entriesconstructor for nested or multithreaded iteration. -
values
Returns an iterator for the values in the map. Remove is supported. -
keys
Returns an iterator for the keys in the map. Remove is supported. -
hashCodeIgnoreCase
Gets a 32-bit thoroughly-random hashCode from the given CharSequence, ignoring the case of any cased letters. Uses wyhash version 4.2, but shrunk down to work on 16-bit char values instead of 64-bit long values. This gets the hash as if all cased letters have been converted to upper case byCategory.caseUp(char); this should be correct for all alphabets in Unicode except Georgian. Typically, place() methods in Sets and Maps here that want case-insensitive hashing would use this with(hashCodeIgnoreCase(text) >>> shift)or(hashCodeIgnoreCase(text) & mask).- Parameters:
data- a non-null CharSequence; often a String, but this has no trouble with a StringBuilder- Returns:
- an int hashCode; quality should be similarly good across any bits
-
hashCodeIgnoreCase
Gets a 32-bit thoroughly-random hashCode from the given CharSequence, ignoring the case of any cased letters. UUses wyhash version 4.2, but shrunk down to work on 16-bit char values instead of 64-bit long values. This gets the hash as if all cased letters have been converted to upper case byCategory.caseUp(char); this should be correct for all alphabets in Unicode except Georgian. Typically, place() methods in Sets and Maps here that want case-insensitive hashing would use this with(hashCodeIgnoreCase(text, seed) >>> shift)or(hashCodeIgnoreCase(text, seed) & mask).- Parameters:
data- a non-null CharSequence; often a String, but this has no trouble with a StringBuilderseed- any int; must be the same between calls if two equivalent values fordatamust be the same- Returns:
- an int hashCode; quality should be similarly good across any bits
-