From 7ec171fbf4097f9ac5c9cf3aebfd268ce98e7d3f Mon Sep 17 00:00:00 2001 From: nathan Date: Sun, 28 Jan 2018 17:57:08 +0100 Subject: [PATCH] Added putAll() method to ObjectIntMap. --- .../util/collections/LockFreeIntBiMap.java | 11 ++---- .../collections/LockFreeObjectIntBiMap.java | 10 +---- .../util/collections/ObjectIntMap.java | 39 +++++++++++++++++++ 3 files changed, 44 insertions(+), 16 deletions(-) diff --git a/src/dorkbox/util/collections/LockFreeIntBiMap.java b/src/dorkbox/util/collections/LockFreeIntBiMap.java index acd8157..ffcf4a0 100644 --- a/src/dorkbox/util/collections/LockFreeIntBiMap.java +++ b/src/dorkbox/util/collections/LockFreeIntBiMap.java @@ -259,15 +259,10 @@ class LockFreeIntBiMap { throw e; } - // only if there are no problems with the creation of the new bimap AND the uniqueness constrain is guaranteed - this.forwardHashMap.putAll(biMap.forwardHashMap); - // there is no putAll() method for ObjectIntMap - this.reverseHashMap.size = biMap.reverseHashMap.size; - this.reverseHashMap.keyTable = biMap.reverseHashMap.keyTable; - this.reverseHashMap.valueTable = biMap.reverseHashMap.valueTable; - this.reverseHashMap.capacity = biMap.reverseHashMap.capacity; - this.reverseHashMap.stashSize = biMap.reverseHashMap.stashSize; + // we have checked to make sure that the bimap is unique, AND have checked that we don't already have any of the key/values in ourselves + this.forwardHashMap.putAll(biMap.forwardHashMap); + this.reverseHashMap.putAll(biMap.reverseHashMap); } /** diff --git a/src/dorkbox/util/collections/LockFreeObjectIntBiMap.java b/src/dorkbox/util/collections/LockFreeObjectIntBiMap.java index 304afd0..917f041 100644 --- a/src/dorkbox/util/collections/LockFreeObjectIntBiMap.java +++ b/src/dorkbox/util/collections/LockFreeObjectIntBiMap.java @@ -237,14 +237,8 @@ class LockFreeObjectIntBiMap { throw e; } - // there is no putAll() method for ObjectIntMap - this.forwardHashMap.size = biMap.forwardHashMap.size; - this.forwardHashMap.keyTable = biMap.forwardHashMap.keyTable; - this.forwardHashMap.valueTable = biMap.forwardHashMap.valueTable; - this.forwardHashMap.capacity = biMap.forwardHashMap.capacity; - this.forwardHashMap.stashSize = biMap.forwardHashMap.stashSize; - - // only if there are no problems with the creation of the new bimap AND the uniqueness constrain is guaranteed + // we have checked to make sure that the bimap is unique, AND have checked that we don't already have any of the key/values in ourselves + this.forwardHashMap.putAll(biMap.forwardHashMap); this.reverseHashMap.putAll(biMap.reverseHashMap); } diff --git a/src/dorkbox/util/collections/ObjectIntMap.java b/src/dorkbox/util/collections/ObjectIntMap.java index a0a5629..cb5c153 100644 --- a/src/dorkbox/util/collections/ObjectIntMap.java +++ b/src/dorkbox/util/collections/ObjectIntMap.java @@ -43,6 +43,31 @@ public class ObjectIntMap { private int stashCapacity; private int pushIterations; + public static + void main(String[] args) { + ObjectIntMap test = new ObjectIntMap<>(4); + String one = "One"; + String four = "Four"; + + test.put(one, 1); + test.put("Two", 2); + test.put("Three", 3); + test.put(four, 4); + test.put(four, 1); + test.put(one, 13); + + ObjectIntMap test2 = new ObjectIntMap<>(2); + test2.put(one, 11); + test2.put(four, 44); + test2.put("Five", 55); + + test2.putAll(test); + + + System.out.println(test.toString()); + System.out.println(test2.toString()); + } + /** Creates a new map with an initial capacity of 32 and a load factor of 0.8. This map will hold 25 items before growing the * backing table. */ public ObjectIntMap () { @@ -91,6 +116,20 @@ public class ObjectIntMap { this.size = map.size; } + public + void putAll(final ObjectIntMap map) { + K[] keyTable = map.keyTable; + int[] valueTable = map.valueTable; + + for (int i = 0, length = map.capacity + map.stashSize; i < length; i++) { + K k = keyTable[i]; + int v = valueTable[i]; + if (k != null && v != 0) { + put(k, v); + } + } + } + public void put (K key, int value) { if (key == null) { throw new IllegalArgumentException("key cannot be null.");