From 787a154042acccf5afcaccfd78c7c8a68cc6eee1 Mon Sep 17 00:00:00 2001 From: Speiger Date: Sun, 20 Dec 2020 05:45:16 +0100 Subject: [PATCH] Fixed Bugs and Completion of Sets -Added: AVLTreeSet -Added: Sets -Changed: Boolean has all Set classes disabled (For Now) --- .../src/builder/example/GlobalVariables.java | 2 + .../src/builder/example/TestBuilder.java | 12 +- .../templates/sets/AVLTreeSet.template | 994 ++++++++++++++++++ .../templates/sets/RBTreeSet.template | 517 +++------ .../templates/utils/Iterators.template | 36 +- .../collections/templates/utils/Sets.template | 362 +++++++ 6 files changed, 1553 insertions(+), 370 deletions(-) create mode 100644 src/main/resources/speiger/assets/collections/templates/sets/AVLTreeSet.template create mode 100644 src/main/resources/speiger/assets/collections/templates/utils/Sets.template diff --git a/src/main/java/speiger/src/builder/example/GlobalVariables.java b/src/main/java/speiger/src/builder/example/GlobalVariables.java index cddcbda..f8e58b5 100644 --- a/src/main/java/speiger/src/builder/example/GlobalVariables.java +++ b/src/main/java/speiger/src/builder/example/GlobalVariables.java @@ -79,6 +79,7 @@ public class GlobalVariables addClassMapper("CUSTOM_HASH_SET", "OpenCustomHashSet"); addClassMapper("HASH_SET", "OpenHashSet"); addClassMapper("RB_TREE_SET", "RBTreeSet"); + addClassMapper("AVL_TREE_SET", "AVLTreeSet"); addClassMapper("ARRAY_SET", "ArraySet"); //Abstract Classes @@ -89,6 +90,7 @@ public class GlobalVariables //Helper Classes addClassMapper("LISTS", "Lists"); + addClassMapper("SETS", "Sets"); addClassMapper("COLLECTIONS", "Collections"); addClassMapper("ARRAYS", "Arrays"); addClassMapper("ITERATORS", "Iterators"); diff --git a/src/main/java/speiger/src/builder/example/TestBuilder.java b/src/main/java/speiger/src/builder/example/TestBuilder.java index 88dbb26..03b0ed1 100644 --- a/src/main/java/speiger/src/builder/example/TestBuilder.java +++ b/src/main/java/speiger/src/builder/example/TestBuilder.java @@ -20,7 +20,6 @@ public class TestBuilder extends TemplateProcessor public static final ClassType[] TYPE = ClassType.values(); List varibles = new ArrayList(); - public TestBuilder() { super(Paths.get("src\\main\\resources\\speiger\\assets\\collections\\templates\\"), Paths.get("src\\main\\java\\speiger\\src\\collections\\"), Paths.get("src\\main\\resources\\speiger\\assets\\collections\\")); @@ -59,6 +58,17 @@ public class TestBuilder extends TemplateProcessor blocked.put("Consumer", EnumSet.of(ClassType.OBJECT)); blocked.put("Comparator", EnumSet.of(ClassType.OBJECT)); blocked.put("Stack", EnumSet.of(ClassType.OBJECT)); + blocked.put("Sets", EnumSet.of(ClassType.BOOLEAN)); + blocked.put("ArraySet", EnumSet.of(ClassType.BOOLEAN)); + blocked.put("AVLTreeSet", EnumSet.of(ClassType.BOOLEAN)); + blocked.put("RBTreeSet", EnumSet.of(ClassType.BOOLEAN)); + blocked.put("RBTreeSet", EnumSet.of(ClassType.BOOLEAN)); + blocked.put("SortedSet", EnumSet.of(ClassType.BOOLEAN)); + blocked.put("NavigableSet", EnumSet.of(ClassType.BOOLEAN)); + blocked.put("OpenHashSet", EnumSet.of(ClassType.BOOLEAN)); + blocked.put("OpenCustomHashSet", EnumSet.of(ClassType.BOOLEAN)); + blocked.put("LinkedOpenHashSet", EnumSet.of(ClassType.BOOLEAN)); + blocked.put("LinkedOpenCustomHashSet", EnumSet.of(ClassType.BOOLEAN)); } @Override diff --git a/src/main/resources/speiger/assets/collections/templates/sets/AVLTreeSet.template b/src/main/resources/speiger/assets/collections/templates/sets/AVLTreeSet.template new file mode 100644 index 0000000..ae3daa2 --- /dev/null +++ b/src/main/resources/speiger/assets/collections/templates/sets/AVLTreeSet.template @@ -0,0 +1,994 @@ +package speiger.src.collections.PACKAGE.sets; + +import java.util.NoSuchElementException; +#if TYPE_OBJECT +import java.util.Comparator; +#endif +import speiger.src.collections.PACKAGE.collections.BI_ITERATOR; +#if !TYPE_OBJECT +import speiger.src.collections.PACKAGE.functions.COMPARATOR; +#endif +import speiger.src.collections.PACKAGE.lists.LIST_ITERATOR; + +public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE implements NAVIGABLE_SET KEY_GENERIC_TYPE +{ + protected transient Entry KEY_GENERIC_TYPE tree; + protected transient Entry KEY_GENERIC_TYPE first; + protected transient Entry KEY_GENERIC_TYPE last; + protected int size = 0; + protected transient COMPARATOR KEY_GENERIC_TYPE comparator; +#if !TYPE_OBJECT +#if TYPE_BOOLEAN + protected KEY_TYPE defaultMaxNotFound = CLASS_TYPE.FALSE; + protected KEY_TYPE defaultMinNotFound = CLASS_TYPE.TRUE; +#else + protected KEY_TYPE defaultMaxNotFound = CLASS_TYPE.MIN_VALUE; + protected KEY_TYPE defaultMinNotFound = CLASS_TYPE.MAX_VALUE; +#endif +#endif + + public AVL_TREE_SET() { + } + +#if !TYPE_OBJECT + @Override + public void setDefaultMaxValue(KEY_TYPE value) { defaultMaxNotFound = value; } + @Override + public KEY_TYPE getDefaultMaxValue() { return defaultMaxNotFound; } + @Override + public void setDefaultMinValue(KEY_TYPE value) { defaultMinNotFound = value; } + @Override + public KEY_TYPE getDefaultMinValue() { return defaultMinNotFound; } + +#endif + @Override + public boolean add(KEY_TYPE o) { + if(tree == null) { + tree = first = last = new EntryBRACES(o, null); + size++; + return true; + } + int compare = 0; + Entry KEY_GENERIC_TYPE parent = tree; + while(true) { + if((compare = compare(o, parent.key)) == 0) return false; + if(compare < 0) { + if(parent.left == null) break; + parent = parent.left; + } + else if(compare > 0) { + if(parent.right == null) break; + parent = parent.right; + } + } + Entry KEY_GENERIC_TYPE adding = new EntryBRACES(o, parent); + if(compare < 0) { + parent.left = adding; + if(parent == first) first = adding; + } + else { + parent.right = adding; + if(parent == last) last = adding; + } + fixAfterInsertion(adding); + size++; + return false; + } + + @Override + public boolean addAndMoveToFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); } + + @Override + public boolean addAndMoveToLast(KEY_TYPE o) { throw new UnsupportedOperationException(); } + + @Override + public boolean moveToFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); } + + @Override + public boolean moveToLast(KEY_TYPE o) { throw new UnsupportedOperationException(); } + + @Override + public KEY_TYPE lower(KEY_TYPE e) { + Entry KEY_GENERIC_TYPE node = findLowerNode(e); +#if TYPE_OBJECT + return node != null ? node.key : null; +#else + return node != null ? node.key : defaultMinNotFound; +#endif + } + + @Override + public KEY_TYPE floor(KEY_TYPE e) { + Entry KEY_GENERIC_TYPE node = findFloorNode(e); +#if TYPE_OBJECT + return node != null ? node.key : null; +#else + return node != null ? node.key : defaultMinNotFound; +#endif + } + + @Override + public KEY_TYPE higher(KEY_TYPE e) { + Entry KEY_GENERIC_TYPE node = findHigherNode(e); +#if TYPE_OBJECT + return node != null ? node.key : null; +#else + return node != null ? node.key : defaultMaxNotFound; +#endif + } + + @Override + public KEY_TYPE ceiling(KEY_TYPE e) { + Entry KEY_GENERIC_TYPE node = findCeilingNode(e); +#if TYPE_OBJECT + return node != null ? node.key : null; +#else + return node != null ? node.key : defaultMaxNotFound; +#endif + } + + protected Entry KEY_GENERIC_TYPE findNode(KEY_TYPE o) { + Entry KEY_GENERIC_TYPE node = tree; + int compare; + while(node != null) { + if((compare = compare(o, node.key)) == 0) return node; + if(compare < 0) node = node.left; + else node = node.right; + } + return null; + } + + protected Entry KEY_GENERIC_TYPE findLowerNode(KEY_TYPE e) { + Entry KEY_GENERIC_TYPE entry = tree; + while(entry != null) { + if(compare(e, entry.key) > 0) { + if(entry.right != null) entry = entry.right; + else return entry; + } + else { + if(entry.left != null) entry = entry.left; + else { + Entry KEY_GENERIC_TYPE parent = entry.parent; + while(parent != null && parent.left == entry) { + entry = parent; + parent = parent.parent; + } + return parent; + } + } + } + return null; + } + + protected Entry KEY_GENERIC_TYPE findFloorNode(KEY_TYPE e) { + Entry KEY_GENERIC_TYPE entry = tree; + int compare; + while(entry != null) { + if((compare = compare(e, entry.key)) > 0) { + if(entry.right == null) break; + entry = entry.right; + continue; + } + else if(compare < 0) { + if(entry.left != null) entry = entry.left; + else { + Entry KEY_GENERIC_TYPE parent = entry.parent; + while(parent != null && parent.left == entry) { + entry = parent; + parent = parent.parent; + } + return parent; + } + continue; + } + break; + } + return entry; + } + + protected Entry KEY_GENERIC_TYPE findCeilingNode(KEY_TYPE e) { + Entry KEY_GENERIC_TYPE entry = tree; + while(entry != null) { + if(compare(e, entry.key) < 0) { + if(entry.left != null) entry = entry.left; + else return entry; + } + else { + if(entry.right != null) entry = entry.right; + else { + Entry KEY_GENERIC_TYPE parent = entry.parent; + while(parent != null && parent.right == entry) { + entry = parent; + parent = parent.parent; + } + return parent; + } + } + } + return null; + } + + protected Entry KEY_GENERIC_TYPE findHigherNode(KEY_TYPE e) { + Entry KEY_GENERIC_TYPE entry = tree; + int compare; + while(entry != null) { + if((compare = compare(e, entry.key)) < 0) { + if(entry.left == null) break; + entry = entry.left; + continue; + } + else if(compare < 0) { + if(entry.right != null) entry = entry.right; + else { + Entry KEY_GENERIC_TYPE parent = entry.parent; + while(parent != null && parent.right == entry) { + entry = parent; + parent = parent.parent; + } + return parent; + } + continue; + } + break; + } + return entry; + } + +#if !TYPE_OBJECT + @Override + public boolean contains(KEY_TYPE e) { + return findNode(e) != null; + } + +#else + @Override + public boolean contains(Object e) { + return findNode((T)e) != null; + } + +#endif + @Override + public KEY_TYPE FIRST_KEY() { + if(tree == null) throw new NoSuchElementException(); + return first.key; + } + + @Override + public KEY_TYPE LAST_KEY() { + if(tree == null) throw new NoSuchElementException(); + return last.key; + } + +#if !TYPE_OBJECT + @Override + public boolean remove(KEY_TYPE o) { + if(tree == null) return false; + Entry KEY_GENERIC_TYPE entry = findNode(o); + if(entry != null) { + removeNode(entry); + return true; + } + return false; + } + +#else + @Override + public boolean remove(Object o) { + if(tree == null) return false; + Entry KEY_GENERIC_TYPE entry = findNode((T)o); + if(entry != null) { + removeNode(entry); + return true; + } + return false; + } + +#endif + @Override + public KEY_TYPE POLL_FIRST_KEY() { + if(tree == null) throw new NoSuchElementException(); + KEY_TYPE value = first.key; + removeNode(first); + return value; + } + + @Override + public KEY_TYPE POLL_LAST_KEY() { + if(tree == null) throw new NoSuchElementException(); + KEY_TYPE value = last.key; + removeNode(last); + return value; + } + + @Override + public int size() { return size; } + + @Override + public COMPARATOR KEY_GENERIC_TYPE comparator() { return comparator; } + + @Override + public BI_ITERATOR KEY_GENERIC_TYPE iterator() { return new SetIterator(false); } + + @Override + public BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement) { + Entry KEY_GENERIC_TYPE entry = findNode(fromElement); + return entry == null ? null : new SetIterator(entry); + } + + @Override + public BI_ITERATOR KEY_GENERIC_TYPE descendingIterator() { return new SetIterator(true); } + + @Override + public NAVIGABLE_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, boolean fromInclusive, KEY_TYPE toElement, boolean toInclusive) { + return new AscendingSubSetBRACES(this, false, fromElement, fromInclusive, false, toElement, toInclusive); + } + + @Override + public NAVIGABLE_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement, boolean inclusive) { + return new AscendingSubSetBRACES(this, true, EMPTY_VALUE, true, false, toElement, inclusive); + } + + @Override + public NAVIGABLE_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement, boolean inclusive) { + return new AscendingSubSetBRACES(this, false, fromElement, inclusive, true, EMPTY_VALUE, true); + } + + @Override + public NAVIGABLE_SET KEY_GENERIC_TYPE descendingSet() { + return new DescendingSubSetBRACES(this, true, EMPTY_VALUE, true, true, EMPTY_VALUE, true); + } + + protected void removeNode(Entry KEY_GENERIC_TYPE entry) { + size--; + if(entry.needsSuccessor()) { + Entry KEY_GENERIC_TYPE successor = entry.next(); + entry.key = successor.key; + entry = successor; + } + Entry KEY_GENERIC_TYPE replacement = entry.left != null ? entry.left : entry.right; + if(replacement != null) { + if(entry.replace(replacement)) tree = replacement; + if(entry == first) first = replacement; + if(entry == last) last = entry.right != null ? entry.right : replacement; + entry.left = entry.right = entry.parent = null; + fixAfterDeletion(replacement); + } + else if(entry.parent == null) tree = first = last = null; + else { + fixAfterDeletion(entry); + entry.replace(null); + if(entry.parent != null) { + Entry KEY_GENERIC_TYPE parent = entry.parent; + if(entry == first) first = parent.left != null ? parent.left : parent; + if(entry == last) last = entry.right != null ? parent.right : parent; + } + entry.parent = null; + } + } + + protected int compare(KEY_TYPE k, KEY_TYPE v) { return comparator != null ? comparator.compare(k, v) : COMPARE_TO_KEY(k, v);} + + /** From CLR */ + protected void rotateLeft(Entry KEY_GENERIC_TYPE entry) { + if(entry != null) { + Entry KEY_GENERIC_TYPE right = entry.right; + entry.right = right.left; + if(right.left != null) right.left.parent = entry; + right.parent = entry.parent; + if(entry.parent == null) tree = right; + else if(entry.parent.left == entry) entry.parent.left = right; + else entry.parent.right = right; + right.left = entry; + entry.parent = right; + } + } + + /** From CLR */ + protected void rotateRight(Entry KEY_GENERIC_TYPE entry) { + if(entry != null) { + Entry KEY_GENERIC_TYPE left = entry.left; + entry.left = left.right; + if(left.right != null) left.right.parent = entry; + left.parent = entry.parent; + if(entry.parent == null) tree = left; + else if(entry.parent.right == entry) entry.parent.right = left; + else entry.parent.left = left; + left.right = entry; + entry.parent = left; + } + } + + /** From CLR */ + protected void fixAfterInsertion(Entry KEY_GENERIC_TYPE entry) { + while(entry != null) { + entry.updateHeight(); + int balance = entry.getBalance(); + if(balance > 1) { + int compare = entry.left.getBalance(); + if(compare > 0) rotateRight(entry); + else if(compare < 0) { + rotateLeft(entry.left); + rotateRight(entry); + } + } + else if(balance < -1) { + int compare = entry.right.getBalance(); + if(compare < 0) rotateLeft(entry); + else if(compare > 0) { + rotateRight(entry.right); + rotateLeft(entry); + } + } + entry = entry.parent; + } + } + + /** From CLR */ + protected void fixAfterDeletion(Entry KEY_GENERIC_TYPE entry) { + if(entry != null) { + entry.updateHeight(); + int balance = entry.getBalance(); + if(balance > 1) { + int subBalance = entry.left.getBalance(); + if(subBalance >= 0) rotateRight(entry); + else { + rotateLeft(entry.left); + rotateRight(entry); + } + } + else if(balance < -1) + { + int subBalance = entry.right.getBalance(); + if(subBalance <= 0) rotateLeft(entry); + else { + rotateRight(entry.right); + rotateLeft(entry); + } + } + entry = entry.parent; + } + } + + private static class AscendingSubSet KEY_GENERIC_TYPE extends SubSet KEY_GENERIC_TYPE + { + AscendingSubSet(AVL_TREE_SET KEY_GENERIC_TYPE set, boolean fromStart, KEY_TYPE start, boolean loInclusive, boolean toEnd, KEY_TYPE end, boolean hiInclusive) { + super(set, fromStart, start, loInclusive, toEnd, end, hiInclusive); + } + + @Override + public NAVIGABLE_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, boolean fromInclusive, KEY_TYPE toElement, boolean toInclusive) { + if(!inRange(fromElement, fromInclusive)) throw new IllegalArgumentException("fromElement is not in Range"); + if(!inRange(toElement, toInclusive)) throw new IllegalArgumentException("toElement is not in Range"); + return new AscendingSubSetBRACES(set, false, fromElement, fromInclusive, false, toElement, toInclusive); + } + + @Override + public NAVIGABLE_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement, boolean inclusive) { + if(!inRange(toElement, inclusive)) throw new IllegalArgumentException("toElement is not in Range"); + return new AscendingSubSetBRACES(set, fromStart, start, loInclusive, false, toElement, inclusive); + } + + @Override + public NAVIGABLE_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement, boolean inclusive) { + if(!inRange(fromElement, inclusive)) throw new IllegalArgumentException("fromElement is not in Range"); + return new AscendingSubSetBRACES(set, false, fromElement, inclusive, toEnd, end, hiInclusive); + } + + @Override + public BI_ITERATOR KEY_GENERIC_TYPE iterator() { return new SubSetIterator(findLowest()); } + + @Override + public BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement) { + Entry KEY_GENERIC_TYPE entry = set.findNode(fromElement); + return entry == null || !inClosedRange(fromElement) ? null : new SubSetIterator(entry); + } + + @Override + public BI_ITERATOR KEY_GENERIC_TYPE descendingIterator() { return new SubSetIterator(findHighest()); } + + @Override + public NAVIGABLE_SET KEY_GENERIC_TYPE descendingSet() { + return new DescendingSubSetBRACES(set, fromStart, start, loInclusive, toEnd, end, hiInclusive); + } + } + + private static class DescendingSubSet KEY_GENERIC_TYPE extends SubSet KEY_GENERIC_TYPE + { + COMPARATOR KEY_GENERIC_TYPE comparator; + DescendingSubSet(AVL_TREE_SET KEY_GENERIC_TYPE set, boolean fromStart, KEY_TYPE start, boolean loInclusive, boolean toEnd, KEY_TYPE end, boolean hiInclusive) { + super(set, fromStart, start, loInclusive, toEnd, end, hiInclusive); + comparator = set.comparator().reversed(); + } + + @Override + public COMPARATOR KEY_GENERIC_TYPE comparator() { return comparator; } + + @Override + public NAVIGABLE_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, boolean fromInclusive, KEY_TYPE toElement, boolean toInclusive) { + if(!inRange(fromElement, fromInclusive)) throw new IllegalArgumentException("fromElement is not in Range"); + if(!inRange(toElement, toInclusive)) throw new IllegalArgumentException("toElement is not in Range"); + return new DescendingSubSetBRACES(set, false, toElement, toInclusive, false, fromElement, fromInclusive); + } + + @Override + public NAVIGABLE_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement, boolean inclusive) { + if(!inRange(toElement, inclusive)) throw new IllegalArgumentException("toElement is not in Range"); + return new DescendingSubSetBRACES(set, false, toElement, inclusive, toEnd, end, hiInclusive); + } + + @Override + public NAVIGABLE_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement, boolean inclusive) { + if(!inRange(fromElement, inclusive)) throw new IllegalArgumentException("fromElement is not in Range"); + return new DescendingSubSetBRACES(set, fromStart, start, loInclusive, false, fromElement, inclusive); + } + + @Override + public KEY_TYPE FIRST_KEY() { return super.LAST_KEY(); } + + @Override + public KEY_TYPE POLL_FIRST_KEY() { return super.POLL_LAST_KEY(); } + + @Override + public KEY_TYPE LAST_KEY() { return super.FIRST_KEY(); } + + @Override + public KEY_TYPE POLL_LAST_KEY() { return super.POLL_FIRST_KEY(); } + + @Override + public KEY_TYPE lower(KEY_TYPE e) { return super.higher(e); } + + @Override + public KEY_TYPE floor(KEY_TYPE e) { return super.ceiling(e); } + + @Override + public KEY_TYPE ceiling(KEY_TYPE e) { return super.floor(e); } + + @Override + public KEY_TYPE higher(KEY_TYPE e) { return super.lower(e); } + + @Override + public BI_ITERATOR KEY_GENERIC_TYPE iterator() { return new DescendingSubIterator(findHighest()); } + + @Override + public BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement) { + Entry KEY_GENERIC_TYPE entry = set.findNode(fromElement); + return entry == null || !inClosedRange(fromElement) ? null : new DescendingSubIterator(entry); + } + + @Override + public BI_ITERATOR KEY_GENERIC_TYPE descendingIterator() { return new DescendingSubIterator(findLowest()); } + + @Override + public NAVIGABLE_SET KEY_GENERIC_TYPE descendingSet() { + return new AscendingSubSetBRACES(set, fromStart, start, loInclusive, toEnd, end, hiInclusive); + } + + private class DescendingSubIterator extends SubSetIterator + { + public DescendingSubIterator(Entry KEY_GENERIC_TYPE entry) { + super(entry); + } + + @Override + protected void updateNext() { + next = current.previous(); + if(!toEnd && next != null && bottomReached(next)) next = null; + } + + @Override + protected void updatePrevious() { + previous = current.next(); + if(!fromStart && previous != null && topReached(previous)) previous = null; + } + } + } + + private static abstract class SubSet KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE implements NAVIGABLE_SET KEY_GENERIC_TYPE + { + AVL_TREE_SET KEY_GENERIC_TYPE set; + KEY_TYPE start; + KEY_TYPE end; + boolean fromStart; + boolean toEnd; + boolean loInclusive; + boolean hiInclusive; + + SubSet(AVL_TREE_SET KEY_GENERIC_TYPE set, boolean fromStart, KEY_TYPE start, boolean loInclusive, boolean toEnd, KEY_TYPE end, boolean hiInclusive) { + this.set = set; + this.start = start; + this.end = end; + this.fromStart = fromStart; + this.toEnd = toEnd; + this.loInclusive = loInclusive; + this.hiInclusive = hiInclusive; + } + +#if !TYPE_OBJECT + @Override + public void setDefaultMaxValue(KEY_TYPE value) { throw new UnsupportedOperationException(); } + + @Override + public KEY_TYPE getDefaultMaxValue() { return set.getDefaultMaxValue(); } + + @Override + public void setDefaultMinValue(KEY_TYPE value) { throw new UnsupportedOperationException(); } + + @Override + public KEY_TYPE getDefaultMinValue() { return set.getDefaultMinValue(); } + +#else + public KEY_TYPE getDefaultMaxValue() { return null; } + + public KEY_TYPE getDefaultMinValue() { return null; } + +#endif + boolean tooLow(KEY_TYPE key) { return !fromStart && (loInclusive ? set.compare(key, start) < 0 : set.compare(key, start) <= 0); } + boolean tooHigh(KEY_TYPE key) { return !toEnd && (hiInclusive ? set.compare(key, end) > 0 : set.compare(key, end) >= 0); } + boolean inRange(KEY_TYPE key) { return !tooLow(key) && !tooHigh(key); } + boolean inClosedRange(KEY_TYPE key) { return (fromStart || set.compare(key, start) >= 0) && (toEnd || set.compare(end, key) >= 0); } + boolean inRange(KEY_TYPE key, boolean inclusive) { return inclusive ? inRange(key) : inClosedRange(key); } + + @Override + public boolean addAndMoveToFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); } + + @Override + public boolean addAndMoveToLast(KEY_TYPE o) { throw new UnsupportedOperationException(); } + + @Override + public boolean moveToFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); } + + @Override + public boolean moveToLast(KEY_TYPE o) { throw new UnsupportedOperationException(); } + + @Override + public COMPARATOR KEY_GENERIC_TYPE comparator() { return set.comparator(); } + + @Override + public KEY_TYPE FIRST_KEY() { + Entry KEY_GENERIC_TYPE entry = findLowest(); + return entry == null ? getDefaultMaxValue() : entry.key; + } + + protected Entry KEY_GENERIC_TYPE findLowest() { + if(fromStart) return set.first; + Entry KEY_GENERIC_TYPE entry = loInclusive ? set.findCeilingNode(start) : set.findHigherNode(start); + return entry == null || tooHigh(entry.key) ? null : entry; + } + + @Override + public KEY_TYPE POLL_FIRST_KEY() { + if(fromStart) return set.POLL_FIRST_KEY(); + Entry KEY_GENERIC_TYPE entry = loInclusive ? set.findCeilingNode(start) : set.findHigherNode(start); + if(entry != null && !tooHigh(entry.key)) { + KEY_TYPE value = entry.key; + set.removeNode(entry); + return value; + } + return getDefaultMaxValue(); + } + + @Override + public KEY_TYPE LAST_KEY() { + Entry KEY_GENERIC_TYPE entry = findHighest(); + return entry == null ? getDefaultMinValue() : entry.key; + } + + protected Entry KEY_GENERIC_TYPE findHighest() { + if(toEnd) return set.last; + Entry KEY_GENERIC_TYPE entry = hiInclusive ? set.findFloorNode(end) : set.findLowerNode(end); + return entry == null || tooLow(entry.key) ? null : entry; + } + + @Override + public KEY_TYPE POLL_LAST_KEY() { + if(toEnd) return set.POLL_LAST_KEY(); + Entry KEY_GENERIC_TYPE entry = hiInclusive ? set.findFloorNode(end) : set.findLowerNode(end); + if(entry != null && !tooLow(entry.key)) { + KEY_TYPE value = entry.key; + set.removeNode(entry); + return value; + } + return getDefaultMinValue(); + } + + @Override + public boolean add(KEY_TYPE o) { + if(!inRange(o)) throw new IllegalArgumentException("Key is out of range"); + return set.add(o); + } + +#if !TYPE_OBJECT + @Override + public boolean contains(KEY_TYPE e) { + return inRange(e) && set.contains(e); + } + + @Override + public boolean remove(KEY_TYPE o) { + return inRange(o) && set.remove(o); + } + +#else + @Override + public boolean contains(Object e) { + return inRange((T)e) && set.contains((T)e); + } + + @Override + public boolean remove(Object o) { + return inRange((T)o) && set.remove((T)o); + } + +#endif + @Override + public KEY_TYPE lower(KEY_TYPE e) { + if(tooHigh(e)) { + Entry KEY_GENERIC_TYPE entry = findHighest(); + return entry == null ? getDefaultMinValue() : entry.key; + } + Entry KEY_GENERIC_TYPE entry = set.findLowerNode(e); + return entry == null || tooHigh(entry.key) ? getDefaultMaxValue() : entry.key; + } + + @Override + public KEY_TYPE floor(KEY_TYPE e) { + if(tooHigh(e)) { + Entry KEY_GENERIC_TYPE entry = findHighest(); + return entry == null ? getDefaultMinValue() : entry.key; + } + Entry KEY_GENERIC_TYPE entry = set.findFloorNode(e); + return entry == null || tooHigh(entry.key) ? getDefaultMaxValue() : entry.key; + } + + @Override + public KEY_TYPE ceiling(KEY_TYPE e) { + if(tooLow(e)) { + Entry KEY_GENERIC_TYPE entry = findLowest(); + return entry == null ? getDefaultMaxValue() : entry.key; + } + Entry KEY_GENERIC_TYPE entry = set.findCeilingNode(e); + return entry == null || tooLow(entry.key) ? getDefaultMinValue() : entry.key; + } + + @Override + public KEY_TYPE higher(KEY_TYPE e) { + if(tooLow(e)) { + Entry KEY_GENERIC_TYPE entry = findLowest(); + return entry == null ? getDefaultMaxValue() : entry.key; + } + Entry KEY_GENERIC_TYPE entry = set.findHigherNode(e); + return entry == null || tooLow(entry.key) ? getDefaultMinValue() : entry.key; + } + + @Override + public int size() { + return fromStart && toEnd ? set.size() : iterator().skip(Integer.MAX_VALUE); + } + + class SubSetIterator implements LIST_ITERATOR KEY_GENERIC_TYPE + { + Entry KEY_GENERIC_TYPE previous; + Entry KEY_GENERIC_TYPE next; + Entry KEY_GENERIC_TYPE current; + int index = 0; + + public SubSetIterator(Entry KEY_GENERIC_TYPE entry) { + next = entry; + previous = entry == null ? null : entry.previous(); + } + + @Override + public boolean hasNext() { + return next != null; + } + + @Override + public boolean hasPrevious() { + return previous != null; + } + + @Override + public int nextIndex() { + return index; + } + + @Override + public int previousIndex() { + return index - 1; + } + + boolean topReached(Entry KEY_GENERIC_TYPE entry) { + return hiInclusive ? set.compare(entry.key, end) >= 0 : set.compare(entry.key, end) > 0; + } + + boolean bottomReached(Entry KEY_GENERIC_TYPE entry) { + return loInclusive ? set.compare(entry.key, start) <= 0 : set.compare(entry.key, start) < 0; + } + + protected void updateNext() { + next = current.next(); + if(!toEnd && next != null && topReached(next)) next = null; + } + + protected void updatePrevious() { + previous = current.previous(); + if(!fromStart && previous != null && bottomReached(previous)) previous = null; + } + + @Override + public KEY_TYPE NEXT() { + if(!hasNext()) throw new NoSuchElementException(); + current = previous = next; + updateNext(); + index++; + return current.key; + } + + @Override + public KEY_TYPE PREVIOUS() { + if(!hasPrevious()) throw new NoSuchElementException(); + current = next = previous; + updatePrevious(); + index--; + return current.key; + } + + @Override + public void remove() { + if(current == null) throw new IllegalStateException(); + if(current == previous) index--; + updateNext(); + updatePrevious(); + set.removeNode(current); + current = null; + } + + @Override + public void set(KEY_TYPE e){ throw new UnsupportedOperationException(); } + + @Override + public void add(KEY_TYPE e) { throw new UnsupportedOperationException(); } + } + } + + private class SetIterator implements LIST_ITERATOR KEY_GENERIC_TYPE + { + Entry KEY_GENERIC_TYPE previous; + Entry KEY_GENERIC_TYPE next; + Entry KEY_GENERIC_TYPE current; + int index = 0; + + public SetIterator(boolean descending) { + if(descending) previous = last; + else next = first; + } + + public SetIterator(Entry KEY_GENERIC_TYPE entry) { + next = entry; + previous = entry.previous(); + } + + @Override + public boolean hasNext() { + return next != null; + } + + @Override + public boolean hasPrevious() { + return previous != null; + } + + @Override + public int nextIndex() { + return index; + } + + @Override + public int previousIndex() { + return index - 1; + } + + protected void updateNext() { + next = current.next(); + } + + protected void updatePrevious() { + previous = current.previous(); + } + + @Override + public KEY_TYPE NEXT() { + if(!hasNext()) throw new NoSuchElementException(); + current = previous = next; + updateNext(); + index++; + return current.key; + } + + @Override + public KEY_TYPE PREVIOUS() { + if(!hasPrevious()) throw new NoSuchElementException(); + current = next = previous; + updatePrevious(); + index--; + return current.key; + } + + @Override + public void remove() { + if(current == null) throw new IllegalStateException(); + if(current == previous) index--; + updateNext(); + updatePrevious(); + removeNode(current); + current = null; + } + + @Override + public void set(KEY_TYPE e){ throw new UnsupportedOperationException(); } + + @Override + public void add(KEY_TYPE e) { throw new UnsupportedOperationException(); } + } + + private static final class Entry KEY_GENERIC_TYPE + { + KEY_TYPE key; + int state; + Entry KEY_GENERIC_TYPE parent; + Entry KEY_GENERIC_TYPE left; + Entry KEY_GENERIC_TYPE right; + + Entry(KEY_TYPE key, Entry KEY_GENERIC_TYPE parent) { + this.key = key; + this.parent = parent; + } + + int getHeight() { return state; } + + void updateHeight() { state = (1 + Math.max(left == null ? 0 : left.getHeight(), right == null ? 0 : right.getHeight())); } + + int getBalance() { return (left == null ? 0 : left.getHeight()) - (right == null ? 0 : right.getBalance()); } + + boolean needsSuccessor() { return left != null && right != null; } + + boolean replace(Entry KEY_GENERIC_TYPE entry) { + if(entry != null) entry.parent = parent; + if(parent != null) { + if(parent.left == this) parent.left = entry; + else parent.right = entry; + } + return parent == null; + } + + Entry KEY_GENERIC_TYPE next() { + if(right != null) { + Entry KEY_GENERIC_TYPE parent = right; + while(parent.left != null) parent = parent.left; + return parent; + } + Entry KEY_GENERIC_TYPE parent = this.parent; + Entry KEY_GENERIC_TYPE control = this; + while(parent != null && control == parent.right) { + control = parent; + parent = parent.parent; + } + return parent; + } + + Entry KEY_GENERIC_TYPE previous() { + if(left != null) { + Entry KEY_GENERIC_TYPE parent = left; + while(parent.right != null) parent = parent.right; + return parent; + } + Entry KEY_GENERIC_TYPE parent = this.parent; + Entry KEY_GENERIC_TYPE control = this; + while(parent != null && control == parent.left) { + control = parent; + parent = parent.parent; + } + return parent; + } + } +} diff --git a/src/main/resources/speiger/assets/collections/templates/sets/RBTreeSet.template b/src/main/resources/speiger/assets/collections/templates/sets/RBTreeSet.template index d563f3e..1bcc45d 100644 --- a/src/main/resources/speiger/assets/collections/templates/sets/RBTreeSet.template +++ b/src/main/resources/speiger/assets/collections/templates/sets/RBTreeSet.template @@ -27,70 +27,46 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE #endif #endif - public RB_TREE_SET() - { + public RB_TREE_SET() { } #if !TYPE_OBJECT @Override - public void setDefaultMaxValue(KEY_TYPE value) - { - defaultMaxNotFound = value; - } - + public void setDefaultMaxValue(KEY_TYPE value) { defaultMaxNotFound = value; } @Override - public KEY_TYPE getDefaultMaxValue() - { - return defaultMaxNotFound; - } - + public KEY_TYPE getDefaultMaxValue() { return defaultMaxNotFound; } @Override - public void setDefaultMinValue(KEY_TYPE value) - { - defaultMinNotFound = value; - } - + public void setDefaultMinValue(KEY_TYPE value) { defaultMinNotFound = value; } @Override - public KEY_TYPE getDefaultMinValue() - { - return defaultMinNotFound; - } + public KEY_TYPE getDefaultMinValue() { return defaultMinNotFound; } #endif @Override - public boolean add(KEY_TYPE o) - { - if(tree == null) - { + public boolean add(KEY_TYPE o) { + if(tree == null) { tree = first = last = new EntryBRACES(o, null); size++; return true; } int compare = 0; Entry KEY_GENERIC_TYPE parent = tree; - while(true) - { + while(true) { if((compare = compare(o, parent.key)) == 0) return false; - if(compare < 0) - { + if(compare < 0) { if(parent.left == null) break; parent = parent.left; } - else if(compare > 0) - { + else if(compare > 0) { if(parent.right == null) break; parent = parent.right; } - } Entry KEY_GENERIC_TYPE adding = new EntryBRACES(o, parent); - if(compare < 0) - { + if(compare < 0) { parent.left = adding; if(parent == first) first = adding; } - else - { + else { parent.right = adding; if(parent == last) last = adding; } @@ -100,32 +76,19 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE } @Override - public boolean addAndMoveToFirst(KEY_TYPE o) - { - throw new UnsupportedOperationException(); - } + public boolean addAndMoveToFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); } @Override - public boolean addAndMoveToLast(KEY_TYPE o) - { - throw new UnsupportedOperationException(); - } + public boolean addAndMoveToLast(KEY_TYPE o) { throw new UnsupportedOperationException(); } @Override - public boolean moveToFirst(KEY_TYPE o) - { - throw new UnsupportedOperationException(); - } + public boolean moveToFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); } @Override - public boolean moveToLast(KEY_TYPE o) - { - throw new UnsupportedOperationException(); - } + public boolean moveToLast(KEY_TYPE o) { throw new UnsupportedOperationException(); } @Override - public KEY_TYPE lower(KEY_TYPE e) - { + public KEY_TYPE lower(KEY_TYPE e) { Entry KEY_GENERIC_TYPE node = findLowerNode(e); #if TYPE_OBJECT return node != null ? node.key : null; @@ -135,8 +98,7 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE } @Override - public KEY_TYPE floor(KEY_TYPE e) - { + public KEY_TYPE floor(KEY_TYPE e) { Entry KEY_GENERIC_TYPE node = findFloorNode(e); #if TYPE_OBJECT return node != null ? node.key : null; @@ -146,8 +108,7 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE } @Override - public KEY_TYPE higher(KEY_TYPE e) - { + public KEY_TYPE higher(KEY_TYPE e) { Entry KEY_GENERIC_TYPE node = findHigherNode(e); #if TYPE_OBJECT return node != null ? node.key : null; @@ -157,8 +118,7 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE } @Override - public KEY_TYPE ceiling(KEY_TYPE e) - { + public KEY_TYPE ceiling(KEY_TYPE e) { Entry KEY_GENERIC_TYPE node = findCeilingNode(e); #if TYPE_OBJECT return node != null ? node.key : null; @@ -167,12 +127,10 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE #endif } - protected Entry KEY_GENERIC_TYPE findNode(KEY_TYPE o) - { + protected Entry KEY_GENERIC_TYPE findNode(KEY_TYPE o) { Entry KEY_GENERIC_TYPE node = tree; int compare; - while(node != null) - { + while(node != null) { if((compare = compare(o, node.key)) == 0) return node; if(compare < 0) node = node.left; else node = node.right; @@ -250,8 +208,7 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE return null; } - protected Entry KEY_GENERIC_TYPE findHigherNode(KEY_TYPE e) - { + protected Entry KEY_GENERIC_TYPE findHigherNode(KEY_TYPE e) { Entry KEY_GENERIC_TYPE entry = tree; int compare; while(entry != null) { @@ -279,42 +236,35 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE #if !TYPE_OBJECT @Override - public boolean contains(KEY_TYPE e) - { + public boolean contains(KEY_TYPE e) { return findNode(e) != null; } #else @Override - public boolean contains(Object e) - { + public boolean contains(Object e) { return findNode((T)e) != null; } #endif @Override - public KEY_TYPE FIRST_KEY() - { + public KEY_TYPE FIRST_KEY() { if(tree == null) throw new NoSuchElementException(); return first.key; } @Override - public KEY_TYPE LAST_KEY() - { + public KEY_TYPE LAST_KEY() { if(tree == null) throw new NoSuchElementException(); return last.key; } #if !TYPE_OBJECT @Override - public boolean remove(KEY_TYPE o) - { - if(tree == null) - return false; + public boolean remove(KEY_TYPE o) { + if(tree == null) return false; Entry KEY_GENERIC_TYPE entry = findNode(o); - if(entry != null) - { + if(entry != null) { removeNode(entry); return true; } @@ -323,13 +273,10 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE #else @Override - public boolean remove(Object o) - { - if(tree == null) - return false; + public boolean remove(Object o) { + if(tree == null) return false; Entry KEY_GENERIC_TYPE entry = findNode((T)o); - if(entry != null) - { + if(entry != null) { removeNode(entry); return true; } @@ -338,8 +285,7 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE #endif @Override - public KEY_TYPE POLL_FIRST_KEY() - { + public KEY_TYPE POLL_FIRST_KEY() { if(tree == null) throw new NoSuchElementException(); KEY_TYPE value = first.key; removeNode(first); @@ -347,8 +293,7 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE } @Override - public KEY_TYPE POLL_LAST_KEY() - { + public KEY_TYPE POLL_LAST_KEY() { if(tree == null) throw new NoSuchElementException(); KEY_TYPE value = last.key; removeNode(last); @@ -356,96 +301,67 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE } @Override - public int size() - { - return size; - } + public int size() { return size; } @Override - public COMPARATOR KEY_GENERIC_TYPE comparator() - { - return comparator; - } + public COMPARATOR KEY_GENERIC_TYPE comparator() { return comparator; } @Override - public BI_ITERATOR KEY_GENERIC_TYPE iterator() - { - return new SetIterator(false); - } + public BI_ITERATOR KEY_GENERIC_TYPE iterator() { return new SetIterator(false); } @Override - public BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement) - { + public BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement) { Entry KEY_GENERIC_TYPE entry = findNode(fromElement); return entry == null ? null : new SetIterator(entry); } @Override - public BI_ITERATOR KEY_GENERIC_TYPE descendingIterator() - { - return new SetIterator(true); - } + public BI_ITERATOR KEY_GENERIC_TYPE descendingIterator() { return new SetIterator(true); } @Override - public NAVIGABLE_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, boolean fromInclusive, KEY_TYPE toElement, boolean toInclusive) - { + public NAVIGABLE_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, boolean fromInclusive, KEY_TYPE toElement, boolean toInclusive) { return new AscendingSubSetBRACES(this, false, fromElement, fromInclusive, false, toElement, toInclusive); } @Override - public NAVIGABLE_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement, boolean inclusive) - { + public NAVIGABLE_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement, boolean inclusive) { return new AscendingSubSetBRACES(this, true, EMPTY_VALUE, true, false, toElement, inclusive); } @Override - public NAVIGABLE_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement, boolean inclusive) - { + public NAVIGABLE_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement, boolean inclusive) { return new AscendingSubSetBRACES(this, false, fromElement, inclusive, true, EMPTY_VALUE, true); } @Override - public NAVIGABLE_SET KEY_GENERIC_TYPE descendingSet() - { + public NAVIGABLE_SET KEY_GENERIC_TYPE descendingSet() { return new DescendingSubSetBRACES(this, true, EMPTY_VALUE, true, true, EMPTY_VALUE, true); } - protected void removeNode(Entry KEY_GENERIC_TYPE entry) - { + protected void removeNode(Entry KEY_GENERIC_TYPE entry) { size--; - if(entry.needsSuccessor()) - { + if(entry.needsSuccessor()) { Entry KEY_GENERIC_TYPE successor = entry.next(); entry.key = successor.key; entry = successor; } Entry KEY_GENERIC_TYPE replacement = entry.left != null ? entry.left : entry.right; - if(replacement != null) - { - if(entry.replace(replacement)) - tree = replacement; - if(entry == first) - first = replacement; - if(entry == last) - last = entry.right != null ? entry.right : replacement; - + if(replacement != null) { + if(entry.replace(replacement)) tree = replacement; + if(entry == first) first = replacement; + if(entry == last) last = entry.right != null ? entry.right : replacement; entry.left = entry.right = entry.parent = null; if(entry.isBlack()) fixAfterDeletion(replacement); } - else if(entry.parent == null) - tree = first = last = null; - else - { + else if(entry.parent == null) tree = first = last = null; + else { if(entry.isBlack()) fixAfterDeletion(entry); entry.replace(null); - if(entry.parent != null) - { + if(entry.parent != null) { Entry KEY_GENERIC_TYPE parent = entry.parent; - if(entry == first) - first = parent.left != null ? parent.left : parent; - if(entry == last) - last = entry.right != null ? parent.right : parent; + if(entry == first) first = parent.left != null ? parent.left : parent; + if(entry == last) last = entry.right != null ? parent.right : parent; } entry.parent = null; } @@ -561,7 +477,7 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE entry = tree; } } - else { // symmetric + else { Entry KEY_GENERIC_TYPE sib = leftOf(parentOf(entry)); if(!isBlack(sib)) { setBlack(sib, true); @@ -593,55 +509,43 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE private static class AscendingSubSet KEY_GENERIC_TYPE extends SubSet KEY_GENERIC_TYPE { - AscendingSubSet(RB_TREE_SET KEY_GENERIC_TYPE set, boolean fromStart, KEY_TYPE start, boolean loInclusive, boolean toEnd, KEY_TYPE end, boolean hiInclusive) - { + AscendingSubSet(RB_TREE_SET KEY_GENERIC_TYPE set, boolean fromStart, KEY_TYPE start, boolean loInclusive, boolean toEnd, KEY_TYPE end, boolean hiInclusive) { super(set, fromStart, start, loInclusive, toEnd, end, hiInclusive); } @Override - public NAVIGABLE_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, boolean fromInclusive, KEY_TYPE toElement, boolean toInclusive) - { + public NAVIGABLE_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, boolean fromInclusive, KEY_TYPE toElement, boolean toInclusive) { if(!inRange(fromElement, fromInclusive)) throw new IllegalArgumentException("fromElement is not in Range"); if(!inRange(toElement, toInclusive)) throw new IllegalArgumentException("toElement is not in Range"); return new AscendingSubSetBRACES(set, false, fromElement, fromInclusive, false, toElement, toInclusive); } @Override - public NAVIGABLE_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement, boolean inclusive) - { + public NAVIGABLE_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement, boolean inclusive) { if(!inRange(toElement, inclusive)) throw new IllegalArgumentException("toElement is not in Range"); return new AscendingSubSetBRACES(set, fromStart, start, loInclusive, false, toElement, inclusive); } @Override - public NAVIGABLE_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement, boolean inclusive) - { + public NAVIGABLE_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement, boolean inclusive) { if(!inRange(fromElement, inclusive)) throw new IllegalArgumentException("fromElement is not in Range"); return new AscendingSubSetBRACES(set, false, fromElement, inclusive, toEnd, end, hiInclusive); } @Override - public BI_ITERATOR KEY_GENERIC_TYPE iterator() - { - return new SubSetIterator(findLowest()); - } + public BI_ITERATOR KEY_GENERIC_TYPE iterator() { return new SubSetIterator(findLowest()); } @Override - public BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement) - { + public BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement) { Entry KEY_GENERIC_TYPE entry = set.findNode(fromElement); return entry == null || !inClosedRange(fromElement) ? null : new SubSetIterator(entry); } @Override - public BI_ITERATOR KEY_GENERIC_TYPE descendingIterator() - { - return new SubSetIterator(findHighest()); - } + public BI_ITERATOR KEY_GENERIC_TYPE descendingIterator() { return new SubSetIterator(findHighest()); } @Override - public NAVIGABLE_SET KEY_GENERIC_TYPE descendingSet() - { + public NAVIGABLE_SET KEY_GENERIC_TYPE descendingSet() { return new DescendingSubSetBRACES(set, fromStart, start, loInclusive, toEnd, end, hiInclusive); } } @@ -649,130 +553,88 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE private static class DescendingSubSet KEY_GENERIC_TYPE extends SubSet KEY_GENERIC_TYPE { COMPARATOR KEY_GENERIC_TYPE comparator; - DescendingSubSet(RB_TREE_SET KEY_GENERIC_TYPE set, boolean fromStart, KEY_TYPE start, boolean loInclusive, boolean toEnd, KEY_TYPE end, boolean hiInclusive) - { + DescendingSubSet(RB_TREE_SET KEY_GENERIC_TYPE set, boolean fromStart, KEY_TYPE start, boolean loInclusive, boolean toEnd, KEY_TYPE end, boolean hiInclusive) { super(set, fromStart, start, loInclusive, toEnd, end, hiInclusive); comparator = set.comparator().reversed(); } @Override - public COMPARATOR KEY_GENERIC_TYPE comparator() - { - return comparator; - } + public COMPARATOR KEY_GENERIC_TYPE comparator() { return comparator; } @Override - public NAVIGABLE_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, boolean fromInclusive, KEY_TYPE toElement, boolean toInclusive) - { + public NAVIGABLE_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, boolean fromInclusive, KEY_TYPE toElement, boolean toInclusive) { if(!inRange(fromElement, fromInclusive)) throw new IllegalArgumentException("fromElement is not in Range"); if(!inRange(toElement, toInclusive)) throw new IllegalArgumentException("toElement is not in Range"); return new DescendingSubSetBRACES(set, false, toElement, toInclusive, false, fromElement, fromInclusive); } @Override - public NAVIGABLE_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement, boolean inclusive) - { + public NAVIGABLE_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement, boolean inclusive) { if(!inRange(toElement, inclusive)) throw new IllegalArgumentException("toElement is not in Range"); return new DescendingSubSetBRACES(set, false, toElement, inclusive, toEnd, end, hiInclusive); } @Override - public NAVIGABLE_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement, boolean inclusive) - { + public NAVIGABLE_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement, boolean inclusive) { if(!inRange(fromElement, inclusive)) throw new IllegalArgumentException("fromElement is not in Range"); return new DescendingSubSetBRACES(set, fromStart, start, loInclusive, false, fromElement, inclusive); } @Override - public KEY_TYPE FIRST_KEY() - { - return super.LAST_KEY(); - } + public KEY_TYPE FIRST_KEY() { return super.LAST_KEY(); } @Override - public KEY_TYPE POLL_FIRST_KEY() - { - return super.POLL_LAST_KEY(); - } + public KEY_TYPE POLL_FIRST_KEY() { return super.POLL_LAST_KEY(); } @Override - public KEY_TYPE LAST_KEY() - { - return super.FIRST_KEY(); - } + public KEY_TYPE LAST_KEY() { return super.FIRST_KEY(); } @Override - public KEY_TYPE POLL_LAST_KEY() - { - return super.POLL_FIRST_KEY(); - } + public KEY_TYPE POLL_LAST_KEY() { return super.POLL_FIRST_KEY(); } @Override - public KEY_TYPE lower(KEY_TYPE e) - { - return super.higher(e); - } + public KEY_TYPE lower(KEY_TYPE e) { return super.higher(e); } @Override - public KEY_TYPE floor(KEY_TYPE e) - { - return super.ceiling(e); - } + public KEY_TYPE floor(KEY_TYPE e) { return super.ceiling(e); } @Override - public KEY_TYPE ceiling(KEY_TYPE e) - { - return super.floor(e); - } + public KEY_TYPE ceiling(KEY_TYPE e) { return super.floor(e); } @Override - public KEY_TYPE higher(KEY_TYPE e) - { - return super.lower(e); - } + public KEY_TYPE higher(KEY_TYPE e) { return super.lower(e); } @Override - public BI_ITERATOR KEY_GENERIC_TYPE iterator() - { - return new DescendingSubIterator(findHighest()); - } + public BI_ITERATOR KEY_GENERIC_TYPE iterator() { return new DescendingSubIterator(findHighest()); } @Override - public BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement) - { + public BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement) { Entry KEY_GENERIC_TYPE entry = set.findNode(fromElement); return entry == null || !inClosedRange(fromElement) ? null : new DescendingSubIterator(entry); } @Override - public BI_ITERATOR KEY_GENERIC_TYPE descendingIterator() - { - return new DescendingSubIterator(findLowest()); - } + public BI_ITERATOR KEY_GENERIC_TYPE descendingIterator() { return new DescendingSubIterator(findLowest()); } @Override - public NAVIGABLE_SET KEY_GENERIC_TYPE descendingSet() - { + public NAVIGABLE_SET KEY_GENERIC_TYPE descendingSet() { return new AscendingSubSetBRACES(set, fromStart, start, loInclusive, toEnd, end, hiInclusive); } private class DescendingSubIterator extends SubSetIterator { - public DescendingSubIterator(Entry KEY_GENERIC_TYPE entry) - { + public DescendingSubIterator(Entry KEY_GENERIC_TYPE entry) { super(entry); } @Override - protected void updateNext() - { + protected void updateNext() { next = current.previous(); if(!toEnd && next != null && bottomReached(next)) next = null; } @Override - protected void updatePrevious() - { + protected void updatePrevious() { previous = current.next(); if(!fromStart && previous != null && topReached(previous)) previous = null; } @@ -789,8 +651,7 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE boolean loInclusive; boolean hiInclusive; - SubSet(RB_TREE_SET KEY_GENERIC_TYPE set, boolean fromStart, KEY_TYPE start, boolean loInclusive, boolean toEnd, KEY_TYPE end, boolean hiInclusive) - { + SubSet(RB_TREE_SET KEY_GENERIC_TYPE set, boolean fromStart, KEY_TYPE start, boolean loInclusive, boolean toEnd, KEY_TYPE end, boolean hiInclusive) { this.set = set; this.start = start; this.end = end; @@ -819,30 +680,11 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE public KEY_TYPE getDefaultMinValue() { return null; } #endif - boolean tooLow(KEY_TYPE key) - { - return !fromStart && (loInclusive ? set.compare(key, start) < 0 : set.compare(key, start) <= 0); - } - - boolean tooHigh(KEY_TYPE key) - { - return !toEnd && (hiInclusive ? set.compare(key, end) > 0 : set.compare(key, end) >= 0); - } - - boolean inRange(KEY_TYPE key) - { - return !tooLow(key) && !tooHigh(key); - } - - boolean inClosedRange(KEY_TYPE key) - { - return (fromStart || set.compare(key, start) >= 0) && (toEnd || set.compare(end, key) >= 0); - } - - boolean inRange(KEY_TYPE key, boolean inclusive) - { - return inclusive ? inRange(key) : inClosedRange(key); - } + boolean tooLow(KEY_TYPE key) { return !fromStart && (loInclusive ? set.compare(key, start) < 0 : set.compare(key, start) <= 0); } + boolean tooHigh(KEY_TYPE key) { return !toEnd && (hiInclusive ? set.compare(key, end) > 0 : set.compare(key, end) >= 0); } + boolean inRange(KEY_TYPE key) { return !tooLow(key) && !tooHigh(key); } + boolean inClosedRange(KEY_TYPE key) { return (fromStart || set.compare(key, start) >= 0) && (toEnd || set.compare(end, key) >= 0); } + boolean inRange(KEY_TYPE key, boolean inclusive) { return inclusive ? inRange(key) : inClosedRange(key); } @Override public boolean addAndMoveToFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); } @@ -860,26 +702,22 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE public COMPARATOR KEY_GENERIC_TYPE comparator() { return set.comparator(); } @Override - public KEY_TYPE FIRST_KEY() - { + public KEY_TYPE FIRST_KEY() { Entry KEY_GENERIC_TYPE entry = findLowest(); return entry == null ? getDefaultMaxValue() : entry.key; } - protected Entry KEY_GENERIC_TYPE findLowest() - { + protected Entry KEY_GENERIC_TYPE findLowest() { if(fromStart) return set.first; Entry KEY_GENERIC_TYPE entry = loInclusive ? set.findCeilingNode(start) : set.findHigherNode(start); return entry == null || tooHigh(entry.key) ? null : entry; } @Override - public KEY_TYPE POLL_FIRST_KEY() - { + public KEY_TYPE POLL_FIRST_KEY() { if(fromStart) return set.POLL_FIRST_KEY(); Entry KEY_GENERIC_TYPE entry = loInclusive ? set.findCeilingNode(start) : set.findHigherNode(start); - if(entry != null && !tooHigh(entry.key)) - { + if(entry != null && !tooHigh(entry.key)) { KEY_TYPE value = entry.key; set.removeNode(entry); return value; @@ -888,26 +726,22 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE } @Override - public KEY_TYPE LAST_KEY() - { + public KEY_TYPE LAST_KEY() { Entry KEY_GENERIC_TYPE entry = findHighest(); return entry == null ? getDefaultMinValue() : entry.key; } - protected Entry KEY_GENERIC_TYPE findHighest() - { + protected Entry KEY_GENERIC_TYPE findHighest() { if(toEnd) return set.last; Entry KEY_GENERIC_TYPE entry = hiInclusive ? set.findFloorNode(end) : set.findLowerNode(end); return entry == null || tooLow(entry.key) ? null : entry; } @Override - public KEY_TYPE POLL_LAST_KEY() - { + public KEY_TYPE POLL_LAST_KEY() { if(toEnd) return set.POLL_LAST_KEY(); Entry KEY_GENERIC_TYPE entry = hiInclusive ? set.findFloorNode(end) : set.findLowerNode(end); - if(entry != null && !tooLow(entry.key)) - { + if(entry != null && !tooLow(entry.key)) { KEY_TYPE value = entry.key; set.removeNode(entry); return value; @@ -916,44 +750,37 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE } @Override - public boolean add(KEY_TYPE o) - { + public boolean add(KEY_TYPE o) { if(!inRange(o)) throw new IllegalArgumentException("Key is out of range"); return set.add(o); } #if !TYPE_OBJECT @Override - public boolean contains(KEY_TYPE e) - { + public boolean contains(KEY_TYPE e) { return inRange(e) && set.contains(e); } @Override - public boolean remove(KEY_TYPE o) - { + public boolean remove(KEY_TYPE o) { return inRange(o) && set.remove(o); } #else @Override - public boolean contains(Object e) - { + public boolean contains(Object e) { return inRange((T)e) && set.contains((T)e); } @Override - public boolean remove(Object o) - { + public boolean remove(Object o) { return inRange((T)o) && set.remove((T)o); } #endif @Override - public KEY_TYPE lower(KEY_TYPE e) - { - if(tooHigh(e)) - { + public KEY_TYPE lower(KEY_TYPE e) { + if(tooHigh(e)) { Entry KEY_GENERIC_TYPE entry = findHighest(); return entry == null ? getDefaultMinValue() : entry.key; } @@ -962,10 +789,8 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE } @Override - public KEY_TYPE floor(KEY_TYPE e) - { - if(tooHigh(e)) - { + public KEY_TYPE floor(KEY_TYPE e) { + if(tooHigh(e)) { Entry KEY_GENERIC_TYPE entry = findHighest(); return entry == null ? getDefaultMinValue() : entry.key; } @@ -974,10 +799,8 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE } @Override - public KEY_TYPE ceiling(KEY_TYPE e) - { - if(tooLow(e)) - { + public KEY_TYPE ceiling(KEY_TYPE e) { + if(tooLow(e)) { Entry KEY_GENERIC_TYPE entry = findLowest(); return entry == null ? getDefaultMaxValue() : entry.key; } @@ -986,10 +809,8 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE } @Override - public KEY_TYPE higher(KEY_TYPE e) - { - if(tooLow(e)) - { + public KEY_TYPE higher(KEY_TYPE e) { + if(tooLow(e)) { Entry KEY_GENERIC_TYPE entry = findLowest(); return entry == null ? getDefaultMaxValue() : entry.key; } @@ -998,8 +819,7 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE } @Override - public int size() - { + public int size() { return fromStart && toEnd ? set.size() : iterator().skip(Integer.MAX_VALUE); } @@ -1010,61 +830,51 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE Entry KEY_GENERIC_TYPE current; int index = 0; - public SubSetIterator(Entry KEY_GENERIC_TYPE entry) - { + public SubSetIterator(Entry KEY_GENERIC_TYPE entry) { next = entry; previous = entry == null ? null : entry.previous(); } @Override - public boolean hasNext() - { + public boolean hasNext() { return next != null; } @Override - public boolean hasPrevious() - { + public boolean hasPrevious() { return previous != null; } @Override - public int nextIndex() - { + public int nextIndex() { return index; } @Override - public int previousIndex() - { + public int previousIndex() { return index - 1; } - boolean topReached(Entry KEY_GENERIC_TYPE entry) - { + boolean topReached(Entry KEY_GENERIC_TYPE entry) { return hiInclusive ? set.compare(entry.key, end) >= 0 : set.compare(entry.key, end) > 0; } - boolean bottomReached(Entry KEY_GENERIC_TYPE entry) - { + boolean bottomReached(Entry KEY_GENERIC_TYPE entry) { return loInclusive ? set.compare(entry.key, start) <= 0 : set.compare(entry.key, start) < 0; } - protected void updateNext() - { + protected void updateNext() { next = current.next(); if(!toEnd && next != null && topReached(next)) next = null; } - protected void updatePrevious() - { + protected void updatePrevious() { previous = current.previous(); if(!fromStart && previous != null && bottomReached(previous)) previous = null; } @Override - public KEY_TYPE NEXT() - { + public KEY_TYPE NEXT() { if(!hasNext()) throw new NoSuchElementException(); current = previous = next; updateNext(); @@ -1073,8 +883,7 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE } @Override - public KEY_TYPE PREVIOUS() - { + public KEY_TYPE PREVIOUS() { if(!hasPrevious()) throw new NoSuchElementException(); current = next = previous; updatePrevious(); @@ -1083,8 +892,7 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE } @Override - public void remove() - { + public void remove() { if(current == null) throw new IllegalStateException(); if(current == previous) index--; updateNext(); @@ -1108,55 +916,46 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE Entry KEY_GENERIC_TYPE current; int index = 0; - public SetIterator(boolean descending) - { + public SetIterator(boolean descending) { if(descending) previous = last; else next = first; } - public SetIterator(Entry KEY_GENERIC_TYPE entry) - { + public SetIterator(Entry KEY_GENERIC_TYPE entry) { next = entry; previous = entry.previous(); } @Override - public boolean hasNext() - { + public boolean hasNext() { return next != null; } @Override - public boolean hasPrevious() - { + public boolean hasPrevious() { return previous != null; } @Override - public int nextIndex() - { + public int nextIndex() { return index; } @Override - public int previousIndex() - { + public int previousIndex() { return index - 1; } - protected void updateNext() - { + protected void updateNext() { next = current.next(); } - protected void updatePrevious() - { + protected void updatePrevious() { previous = current.previous(); } @Override - public KEY_TYPE NEXT() - { + public KEY_TYPE NEXT() { if(!hasNext()) throw new NoSuchElementException(); current = previous = next; updateNext(); @@ -1165,8 +964,7 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE } @Override - public KEY_TYPE PREVIOUS() - { + public KEY_TYPE PREVIOUS() { if(!hasPrevious()) throw new NoSuchElementException(); current = next = previous; updatePrevious(); @@ -1175,8 +973,7 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE } @Override - public void remove() - { + public void remove() { if(current == null) throw new IllegalStateException(); if(current == previous) index--; updateNext(); @@ -1202,71 +999,55 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE Entry KEY_GENERIC_TYPE left; Entry KEY_GENERIC_TYPE right; - Entry(KEY_TYPE key, Entry KEY_GENERIC_TYPE parent) - { + Entry(KEY_TYPE key, Entry KEY_GENERIC_TYPE parent) { this.key = key; this.parent = parent; } - boolean isBlack() - { + boolean isBlack() { return (state & BLACK) != 0; } - void setBlack(boolean value) - { + void setBlack(boolean value) { if(value) state |= BLACK; else state &= ~BLACK; } - boolean needsSuccessor() - { - return left != null && right != null; - } + boolean needsSuccessor() { return left != null && right != null; } - boolean replace(Entry KEY_GENERIC_TYPE entry) - { + boolean replace(Entry KEY_GENERIC_TYPE entry) { if(entry != null) entry.parent = parent; - if(parent != null) - { - if(parent.left == this) - parent.left = entry; - else - parent.right = entry; + if(parent != null) { + if(parent.left == this) parent.left = entry; + else parent.right = entry; } return parent == null; } - Entry KEY_GENERIC_TYPE next() - { - if(right != null) - { + Entry KEY_GENERIC_TYPE next() { + if(right != null) { Entry KEY_GENERIC_TYPE parent = right; while(parent.left != null) parent = parent.left; return parent; } Entry KEY_GENERIC_TYPE parent = this.parent; Entry KEY_GENERIC_TYPE control = this; - while(parent != null && control == parent.right) - { + while(parent != null && control == parent.right) { control = parent; parent = parent.parent; } return parent; } - Entry KEY_GENERIC_TYPE previous() - { - if(left != null) - { + Entry KEY_GENERIC_TYPE previous() { + if(left != null) { Entry KEY_GENERIC_TYPE parent = left; while(parent.right != null) parent = parent.right; return parent; } Entry KEY_GENERIC_TYPE parent = this.parent; Entry KEY_GENERIC_TYPE control = this; - while(parent != null && control == parent.left) - { + while(parent != null && control == parent.left) { control = parent; parent = parent.parent; } diff --git a/src/main/resources/speiger/assets/collections/templates/utils/Iterators.template b/src/main/resources/speiger/assets/collections/templates/utils/Iterators.template index bcd10eb..f51c16e 100644 --- a/src/main/resources/speiger/assets/collections/templates/utils/Iterators.template +++ b/src/main/resources/speiger/assets/collections/templates/utils/Iterators.template @@ -3,6 +3,7 @@ package speiger.src.collections.PACKAGE.utils; import java.util.Iterator; import speiger.src.collections.PACKAGE.collections.ITERATOR; import speiger.src.collections.PACKAGE.lists.LIST_ITERATOR; +import speiger.src.collections.PACKAGE.collections.BI_ITERATOR; /** * A Helper class for Iterators @@ -32,6 +33,10 @@ public class ITERATORS return iterator instanceof UnmodifiableIterator ? iterator : new UnmodifiableIteratorBRACES(iterator); } + public static GENERIC_BRACES BI_ITERATOR KEY_GENERIC_TYPE unmodifiable(BI_ITERATOR KEY_GENERIC_TYPE iterator) { + return iterator instanceof UnmodifiableBiIterator ? iterator : new UnmodifiableBiIteratorBRACES(iterator); + } + /** * Returns a Immutable ListIterator instance based on the instance given. * @param l that should be made immutable/unmodifyable @@ -260,7 +265,36 @@ public class ITERATORS @Override public void add(KEY_TYPE e) { throw new UnsupportedOperationException(); } -} + } + + private static class UnmodifiableBiIterator KEY_GENERIC_TYPE implements BI_ITERATOR KEY_GENERIC_TYPE + { + BI_ITERATOR KEY_GENERIC_TYPE iter; + + UnmodifiableBiIterator(BI_ITERATOR KEY_GENERIC_TYPE iter) { + this.iter = iter; + } + + @Override + public KEY_TYPE NEXT() { + return iter.NEXT(); + } + + @Override + public boolean hasNext() { + return iter.hasNext(); + } + + @Override + public boolean hasPrevious() { + return iter.hasPrevious(); + } + + @Override + public KEY_TYPE PREVIOUS() { + return iter.PREVIOUS(); + } + } private static class UnmodifiableIterator KEY_GENERIC_TYPE implements ITERATOR KEY_GENERIC_TYPE { diff --git a/src/main/resources/speiger/assets/collections/templates/utils/Sets.template b/src/main/resources/speiger/assets/collections/templates/utils/Sets.template new file mode 100644 index 0000000..adac6f1 --- /dev/null +++ b/src/main/resources/speiger/assets/collections/templates/utils/Sets.template @@ -0,0 +1,362 @@ +package speiger.src.collections.PACKAGE.utils; + +#if TYPE_OBJECT +import java.util.Comparator; + +#endif +import speiger.src.collections.PACKAGE.collections.BI_ITERATOR; +#if !TYPE_OBJECT +import speiger.src.collections.PACKAGE.functions.COMPARATOR; +#endif +import speiger.src.collections.PACKAGE.sets.NAVIGABLE_SET; +import speiger.src.collections.PACKAGE.sets.SET; +import speiger.src.collections.PACKAGE.sets.SORTED_SET; +import speiger.src.collections.PACKAGE.utils.COLLECTIONS.EmptyCollection; +import speiger.src.collections.PACKAGE.utils.COLLECTIONS.SynchronizedCollection; +import speiger.src.collections.PACKAGE.utils.COLLECTIONS.UnmodifiableCollection; + +public class SETS +{ + public static final SET NO_GENERIC_TYPE EMPTY = new EmptySetBRACES(); + + public static GENERIC_BRACES SET KEY_GENERIC_TYPE empty() { +#if TYPE_OBJECT + return (SET)EMPTY; +#else + return EMPTY; +#endif + } + + public static GENERIC_BRACES SET KEY_GENERIC_TYPE synchronizedSet(SET KEY_GENERIC_TYPE s) { + return s instanceof SynchronizedSet ? s : new SynchronizedSetBRACES(s); + } + + public static GENERIC_BRACES SET KEY_GENERIC_TYPE synchronizedSet(SET KEY_GENERIC_TYPE s, Object mutex) { + return s instanceof SynchronizedSet ? s : new SynchronizedSetBRACES(s, mutex); + } + + public static GENERIC_BRACES SORTED_SET KEY_GENERIC_TYPE synchronizedSet(SORTED_SET KEY_GENERIC_TYPE s) { + return s instanceof SynchronizedSortedSet ? s : new SynchronizedSortedSetBRACES(s); + } + + public static GENERIC_BRACES SORTED_SET KEY_GENERIC_TYPE synchronizedSet(SORTED_SET KEY_GENERIC_TYPE s, Object mutex) { + return s instanceof SynchronizedSortedSet ? s : new SynchronizedSortedSetBRACES(s, mutex); + } + + public static GENERIC_BRACES NAVIGABLE_SET KEY_GENERIC_TYPE synchronizedSet(NAVIGABLE_SET KEY_GENERIC_TYPE s) { + return s instanceof SynchronizedNavigableSet ? s : new SynchronizedNavigableSetBRACES(s); + } + + public static GENERIC_BRACES NAVIGABLE_SET KEY_GENERIC_TYPE synchronizedSet(NAVIGABLE_SET KEY_GENERIC_TYPE s, Object mutex) { + return s instanceof SynchronizedNavigableSet ? s : new SynchronizedNavigableSetBRACES(s, mutex); + } + + public static GENERIC_BRACES SET KEY_GENERIC_TYPE unmodifiable(SET KEY_GENERIC_TYPE s) { + return s instanceof SynchronizedSet ? s : new SynchronizedSetBRACES(s); + } + + public static GENERIC_BRACES SORTED_SET KEY_GENERIC_TYPE unmodifiable(SORTED_SET KEY_GENERIC_TYPE s) { + return s instanceof SynchronizedSortedSet ? s : new SynchronizedSortedSetBRACES(s); + } + + public static GENERIC_BRACES NAVIGABLE_SET KEY_GENERIC_TYPE unmodifiable(NAVIGABLE_SET KEY_GENERIC_TYPE s) { + return s instanceof UnmodifiableNavigableSet ? s : new UnmodifiableNavigableSetBRACES(s); + } + + public static class EmptySet KEY_GENERIC_TYPE extends EmptyCollection KEY_GENERIC_TYPE implements SET KEY_GENERIC_TYPE + { +#if !TYPE_OBJECT + @Override + public boolean remove(KEY_TYPE o) { throw new UnsupportedOperationException(); } +#endif + } + + public static class UnmodifiableNavigableSet KEY_GENERIC_TYPE extends UnmodifiableSortedSet KEY_GENERIC_TYPE implements NAVIGABLE_SET KEY_GENERIC_TYPE + { + NAVIGABLE_SET KEY_GENERIC_TYPE n; + + public UnmodifiableNavigableSet(NAVIGABLE_SET KEY_GENERIC_TYPE c) { + super(c); + n = c; + } + +#if !TYPE_OBJECT + @Override + public boolean contains(KEY_TYPE o) { return n.contains(o); } + +#endif + @Override + @Deprecated + public boolean contains(Object o) { return n.contains(o); } + + @Override + public KEY_TYPE lower(KEY_TYPE e) { return n.lower(e); } + + @Override + public KEY_TYPE floor(KEY_TYPE e) { return n.floor(e); } + + @Override + public KEY_TYPE ceiling(KEY_TYPE e) { return n.ceiling(e); } + + @Override + public KEY_TYPE higher(KEY_TYPE e) { return n.higher(e); } + +#if !TYPE_OBJECT + @Override + public void setDefaultMaxValue(KEY_TYPE e) { throw new UnsupportedOperationException(); } + + @Override + public KEY_TYPE getDefaultMaxValue() { return n.getDefaultMaxValue(); } + + @Override + public void setDefaultMinValue(KEY_TYPE e) { throw new UnsupportedOperationException(); } + + @Override + public KEY_TYPE getDefaultMinValue() { return n.getDefaultMinValue(); } + +#endif + @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)); } + + @Override + public NAVIGABLE_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement, boolean inclusive) { return unmodifiable(n.headSet(toElement, inclusive)); } + + @Override + public NAVIGABLE_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement, boolean inclusive) { return unmodifiable(n.tailSet(fromElement, inclusive)); } + + @Override + public BI_ITERATOR KEY_GENERIC_TYPE descendingIterator() { return ITERATORS.unmodifiable(n.descendingIterator()); } + + @Override + public NAVIGABLE_SET KEY_GENERIC_TYPE descendingSet() { return unmodifiable(n.descendingSet()); } + + @Override + public NAVIGABLE_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, KEY_TYPE toElement) { return unmodifiable(n.subSet(fromElement, toElement)); } + + @Override + public NAVIGABLE_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement) { return unmodifiable(n.headSet(toElement)); } + + @Override + public NAVIGABLE_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement) { return unmodifiable(n.tailSet(fromElement)); } + + } + + public static class UnmodifiableSortedSet KEY_GENERIC_TYPE extends UnmodifiableSet KEY_GENERIC_TYPE implements SORTED_SET KEY_GENERIC_TYPE + { + SORTED_SET KEY_GENERIC_TYPE s; + public UnmodifiableSortedSet(SORTED_SET KEY_GENERIC_TYPE c) + { + super(c); + s = c; + } + + @Override + public boolean addAndMoveToFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); } + + @Override + public boolean addAndMoveToLast(KEY_TYPE o) { throw new UnsupportedOperationException(); } + + @Override + public boolean moveToFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); } + + @Override + public boolean moveToLast(KEY_TYPE o) { throw new UnsupportedOperationException(); } + + @Override + public COMPARATOR KEY_GENERIC_TYPE comparator() { return s.comparator(); } + + @Override + public BI_ITERATOR KEY_GENERIC_TYPE iterator() { return ITERATORS.unmodifiable(s.iterator()); } + + @Override + public BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement) { return ITERATORS.unmodifiable(s.iterator(fromElement)); } + + @Override + public SORTED_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, KEY_TYPE toElement) { return unmodifiable(s.subSet(fromElement, toElement)); } + + @Override + public SORTED_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement) { return unmodifiable(s.headSet(toElement)); } + + @Override + public SORTED_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement) { return unmodifiable(s.tailSet(fromElement)); } + + @Override + public KEY_TYPE FIRST_KEY() { return s.FIRST_KEY(); } + + @Override + public KEY_TYPE POLL_FIRST_KEY() { throw new UnsupportedOperationException(); } + + @Override + public KEY_TYPE LAST_KEY() { return s.LAST_KEY(); } + + @Override + public KEY_TYPE POLL_LAST_KEY() { throw new UnsupportedOperationException(); } + } + + public static class UnmodifiableSet KEY_GENERIC_TYPE extends UnmodifiableCollection KEY_GENERIC_TYPE implements SET KEY_GENERIC_TYPE + { + SET KEY_GENERIC_TYPE s; + + public UnmodifiableSet(SET KEY_GENERIC_TYPE c) + { + super(c); + s = c; + } + +#if !TYPE_OBJECT + @Override + public boolean remove(KEY_TYPE o) { throw new UnsupportedOperationException(); } +#endif + } + + public static class SynchronizedNavigableSet KEY_GENERIC_TYPE extends SynchronizedSortedSet KEY_GENERIC_TYPE implements NAVIGABLE_SET KEY_GENERIC_TYPE + { + NAVIGABLE_SET KEY_GENERIC_TYPE n; + + public SynchronizedNavigableSet(NAVIGABLE_SET KEY_GENERIC_TYPE c) { + super(c); + n = c; + } + + public SynchronizedNavigableSet(NAVIGABLE_SET KEY_GENERIC_TYPE c, Object mutex) { + super(c, mutex); + n = c; + } + + @Override + @Deprecated + public boolean contains(Object o) { synchronized(mutex) { return n.contains(o); } } + +#if !TYPE_OBJECT + @Override + public boolean contains(KEY_TYPE o) { synchronized(mutex) { return n.contains(o); } } + +#endif + @Override + public KEY_TYPE lower(KEY_TYPE e) { synchronized(mutex) { return n.lower(e); } } + + @Override + public KEY_TYPE floor(KEY_TYPE e) { synchronized(mutex) { return n.floor(e); } } + + @Override + public KEY_TYPE ceiling(KEY_TYPE e) { synchronized(mutex) { return n.ceiling(e); } } + + @Override + public KEY_TYPE higher(KEY_TYPE e) { synchronized(mutex) { return n.higher(e); } } + +#if !TYPE_OBJECT + @Override + public void setDefaultMaxValue(KEY_TYPE e) { synchronized(mutex) { n.setDefaultMaxValue(e); } } + + @Override + public KEY_TYPE getDefaultMaxValue() { synchronized(mutex) { return n.getDefaultMaxValue(); } } + + @Override + public void setDefaultMinValue(KEY_TYPE e) { synchronized(mutex) { n.setDefaultMinValue(e); } } + + @Override + public KEY_TYPE getDefaultMinValue() { synchronized(mutex) { return n.getDefaultMinValue(); } } + +#endif + @Override + public NAVIGABLE_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, boolean fromInclusive, KEY_TYPE toElement, boolean toInclusive) { synchronized(mutex) { return synchronizedSet(n.subSet(fromElement, fromInclusive, toElement, toInclusive), mutex); } } + + @Override + public NAVIGABLE_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement, boolean inclusive) { synchronized(mutex) { return synchronizedSet(n.headSet(toElement, inclusive), mutex); } } + + @Override + public NAVIGABLE_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement, boolean inclusive) { synchronized(mutex) { return synchronizedSet(n.tailSet(fromElement, inclusive), mutex); } } + + @Override + public BI_ITERATOR KEY_GENERIC_TYPE descendingIterator() { synchronized(mutex) { return n.descendingIterator(); } } + + @Override + public NAVIGABLE_SET KEY_GENERIC_TYPE descendingSet() { synchronized(mutex) { return synchronizedSet(n.descendingSet(), mutex); } } + + @Override + public NAVIGABLE_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, KEY_TYPE toElement) { synchronized(mutex) { return synchronizedSet(n.subSet(fromElement, toElement), mutex); } } + + @Override + public NAVIGABLE_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement) { synchronized(mutex) { return synchronizedSet(n.headSet(toElement), mutex); } } + + @Override + public NAVIGABLE_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement) { synchronized(mutex) { return synchronizedSet(n.tailSet(fromElement), mutex); } } + } + + public static class SynchronizedSortedSet KEY_GENERIC_TYPE extends SynchronizedSet KEY_GENERIC_TYPE implements SORTED_SET KEY_GENERIC_TYPE + { + SORTED_SET KEY_GENERIC_TYPE s; + + public SynchronizedSortedSet(SORTED_SET KEY_GENERIC_TYPE c) { + super(c); + s = c; + } + + public SynchronizedSortedSet(SORTED_SET KEY_GENERIC_TYPE c, Object mutex) { + super(c, mutex); + s = c; + } + + @Override + public boolean addAndMoveToFirst(KEY_TYPE o) { synchronized(mutex) { return s.addAndMoveToFirst(o); } } + + @Override + public boolean addAndMoveToLast(KEY_TYPE o) { synchronized(mutex) { return s.addAndMoveToLast(o); } } + + @Override + public boolean moveToFirst(KEY_TYPE o) { synchronized(mutex) { return s.moveToFirst(o); } } + + @Override + public boolean moveToLast(KEY_TYPE o) { synchronized(mutex) { return s.moveToLast(o); } } + + @Override + public COMPARATOR KEY_GENERIC_TYPE comparator(){ synchronized(mutex) { return s.comparator(); } } + + @Override + public BI_ITERATOR KEY_GENERIC_TYPE iterator() { synchronized(mutex) { return s.iterator(); } } + + @Override + public BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement) { synchronized(mutex) { return s.iterator(fromElement); } } + + @Override + public SORTED_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, KEY_TYPE toElement) { synchronized(mutex) { return synchronizedSet(s.subSet(fromElement, toElement), mutex); } } + + @Override + public SORTED_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement) { synchronized(mutex) { return synchronizedSet(s.headSet(toElement), mutex); } } + + @Override + public SORTED_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement) { synchronized(mutex) { return synchronizedSet(s.tailSet(fromElement), mutex); } } + + @Override + public KEY_TYPE FIRST_KEY() { synchronized(mutex) { return s.FIRST_KEY(); } } + + @Override + public KEY_TYPE POLL_FIRST_KEY() { synchronized(mutex) { return s.POLL_FIRST_KEY(); } } + + @Override + public KEY_TYPE LAST_KEY() { synchronized(mutex) { return s.LAST_KEY(); } } + + @Override + public KEY_TYPE POLL_LAST_KEY() { synchronized(mutex) { return s.POLL_LAST_KEY(); } } + } + + public static class SynchronizedSet KEY_GENERIC_TYPE extends SynchronizedCollection KEY_GENERIC_TYPE implements SET KEY_GENERIC_TYPE + { + SET KEY_GENERIC_TYPE s; + + public SynchronizedSet(SET KEY_GENERIC_TYPE c) { + super(c); + s = c; + } + + public SynchronizedSet(SET KEY_GENERIC_TYPE c, Object mutex) { + super(c, mutex); + s = c; + } + +#if !TYPE_OBJECT + @Override + public boolean remove(KEY_TYPE o) { synchronized(mutex) { return s.remove(o); } } +#endif + } +}