Sets are now Copyable

This commit is contained in:
Speiger 2021-10-06 17:43:46 +02:00
parent 07b715dd4c
commit 6eded1f4be
22 changed files with 238 additions and 2 deletions

View File

@ -653,6 +653,9 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
return new FastEntryIterator(fromElement);
}
@Override
public MapEntrySet copy() { throw new UnsupportedOperationException(); }
@Override
public void forEach(Consumer<? super MAP.Entry KEY_VALUE_GENERIC_TYPE> action) {
int index = firstIndex;
@ -861,6 +864,9 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
return new KeyIterator(fromElement);
}
@Override
public KeySet copy() { throw new UnsupportedOperationException(); }
@Override
public int size() {
return LINKED_CUSTOM_HASH_MAP.this.size();

View File

@ -964,6 +964,9 @@ public class CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VAL
return new KeyIterator();
}
@Override
public KeySet copy() { throw new UnsupportedOperationException(); }
@Override
public int size() {
return CUSTOM_HASH_MAP.this.size();

View File

@ -630,6 +630,9 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
return new FastEntryIterator(fromElement);
}
@Override
public MapEntrySet copy() { throw new UnsupportedOperationException(); }
@Override
public void forEach(Consumer<? super MAP.Entry KEY_VALUE_GENERIC_TYPE> action) {
int index = firstIndex;
@ -838,6 +841,9 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
return new KeyIterator(fromElement);
}
@Override
public KeySet copy() { throw new UnsupportedOperationException(); }
@Override
public int size() {
return LINKED_HASH_MAP.this.size();

View File

@ -934,6 +934,9 @@ public class HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENE
HASH_MAP.this.clear();
}
@Override
public KeySet copy() { throw new UnsupportedOperationException(); }
@Override
public void forEach(CONSUMER KEY_SUPER_GENERIC_TYPE action) {
if(containsNull) action.accept(keys[nullIndex]);

View File

@ -589,6 +589,9 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_
return new FastEntryIterator(fromElement);
}
@Override
public MapEntrySet copy() { throw new UnsupportedOperationException(); }
@Override
public void forEach(Consumer<? super MAP.Entry KEY_VALUE_GENERIC_TYPE> action) {
int index = firstIndex;
@ -773,6 +776,9 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_
return new KeyIterator(fromElement);
}
@Override
public KeySet copy() { throw new UnsupportedOperationException(); }
@Override
public int size() {
return IMMUTABLE_HASH_MAP.this.size();

View File

@ -1133,6 +1133,9 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN
return new SubFastEntryIterator(fromElement);
}
@Override
public SubMapEntrySet copy() { throw new UnsupportedOperationException(); }
@Override
public void forEach(Consumer<? super MAP.Entry KEY_VALUE_GENERIC_TYPE> action) {
Objects.requireNonNull(action);
@ -1316,6 +1319,9 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN
@Override
public KEY_TYPE POLL_LAST_KEY() { return POLL_LAST_ENTRY_KEY(); }
@Override
public SubKeySet copy() { throw new UnsupportedOperationException(); }
@Override
public void forEach(CONSUMER KEY_SUPER_GENERIC_TYPE action) {
Objects.requireNonNull(action);
@ -1698,6 +1704,9 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN
return new FastEntryIterator(fromElement);
}
@Override
public MapEntrySet copy() { throw new UnsupportedOperationException(); }
@Override
public void forEach(Consumer<? super MAP.Entry KEY_VALUE_GENERIC_TYPE> action) {
Objects.requireNonNull(action);
@ -1882,6 +1891,9 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN
@Override
public KEY_TYPE POLL_LAST_KEY() { return POLL_LAST_ENTRY_KEY(); }
@Override
public KeySet copy() { throw new UnsupportedOperationException(); }
@Override
public void forEach(CONSUMER KEY_SUPER_GENERIC_TYPE action) {
Objects.requireNonNull(action);

View File

@ -351,6 +351,8 @@ public class LINKED_ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ENUM_MAP KEY_VA
return new FastEntryIterator(fromElement);
}
public MapEntrySet copy() { throw new UnsupportedOperationException(); }
@Override
public void forEach(Consumer<? super MAP.Entry KEY_VALUE_GENERIC_TYPE> action) {
int index = firstIndex;
@ -477,6 +479,8 @@ public class LINKED_ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ENUM_MAP KEY_VA
return new KeyIterator(fromElement);
}
public KeySet copy() { throw new UnsupportedOperationException(); }
@Override
public int size() {
return LINKED_ENUM_MAP.this.size();

View File

@ -1952,6 +1952,9 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_
}
#endif
@Override
public KeySet KEY_VALUE_GENERIC_TYPE copy() { throw new UnsupportedOperationException(); }
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, boolean fromInclusive, KEY_TYPE toElement, boolean toInclusive) {
return new KeySetKV_BRACES(m.subMap(fromElement, fromInclusive, toElement, toInclusive));

View File

@ -2018,6 +2018,9 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G
}
#endif
@Override
public KeySet KEY_VALUE_GENERIC_TYPE copy() { throw new UnsupportedOperationException(); }
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, boolean fromInclusive, KEY_TYPE toElement, boolean toInclusive) {
return new KeySetKV_BRACES(m.subMap(fromElement, fromInclusive, toElement, toInclusive));

View File

@ -538,6 +538,21 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
tree = null;
}
public AVL_TREE_SET KEY_GENERIC_TYPE copy() {
AVL_TREE_SET KEY_GENERIC_TYPE set = new AVL_TREE_SETBRACES();
set.size = size;
if(tree != null) {
set.tree = tree.copy();
Entry KEY_GENERIC_TYPE lastFound = null;
for(Entry KEY_GENERIC_TYPE entry = tree;entry != null;entry = entry.left) lastFound = entry;
set.first = lastFound;
lastFound = null;
for(Entry KEY_GENERIC_TYPE entry = tree;entry != null;entry = entry.right) lastFound = entry;
set.last = lastFound;
}
return set;
}
@Override
public COMPARATOR KEY_GENERIC_TYPE comparator() { return comparator; }
@ -1009,6 +1024,9 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
return fromStart && toEnd ? set.size() : iterator().skip(Integer.MAX_VALUE);
}
@Override
public SubSet KEY_GENERIC_TYPE copy() { throw new UnsupportedOperationException(); }
@Override
public void forEach(CONSUMER KEY_SUPER_GENERIC_TYPE action) {
Objects.requireNonNull(action);
@ -1250,6 +1268,22 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
this.parent = parent;
}
Entry KEY_GENERIC_TYPE copy() {
Entry KEY_GENERIC_TYPE entry = new EntryBRACES(key, null);
entry.state = state;
if(left != null) {
Entry KEY_GENERIC_TYPE newLeft = left.copy();
entry.left = newLeft;
newLeft.parent = entry;
}
if(right != null) {
Entry KEY_GENERIC_TYPE newRight = right.copy();
entry.right = newRight;
newRight.parent = entry;
}
return entry;
}
int getHeight() { return state; }
void updateHeight() { state = (1 + Math.max(left == null ? -1 : left.getHeight(), right == null ? -1 : right.getHeight())); }

View File

@ -15,6 +15,8 @@ public abstract class ABSTRACT_SET KEY_GENERIC_TYPE extends ABSTRACT_COLLECTION
{
@Override
public abstract ITERATOR KEY_GENERIC_TYPE iterator();
@Override
public ABSTRACT_SET KEY_GENERIC_TYPE copy() { throw new UnsupportedOperationException(); }
@Override
public int hashCode() {

View File

@ -450,6 +450,13 @@ public class ARRAY_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE im
return new SubSet(fromIndex, size - fromIndex);
}
public ARRAY_SET KEY_GENERIC_TYPE copy() {
ARRAY_SET KEY_GENERIC_TYPE set = new ARRAY_SETBRACES();
set.data = Arrays.copyOf(data, data.length);
set.size = size;
return set;
}
@Override
public COMPARATOR KEY_GENERIC_TYPE comparator() {
return null;
@ -675,7 +682,10 @@ public class ARRAY_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE im
if(index != -1) return new SetIterator(index);
throw new NoSuchElementException();
}
@Override
public SubSet copy() { throw new UnsupportedOperationException(); }
@Override
public SORTED_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, KEY_TYPE toElement) {
int fromIndex = findIndex(fromElement);

View File

@ -4,6 +4,7 @@ package speiger.src.collections.PACKAGE.sets;
import java.util.Comparator;
import java.util.function.Consumer;
#endif
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
@ -50,6 +51,12 @@ public class IMMUTABLE_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERI
/** The Last Index in the Map */
protected int lastIndex = -1;
/**
* Helper constructor to optimize the copying
* Only accessible through implementations
*/
protected IMMUTABLE_HASH_SET() {}
/**
* Helper constructor that allow to create a set from unboxed values
* @param array the elements that should be put into the set
@ -372,6 +379,20 @@ public class IMMUTABLE_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERI
return new SetIterator(fromElement);
}
@Override
public IMMUTABLE_HASH_SET KEY_GENERIC_TYPE copy() {
IMMUTABLE_HASH_SET KEY_GENERIC_TYPE set = new IMMUTABLE_HASH_SETBRACES();
set.containsNull = containsNull;
set.firstIndex = firstIndex;
set.lastIndex = lastIndex;
set.size = size;
set.mask = mask;
set.nullIndex = nullIndex;
set.keys = Arrays.copyOf(keys, keys.length);
set.links = Arrays.copyOf(links, links.length);
return set;
}
@Override
public COMPARATOR KEY_GENERIC_TYPE comparator() { return null; }

View File

@ -4,6 +4,7 @@ package speiger.src.collections.PACKAGE.sets;
import java.util.Comparator;
import java.util.function.Consumer;
#endif
import java.util.Arrays;
import java.util.Objects;
import java.util.Collection;
import java.util.Iterator;
@ -609,6 +610,21 @@ public class LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE extends CUSTOM_HASH_SET KEY
return new SetIterator(fromElement);
}
@Override
public LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE copy() {
LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE set = new LINKED_CUSTOM_HASH_SETBRACES(0, loadFactor, strategy);
set.minCapacity = minCapacity;
set.mask = mask;
set.maxFill = maxFill;
set.nullIndex = nullIndex;
set.containsNull = containsNull;
set.size = size;
set.keys = Arrays.copyOf(keys, keys.length);
set.firstIndex = firstIndex;
set.lastIndex = lastIndex;
return set;
}
@Override
public COMPARATOR KEY_GENERIC_TYPE comparator() { return null; }

View File

@ -3,6 +3,7 @@ package speiger.src.collections.PACKAGE.sets;
#if TYPE_OBJECT
import java.util.Comparator;
#endif
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
@ -580,6 +581,21 @@ public class LINKED_HASH_SET KEY_GENERIC_TYPE extends HASH_SET KEY_GENERIC_TYPE
return new SetIterator(fromElement);
}
@Override
public LINKED_HASH_SET KEY_GENERIC_TYPE copy() {
LINKED_HASH_SET KEY_GENERIC_TYPE set = new LINKED_HASH_SETBRACES(0, loadFactor);
set.minCapacity = minCapacity;
set.mask = mask;
set.maxFill = maxFill;
set.nullIndex = nullIndex;
set.containsNull = containsNull;
set.size = size;
set.keys = Arrays.copyOf(keys, keys.length);
set.firstIndex = firstIndex;
set.lastIndex = lastIndex;
return set;
}
@Override
public COMPARATOR KEY_GENERIC_TYPE comparator() { return null; }

View File

@ -115,6 +115,8 @@ public interface NAVIGABLE_SET KEY_GENERIC_TYPE extends NavigableSet<CLASS_TYPE>
/** @return a Type Specific desendingSet */
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE descendingSet();
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE copy();
/**
* A Type Specific Type Splititerator to reduce boxing/unboxing

View File

@ -445,6 +445,19 @@ public class CUSTOM_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_T
return new SetIterator();
}
@Override
public CUSTOM_HASH_SET KEY_GENERIC_TYPE copy() {
CUSTOM_HASH_SET KEY_GENERIC_TYPE set = new CUSTOM_HASH_SETBRACES(0, loadFactor, strategy);
set.minCapacity = minCapacity;
set.mask = mask;
set.maxFill = maxFill;
set.nullIndex = nullIndex;
set.containsNull = containsNull;
set.size = size;
set.keys = Arrays.copyOf(keys, keys.length);
return set;
}
@Override
public void clear() {
if(size == 0) return;

View File

@ -477,6 +477,19 @@ public class HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE imp
return new SetIterator();
}
@Override
public HASH_SET KEY_GENERIC_TYPE copy() {
HASH_SET KEY_GENERIC_TYPE set = new HASH_SETBRACES(0, loadFactor);
set.minCapacity = minCapacity;
set.mask = mask;
set.maxFill = maxFill;
set.nullIndex = nullIndex;
set.containsNull = containsNull;
set.size = size;
set.keys = Arrays.copyOf(keys, keys.length);
return set;
}
@Override
public void clear() {
if(size == 0) return;

View File

@ -540,6 +540,21 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
tree = null;
}
public RB_TREE_SET KEY_GENERIC_TYPE copy() {
RB_TREE_SET KEY_GENERIC_TYPE set = new RB_TREE_SETBRACES();
set.size = size;
if(tree != null) {
set.tree = tree.copy();
Entry KEY_GENERIC_TYPE lastFound = null;
for(Entry KEY_GENERIC_TYPE entry = tree;entry != null;entry = entry.left) lastFound = entry;
set.first = lastFound;
lastFound = null;
for(Entry KEY_GENERIC_TYPE entry = tree;entry != null;entry = entry.right) lastFound = entry;
set.last = lastFound;
}
return set;
}
@Override
public COMPARATOR KEY_GENERIC_TYPE comparator() { return comparator; }
@ -1070,6 +1085,9 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
return fromStart && toEnd ? set.size() : iterator().skip(Integer.MAX_VALUE);
}
@Override
public SubSet KEY_GENERIC_TYPE copy() { throw new UnsupportedOperationException(); }
@Override
public void forEach(CONSUMER KEY_SUPER_GENERIC_TYPE action) {
Objects.requireNonNull(action);
@ -1314,6 +1332,22 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
this.parent = parent;
}
Entry KEY_GENERIC_TYPE copy() {
Entry KEY_GENERIC_TYPE entry = new EntryBRACES(key, null);
entry.state = state;
if(left != null) {
Entry KEY_GENERIC_TYPE newLeft = left.copy();
entry.left = newLeft;
newLeft.parent = entry;
}
if(right != null) {
Entry KEY_GENERIC_TYPE newRight = right.copy();
entry.right = newRight;
newRight.parent = entry;
}
return entry;
}
boolean isBlack() {
return (state & BLACK) != 0;
}

View File

@ -16,6 +16,9 @@ public interface SET KEY_GENERIC_TYPE extends Set<CLASS_TYPE>, COLLECTION KEY_GE
@Override
public ITERATOR KEY_GENERIC_TYPE iterator();
@Override
public SET KEY_GENERIC_TYPE copy();
#if !TYPE_OBJECT
/**
* A Type Specific remove function to reduce boxing/unboxing

View File

@ -60,6 +60,9 @@ public interface SORTED_SET KEY_GENERIC_TYPE extends SET KEY_GENERIC_TYPE, Sorte
@Override
public COMPARATOR KEY_GENERIC_TYPE comparator();
@Override
public SORTED_SET KEY_GENERIC_TYPE copy();
@Override
public BI_ITERATOR KEY_GENERIC_TYPE iterator();
/**

View File

@ -190,6 +190,9 @@ public class SETS
}
@Override
public int size() { return 1; }
@Override
public SingletonSet KEY_GENERIC_TYPE copy() { return new SingletonSetBRACES(element); }
}
private static class EmptySet KEY_GENERIC_TYPE extends EmptyCollection KEY_GENERIC_TYPE implements SET KEY_GENERIC_TYPE
@ -198,6 +201,8 @@ public class SETS
@Override
public boolean remove(KEY_TYPE o) { throw new UnsupportedOperationException(); }
#endif
@Override
public EmptySet KEY_GENERIC_TYPE copy() { return this; }
}
#if !TYPE_BOOLEAN
@ -245,6 +250,9 @@ public class SETS
public KEY_TYPE getDefaultMinValue() { return n.getDefaultMinValue(); }
#endif
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE copy() { throw new UnsupportedOperationException(); }
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, boolean fromInclusive, KEY_TYPE toElement, boolean toInclusive) { return unmodifiable(n.subSet(fromElement, fromInclusive, toElement, toInclusive)); }
@ -300,6 +308,9 @@ public class SETS
@Override
public BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement) { return ITERATORS.unmodifiable(s.iterator(fromElement)); }
@Override
public SORTED_SET KEY_GENERIC_TYPE copy() { throw new UnsupportedOperationException(); }
@Override
public SORTED_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, KEY_TYPE toElement) { return unmodifiable(s.subSet(fromElement, toElement)); }
@ -336,6 +347,9 @@ public class SETS
s = c;
}
@Override
public SET KEY_GENERIC_TYPE copy() { throw new UnsupportedOperationException(); }
#if !TYPE_OBJECT
@Override
public boolean remove(KEY_TYPE o) { throw new UnsupportedOperationException(); }
@ -412,6 +426,9 @@ public class SETS
public KEY_TYPE getDefaultMinValue() { synchronized(mutex) { return n.getDefaultMinValue(); } }
#endif
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE copy() { throw new UnsupportedOperationException(); }
@Override
public NAVIGABLE_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, boolean fromInclusive, KEY_TYPE toElement, boolean toInclusive) { synchronized(mutex) { return synchronize(n.subSet(fromElement, fromInclusive, toElement, toInclusive), mutex); } }
@ -492,7 +509,10 @@ public class SETS
@Override
public BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement) { synchronized(mutex) { return s.iterator(fromElement); } }
@Override
public SORTED_SET KEY_GENERIC_TYPE copy() { throw new UnsupportedOperationException(); }
@Override
public SORTED_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, KEY_TYPE toElement) { synchronized(mutex) { return synchronize(s.subSet(fromElement, toElement), mutex); } }
@ -556,6 +576,9 @@ public class SETS
#endif
}
@Override
public SET KEY_GENERIC_TYPE copy() { throw new UnsupportedOperationException(); }
#if !TYPE_OBJECT
@Override
public boolean remove(KEY_TYPE o) { synchronized(mutex) { return s.remove(o); } }