Start of adding copyable collections. Starting with Lists.

This commit is contained in:
Speiger 2021-10-06 16:30:53 +02:00
parent 1f1aa995df
commit 07b715dd4c
7 changed files with 54 additions and 0 deletions

View File

@ -123,6 +123,14 @@ public interface COLLECTION KEY_GENERIC_TYPE extends Collection<CLASS_TYPE>, 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.

View File

@ -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;

View File

@ -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));

View File

@ -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
*

View File

@ -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;

View File

@ -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}

View File

@ -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; }
}
}