Maps are now copyable too

This commit is contained in:
Speiger 2021-10-06 18:32:10 +02:00
parent dff173222d
commit 61d7a88c82
16 changed files with 223 additions and 4 deletions

View File

@ -128,6 +128,7 @@ public interface COLLECTION KEY_GENERIC_TYPE extends Collection<CLASS_TYPE>, ITE
* This function is more optimized then a copy constructor since the Collection does not have to be unsorted/resorted.
* It can be compared to Cloneable but with less exception risk
* @return a Shallow Copy of the collection
* @note Wrappers and view collections will not support this feature
*/
public COLLECTION KEY_GENERIC_TYPE copy();

View File

@ -47,6 +47,11 @@ public abstract class ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE extends AbstractMap<CL
return this;
}
@Override
public MAP KEY_VALUE_GENERIC_TYPE copy() {
throw new UnsupportedOperationException();
}
#if !TYPE_OBJECT || !VALUE_OBJECT
@Override
@Deprecated

View File

@ -1,5 +1,6 @@
package speiger.src.collections.PACKAGE.maps.impl.customHash;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Map;
import java.util.NoSuchElementException;
@ -336,6 +337,23 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
return values[index];
}
@Override
public LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE copy() {
LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE map = new LINKED_CUSTOM_HASH_MAPKV_BRACES(0, loadFactor, strategy);
map.minCapacity = minCapacity;
map.mask = mask;
map.maxFill = maxFill;
map.nullIndex = nullIndex;
map.containsNull = containsNull;
map.size = size;
map.keys = Arrays.copyOf(keys, keys.length);
map.values = Arrays.copyOf(values, values.length);
map.links = Arrays.copyOf(links, links.length);
map.firstIndex = firstIndex;
map.lastIndex = lastIndex;
return map;
}
@Override
public COMPARATOR KEY_GENERIC_TYPE comparator() {
return null;

View File

@ -423,6 +423,20 @@ public class CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VAL
}
#endif
@Override
public CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE copy() {
CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE map = new CUSTOM_HASH_MAPKV_BRACES(0, loadFactor, strategy);
map.minCapacity = minCapacity;
map.mask = mask;
map.maxFill = maxFill;
map.nullIndex = nullIndex;
map.containsNull = containsNull;
map.size = size;
map.keys = Arrays.copyOf(keys, keys.length);
map.values = Arrays.copyOf(values, values.length);
return map;
}
@Override
public ObjectSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> ENTRY_SET() {
if(entrySet == null) entrySet = new MapEntrySet();

View File

@ -1,5 +1,6 @@
package speiger.src.collections.PACKAGE.maps.impl.hash;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Map;
import java.util.NoSuchElementException;
@ -313,6 +314,23 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
return values[index];
}
@Override
public LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE copy() {
LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE map = new LINKED_HASH_MAPKV_BRACES(0, loadFactor);
map.minCapacity = minCapacity;
map.mask = mask;
map.maxFill = maxFill;
map.nullIndex = nullIndex;
map.containsNull = containsNull;
map.size = size;
map.keys = Arrays.copyOf(keys, keys.length);
map.values = Arrays.copyOf(values, values.length);
map.links = Arrays.copyOf(links, links.length);
map.firstIndex = firstIndex;
map.lastIndex = lastIndex;
return map;
}
@Override
public COMPARATOR KEY_GENERIC_TYPE comparator() {
return null;

View File

@ -383,7 +383,21 @@ public class HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENE
return slot < 0 ? defaultValue : values[slot];
}
#endif
#endif
@Override
public HASH_MAP KEY_VALUE_GENERIC_TYPE copy() {
HASH_MAP KEY_VALUE_GENERIC_TYPE map = new HASH_MAPKV_BRACES(0, loadFactor);
map.minCapacity = minCapacity;
map.mask = mask;
map.maxFill = maxFill;
map.nullIndex = nullIndex;
map.containsNull = containsNull;
map.size = size;
map.keys = Arrays.copyOf(keys, keys.length);
map.values = Arrays.copyOf(values, values.length);
return map;
}
@Override
public ObjectSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> ENTRY_SET() {
if(entrySet == null) entrySet = new MapEntrySet();

View File

@ -1,7 +1,8 @@
package speiger.src.collections.PACKAGE.maps.impl.immutable;
import java.util.Map;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.function.Consumer;
@ -103,6 +104,10 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_
/** Amount of Elements stored in the HashMap */
protected int size;
/**
* Helper constructor for copying the Map
*/
protected IMMUTABLE_HASH_MAP() {}
#if !TYPE_OBJECT || !VALUE_OBJECT
/**
@ -412,6 +417,21 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_
return valuesC;
}
@Override
public IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE copy() {
IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE map = new IMMUTABLE_HASH_MAPKV_BRACES();
map.mask = mask;
map.nullIndex = nullIndex;
map.containsNull = containsNull;
map.size = size;
map.keys = Arrays.copyOf(keys, keys.length);
map.values = Arrays.copyOf(values, values.length);
map.links = Arrays.copyOf(links, links.length);
map.firstIndex = firstIndex;
map.lastIndex = lastIndex;
return map;
}
@Override
public COMPARATOR KEY_GENERIC_TYPE comparator() { return null; }
@Override

View File

@ -554,6 +554,14 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN
size = 0;
}
public ARRAY_MAP KEY_VALUE_GENERIC_TYPE copy() {
ARRAY_MAP KEY_VALUE_GENERIC_TYPE map = new ARRAY_MAPKV_BRACES();
map.size = size;
map.keys = Arrays.copyOf(keys, keys.length);
map.values = Arrays.copyOf(values, keys.length);
return map;
}
@Override
public COMPARATOR KEY_GENERIC_TYPE comparator() {
return null;
@ -981,6 +989,11 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN
return entrySet;
}
@Override
public SubMap copy() {
throw new UnsupportedOperationException();
}
@Override
public COMPARATOR KEY_GENERIC_TYPE comparator() {
return null;

View File

@ -176,6 +176,15 @@ public class ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE
}
#endif
@Override
public ENUM_MAP KEY_VALUE_GENERIC_TYPE copy() {
ENUM_MAP KEY_VALUE_GENERIC_TYPE map = new ENUM_MAPKV_BRACES(keyType);
map.size = size;
System.arraycopy(present, 0, map.present, 0, Math.min(present.length, map.present.length));
System.arraycopy(values, 0, map.values, 0, Math.min(values.length, map.values.length));
return map;
}
@Override
public ObjectSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> ENTRY_SET() {
if(entrySet == null) entrySet = new EntrySet();

View File

@ -127,6 +127,18 @@ public class LINKED_ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ENUM_MAP KEY_VA
return values[index];
}
@Override
public LINKED_ENUM_MAP KEY_VALUE_GENERIC_TYPE copy() {
LINKED_ENUM_MAP KEY_VALUE_GENERIC_TYPE map = new LINKED_ENUM_MAPKV_BRACES(keyType);
map.size = size;
System.arraycopy(present, 0, map.present, 0, Math.min(present.length, map.present.length));
System.arraycopy(values, 0, map.values, 0, Math.min(values.length, map.values.length));
System.arraycopy(links, 0, map.links, 0, Math.min(links.length, map.links.length));
map.firstIndex = firstIndex;
map.lastIndex = lastIndex;
return map;
}
@Override
public COMPARATOR KEY_GENERIC_TYPE comparator() {
return null;

View File

@ -610,6 +610,21 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_
return new KeyIterator(findNode(key));
}
public AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE copy() {
AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE set = new AVL_TREE_MAPKV_BRACES();
set.size = size;
if(tree != null) {
set.tree = tree.copy();
Entry KEY_VALUE_GENERIC_TYPE lastFound = null;
for(Entry KEY_VALUE_GENERIC_TYPE entry = tree;entry != null;entry = entry.left) lastFound = entry;
set.first = lastFound;
lastFound = null;
for(Entry KEY_VALUE_GENERIC_TYPE entry = tree;entry != null;entry = entry.right) lastFound = entry;
set.last = lastFound;
}
return set;
}
@Override
public SORTED_SET KEY_GENERIC_TYPE keySet() {
return navigableKeySet();
@ -1133,7 +1148,8 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_
abstract AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE next(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry);
abstract AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE previous(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry);
@Override
public NavigableSubMap KEY_VALUE_GENERIC_TYPE copy() { throw new UnsupportedOperationException(); }
@Override
public abstract NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE descendingMap();
@Override
@ -2323,6 +2339,22 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_
this.parent = parent;
}
Entry KEY_VALUE_GENERIC_TYPE copy() {
Entry KEY_VALUE_GENERIC_TYPE entry = new EntryKV_BRACES(key, value, null);
entry.state = state;
if(left != null) {
Entry KEY_VALUE_GENERIC_TYPE newLeft = left.copy();
entry.left = newLeft;
newLeft.parent = entry;
}
if(right != null) {
Entry KEY_VALUE_GENERIC_TYPE newRight = right.copy();
entry.right = newRight;
newRight.parent = entry;
}
return entry;
}
@Override
public KEY_TYPE ENTRY_KEY() {
return key;

View File

@ -609,6 +609,21 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G
return new KeyIterator(findNode(key));
}
public RB_TREE_MAP KEY_VALUE_GENERIC_TYPE copy() {
RB_TREE_MAP KEY_VALUE_GENERIC_TYPE set = new RB_TREE_MAPKV_BRACES();
set.size = size;
if(tree != null) {
set.tree = tree.copy();
Entry KEY_VALUE_GENERIC_TYPE lastFound = null;
for(Entry KEY_VALUE_GENERIC_TYPE entry = tree;entry != null;entry = entry.left) lastFound = entry;
set.first = lastFound;
lastFound = null;
for(Entry KEY_VALUE_GENERIC_TYPE entry = tree;entry != null;entry = entry.right) lastFound = entry;
set.last = lastFound;
}
return set;
}
@Override
public SORTED_SET KEY_GENERIC_TYPE keySet() {
return navigableKeySet();
@ -1187,6 +1202,8 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G
abstract RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE next(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry);
abstract RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE previous(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry);
@Override
public NavigableSubMap KEY_VALUE_GENERIC_TYPE copy() { throw new UnsupportedOperationException(); }
@Override
public abstract NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE descendingMap();
@Override
@ -2391,6 +2408,22 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G
this.parent = parent;
}
Entry KEY_VALUE_GENERIC_TYPE copy() {
Entry KEY_VALUE_GENERIC_TYPE entry = new EntryKV_BRACES(key, value, null);
entry.state = state;
if(left != null) {
Entry KEY_VALUE_GENERIC_TYPE newLeft = left.copy();
entry.left = newLeft;
newLeft.parent = entry;
}
if(right != null) {
Entry KEY_VALUE_GENERIC_TYPE newRight = right.copy();
entry.right = newRight;
newRight.parent = entry;
}
return entry;
}
@Override
public KEY_TYPE ENTRY_KEY() {
return key;

View File

@ -59,6 +59,15 @@ public interface MAP KEY_VALUE_GENERIC_TYPE extends Map<CLASS_TYPE, CLASS_VALUE_
*/
public MAP KEY_VALUE_GENERIC_TYPE setDefaultReturnValue(VALUE_TYPE v);
/**
* A Function that does a shallow clone of the Map itself.
* This function is more optimized then a copy constructor since the Map does not have to be unsorted/resorted.
* It can be compared to Cloneable but with less exception risk
* @return a Shallow Copy of the Map
* @note Wrappers and view Maps will not support this feature
*/
public MAP KEY_VALUE_GENERIC_TYPE copy();
/**
* Type Specific method to reduce boxing/unboxing of values
* @param key the key that should be inserted,

View File

@ -10,6 +10,8 @@ import speiger.src.collections.PACKAGE.sets.NAVIGABLE_SET;
*/
public interface NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE extends SORTED_MAP KEY_VALUE_GENERIC_TYPE, NavigableMap<CLASS_TYPE, CLASS_VALUE_TYPE>
{
@Override
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE copy();
/** @return a Type Specific desendingMap */
@Override
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE descendingMap();

View File

@ -75,6 +75,9 @@ public interface SORTED_MAP KEY_VALUE_GENERIC_TYPE extends SortedMap<CLASS_TYPE,
@Override
public COMPARATOR KEY_GENERIC_TYPE comparator();
@Override
public SORTED_MAP KEY_VALUE_GENERIC_TYPE copy();
@Override
public SET KEY_GENERIC_TYPE keySet();
@Override

View File

@ -277,6 +277,8 @@ public class MAPS
@Override
public VALUE_TYPE getOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue) { return EQUALS_KEY_TYPE(key, this.key) ? value : defaultValue; }
#endif
@Override
public SingletonMap KEY_VALUE_GENERIC_TYPE copy() { return new SingletonMapKV_BRACES(key, value); }
@Override
public SET KEY_GENERIC_TYPE keySet() {
if(keySet == null) keySet = SETS.singleton(key);
@ -329,6 +331,8 @@ public class MAPS
public VALUE_COLLECTION VALUE_GENERIC_TYPE values() { return VALUE_COLLECTIONS.empty(); }
@Override
public ObjectSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> ENTRY_SET() { return ObjectSets.empty(); }
@Override
public EmptyMap KEY_VALUE_GENERIC_TYPE copy() { return this; }
}
/**
@ -415,6 +419,8 @@ public class MAPS
public MAP.Entry KEY_VALUE_GENERIC_TYPE floorEntry(KEY_TYPE key) { return unmodifiable(map.floorEntry(key)); }
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE ceilingEntry(KEY_TYPE key) { return unmodifiable(map.ceilingEntry(key)); }
@Override
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE copy() { throw new UnsupportedOperationException(); }
}
/**
@ -462,6 +468,8 @@ public class MAPS
public VALUE_TYPE FIRST_ENTRY_VALUE() { return map.FIRST_ENTRY_VALUE(); }
@Override
public VALUE_TYPE LAST_ENTRY_VALUE() { return map.LAST_ENTRY_VALUE(); }
@Override
public SORTED_MAP KEY_VALUE_GENERIC_TYPE copy() { throw new UnsupportedOperationException(); }
}
/**
@ -501,6 +509,8 @@ public class MAPS
@Override
public VALUE_TYPE getOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue) { return map.getOrDefault(key, defaultValue); }
#endif
@Override
public MAP KEY_VALUE_GENERIC_TYPE copy() { throw new UnsupportedOperationException(); }
@Override
public SET KEY_GENERIC_TYPE keySet() {
@ -614,7 +624,9 @@ public class MAPS
public MAP.Entry KEY_VALUE_GENERIC_TYPE floorEntry(KEY_TYPE key) { synchronized(mutex) { return map.floorEntry(key); } }
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE ceilingEntry(KEY_TYPE key) { synchronized(mutex) { return map.ceilingEntry(key); } }
#if !TYPE_OBJECT
@Override
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE copy() { throw new UnsupportedOperationException(); }
#if !TYPE_OBJECT
@Override
@Deprecated
public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE subMap(CLASS_TYPE fromKey, boolean fromInclusive, CLASS_TYPE toKey, boolean toInclusive) { synchronized(mutex) { return synchronize(map.subMap(fromKey, fromInclusive, toKey, toInclusive), mutex); } }
@ -718,6 +730,8 @@ public class MAPS
public VALUE_TYPE FIRST_ENTRY_VALUE() { synchronized(mutex) { return map.FIRST_ENTRY_VALUE(); } }
@Override
public VALUE_TYPE LAST_ENTRY_VALUE() { synchronized(mutex) { return map.LAST_ENTRY_VALUE(); } }
@Override
public SORTED_MAP KEY_VALUE_GENERIC_TYPE copy() { throw new UnsupportedOperationException(); }
#if !TYPE_OBJECT
@Override
@Deprecated
@ -831,6 +845,8 @@ public class MAPS
@Override
public int size() { synchronized(mutex) { return super.size(); } }
@Override
public MAP KEY_VALUE_GENERIC_TYPE copy() { throw new UnsupportedOperationException(); }
@Override
public SET KEY_GENERIC_TYPE keySet() {
if(keys == null) keys = SETS.synchronize(map.keySet(), mutex);
return keys;