Optimize IntObjectHashMap handling of negative keys.

Motivation:

The hashIndex method currently uses a conditional to handle negative
keys. This could be done without a conditional to slightly improve
performance.

Modifications:

Modified hashIndex() to avoid using a conditional.

Result:

Slight performance improvement to hashIndex().
This commit is contained in:
nmittler 2014-10-20 10:19:54 -07:00
parent f9706fda42
commit bb397c183b

View File

@ -312,11 +312,9 @@ public class IntObjectHashMap<V> implements IntObjectMap<V>, Iterable<IntObjectM
* Returns the hashed index for the given key. * Returns the hashed index for the given key.
*/ */
private int hashIndex(int key) { private int hashIndex(int key) {
int hash = key % keys.length; int length = keys.length;
if (hash < 0) { // Allowing for negative keys by adding the length after the first mod operation.
hash += keys.length; return (key % length + length) % length;
}
return hash;
} }
/** /**