From 61d7a88c82bcd90f0b57fa59e2d95b970e8f76fe Mon Sep 17 00:00:00 2001 From: Speiger Date: Wed, 6 Oct 2021 18:32:10 +0200 Subject: [PATCH] Maps are now copyable too --- .../templates/collections/Collection.template | 1 + .../maps/abstracts/AbstractMap.template | 5 +++ .../LinkedOpenCustomHashMap.template | 18 ++++++++++ .../customHash/OpenCustomHashMap.template | 14 ++++++++ .../maps/impl/hash/LinkedOpenHashMap.template | 18 ++++++++++ .../maps/impl/hash/OpenHashMap.template | 16 ++++++++- .../immutable/ImmutableOpenHashMap.template | 22 +++++++++++- .../maps/impl/misc/ArrayMap.template | 13 +++++++ .../templates/maps/impl/misc/EnumMap.template | 9 +++++ .../maps/impl/misc/LinkedEnumMap.template | 12 +++++++ .../maps/impl/tree/AVLTreeMap.template | 34 ++++++++++++++++++- .../maps/impl/tree/RBTreeMap.template | 33 ++++++++++++++++++ .../templates/maps/interfaces/Map.template | 9 +++++ .../maps/interfaces/NavigableMap.template | 2 ++ .../maps/interfaces/SortedMap.template | 3 ++ .../templates/utils/maps/Maps.template | 18 +++++++++- 16 files changed, 223 insertions(+), 4 deletions(-) diff --git a/src/builder/resources/speiger/assets/collections/templates/collections/Collection.template b/src/builder/resources/speiger/assets/collections/templates/collections/Collection.template index 883a94b..24b14a4 100644 --- a/src/builder/resources/speiger/assets/collections/templates/collections/Collection.template +++ b/src/builder/resources/speiger/assets/collections/templates/collections/Collection.template @@ -128,6 +128,7 @@ public interface COLLECTION KEY_GENERIC_TYPE extends Collection, 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(); diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/abstracts/AbstractMap.template b/src/builder/resources/speiger/assets/collections/templates/maps/abstracts/AbstractMap.template index aeff52d..9ed3f62 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/abstracts/AbstractMap.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/abstracts/AbstractMap.template @@ -47,6 +47,11 @@ public abstract class ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE extends AbstractMap ENTRY_SET() { if(entrySet == null) entrySet = new MapEntrySet(); diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/impl/hash/LinkedOpenHashMap.template b/src/builder/resources/speiger/assets/collections/templates/maps/impl/hash/LinkedOpenHashMap.template index 83f7832..8675f11 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/impl/hash/LinkedOpenHashMap.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/impl/hash/LinkedOpenHashMap.template @@ -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; diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/impl/hash/OpenHashMap.template b/src/builder/resources/speiger/assets/collections/templates/maps/impl/hash/OpenHashMap.template index 74f1fbb..b17f665 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/impl/hash/OpenHashMap.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/impl/hash/OpenHashMap.template @@ -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 ENTRY_SET() { if(entrySet == null) entrySet = new MapEntrySet(); diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/impl/immutable/ImmutableOpenHashMap.template b/src/builder/resources/speiger/assets/collections/templates/maps/impl/immutable/ImmutableOpenHashMap.template index 94c8339..0a51ec5 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/impl/immutable/ImmutableOpenHashMap.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/impl/immutable/ImmutableOpenHashMap.template @@ -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 diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/ArrayMap.template b/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/ArrayMap.template index 21a02c9..18894ba 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/ArrayMap.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/ArrayMap.template @@ -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; diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/EnumMap.template b/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/EnumMap.template index ff5ee31..be6decc 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/EnumMap.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/EnumMap.template @@ -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 ENTRY_SET() { if(entrySet == null) entrySet = new EntrySet(); diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/LinkedEnumMap.template b/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/LinkedEnumMap.template index 4704c0b..f0192ee 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/LinkedEnumMap.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/LinkedEnumMap.template @@ -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; diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/impl/tree/AVLTreeMap.template b/src/builder/resources/speiger/assets/collections/templates/maps/impl/tree/AVLTreeMap.template index ad126ff..b3064c1 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/impl/tree/AVLTreeMap.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/impl/tree/AVLTreeMap.template @@ -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; diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/impl/tree/RBTreeMap.template b/src/builder/resources/speiger/assets/collections/templates/maps/impl/tree/RBTreeMap.template index 8e13d3b..63a8eef 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/impl/tree/RBTreeMap.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/impl/tree/RBTreeMap.template @@ -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; diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/interfaces/Map.template b/src/builder/resources/speiger/assets/collections/templates/maps/interfaces/Map.template index df5e9ff..996e885 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/interfaces/Map.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/interfaces/Map.template @@ -59,6 +59,15 @@ public interface MAP KEY_VALUE_GENERIC_TYPE extends Map { + @Override + public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE copy(); /** @return a Type Specific desendingMap */ @Override public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE descendingMap(); diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/interfaces/SortedMap.template b/src/builder/resources/speiger/assets/collections/templates/maps/interfaces/SortedMap.template index 8d182cb..72c070f 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/interfaces/SortedMap.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/interfaces/SortedMap.template @@ -75,6 +75,9 @@ public interface SORTED_MAP KEY_VALUE_GENERIC_TYPE extends SortedMap 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;