From 07b715dd4c7ecded4c443a39b3a7fbb4bce2cae8 Mon Sep 17 00:00:00 2001 From: Speiger Date: Wed, 6 Oct 2021 16:30:53 +0200 Subject: [PATCH] Start of adding copyable collections. Starting with Lists. --- .../templates/collections/Collection.template | 8 ++++++++ .../templates/lists/AbstractList.template | 2 ++ .../templates/lists/ArrayList.template | 8 ++++++++ .../templates/lists/ImmutableList.template | 5 +++++ .../templates/lists/LinkedList.template | 17 +++++++++++++++++ .../collections/templates/lists/List.template | 2 ++ .../collections/templates/utils/Lists.template | 12 ++++++++++++ 7 files changed, 54 insertions(+) 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 35e2fc94..5af174f2 100644 --- a/src/builder/resources/speiger/assets/collections/templates/collections/Collection.template +++ b/src/builder/resources/speiger/assets/collections/templates/collections/Collection.template @@ -123,6 +123,14 @@ public interface COLLECTION KEY_GENERIC_TYPE extends Collection, ITE */ public boolean retainAll(COLLECTION KEY_GENERIC_TYPE c); + /** + * A Function that does a shallow clone of the Collection itself. + * 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 + */ + public default COLLECTION KEY_GENERIC_TYPE copy() { throw new UnsupportedOperationException(); } + #if !TYPE_OBJECT /** * A Type-Specific toArray function that delegates to {@link #TO_ARRAY(KEY_TYPE[])} with a newly created array. diff --git a/src/builder/resources/speiger/assets/collections/templates/lists/AbstractList.template b/src/builder/resources/speiger/assets/collections/templates/lists/AbstractList.template index 7d21f5e3..5342873b 100644 --- a/src/builder/resources/speiger/assets/collections/templates/lists/AbstractList.template +++ b/src/builder/resources/speiger/assets/collections/templates/lists/AbstractList.template @@ -229,6 +229,8 @@ public abstract class ABSTRACT_LIST KEY_GENERIC_TYPE extends ABSTRACT_COLLECTION while(size < size()) REMOVE(size() - 1); } + public ABSTRACT_LIST KEY_GENERIC_TYPE copy() { throw new UnsupportedOperationException(); } + private class SUB_LIST extends ABSTRACT_LIST KEY_GENERIC_TYPE { ABSTRACT_LIST KEY_GENERIC_TYPE l; int offset; diff --git a/src/builder/resources/speiger/assets/collections/templates/lists/ArrayList.template b/src/builder/resources/speiger/assets/collections/templates/lists/ArrayList.template index cc91dddc..25aca449 100644 --- a/src/builder/resources/speiger/assets/collections/templates/lists/ArrayList.template +++ b/src/builder/resources/speiger/assets/collections/templates/lists/ArrayList.template @@ -1015,6 +1015,14 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE grow(size); } + @Override + public ARRAY_LIST KEY_GENERIC_TYPE copy() { + ARRAY_LIST KEY_GENERIC_TYPE list = new ARRAY_LISTBRACES(); + list.data = Arrays.copyOf(data, data.length); + list.size = size; + return list; + } + protected void grow(int capacity) { if(capacity < data.length) return; data = Arrays.copyOf(data, data == ARRAYS.EMPTY_ARRAY ? Math.max(DEFAULT_ARRAY_SIZE, capacity) : (int)Math.max(Math.min((long)data.length + (data.length >> 1), SanityChecks.MAX_ARRAY_SIZE), capacity)); diff --git a/src/builder/resources/speiger/assets/collections/templates/lists/ImmutableList.template b/src/builder/resources/speiger/assets/collections/templates/lists/ImmutableList.template index 0dbfd8ea..7e48377b 100644 --- a/src/builder/resources/speiger/assets/collections/templates/lists/ImmutableList.template +++ b/src/builder/resources/speiger/assets/collections/templates/lists/ImmutableList.template @@ -261,6 +261,11 @@ public class IMMUTABLE_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_T return data[index]; } + @Override + public IMMUTABLE_LIST KEY_GENERIC_TYPE copy() { + return new IMMUTABLE_LISTBRACES(Arrays.copyOf(data, data.length)); + } + /** * A Type Specific foreach function that reduces (un)boxing * diff --git a/src/builder/resources/speiger/assets/collections/templates/lists/LinkedList.template b/src/builder/resources/speiger/assets/collections/templates/lists/LinkedList.template index 18426619..ca384dd7 100644 --- a/src/builder/resources/speiger/assets/collections/templates/lists/LinkedList.template +++ b/src/builder/resources/speiger/assets/collections/templates/lists/LinkedList.template @@ -820,6 +820,23 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE size = 0; } + @Override + public LINKED_LIST KEY_GENERIC_TYPE copy() { + LINKED_LIST KEY_GENERIC_TYPE list = new LINKED_LISTBRACES(); + list.size = size; + if(first != null) { + list.first = new EntryBRACES(first.value, null, null); + Entry KEY_GENERIC_TYPE lastReturned = list.first; + for(Entry KEY_GENERIC_TYPE entry = first.next;entry != null;entry = entry.next) { + Entry KEY_GENERIC_TYPE next = new EntryBRACES(entry.value, lastReturned, null); + lastReturned.next = next; + lastReturned = next; + } + list.last = lastReturned; + } + return list; + } + protected Entry KEY_GENERIC_TYPE getNode(int index) { if(index < size >> 2) { Entry KEY_GENERIC_TYPE x = first; diff --git a/src/builder/resources/speiger/assets/collections/templates/lists/List.template b/src/builder/resources/speiger/assets/collections/templates/lists/List.template index e90aa53e..ebb2677c 100644 --- a/src/builder/resources/speiger/assets/collections/templates/lists/List.template +++ b/src/builder/resources/speiger/assets/collections/templates/lists/List.template @@ -339,6 +339,8 @@ public interface LIST KEY_GENERIC_TYPE extends COLLECTION KEY_GENERIC_TYPE, List */ public void size(int size); + @Override + public LIST KEY_GENERIC_TYPE copy(); #if !TYPE_OBJECT /** {@inheritDoc} diff --git a/src/builder/resources/speiger/assets/collections/templates/utils/Lists.template b/src/builder/resources/speiger/assets/collections/templates/utils/Lists.template index 6c153512..27b59a0c 100644 --- a/src/builder/resources/speiger/assets/collections/templates/utils/Lists.template +++ b/src/builder/resources/speiger/assets/collections/templates/utils/Lists.template @@ -216,6 +216,9 @@ public class LISTS #endif @Override public int size() { return 1; } + + @Override + public SingletonList KEY_GENERIC_TYPE copy() { return new SingletonListBRACES(element); } } private static class SynchronizedArrayList KEY_GENERIC_TYPE extends SynchronizedList KEY_GENERIC_TYPE implements IARRAY KEY_GENERIC_TYPE @@ -372,6 +375,9 @@ public class LISTS @Override public void size(int size) { synchronized(mutex) { l.size(size); } } + + @Override + public LIST KEY_GENERIC_TYPE copy() { throw new UnsupportedOperationException(); } } private static class UnmodifiableRandomList KEY_GENERIC_TYPE extends UnmodifiableList KEY_GENERIC_TYPE implements RandomAccess @@ -481,6 +487,9 @@ public class LISTS @Override public void size(int size) { throw new UnsupportedOperationException(); } + + @Override + public LIST KEY_GENERIC_TYPE copy() { throw new UnsupportedOperationException(); } } private static class EmptyList KEY_GENERIC_TYPE extends COLLECTIONS.EmptyCollection KEY_GENERIC_TYPE implements LIST KEY_GENERIC_TYPE @@ -563,5 +572,8 @@ public class LISTS @Override public void size(int size) { throw new UnsupportedOperationException(); } + + @Override + public EmptyList KEY_GENERIC_TYPE copy() { return this; } } }