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 dd5b2c30c5
commit 30060b6083

View File

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