From 5d72a51001a9b166f68fceee36b886046030497c Mon Sep 17 00:00:00 2001 From: asofold Date: Mon, 8 Feb 2016 01:35:24 +0100 Subject: [PATCH] Fix hash code use. --- .../nocheatplus/utilities/ds/map/HashMapLOW.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/NCPCommons/src/main/java/fr/neatmonster/nocheatplus/utilities/ds/map/HashMapLOW.java b/NCPCommons/src/main/java/fr/neatmonster/nocheatplus/utilities/ds/map/HashMapLOW.java index c4ad7990..51fe29b4 100644 --- a/NCPCommons/src/main/java/fr/neatmonster/nocheatplus/utilities/ds/map/HashMapLOW.java +++ b/NCPCommons/src/main/java/fr/neatmonster/nocheatplus/utilities/ds/map/HashMapLOW.java @@ -36,6 +36,10 @@ public class HashMapLOW { return key == null ? 0 : key.hashCode(); } + private static int getBucketIndex(final int hashCode, final int buckets) { + return Math.abs(hashCode) % buckets; + } + static class LHMEntry implements Entry { final int hashCode; @@ -436,7 +440,7 @@ public class HashMapLOW { for (int j = 0; j < bucket.contents.length; j++) { final LHMEntry entry = bucket.contents[j]; if (entry != null) { - final int newIndex = entry.hashCode % newLength; + final int newIndex = getBucketIndex(entry.hashCode, newLength); LHMBucket newBucket = newBuckets[newIndex]; if (newBucket == null) { newBucket = new LHMBucket(); @@ -486,7 +490,7 @@ public class HashMapLOW { public V put(final K key, final V value) { final int hashCode = getHashCode(key); lock.lock(); - final int index = hashCode % buckets.length; + final int index = getBucketIndex(hashCode, buckets.length); LHMBucket bucket = buckets[index]; if (bucket == null) { bucket = new LHMBucket(); @@ -527,7 +531,7 @@ public class HashMapLOW { * @return */ private V removeUnderLock(final int hashCode, final K key) { - final int index = hashCode % buckets.length; + final int index = getBucketIndex(hashCode, buckets.length); final LHMBucket bucket = buckets[index]; if (bucket == null || bucket.size == 0) { return null; @@ -566,7 +570,7 @@ public class HashMapLOW { public V get(final K key) { final int hashCode = getHashCode(key); final LHMBucket[] buckets = this.buckets; - final LHMBucket bucket = buckets[hashCode % buckets.length]; + final LHMBucket bucket = buckets[getBucketIndex(hashCode, buckets.length)]; if (bucket == null || bucket.size == 0) { return null; } @@ -598,7 +602,7 @@ public class HashMapLOW { public boolean containsKey(final K key) { final int hashCode = getHashCode(key); final LHMBucket[] buckets = this.buckets; - final LHMBucket bucket = buckets[hashCode % buckets.length]; + final LHMBucket bucket = buckets[getBucketIndex(hashCode, buckets.length)]; if (bucket == null || bucket.size == 0) { return false; }