Added putAll() method to ObjectIntMap.

This commit is contained in:
nathan 2018-01-28 17:57:08 +01:00
parent 3be511d91d
commit 7ec171fbf4
3 changed files with 44 additions and 16 deletions

View File

@ -259,15 +259,10 @@ class LockFreeIntBiMap<V> {
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);
}
/**

View File

@ -237,14 +237,8 @@ class LockFreeObjectIntBiMap<V> {
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);
}

View File

@ -43,6 +43,31 @@ public class ObjectIntMap<K> {
private int stashCapacity;
private int pushIterations;
public static
void main(String[] args) {
ObjectIntMap<String> 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<String> 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<K> {
this.size = map.size;
}
public
void putAll(final ObjectIntMap<? extends K> 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.");