From caf2f22e9ffbeb1a581ce5ea83eb46b7cb24bd0d Mon Sep 17 00:00:00 2001 From: Speiger Date: Mon, 13 Dec 2021 10:41:20 +0100 Subject: [PATCH] Finished the Basic Unit test frame work to ensure stablity. -Added: 150k Unit tests. -Fixed: Reworked the NavigableSet/Map implementations of RBTree/AVLTree/Array Sets/Maps --- Changelog.md | 4 + build.gradle | 1 - .../maps/impl/misc/ArrayMap.template | 2170 +++++++-------- .../maps/impl/tree/AVLTreeMap.template | 2378 ++++++++-------- .../maps/impl/tree/RBTreeMap.template | 2407 +++++++++-------- .../maps/interfaces/NavigableMap.template | 8 +- .../templates/sets/AVLTreeSet.template | 742 ++--- .../templates/sets/ArraySet.template | 718 ++--- .../templates/sets/RBTreeSet.template | 737 ++--- .../ints/maps/Int2IntArrayMapTest.java | 6 + .../ints/sets/IntArraySetTests.java | 5 + .../objects/map/ObjectMapTests.java | 35 +- .../objects/set/ObjectSetTests.java | 31 +- 13 files changed, 4790 insertions(+), 4452 deletions(-) diff --git a/Changelog.md b/Changelog.md index 171dc4ec..c7dde380 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,5 +1,9 @@ # Changelog of versions +### Version 0.5.1 +- Fixed: Reworked the NavigableSet/Map implementations of RBTree/AVLTree/Array Sets/Maps so they are now deemed stable. +- Added: Another 150k Unit tests. + ### Version 0.5.0 - Added: 2 Helper functions to find out how many bits are required to store a Number. - Added: pour function directly into Iterable which allows to collect all elements in the Iterable directly. diff --git a/build.gradle b/build.gradle index 05092663..2d47a244 100644 --- a/build.gradle +++ b/build.gradle @@ -46,7 +46,6 @@ dependencies { runtimeOnly 'de.speiger:Simple-Code-Generator:1.0.5' testImplementation 'junit:junit:4.12' testImplementation 'com.google.guava:guava-testlib:31.0.1-jre' - } task generateSource(type: JavaExec) { diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/ArrayMap.template b/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/ArrayMap.template index e9a40b4e..74629f10 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/ArrayMap.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/ArrayMap.template @@ -51,7 +51,7 @@ import speiger.src.collections.objects.functions.function.Object2BooleanFunction #endif #endif #if !SAME_TYPE -#if !TYPE_OBJECT +#if VALUE_OBJECT import speiger.src.collections.objects.functions.consumer.VALUE_BI_FROM_OBJECT_CONSUMER; #endif @@ -69,7 +69,6 @@ import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSortedSet; -import speiger.src.collections.objects.sets.ObjectSet; #endif import speiger.src.collections.utils.HashUtil; @@ -593,27 +592,32 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN return null; } + /** + * Unsupported for now. Implementation is buggy and does not support the Java Standard with these functions. + * It is a Unsorted Sorted Map. Thats why the SubMap implementation will be disabled until a better solution is found. + * To give a simple reason: LinkedHashMaps are also not SortedSets even so they could be. + * @throws UnsupportedOperationException + */ @Override - public SORTED_MAP KEY_VALUE_GENERIC_TYPE subMap(KEY_TYPE fromKey, KEY_TYPE toKey) { - int fromIndex = findIndex(fromKey); - int toIndex = findIndex(toKey); - if(fromIndex == -1 || toIndex == -1) throw new NoSuchElementException(); - return new SubMap(fromIndex, toIndex - fromIndex + 1); - } + public SORTED_MAP KEY_VALUE_GENERIC_TYPE subMap(KEY_TYPE fromKey, KEY_TYPE toKey) { throw new UnsupportedOperationException(); } + /** + * Unsupported for now. Implementation is buggy and does not support the Java Standard with these functions. + * It is a Unsorted Sorted Map. Thats why the SubMap implementation will be disabled until a better solution is found. + * To give a simple reason: LinkedHashMaps are also not SortedSets even so they could be. + * @throws UnsupportedOperationException + */ @Override - public SORTED_MAP KEY_VALUE_GENERIC_TYPE headMap(KEY_TYPE toKey) { - int toIndex = findIndex(toKey); - if(toIndex == -1) throw new NoSuchElementException(); - return new SubMap(0, toIndex + 1); - } + public SORTED_MAP KEY_VALUE_GENERIC_TYPE headMap(KEY_TYPE toKey) { throw new UnsupportedOperationException(); } + /** + * Unsupported for now. Implementation is buggy and does not support the Java Standard with these functions. + * It is a Unsorted Sorted Map. Thats why the SubMap implementation will be disabled until a better solution is found. + * To give a simple reason: LinkedHashMaps are also not SortedSets even so they could be. + * @throws UnsupportedOperationException + */ @Override - public SORTED_MAP KEY_VALUE_GENERIC_TYPE tailMap(KEY_TYPE fromKey) { - int fromIndex = findIndex(fromKey); - if(fromIndex == -1) throw new NoSuchElementException(); - return new SubMap(fromIndex, size - fromIndex); - } + public SORTED_MAP KEY_VALUE_GENERIC_TYPE tailMap(KEY_TYPE fromKey) { throw new UnsupportedOperationException(); } protected void moveIndexToFirst(int index) { if(index == 0) return; @@ -723,1071 +727,1071 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN return -1; } - private class SubMap extends ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE implements SORTED_MAP KEY_VALUE_GENERIC_TYPE { - int offset; - int length; - - SET KEY_GENERIC_TYPE keySet; - VALUE_COLLECTION VALUE_GENERIC_TYPE valuesC; - FastSortedSet KEY_VALUE_GENERIC_TYPE entrySet; - - public SubMap(int offset, int length) { - this.offset = offset; - this.length = length; - } - - int end() { return offset+length; } - - @Override - public VALUE_TYPE put(KEY_TYPE key, VALUE_TYPE value) { - int index = findIndex(key); - if(index < 0) { - insertIndex(end(), key, value); - length++; - return getDefaultReturnValue(); - } - VALUE_TYPE oldValue = values[index]; - values[index] = value; - return oldValue; - } - - @Override - public VALUE_TYPE putIfAbsent(KEY_TYPE key, VALUE_TYPE value) { - int index = findIndex(key); - if(index >= 0) { - insertIndex(end(), key, value); - length++; - } - return getDefaultReturnValue(); - } - -#if VALUE_PRIMITIVES - @Override - public VALUE_TYPE addTo(KEY_TYPE key, VALUE_TYPE value) { - int index = findIndex(key); - if(index < 0) { - insertIndex(end(), key, value); - length++; - return getDefaultReturnValue(); - } - VALUE_TYPE oldValue = values[index]; - values[index] += value; - return oldValue; - } - -#endif - @Override - public VALUE_TYPE putAndMoveToFirst(KEY_TYPE key, VALUE_TYPE value) { - int index = findIndex(key); - if(index < 0) { - insertIndex(offset, key, value); - length++; - return getDefaultReturnValue(); - } - VALUE_TYPE lastValue = values[index]; - values[index] = value; - moveIndexToFirst(index); - return lastValue; - } - - @Override - public VALUE_TYPE putAndMoveToLast(KEY_TYPE key, VALUE_TYPE value) { - int index = findIndex(key); - if(index < 0) { - insertIndex(end(), key, value); - length++; - return getDefaultReturnValue(); - } - VALUE_TYPE lastValue = values[index]; - values[index] = value; - moveIndexToLast(index); - return lastValue; - } - - @Override - public boolean moveToFirst(KEY_TYPE key) { - int index = findIndex(key); - if(index > 0) { - moveIndexToFirst(index); - return true; - } - return false; - } - - @Override - public boolean moveToLast(KEY_TYPE key) { - int index = findIndex(key); - if(index < size()-1) { - moveIndexToLast(index); - return true; - } - return false; - } - -#if !TYPE_OBJECT - @Override - public boolean containsKey(KEY_TYPE key) { - return findIndex(key) >= 0; - } - -#endif -#if !VALUE_OBJECT - @Override - public boolean containsValue(VALUE_TYPE value) { - return findValue(value) >= 0; - } - -#endif - @Override - public boolean containsKey(Object key) { - return findIndex(key) >= 0; - } - - @Override - public boolean containsValue(Object value) { - return findValue(value) >= 0; - } - - @Override - public VALUE_TYPE GET_VALUE(KEY_TYPE key) { - int index = findIndex(key); - return index < 0 ? getDefaultReturnValue() : values[index]; - } - -#if TYPE_OBJECT && VALUE_OBJECT - @Override - public VALUE_TYPE getOrDefault(Object key, VALUE_TYPE defaultValue) { - int index = findIndex(key); - return index < 0 ? defaultValue : values[index]; - } - -#else - @Override - public VALUE_TYPE getOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue) { - int index = findIndex(key); - return index < 0 ? defaultValue : values[index]; - } - -#endif - @Override - public VALUE_TYPE getAndMoveToFirst(KEY_TYPE key) { - int index = findIndex(key); - if(index >= 0) { - VALUE_TYPE value = values[index]; - moveIndexToFirst(index); - return value; - } - return getDefaultReturnValue(); - } - - @Override - public VALUE_TYPE getAndMoveToLast(KEY_TYPE key) { - int index = findIndex(key); - if(index >= 0) { - VALUE_TYPE value = values[index]; - moveIndexToLast(index); - return value; - } - return getDefaultReturnValue(); - } - - @Override - public KEY_TYPE FIRST_ENTRY_KEY() { - if(length <= 0) throw new NoSuchElementException(); - return keys[offset]; - } - - @Override - public KEY_TYPE LAST_ENTRY_KEY() { - if(length <= 0) throw new NoSuchElementException(); - return keys[end()-1]; - } - - @Override - public VALUE_TYPE FIRST_ENTRY_VALUE() { - if(length <= 0) throw new NoSuchElementException(); - return values[end()-1]; - } - - @Override - public VALUE_TYPE LAST_ENTRY_VALUE() { - if(length <= 0) throw new NoSuchElementException(); - return values[offset]; - } - - @Override - public KEY_TYPE POLL_FIRST_ENTRY_KEY() { - if(length == 0) throw new NoSuchElementException(); - KEY_TYPE result = keys[offset]; - removeIndex(offset); - length--; - return result; - } - - @Override - public KEY_TYPE POLL_LAST_ENTRY_KEY() { - if(length == 0) throw new NoSuchElementException(); - KEY_TYPE result = keys[end()-1]; - removeIndex(end()-1); - length--; - return result; - } - - @Override - public VALUE_TYPE REMOVE_VALUE(KEY_TYPE key) { - int index = findIndex(key); - if(index < 0) return getDefaultReturnValue(); - VALUE_TYPE value = values[index]; - removeIndex(index); - length--; - return value; - } - - @Override - public VALUE_TYPE REMOVE_VALUEOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue) { - int index = findIndex(key); - if(index < 0) return defaultValue; - VALUE_TYPE value = values[index]; - removeIndex(index); - length--; - return value; - } - -#if !TYPE_OBJECT || !VALUE_OBJECT - @Override - public boolean remove(KEY_TYPE key, VALUE_TYPE value) { - int index = findIndex(key, value); - if(index < 0) return false; - removeIndex(index); - length--; - return true; - } - -#endif - @Override - public CLASS_VALUE_TYPE remove(Object key) { - int index = findIndex(key); - if(index < 0) return VALUE_TO_OBJ(getDefaultReturnValue()); - VALUE_TYPE value = values[index]; - removeIndex(index); - length--; - return VALUE_TO_OBJ(value); - } - - @Override - public boolean remove(Object key, Object value) { - int index = findIndex(key, value); - if(index < 0) return false; - removeIndex(index); - length--; - return true; - } - - @Override - public int size() { - return length; - } - - @Override - public void clear() { - removeRange(offset, offset+length); - length = 0; - } - - @Override - public SET KEY_GENERIC_TYPE keySet() { - if(keySet == null) keySet = new SubKeySet(); - return keySet; - } - - @Override - public VALUE_COLLECTION VALUE_GENERIC_TYPE values() { - if(valuesC == null) valuesC = new SubValues(); - return valuesC; - } - - @Override - public ObjectSet ENTRY_SET() { - if(entrySet == null) entrySet = new SubMapEntrySet(); - return entrySet; - } - - @Override - public SubMap copy() { - throw new UnsupportedOperationException(); - } - - @Override - public COMPARATOR KEY_GENERIC_TYPE comparator() { - return null; - } - - @Override - public SORTED_MAP KEY_VALUE_GENERIC_TYPE subMap(KEY_TYPE fromKey, KEY_TYPE toKey) { - int fromIndex = findIndex(fromKey); - int toIndex = findIndex(toKey); - if(fromIndex == -1 || toIndex == -1) throw new NoSuchElementException(); - return new SubMap(fromIndex, toIndex - fromIndex + 1); - } - - @Override - public SORTED_MAP KEY_VALUE_GENERIC_TYPE headMap(KEY_TYPE toKey) { - int toIndex = findIndex(toKey); - if(toIndex == -1) throw new NoSuchElementException(); - return new SubMap(offset, toIndex + 1); - } - - @Override - public SORTED_MAP KEY_VALUE_GENERIC_TYPE tailMap(KEY_TYPE fromKey) { - int fromIndex = findIndex(fromKey); - if(fromIndex == -1) throw new NoSuchElementException(); - return new SubMap(fromIndex, size - fromIndex); - } - - protected void moveIndexToFirst(int index) { - if(index == 0) return; - KEY_TYPE key = keys[index]; - VALUE_TYPE value = values[index]; - System.arraycopy(keys, offset, keys, offset+1, index); - System.arraycopy(values, offset, values, offset+1, index); - keys[offset] = key; - values[offset] = value; - } - - protected void moveIndexToLast(int index) { - if(index == length-1) return; - KEY_TYPE key = keys[index]; - VALUE_TYPE value = values[index]; - System.arraycopy(keys, index+1, keys, index, end()-index-1); - System.arraycopy(values, index+1, values, index, end()-index-1); - keys[end()-1] = key; - values[end()-1] = value; - } - -#if !TYPE_OBJECT || !VALUE_OBJECT - protected int findIndex(KEY_TYPE key, VALUE_TYPE value) { - for(int i = length-1;i>=0;i--) - if(KEY_EQUALS(keys[offset+i], key) && VALUE_EQUALS(values[offset+i], value)) return i; - return -1; - } - -#endif -#if !TYPE_OBJECT - protected int findIndex(KEY_TYPE key) { - for(int i = length-1;i>=0;i--) - if(KEY_EQUALS(keys[offset+i], key)) return i+offset; - return -1; - } - -#endif -#if !VALUE_OBJECT - protected int findValue(VALUE_TYPE value) { - for(int i = length-1;i>=0;i--) - if(VALUE_EQUALS(values[offset+i], value)) return i+offset; - return -1; - } - -#endif - protected int findIndex(Object key, Object value) { - for(int i = length-1;i>=0;i--) - if(EQUALS_KEY_TYPE(keys[offset+i], key) && EQUALS_VALUE_TYPE(values[offset+i], value)) return i+offset; - return -1; - } - - protected int findIndex(Object key) { - for(int i = length-1;i>=0;i--) - if(EQUALS_KEY_TYPE(keys[offset+i], key)) return i+offset; - return -1; - } - - protected int findValue(Object value) { - for(int i = length-1;i>=0;i--) - if(EQUALS_VALUE_TYPE(values[offset+i], value)) return i+offset; - return -1; - } - - private class SubMapEntrySet extends AbstractObjectSet implements SORTED_MAP.FastSortedSet KEY_VALUE_GENERIC_TYPE { - @Override - public boolean addAndMoveToFirst(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); } - @Override - public boolean addAndMoveToLast(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); } - - @Override - public boolean moveToFirst(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { - return SubMap.this.moveToFirst(o.ENTRY_KEY()); - } - - @Override - public boolean moveToLast(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { - return SubMap.this.moveToLast(o.ENTRY_KEY()); - } - - @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE first() { - return new BasicEntryKV_BRACES(FIRST_ENTRY_KEY(), FIRST_ENTRY_VALUE()); - } - - @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE last() { - return new BasicEntryKV_BRACES(LAST_ENTRY_KEY(), LAST_ENTRY_VALUE()); - } - - @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE pollFirst() { - BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES(FIRST_ENTRY_KEY(), FIRST_ENTRY_VALUE()); - POLL_FIRST_ENTRY_KEY(); - return entry; - } - - @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE pollLast() { - BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES(LAST_ENTRY_KEY(), LAST_ENTRY_VALUE()); - POLL_LAST_ENTRY_KEY(); - return entry; - } - - @Override - public ObjectBidirectionalIterator iterator() { - return new SubEntryIterator(); - } - - @Override - public ObjectBidirectionalIterator iterator(MAP.Entry KEY_VALUE_GENERIC_TYPE fromElement) { - return new SubEntryIterator(fromElement.ENTRY_KEY()); - } - - @Override - public ObjectBidirectionalIterator fastIterator() { - return new SubFastEntryIterator(); - } - - @Override - public ObjectBidirectionalIterator fastIterator(KEY_TYPE fromElement) { - return new SubFastEntryIterator(fromElement); - } - - @Override - public SubMapEntrySet copy() { throw new UnsupportedOperationException(); } - - @Override - public void forEach(Consumer action) { - Objects.requireNonNull(action); - for(int i = 0;i action) { - Objects.requireNonNull(action); - if(size() <= 0) return; - BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES(); - for(int i = 0;i void forEach(E input, ObjectObjectConsumer action) { - Objects.requireNonNull(action); - for(int i = 0;i filter) { - Objects.requireNonNull(filter); - if(size() <= 0) return false; - BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES(); - for(int i = 0;i filter) { - Objects.requireNonNull(filter); - if(size() <= 0) return true; - BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES(); - for(int i = 0;i filter) { - Objects.requireNonNull(filter); - if(size() <= 0) return true; - BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES(); - for(int i = 0;i E reduce(E identity, BiFunction operator) { - Objects.requireNonNull(operator); - E state = identity; - for(int i = 0;i operator) { - Objects.requireNonNull(operator); - MAP.Entry KEY_VALUE_GENERIC_TYPE state = null; - boolean empty = true; - for(int i = 0;i filter) { - Objects.requireNonNull(filter); - if(size() <= 0) return null; - BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES(); - for(int i = 0;i filter) { - Objects.requireNonNull(filter); - if(size() <= 0) return 0; - int result = 0; - BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES(); - for(int i = 0;i= 0; - } - else { - Map.Entry entry = (Map.Entry)o; - return ARRAY_MAP.this.findIndex(entry.getKey(), entry.getValue()) >= 0; - } - } - return false; - } - - @Override - @Deprecated - public boolean remove(Object o) { - if(o instanceof Map.Entry) { - if(o instanceof MAP.Entry) { - MAP.Entry KEY_VALUE_GENERIC_TYPE entry = (MAP.Entry KEY_VALUE_GENERIC_TYPE)o; - return SubMap.this.remove(entry.ENTRY_KEY(), entry.ENTRY_VALUE()); - } - Map.Entry entry = (Map.Entry)o; - return SubMap.this.remove(entry.getKey(), entry.getValue()); - } - return false; - } - - @Override - public int size() { - return SubMap.this.size(); - } - - @Override - public void clear() { - SubMap.this.clear(); - } - - @Override - public Comparator comparator() { - return null; - } - @Override - public ObjectSortedSet subSet(MAP.Entry KEY_VALUE_GENERIC_TYPE fromElement, MAP.Entry KEY_VALUE_GENERIC_TYPE toElement) { throw new UnsupportedOperationException(); } - @Override - public ObjectSortedSet headSet(MAP.Entry KEY_VALUE_GENERIC_TYPE toElement) { throw new UnsupportedOperationException(); } - @Override - public ObjectSortedSet tailSet(MAP.Entry KEY_VALUE_GENERIC_TYPE fromElement) { throw new UnsupportedOperationException(); } - } - - private class SubKeySet extends ABSTRACT_SET KEY_GENERIC_TYPE implements SORTED_SET KEY_GENERIC_TYPE { -#if TYPE_OBJECT - @Override - public boolean contains(Object e) { return containsKey(e); } - - @Override - public boolean remove(Object o) { - int oldSize = length; - SubMap.this.remove(o); - return length != oldSize; - } - -#else - @Override - public boolean contains(KEY_TYPE e) { return containsKey(e); } - - @Override - public boolean remove(KEY_TYPE o) { - int oldSize = length; - SubMap.this.remove(o); - return length != oldSize; - } - -#endif - @Override - public boolean add(KEY_TYPE o) { throw new UnsupportedOperationException(); } - @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) { return SubMap.this.moveToFirst(o); } - @Override - public boolean moveToLast(KEY_TYPE o) { return SubMap.this.moveToLast(o); } - @Override - public LIST_ITERATOR KEY_GENERIC_TYPE iterator() { return new SubKeyIterator(); } - @Override - public BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement) { return new SubKeyIterator(fromElement); } - @Override - public int size() { return SubMap.this.size(); } - @Override - public void clear() { SubMap.this.clear(); } - @Override - public KEY_TYPE FIRST_KEY() { return FIRST_ENTRY_KEY(); } - @Override - public KEY_TYPE POLL_FIRST_KEY() { return POLL_FIRST_ENTRY_KEY(); } - @Override - public KEY_TYPE LAST_KEY() { return LAST_ENTRY_KEY(); } - @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); - for(int i = 0;i void forEach(E input, BI_FROM_OBJECT_CONSUMER KSK_GENERIC_TYPE action) { - Objects.requireNonNull(action); - if(size() <= 0) return; - for(int i = 0;i KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction operator) { - Objects.requireNonNull(operator); - KEY_SPECIAL_TYPE state = identity; - for(int i = 0;i void forEach(E input, VALUE_BI_FROM_OBJECT_CONSUMER VSV_GENERIC_TYPE action) { - Objects.requireNonNull(action); - for(int i = 0;i VALUE_SPECIAL_TYPE reduce(VALUE_SPECIAL_TYPE identity, BiFunction operator) { - Objects.requireNonNull(operator); - VALUE_SPECIAL_TYPE state = identity; - for(int i = 0;i { - MapEntry entry = new MapEntry(); - - public SubFastEntryIterator() {} - public SubFastEntryIterator(KEY_TYPE from) { - index = findIndex(from); - } - - @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE next() { - entry.index = nextEntry(); - return entry; - } - - @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE previous() { - entry.index = previousEntry(); - return entry; - } - - @Override - public void set(MAP.Entry KEY_VALUE_GENERIC_TYPE e) { throw new UnsupportedOperationException(); } - @Override - public void add(MAP.Entry KEY_VALUE_GENERIC_TYPE e) { throw new UnsupportedOperationException(); } - } - - private class SubEntryIterator extends SubMapIterator implements ObjectListIterator { - MapEntry entry = null; - - public SubEntryIterator() {} - public SubEntryIterator(KEY_TYPE from) { - index = findIndex(from); - } - - @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE next() { - return entry = new MapEntry(nextEntry()); - } - - @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE previous() { - return entry = new MapEntry(previousEntry()); - } - - @Override - public void remove() { - super.remove(); - entry.index = -1; - } - - @Override - public void set(MAP.Entry KEY_VALUE_GENERIC_TYPE e) { throw new UnsupportedOperationException(); } - @Override - public void add(MAP.Entry KEY_VALUE_GENERIC_TYPE e) { throw new UnsupportedOperationException(); } - } - - private class SubKeyIterator extends SubMapIterator implements LIST_ITERATOR KEY_GENERIC_TYPE { - public SubKeyIterator() {} - public SubKeyIterator(KEY_TYPE element) { - index = findIndex(element); - } - @Override - public KEY_TYPE PREVIOUS() { - return keys[previousEntry()]; - } - - @Override - public KEY_TYPE NEXT() { - return keys[nextEntry()]; - } - - @Override - public void set(KEY_TYPE e) { throw new UnsupportedOperationException(); } - - @Override - public void add(KEY_TYPE e) { throw new UnsupportedOperationException(); } - } - - private class SubValueIterator extends SubMapIterator implements VALUE_LIST_ITERATOR VALUE_GENERIC_TYPE { - @Override - public VALUE_TYPE VALUE_PREVIOUS() { - return values[previousEntry()]; - } - - @Override - public VALUE_TYPE VALUE_NEXT() { - return values[nextEntry()]; - } - - @Override - public void set(VALUE_TYPE e) { throw new UnsupportedOperationException(); } - - @Override - public void add(VALUE_TYPE e) { throw new UnsupportedOperationException(); } - } - - private class SubMapIterator { - int index; - int lastReturned = -1; - - public boolean hasNext() { - return index < length; - } - - public boolean hasPrevious() { - return index > 0; - } - - public int nextIndex() { - return index; - } - - public int previousIndex() { - return index-1; - } - - public void remove() { - if(lastReturned == -1) - throw new IllegalStateException(); - removeIndex(lastReturned); - if(lastReturned < index) - index--; - lastReturned = -1; - } - - public int previousEntry() { - if(!hasPrevious()) throw new NoSuchElementException(); - int returnIndex = offset+index; - lastReturned = index--; - return returnIndex; - } - - public int nextEntry() { - if(!hasNext()) throw new NoSuchElementException(); - int returnIndex = offset+index; - lastReturned = index++; - return returnIndex; - } - - public int skip(int amount) { - if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed"); - int steps = Math.min(amount, (size() - 1) - index); - index += steps; - return steps; - } - - public int back(int amount) { - if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed"); - int steps = Math.min(amount, index); - index -= steps; - return steps; - } - } - } +// private class SubMap extends ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE implements SORTED_MAP KEY_VALUE_GENERIC_TYPE { +// int offset; +// int length; +// +// SET KEY_GENERIC_TYPE keySet; +// VALUE_COLLECTION VALUE_GENERIC_TYPE valuesC; +// FastSortedSet KEY_VALUE_GENERIC_TYPE entrySet; +// +// public SubMap(int offset, int length) { +// this.offset = offset; +// this.length = length; +// } +// +// int end() { return offset+length; } +// +// @Override +// public VALUE_TYPE put(KEY_TYPE key, VALUE_TYPE value) { +// int index = findIndex(key); +// if(index < 0) { +// insertIndex(end(), key, value); +// length++; +// return getDefaultReturnValue(); +// } +// VALUE_TYPE oldValue = values[index]; +// values[index] = value; +// return oldValue; +// } +// +// @Override +// public VALUE_TYPE putIfAbsent(KEY_TYPE key, VALUE_TYPE value) { +// int index = findIndex(key); +// if(index >= 0) { +// insertIndex(end(), key, value); +// length++; +// } +// return getDefaultReturnValue(); +// } +// +//#if VALUE_PRIMITIVES +// @Override +// public VALUE_TYPE addTo(KEY_TYPE key, VALUE_TYPE value) { +// int index = findIndex(key); +// if(index < 0) { +// insertIndex(end(), key, value); +// length++; +// return getDefaultReturnValue(); +// } +// VALUE_TYPE oldValue = values[index]; +// values[index] += value; +// return oldValue; +// } +// +//#endif +// @Override +// public VALUE_TYPE putAndMoveToFirst(KEY_TYPE key, VALUE_TYPE value) { +// int index = findIndex(key); +// if(index < 0) { +// insertIndex(offset, key, value); +// length++; +// return getDefaultReturnValue(); +// } +// VALUE_TYPE lastValue = values[index]; +// values[index] = value; +// moveIndexToFirst(index); +// return lastValue; +// } +// +// @Override +// public VALUE_TYPE putAndMoveToLast(KEY_TYPE key, VALUE_TYPE value) { +// int index = findIndex(key); +// if(index < 0) { +// insertIndex(end(), key, value); +// length++; +// return getDefaultReturnValue(); +// } +// VALUE_TYPE lastValue = values[index]; +// values[index] = value; +// moveIndexToLast(index); +// return lastValue; +// } +// +// @Override +// public boolean moveToFirst(KEY_TYPE key) { +// int index = findIndex(key); +// if(index > 0) { +// moveIndexToFirst(index); +// return true; +// } +// return false; +// } +// +// @Override +// public boolean moveToLast(KEY_TYPE key) { +// int index = findIndex(key); +// if(index < size()-1) { +// moveIndexToLast(index); +// return true; +// } +// return false; +// } +// +//#if !TYPE_OBJECT +// @Override +// public boolean containsKey(KEY_TYPE key) { +// return findIndex(key) >= 0; +// } +// +//#endif +//#if !VALUE_OBJECT +// @Override +// public boolean containsValue(VALUE_TYPE value) { +// return findValue(value) >= 0; +// } +// +//#endif +// @Override +// public boolean containsKey(Object key) { +// return findIndex(key) >= 0; +// } +// +// @Override +// public boolean containsValue(Object value) { +// return findValue(value) >= 0; +// } +// +// @Override +// public VALUE_TYPE GET_VALUE(KEY_TYPE key) { +// int index = findIndex(key); +// return index < 0 ? getDefaultReturnValue() : values[index]; +// } +// +//#if TYPE_OBJECT && VALUE_OBJECT +// @Override +// public VALUE_TYPE getOrDefault(Object key, VALUE_TYPE defaultValue) { +// int index = findIndex(key); +// return index < 0 ? defaultValue : values[index]; +// } +// +//#else +// @Override +// public VALUE_TYPE getOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue) { +// int index = findIndex(key); +// return index < 0 ? defaultValue : values[index]; +// } +// +//#endif +// @Override +// public VALUE_TYPE getAndMoveToFirst(KEY_TYPE key) { +// int index = findIndex(key); +// if(index >= 0) { +// VALUE_TYPE value = values[index]; +// moveIndexToFirst(index); +// return value; +// } +// return getDefaultReturnValue(); +// } +// +// @Override +// public VALUE_TYPE getAndMoveToLast(KEY_TYPE key) { +// int index = findIndex(key); +// if(index >= 0) { +// VALUE_TYPE value = values[index]; +// moveIndexToLast(index); +// return value; +// } +// return getDefaultReturnValue(); +// } +// +// @Override +// public KEY_TYPE FIRST_ENTRY_KEY() { +// if(length <= 0) throw new NoSuchElementException(); +// return keys[offset]; +// } +// +// @Override +// public KEY_TYPE LAST_ENTRY_KEY() { +// if(length <= 0) throw new NoSuchElementException(); +// return keys[end()-1]; +// } +// +// @Override +// public VALUE_TYPE FIRST_ENTRY_VALUE() { +// if(length <= 0) throw new NoSuchElementException(); +// return values[end()-1]; +// } +// +// @Override +// public VALUE_TYPE LAST_ENTRY_VALUE() { +// if(length <= 0) throw new NoSuchElementException(); +// return values[offset]; +// } +// +// @Override +// public KEY_TYPE POLL_FIRST_ENTRY_KEY() { +// if(length == 0) throw new NoSuchElementException(); +// KEY_TYPE result = keys[offset]; +// removeIndex(offset); +// length--; +// return result; +// } +// +// @Override +// public KEY_TYPE POLL_LAST_ENTRY_KEY() { +// if(length == 0) throw new NoSuchElementException(); +// KEY_TYPE result = keys[end()-1]; +// removeIndex(end()-1); +// length--; +// return result; +// } +// +// @Override +// public VALUE_TYPE REMOVE_VALUE(KEY_TYPE key) { +// int index = findIndex(key); +// if(index < 0) return getDefaultReturnValue(); +// VALUE_TYPE value = values[index]; +// removeIndex(index); +// length--; +// return value; +// } +// +// @Override +// public VALUE_TYPE REMOVE_VALUEOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue) { +// int index = findIndex(key); +// if(index < 0) return defaultValue; +// VALUE_TYPE value = values[index]; +// removeIndex(index); +// length--; +// return value; +// } +// +//#if !TYPE_OBJECT || !VALUE_OBJECT +// @Override +// public boolean remove(KEY_TYPE key, VALUE_TYPE value) { +// int index = findIndex(key, value); +// if(index < 0) return false; +// removeIndex(index); +// length--; +// return true; +// } +// +//#endif +// @Override +// public CLASS_VALUE_TYPE remove(Object key) { +// int index = findIndex(key); +// if(index < 0) return VALUE_TO_OBJ(getDefaultReturnValue()); +// VALUE_TYPE value = values[index]; +// removeIndex(index); +// length--; +// return VALUE_TO_OBJ(value); +// } +// +// @Override +// public boolean remove(Object key, Object value) { +// int index = findIndex(key, value); +// if(index < 0) return false; +// removeIndex(index); +// length--; +// return true; +// } +// +// @Override +// public int size() { +// return length; +// } +// +// @Override +// public void clear() { +// removeRange(offset, offset+length); +// length = 0; +// } +// +// @Override +// public SET KEY_GENERIC_TYPE keySet() { +// if(keySet == null) keySet = new SubKeySet(); +// return keySet; +// } +// +// @Override +// public VALUE_COLLECTION VALUE_GENERIC_TYPE values() { +// if(valuesC == null) valuesC = new SubValues(); +// return valuesC; +// } +// +// @Override +// public ObjectSet ENTRY_SET() { +// if(entrySet == null) entrySet = new SubMapEntrySet(); +// return entrySet; +// } +// +// @Override +// public SubMap copy() { +// throw new UnsupportedOperationException(); +// } +// +// @Override +// public COMPARATOR KEY_GENERIC_TYPE comparator() { +// return null; +// } +// +// @Override +// public SORTED_MAP KEY_VALUE_GENERIC_TYPE subMap(KEY_TYPE fromKey, KEY_TYPE toKey) { +// int fromIndex = findIndex(fromKey); +// int toIndex = findIndex(toKey); +// if(fromIndex == -1 || toIndex == -1) throw new NoSuchElementException(); +// return new SubMap(fromIndex, toIndex - fromIndex + 1); +// } +// +// @Override +// public SORTED_MAP KEY_VALUE_GENERIC_TYPE headMap(KEY_TYPE toKey) { +// int toIndex = findIndex(toKey); +// if(toIndex == -1) throw new NoSuchElementException(); +// return new SubMap(offset, toIndex + 1); +// } +// +// @Override +// public SORTED_MAP KEY_VALUE_GENERIC_TYPE tailMap(KEY_TYPE fromKey) { +// int fromIndex = findIndex(fromKey); +// if(fromIndex == -1) throw new NoSuchElementException(); +// return new SubMap(fromIndex, size - fromIndex); +// } +// +// protected void moveIndexToFirst(int index) { +// if(index == 0) return; +// KEY_TYPE key = keys[index]; +// VALUE_TYPE value = values[index]; +// System.arraycopy(keys, offset, keys, offset+1, index); +// System.arraycopy(values, offset, values, offset+1, index); +// keys[offset] = key; +// values[offset] = value; +// } +// +// protected void moveIndexToLast(int index) { +// if(index == length-1) return; +// KEY_TYPE key = keys[index]; +// VALUE_TYPE value = values[index]; +// System.arraycopy(keys, index+1, keys, index, end()-index-1); +// System.arraycopy(values, index+1, values, index, end()-index-1); +// keys[end()-1] = key; +// values[end()-1] = value; +// } +// +//#if !TYPE_OBJECT || !VALUE_OBJECT +// protected int findIndex(KEY_TYPE key, VALUE_TYPE value) { +// for(int i = length-1;i>=0;i--) +// if(KEY_EQUALS(keys[offset+i], key) && VALUE_EQUALS(values[offset+i], value)) return i; +// return -1; +// } +// +//#endif +//#if !TYPE_OBJECT +// protected int findIndex(KEY_TYPE key) { +// for(int i = length-1;i>=0;i--) +// if(KEY_EQUALS(keys[offset+i], key)) return i+offset; +// return -1; +// } +// +//#endif +//#if !VALUE_OBJECT +// protected int findValue(VALUE_TYPE value) { +// for(int i = length-1;i>=0;i--) +// if(VALUE_EQUALS(values[offset+i], value)) return i+offset; +// return -1; +// } +// +//#endif +// protected int findIndex(Object key, Object value) { +// for(int i = length-1;i>=0;i--) +// if(EQUALS_KEY_TYPE(keys[offset+i], key) && EQUALS_VALUE_TYPE(values[offset+i], value)) return i+offset; +// return -1; +// } +// +// protected int findIndex(Object key) { +// for(int i = length-1;i>=0;i--) +// if(EQUALS_KEY_TYPE(keys[offset+i], key)) return i+offset; +// return -1; +// } +// +// protected int findValue(Object value) { +// for(int i = length-1;i>=0;i--) +// if(EQUALS_VALUE_TYPE(values[offset+i], value)) return i+offset; +// return -1; +// } +// +// private class SubMapEntrySet extends AbstractObjectSet implements SORTED_MAP.FastSortedSet KEY_VALUE_GENERIC_TYPE { +// @Override +// public boolean addAndMoveToFirst(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); } +// @Override +// public boolean addAndMoveToLast(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { throw new UnsupportedOperationException(); } +// +// @Override +// public boolean moveToFirst(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { +// return SubMap.this.moveToFirst(o.ENTRY_KEY()); +// } +// +// @Override +// public boolean moveToLast(MAP.Entry KEY_VALUE_GENERIC_TYPE o) { +// return SubMap.this.moveToLast(o.ENTRY_KEY()); +// } +// +// @Override +// public MAP.Entry KEY_VALUE_GENERIC_TYPE first() { +// return new BasicEntryKV_BRACES(FIRST_ENTRY_KEY(), FIRST_ENTRY_VALUE()); +// } +// +// @Override +// public MAP.Entry KEY_VALUE_GENERIC_TYPE last() { +// return new BasicEntryKV_BRACES(LAST_ENTRY_KEY(), LAST_ENTRY_VALUE()); +// } +// +// @Override +// public MAP.Entry KEY_VALUE_GENERIC_TYPE pollFirst() { +// BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES(FIRST_ENTRY_KEY(), FIRST_ENTRY_VALUE()); +// POLL_FIRST_ENTRY_KEY(); +// return entry; +// } +// +// @Override +// public MAP.Entry KEY_VALUE_GENERIC_TYPE pollLast() { +// BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES(LAST_ENTRY_KEY(), LAST_ENTRY_VALUE()); +// POLL_LAST_ENTRY_KEY(); +// return entry; +// } +// +// @Override +// public ObjectBidirectionalIterator iterator() { +// return new SubEntryIterator(); +// } +// +// @Override +// public ObjectBidirectionalIterator iterator(MAP.Entry KEY_VALUE_GENERIC_TYPE fromElement) { +// return new SubEntryIterator(fromElement.ENTRY_KEY()); +// } +// +// @Override +// public ObjectBidirectionalIterator fastIterator() { +// return new SubFastEntryIterator(); +// } +// +// @Override +// public ObjectBidirectionalIterator fastIterator(KEY_TYPE fromElement) { +// return new SubFastEntryIterator(fromElement); +// } +// +// @Override +// public SubMapEntrySet copy() { throw new UnsupportedOperationException(); } +// +// @Override +// public void forEach(Consumer action) { +// Objects.requireNonNull(action); +// for(int i = 0;i action) { +// Objects.requireNonNull(action); +// if(size() <= 0) return; +// BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES(); +// for(int i = 0;i void forEach(E input, ObjectObjectConsumer action) { +// Objects.requireNonNull(action); +// for(int i = 0;i filter) { +// Objects.requireNonNull(filter); +// if(size() <= 0) return false; +// BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES(); +// for(int i = 0;i filter) { +// Objects.requireNonNull(filter); +// if(size() <= 0) return true; +// BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES(); +// for(int i = 0;i filter) { +// Objects.requireNonNull(filter); +// if(size() <= 0) return true; +// BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES(); +// for(int i = 0;i E reduce(E identity, BiFunction operator) { +// Objects.requireNonNull(operator); +// E state = identity; +// for(int i = 0;i operator) { +// Objects.requireNonNull(operator); +// MAP.Entry KEY_VALUE_GENERIC_TYPE state = null; +// boolean empty = true; +// for(int i = 0;i filter) { +// Objects.requireNonNull(filter); +// if(size() <= 0) return null; +// BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES(); +// for(int i = 0;i filter) { +// Objects.requireNonNull(filter); +// if(size() <= 0) return 0; +// int result = 0; +// BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES(); +// for(int i = 0;i= 0; +// } +// else { +// Map.Entry entry = (Map.Entry)o; +// return ARRAY_MAP.this.findIndex(entry.getKey(), entry.getValue()) >= 0; +// } +// } +// return false; +// } +// +// @Override +// @Deprecated +// public boolean remove(Object o) { +// if(o instanceof Map.Entry) { +// if(o instanceof MAP.Entry) { +// MAP.Entry KEY_VALUE_GENERIC_TYPE entry = (MAP.Entry KEY_VALUE_GENERIC_TYPE)o; +// return SubMap.this.remove(entry.ENTRY_KEY(), entry.ENTRY_VALUE()); +// } +// Map.Entry entry = (Map.Entry)o; +// return SubMap.this.remove(entry.getKey(), entry.getValue()); +// } +// return false; +// } +// +// @Override +// public int size() { +// return SubMap.this.size(); +// } +// +// @Override +// public void clear() { +// SubMap.this.clear(); +// } +// +// @Override +// public Comparator comparator() { +// return null; +// } +// @Override +// public ObjectSortedSet subSet(MAP.Entry KEY_VALUE_GENERIC_TYPE fromElement, MAP.Entry KEY_VALUE_GENERIC_TYPE toElement) { throw new UnsupportedOperationException(); } +// @Override +// public ObjectSortedSet headSet(MAP.Entry KEY_VALUE_GENERIC_TYPE toElement) { throw new UnsupportedOperationException(); } +// @Override +// public ObjectSortedSet tailSet(MAP.Entry KEY_VALUE_GENERIC_TYPE fromElement) { throw new UnsupportedOperationException(); } +// } +// +// private class SubKeySet extends ABSTRACT_SET KEY_GENERIC_TYPE implements SORTED_SET KEY_GENERIC_TYPE { +//#if TYPE_OBJECT +// @Override +// public boolean contains(Object e) { return containsKey(e); } +// +// @Override +// public boolean remove(Object o) { +// int oldSize = length; +// SubMap.this.remove(o); +// return length != oldSize; +// } +// +//#else +// @Override +// public boolean contains(KEY_TYPE e) { return containsKey(e); } +// +// @Override +// public boolean remove(KEY_TYPE o) { +// int oldSize = length; +// SubMap.this.remove(o); +// return length != oldSize; +// } +// +//#endif +// @Override +// public boolean add(KEY_TYPE o) { throw new UnsupportedOperationException(); } +// @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) { return SubMap.this.moveToFirst(o); } +// @Override +// public boolean moveToLast(KEY_TYPE o) { return SubMap.this.moveToLast(o); } +// @Override +// public LIST_ITERATOR KEY_GENERIC_TYPE iterator() { return new SubKeyIterator(); } +// @Override +// public BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement) { return new SubKeyIterator(fromElement); } +// @Override +// public int size() { return SubMap.this.size(); } +// @Override +// public void clear() { SubMap.this.clear(); } +// @Override +// public KEY_TYPE FIRST_KEY() { return FIRST_ENTRY_KEY(); } +// @Override +// public KEY_TYPE POLL_FIRST_KEY() { return POLL_FIRST_ENTRY_KEY(); } +// @Override +// public KEY_TYPE LAST_KEY() { return LAST_ENTRY_KEY(); } +// @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); +// for(int i = 0;i void forEach(E input, BI_FROM_OBJECT_CONSUMER KSK_GENERIC_TYPE action) { +// Objects.requireNonNull(action); +// if(size() <= 0) return; +// for(int i = 0;i KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction operator) { +// Objects.requireNonNull(operator); +// KEY_SPECIAL_TYPE state = identity; +// for(int i = 0;i void forEach(E input, VALUE_BI_FROM_OBJECT_CONSUMER VSV_GENERIC_TYPE action) { +// Objects.requireNonNull(action); +// for(int i = 0;i VALUE_SPECIAL_TYPE reduce(VALUE_SPECIAL_TYPE identity, BiFunction operator) { +// Objects.requireNonNull(operator); +// VALUE_SPECIAL_TYPE state = identity; +// for(int i = 0;i { +// MapEntry entry = new MapEntry(); +// +// public SubFastEntryIterator() {} +// public SubFastEntryIterator(KEY_TYPE from) { +// index = findIndex(from); +// } +// +// @Override +// public MAP.Entry KEY_VALUE_GENERIC_TYPE next() { +// entry.index = nextEntry(); +// return entry; +// } +// +// @Override +// public MAP.Entry KEY_VALUE_GENERIC_TYPE previous() { +// entry.index = previousEntry(); +// return entry; +// } +// +// @Override +// public void set(MAP.Entry KEY_VALUE_GENERIC_TYPE e) { throw new UnsupportedOperationException(); } +// @Override +// public void add(MAP.Entry KEY_VALUE_GENERIC_TYPE e) { throw new UnsupportedOperationException(); } +// } +// +// private class SubEntryIterator extends SubMapIterator implements ObjectListIterator { +// MapEntry entry = null; +// +// public SubEntryIterator() {} +// public SubEntryIterator(KEY_TYPE from) { +// index = findIndex(from); +// } +// +// @Override +// public MAP.Entry KEY_VALUE_GENERIC_TYPE next() { +// return entry = new MapEntry(nextEntry()); +// } +// +// @Override +// public MAP.Entry KEY_VALUE_GENERIC_TYPE previous() { +// return entry = new MapEntry(previousEntry()); +// } +// +// @Override +// public void remove() { +// super.remove(); +// entry.index = -1; +// } +// +// @Override +// public void set(MAP.Entry KEY_VALUE_GENERIC_TYPE e) { throw new UnsupportedOperationException(); } +// @Override +// public void add(MAP.Entry KEY_VALUE_GENERIC_TYPE e) { throw new UnsupportedOperationException(); } +// } +// +// private class SubKeyIterator extends SubMapIterator implements LIST_ITERATOR KEY_GENERIC_TYPE { +// public SubKeyIterator() {} +// public SubKeyIterator(KEY_TYPE element) { +// index = findIndex(element); +// } +// @Override +// public KEY_TYPE PREVIOUS() { +// return keys[previousEntry()]; +// } +// +// @Override +// public KEY_TYPE NEXT() { +// return keys[nextEntry()]; +// } +// +// @Override +// public void set(KEY_TYPE e) { throw new UnsupportedOperationException(); } +// +// @Override +// public void add(KEY_TYPE e) { throw new UnsupportedOperationException(); } +// } +// +// private class SubValueIterator extends SubMapIterator implements VALUE_LIST_ITERATOR VALUE_GENERIC_TYPE { +// @Override +// public VALUE_TYPE VALUE_PREVIOUS() { +// return values[previousEntry()]; +// } +// +// @Override +// public VALUE_TYPE VALUE_NEXT() { +// return values[nextEntry()]; +// } +// +// @Override +// public void set(VALUE_TYPE e) { throw new UnsupportedOperationException(); } +// +// @Override +// public void add(VALUE_TYPE e) { throw new UnsupportedOperationException(); } +// } +// +// private class SubMapIterator { +// int index; +// int lastReturned = -1; +// +// public boolean hasNext() { +// return index < length; +// } +// +// public boolean hasPrevious() { +// return index > 0; +// } +// +// public int nextIndex() { +// return index; +// } +// +// public int previousIndex() { +// return index-1; +// } +// +// public void remove() { +// if(lastReturned == -1) +// throw new IllegalStateException(); +// removeIndex(lastReturned); +// if(lastReturned < index) +// index--; +// lastReturned = -1; +// } +// +// public int previousEntry() { +// if(!hasPrevious()) throw new NoSuchElementException(); +// int returnIndex = offset+index; +// lastReturned = index--; +// return returnIndex; +// } +// +// public int nextEntry() { +// if(!hasNext()) throw new NoSuchElementException(); +// int returnIndex = offset+index; +// lastReturned = index++; +// return returnIndex; +// } +// +// public int skip(int amount) { +// if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed"); +// int steps = Math.min(amount, (size() - 1) - index); +// index += steps; +// return steps; +// } +// +// public int back(int amount) { +// if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed"); +// int steps = Math.min(amount, index); +// index -= steps; +// return steps; +// } +// } +// } private class MapEntrySet extends AbstractObjectSet implements SORTED_MAP.FastSortedSet KEY_VALUE_GENERIC_TYPE { @Override diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/impl/tree/AVLTreeMap.template b/src/builder/resources/speiger/assets/collections/templates/maps/impl/tree/AVLTreeMap.template index 97293231..804ea485 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/impl/tree/AVLTreeMap.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/impl/tree/AVLTreeMap.template @@ -1,13 +1,14 @@ package speiger.src.collections.PACKAGE.maps.impl.tree; +import java.util.Collections; import java.util.Map; #if TYPE_OBJECT import java.util.Comparator; #endif -import java.util.function.Consumer; -import java.util.function.BiFunction; import java.util.Objects; import java.util.NoSuchElementException; +import java.util.function.Consumer; +import java.util.function.BiFunction; import speiger.src.collections.PACKAGE.collections.BI_ITERATOR; #if !TYPE_OBJECT @@ -24,7 +25,6 @@ import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR; #if !SAME_TYPE import speiger.src.collections.PACKAGE.functions.function.SINGLE_UNARY_OPERATOR; #endif -import speiger.src.collections.PACKAGE.lists.LIST_ITERATOR; import speiger.src.collections.PACKAGE.maps.abstracts.ABSTRACT_MAP; import speiger.src.collections.PACKAGE.maps.interfaces.MAP; import speiger.src.collections.PACKAGE.maps.interfaces.NAVIGABLE_MAP; @@ -32,16 +32,14 @@ import speiger.src.collections.PACKAGE.sets.ABSTRACT_SET; 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.ITERATORS; import speiger.src.collections.PACKAGE.utils.maps.MAPS; import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_ABSTRACT_COLLECTION; import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_COLLECTION; import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_ITERATOR; import speiger.src.collections.VALUE_PACKAGE.functions.VALUE_SUPPLIER; #if !SAME_TYPE +import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_BI_ITERATOR; import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_UNARY_OPERATOR; -import speiger.src.collections.VALUE_PACKAGE.lists.VALUE_LIST_ITERATOR; -import speiger.src.collections.VALUE_PACKAGE.utils.VALUE_ITERATORS; #endif #if !VALUE_OBJECT && !SAME_TYPE import speiger.src.collections.VALUE_PACKAGE.functions.VALUE_CONSUMER; @@ -62,8 +60,7 @@ import speiger.src.collections.objects.functions.consumer.VALUE_BI_FROM_OBJECT_C import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_PREDICATE; #endif #if !TYPE_OBJECT && !VALUE_OBJECT -import speiger.src.collections.objects.lists.ObjectListIterator; -import speiger.src.collections.objects.utils.ObjectIterators; +import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; #endif #if !VALUE_OBJECT import speiger.src.collections.objects.collections.ObjectIterator; @@ -88,11 +85,11 @@ import speiger.src.collections.objects.sets.ObjectSet; public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE implements NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE { /** The center of the Tree */ - protected transient Entry KEY_VALUE_GENERIC_TYPE tree; + protected transient Node KEY_VALUE_GENERIC_TYPE tree; /** The Lowest possible Node */ - protected transient Entry KEY_VALUE_GENERIC_TYPE first; + protected transient Node KEY_VALUE_GENERIC_TYPE first; /** The Highest possible Node */ - protected transient Entry KEY_VALUE_GENERIC_TYPE last; + protected transient Node KEY_VALUE_GENERIC_TYPE last; /** The amount of elements stored in the Map */ protected int size = 0; /** The Sorter of the Tree */ @@ -209,7 +206,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ comparator = comp; putAll(map); } - + #if TYPE_OBJECT /** only used for primitives * @return null @@ -231,18 +228,19 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ public KEY_TYPE getDefaultMinValue() { return defaultMinNotFound; } #endif + @Override public VALUE_TYPE put(KEY_TYPE key, VALUE_TYPE value) { #if TYPE_OBJECT validate(key); #endif if(tree == null) { - tree = first = last = new EntryKV_BRACES(key, value, null); + tree = first = last = new NodeKV_BRACES(key, value, null); size++; return getDefaultReturnValue(); } int compare = 0; - Entry KEY_VALUE_GENERIC_TYPE parent = tree; + Node KEY_VALUE_GENERIC_TYPE parent = tree; while(true) { if((compare = compare(key, parent.key)) == 0) return parent.setValue(value); if(compare < 0) { @@ -254,7 +252,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ parent = parent.right; } } - Entry KEY_VALUE_GENERIC_TYPE adding = new EntryKV_BRACES(key, value, parent); + Node KEY_VALUE_GENERIC_TYPE adding = new NodeKV_BRACES(key, value, parent); if(compare < 0) { parent.left = adding; if(parent == first) first = adding; @@ -274,12 +272,12 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ validate(key); #endif if(tree == null) { - tree = first = last = new EntryKV_BRACES(key, value, null); + tree = first = last = new NodeKV_BRACES(key, value, null); size++; return getDefaultReturnValue(); } int compare = 0; - Entry KEY_VALUE_GENERIC_TYPE parent = tree; + Node KEY_VALUE_GENERIC_TYPE parent = tree; while(true) { if((compare = compare(key, parent.key)) == 0) return parent.value; if(compare < 0) { @@ -291,7 +289,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ parent = parent.right; } } - Entry KEY_VALUE_GENERIC_TYPE adding = new EntryKV_BRACES(key, value, parent); + Node KEY_VALUE_GENERIC_TYPE adding = new NodeKV_BRACES(key, value, parent); if(compare < 0) { parent.left = adding; if(parent == first) first = adding; @@ -312,12 +310,12 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ validate(key); #endif if(tree == null) { - tree = first = last = new EntryKV_BRACES(key, value, null); + tree = first = last = new NodeKV_BRACES(key, value, null); size++; return getDefaultReturnValue(); } int compare = 0; - Entry KEY_VALUE_GENERIC_TYPE parent = tree; + Node KEY_VALUE_GENERIC_TYPE parent = tree; while(true) { if((compare = compare(key, parent.key)) == 0) return parent.addTo(value); if(compare < 0) { @@ -329,7 +327,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ parent = parent.right; } } - Entry KEY_VALUE_GENERIC_TYPE adding = new EntryKV_BRACES(key, value, parent); + Node KEY_VALUE_GENERIC_TYPE adding = new NodeKV_BRACES(key, value, parent); if(compare < 0) { parent.left = adding; if(parent == first) first = adding; @@ -359,7 +357,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ @Override public COMPARATOR KEY_GENERIC_TYPE comparator() { return comparator; } - + #if TYPE_OBJECT @Override public boolean containsKey(Object key) { @@ -373,24 +371,23 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ } #endif - @Override public VALUE_TYPE GET_VALUE(KEY_TYPE key) { - Entry KEY_VALUE_GENERIC_TYPE node = findNode(key); + Node KEY_VALUE_GENERIC_TYPE node = findNode(key); return node == null ? getDefaultReturnValue() : node.value; } #if TYPE_OBJECT && VALUE_OBJECT @Override public VALUE_TYPE getOrDefault(Object key, VALUE_TYPE defaultValue) { - Entry KEY_VALUE_GENERIC_TYPE node = findNode((CLASS_TYPE)key); + Node KEY_VALUE_GENERIC_TYPE node = findNode((CLASS_TYPE)key); return node == null ? defaultValue : node.value; } #else @Override public VALUE_TYPE getOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue) { - Entry KEY_VALUE_GENERIC_TYPE node = findNode(key); + Node KEY_VALUE_GENERIC_TYPE node = findNode(key); return node == null ? defaultValue : node.value; } @@ -403,7 +400,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ @Override public KEY_TYPE POLL_FIRST_ENTRY_KEY() { - if(tree == null) throw new NoSuchElementException(); + if(tree == null) return EMPTY_KEY_VALUE; KEY_TYPE result = first.key; removeNode(first); return result; @@ -417,7 +414,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ @Override public KEY_TYPE POLL_LAST_ENTRY_KEY() { - if(tree == null) throw new NoSuchElementException(); + if(tree == null) return EMPTY_KEY_VALUE; KEY_TYPE result = last.key; removeNode(last); return result; @@ -425,28 +422,28 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE firstEntry() { - if(tree == null) throw new NoSuchElementException(); - return new BasicEntryKV_BRACES(first.key, first.value); + if(tree == null) return null; + return first.export(); } @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE lastEntry() { - if(tree == null) throw new NoSuchElementException(); - return new BasicEntryKV_BRACES(last.key, last.value); + if(tree == null) return null; + return last.export(); } @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE pollFirstEntry() { - if(tree == null) throw new NoSuchElementException(); - BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES(first.key, first.value); + if(tree == null) return null; + BasicEntry KEY_VALUE_GENERIC_TYPE entry = first.export(); removeNode(first); return entry; } @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE pollLastEntry() { - if(tree == null) throw new NoSuchElementException(); - BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES(last.key, last.value); + if(tree == null) return null; + BasicEntry KEY_VALUE_GENERIC_TYPE entry = last.export(); removeNode(last); return entry; } @@ -465,7 +462,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ @Override public VALUE_TYPE REMOVE_VALUE(KEY_TYPE key) { - Entry KEY_VALUE_GENERIC_TYPE entry = findNode(key); + Node KEY_VALUE_GENERIC_TYPE entry = findNode(key); if(entry == null) return getDefaultReturnValue(); VALUE_TYPE value = entry.value; removeNode(entry); @@ -474,7 +471,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ @Override public VALUE_TYPE REMOVE_VALUEOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue) { - Entry KEY_VALUE_GENERIC_TYPE entry = findNode(key); + Node KEY_VALUE_GENERIC_TYPE entry = findNode(key); if(entry == null) return defaultValue; VALUE_TYPE value = entry.value; removeNode(entry); @@ -484,7 +481,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ #if TYPE_OBJECT && VALUE_OBJECT @Override public boolean remove(Object key, Object value) { - Entry KEY_VALUE_GENERIC_TYPE entry = findNode((CLASS_TYPE)key); + Node KEY_VALUE_GENERIC_TYPE entry = findNode((CLASS_TYPE)key); if(entry == null || !Objects.equals(value, entry.value)) return false; removeNode(entry); return true; @@ -493,7 +490,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ #else @Override public boolean remove(KEY_TYPE key, VALUE_TYPE value) { - Entry KEY_VALUE_GENERIC_TYPE entry = findNode(key); + Node KEY_VALUE_GENERIC_TYPE entry = findNode(key); if(entry == null || entry.value != value) return false; removeNode(entry); return true; @@ -502,7 +499,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ #endif @Override public boolean replace(KEY_TYPE key, VALUE_TYPE oldValue, VALUE_TYPE newValue) { - Entry KEY_VALUE_GENERIC_TYPE entry = findNode(key); + Node KEY_VALUE_GENERIC_TYPE entry = findNode(key); if(entry == null || entry.value != oldValue) return false; entry.value = newValue; return true; @@ -510,7 +507,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ @Override public VALUE_TYPE replace(KEY_TYPE key, VALUE_TYPE value) { - Entry KEY_VALUE_GENERIC_TYPE entry = findNode(key); + Node KEY_VALUE_GENERIC_TYPE entry = findNode(key); if(entry == null) return getDefaultReturnValue(); VALUE_TYPE oldValue = entry.value; entry.value = value; @@ -523,7 +520,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ #if TYPE_OBJECT validate(key); #endif - Entry KEY_VALUE_GENERIC_TYPE entry = findNode(key); + Node KEY_VALUE_GENERIC_TYPE entry = findNode(key); if(entry == null) { VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, getDefaultReturnValue()); if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; @@ -545,14 +542,14 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ #if TYPE_OBJECT validate(key); #endif - Entry KEY_VALUE_GENERIC_TYPE entry = findNode(key); + Node KEY_VALUE_GENERIC_TYPE entry = findNode(key); if(entry == null) { VALUE_TYPE newValue = mappingFunction.GET_VALUE(key); if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; put(key, newValue); return newValue; } - if(VALUE_EQUALS(entry.value, getDefaultReturnValue())) { + if(Objects.equals(entry.value, getDefaultReturnValue())) { VALUE_TYPE newValue = mappingFunction.GET_VALUE(key); if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; entry.value = newValue; @@ -566,7 +563,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ #if TYPE_OBJECT validate(key); #endif - Entry KEY_VALUE_GENERIC_TYPE entry = findNode(key); + Node KEY_VALUE_GENERIC_TYPE entry = findNode(key); if(entry == null) { VALUE_TYPE newValue = valueProvider.VALUE_GET_KEY(); if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; @@ -587,7 +584,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ #if TYPE_OBJECT validate(key); #endif - Entry KEY_VALUE_GENERIC_TYPE entry = findNode(key); + Node KEY_VALUE_GENERIC_TYPE entry = findNode(key); if(entry == null || VALUE_EQUALS(entry.value, getDefaultReturnValue())) return getDefaultReturnValue(); VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, entry.value); if(VALUE_EQUALS(newValue, getDefaultReturnValue())) { @@ -604,7 +601,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ #if TYPE_OBJECT validate(key); #endif - Entry KEY_VALUE_GENERIC_TYPE entry = findNode(key); + Node KEY_VALUE_GENERIC_TYPE entry = findNode(key); VALUE_TYPE newValue = entry == null || VALUE_EQUALS(entry.value, getDefaultReturnValue()) ? value : mappingFunction.APPLY_VALUE(entry.value, value); if(VALUE_EQUALS(newValue, getDefaultReturnValue())) { if(entry != null) @@ -620,7 +617,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ Objects.requireNonNull(mappingFunction); for(MAP.Entry KEY_VALUE_GENERIC_TYPE entry : MAPS.fastIterable(m)) { KEY_TYPE key = entry.ENTRY_KEY(); - Entry KEY_VALUE_GENERIC_TYPE subEntry = findNode(key); + Node KEY_VALUE_GENERIC_TYPE subEntry = findNode(key); VALUE_TYPE newValue = subEntry == null || VALUE_EQUALS(subEntry.value, getDefaultReturnValue()) ? entry.ENTRY_VALUE() : mappingFunction.APPLY_VALUE(subEntry.value, entry.ENTRY_VALUE()); if(VALUE_EQUALS(newValue, getDefaultReturnValue())) { if(subEntry != null) @@ -633,7 +630,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ @Override public void forEach(BI_CONSUMER KEY_VALUE_GENERIC_TYPE action) { - for(Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) + for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) action.accept(entry.key, entry.value); } @@ -648,25 +645,29 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ tree = null; } - LIST_ITERATOR KEY_GENERIC_TYPE keyIterator(boolean descending) { - LIST_ITERATOR KEY_GENERIC_TYPE iter = new KeyIterator(descending); - return descending ? ITERATORS.invert(iter) : iter; + protected BI_ITERATOR KEY_GENERIC_TYPE keyIterator() { + return new AscendingKeyIterator(first); } - LIST_ITERATOR KEY_GENERIC_TYPE keyIterator(KEY_TYPE key) { - return new KeyIterator(findNode(key)); + protected BI_ITERATOR KEY_GENERIC_TYPE keyIterator(KEY_TYPE element) { + return null; } + protected BI_ITERATOR KEY_GENERIC_TYPE descendingKeyIterator() { + return new DescendingKeyIterator(last); + } + + @Override public AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE copy() { AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE set = new AVL_TREE_MAPKV_BRACES(); set.size = size; if(tree != null) { set.tree = tree.copy(); - Entry KEY_VALUE_GENERIC_TYPE lastFound = null; - for(Entry KEY_VALUE_GENERIC_TYPE entry = tree;entry != null;entry = entry.left) lastFound = entry; + Node KEY_VALUE_GENERIC_TYPE lastFound = null; + for(Node KEY_VALUE_GENERIC_TYPE entry = tree;entry != null;entry = entry.left) lastFound = entry; set.first = lastFound; lastFound = null; - for(Entry KEY_VALUE_GENERIC_TYPE entry = tree;entry != null;entry = entry.right) lastFound = entry; + for(Node KEY_VALUE_GENERIC_TYPE entry = tree;entry != null;entry = entry.right) lastFound = entry; set.last = lastFound; } return set; @@ -697,7 +698,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ @Override public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE descendingMap() { - return new DescendingSubMapKV_BRACES(this, true, EMPTY_KEY_VALUE, true, true, EMPTY_KEY_VALUE, true); + return new DescendingNaivgableSubMapKV_BRACES(this, true, EMPTY_KEY_VALUE, true, true, EMPTY_KEY_VALUE, true); } @Override @@ -707,69 +708,69 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ @Override public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE subMap(KEY_TYPE fromKey, boolean fromInclusive, KEY_TYPE toKey, boolean toInclusive) { - return new AscendingSubMapKV_BRACES(this, false, fromKey, fromInclusive, false, toKey, toInclusive); + return new AscendingNaivgableSubMapKV_BRACES(this, false, fromKey, fromInclusive, false, toKey, toInclusive); } @Override public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE headMap(KEY_TYPE toKey, boolean inclusive) { - return new AscendingSubMapKV_BRACES(this, true, EMPTY_KEY_VALUE, true, false, toKey, inclusive); + return new AscendingNaivgableSubMapKV_BRACES(this, true, EMPTY_KEY_VALUE, true, false, toKey, inclusive); } @Override public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE tailMap(KEY_TYPE fromKey, boolean inclusive) { - return new AscendingSubMapKV_BRACES(this, false, fromKey, inclusive, true, EMPTY_KEY_VALUE, true); + return new AscendingNaivgableSubMapKV_BRACES(this, false, fromKey, inclusive, true, EMPTY_KEY_VALUE, true); } @Override public KEY_TYPE lowerKey(KEY_TYPE e) { - Entry KEY_VALUE_GENERIC_TYPE node = findLowerNode(e); + Node KEY_VALUE_GENERIC_TYPE node = findLowerNode(e); return node != null ? node.key : getDefaultMinValue(); } @Override public KEY_TYPE floorKey(KEY_TYPE e) { - Entry KEY_VALUE_GENERIC_TYPE node = findFloorNode(e); + Node KEY_VALUE_GENERIC_TYPE node = findFloorNode(e); return node != null ? node.key : getDefaultMinValue(); } @Override public KEY_TYPE higherKey(KEY_TYPE e) { - Entry KEY_VALUE_GENERIC_TYPE node = findHigherNode(e); + Node KEY_VALUE_GENERIC_TYPE node = findHigherNode(e); return node != null ? node.key : getDefaultMaxValue(); } @Override public KEY_TYPE ceilingKey(KEY_TYPE e) { - Entry KEY_VALUE_GENERIC_TYPE node = findCeilingNode(e); + Node KEY_VALUE_GENERIC_TYPE node = findCeilingNode(e); return node != null ? node.key : getDefaultMaxValue(); } @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE lowerEntry(KEY_TYPE key) { - Entry KEY_VALUE_GENERIC_TYPE node = findLowerNode(key); - return node != null ? new BasicEntryKV_BRACES(node.key, node.value) : null; + Node KEY_VALUE_GENERIC_TYPE node = findLowerNode(key); + return node != null ? node.export() : null; } @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE higherEntry(KEY_TYPE key) { - Entry KEY_VALUE_GENERIC_TYPE node = findHigherNode(key); - return node != null ? new BasicEntryKV_BRACES(node.key, node.value) : null; + Node KEY_VALUE_GENERIC_TYPE node = findHigherNode(key); + return node != null ? node.export() : null; } @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE floorEntry(KEY_TYPE key) { - Entry KEY_VALUE_GENERIC_TYPE node = findFloorNode(key); - return node != null ? new BasicEntryKV_BRACES(node.key, node.value) : null; + Node KEY_VALUE_GENERIC_TYPE node = findFloorNode(key); + return node != null ? node.export() : null; } @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE ceilingEntry(KEY_TYPE key) { - Entry KEY_VALUE_GENERIC_TYPE node = findCeilingNode(key); - return node != null ? new BasicEntryKV_BRACES(node.key, node.value) : null; + Node KEY_VALUE_GENERIC_TYPE node = findCeilingNode(key); + return node != null ? node.export() : null; } - protected Entry KEY_VALUE_GENERIC_TYPE findLowerNode(KEY_TYPE key) { - Entry KEY_VALUE_GENERIC_TYPE entry = tree; + protected Node KEY_VALUE_GENERIC_TYPE findLowerNode(KEY_TYPE key) { + Node KEY_VALUE_GENERIC_TYPE entry = tree; while(entry != null) { if(compare(key, entry.key) > 0) { if(entry.right != null) entry = entry.right; @@ -778,7 +779,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ else { if(entry.left != null) entry = entry.left; else { - Entry KEY_VALUE_GENERIC_TYPE parent = entry.parent; + Node KEY_VALUE_GENERIC_TYPE parent = entry.parent; while(parent != null && parent.left == entry) { entry = parent; parent = parent.parent; @@ -790,8 +791,8 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ return null; } - protected Entry KEY_VALUE_GENERIC_TYPE findFloorNode(KEY_TYPE key) { - Entry KEY_VALUE_GENERIC_TYPE entry = tree; + protected Node KEY_VALUE_GENERIC_TYPE findFloorNode(KEY_TYPE key) { + Node KEY_VALUE_GENERIC_TYPE entry = tree; int compare; while(entry != null) { if((compare = compare(key, entry.key)) > 0) { @@ -802,7 +803,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ else if(compare < 0) { if(entry.left != null) entry = entry.left; else { - Entry KEY_VALUE_GENERIC_TYPE parent = entry.parent; + Node KEY_VALUE_GENERIC_TYPE parent = entry.parent; while(parent != null && parent.left == entry) { entry = parent; parent = parent.parent; @@ -816,8 +817,8 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ return entry; } - protected Entry KEY_VALUE_GENERIC_TYPE findCeilingNode(KEY_TYPE key) { - Entry KEY_VALUE_GENERIC_TYPE entry = tree; + protected Node KEY_VALUE_GENERIC_TYPE findCeilingNode(KEY_TYPE key) { + Node KEY_VALUE_GENERIC_TYPE entry = tree; int compare; while(entry != null) { if((compare = compare(key, entry.key)) < 0) { @@ -828,7 +829,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ else if(compare > 0) { if(entry.right != null) entry = entry.right; else { - Entry KEY_VALUE_GENERIC_TYPE parent = entry.parent; + Node KEY_VALUE_GENERIC_TYPE parent = entry.parent; while(parent != null && parent.right == entry) { entry = parent; parent = parent.parent; @@ -842,8 +843,8 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ return entry; } - protected Entry KEY_VALUE_GENERIC_TYPE findHigherNode(KEY_TYPE key) { - Entry KEY_VALUE_GENERIC_TYPE entry = tree; + protected Node KEY_VALUE_GENERIC_TYPE findHigherNode(KEY_TYPE key) { + Node KEY_VALUE_GENERIC_TYPE entry = tree; while(entry != null) { if(compare(key, entry.key) < 0) { if(entry.left != null) entry = entry.left; @@ -852,7 +853,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ else { if(entry.right != null) entry = entry.right; else { - Entry KEY_VALUE_GENERIC_TYPE parent = entry.parent; + Node KEY_VALUE_GENERIC_TYPE parent = entry.parent; while(parent != null && parent.right == entry) { entry = parent; parent = parent.parent; @@ -864,8 +865,8 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ return null; } - protected Entry KEY_VALUE_GENERIC_TYPE findNode(KEY_TYPE key) { - Entry KEY_VALUE_GENERIC_TYPE node = tree; + protected Node KEY_VALUE_GENERIC_TYPE findNode(KEY_TYPE key) { + Node KEY_VALUE_GENERIC_TYPE node = tree; int compare; while(node != null) { if((compare = compare(key, node.key)) == 0) return node; @@ -875,17 +876,17 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ return null; } - protected void removeNode(Entry KEY_VALUE_GENERIC_TYPE entry) { + protected void removeNode(Node KEY_VALUE_GENERIC_TYPE entry) { size--; if(entry.needsSuccessor()) { - Entry KEY_VALUE_GENERIC_TYPE successor = entry.next(); + Node KEY_VALUE_GENERIC_TYPE successor = entry.next(); entry.key = successor.key; entry.value = successor.value; entry = successor; } if(entry.previous() == null) first = entry.next(); if(entry.next() == null) last = entry.previous(); - Entry KEY_VALUE_GENERIC_TYPE replacement = entry.left != null ? entry.left : entry.right; + Node KEY_VALUE_GENERIC_TYPE replacement = entry.left != null ? entry.left : entry.right; if(replacement != null) { if(entry.replace(replacement)) tree = replacement; entry.left = entry.right = entry.parent = null; @@ -903,9 +904,9 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ protected int compare(KEY_TYPE k, KEY_TYPE v) { return comparator != null ? comparator.compare(k, v) : COMPAREABLE_TO_KEY(k, v);} /** From CLR */ - protected void rotateLeft(Entry KEY_VALUE_GENERIC_TYPE entry) { + protected void rotateLeft(Node KEY_VALUE_GENERIC_TYPE entry) { if(entry != null) { - Entry KEY_VALUE_GENERIC_TYPE right = entry.right; + Node KEY_VALUE_GENERIC_TYPE right = entry.right; entry.right = right.left; if(right.left != null) right.left.parent = entry; right.parent = entry.parent; @@ -920,9 +921,9 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ } /** From CLR */ - protected void rotateRight(Entry KEY_VALUE_GENERIC_TYPE entry) { + protected void rotateRight(Node KEY_VALUE_GENERIC_TYPE entry) { if(entry != null) { - Entry KEY_VALUE_GENERIC_TYPE left = entry.left; + Node KEY_VALUE_GENERIC_TYPE left = entry.left; entry.left = left.right; if(left.right != null) left.right.parent = entry; left.parent = entry.parent; @@ -937,7 +938,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ } /** From CLR */ - protected void fixAfterInsertion(Entry KEY_VALUE_GENERIC_TYPE entry) { + protected void fixAfterInsertion(Node KEY_VALUE_GENERIC_TYPE entry) { while(entry != null) { entry.updateHeight(); int balance = entry.getBalance(); @@ -962,7 +963,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ } /** From CLR */ - protected void fixAfterDeletion(Entry KEY_VALUE_GENERIC_TYPE entry) { + protected void fixAfterDeletion(Node KEY_VALUE_GENERIC_TYPE entry) { if(entry != null) { entry.updateHeight(); int balance = entry.getBalance(); @@ -987,254 +988,539 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ } } - static final class AscendingSubMap KEY_VALUE_GENERIC_TYPE extends NavigableSubMap KEY_VALUE_GENERIC_TYPE { + static class KeySet KEY_VALUE_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE implements NAVIGABLE_SET KEY_GENERIC_TYPE + { + NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE map; - public AscendingSubMap(AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE m, boolean fromStart, KEY_TYPE low, boolean loInclusive, boolean toEnd, KEY_TYPE high, boolean hiInclusive) { - super(m, fromStart, low, loInclusive, toEnd, high, hiInclusive); + public KeySet(NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE map) { + this.map = map; } +#if !TYPE_OBJECT @Override - LIST_ITERATOR KEY_GENERIC_TYPE keyIterator(boolean descending) { return new SubMapKeyIterator(descending); } + public void setDefaultMaxValue(KEY_TYPE e) { map.setDefaultMaxValue(e); } @Override - LIST_ITERATOR KEY_GENERIC_TYPE keyIterator(KEY_TYPE key) { - AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = m.findNode(key); - return entry == null || !inClosedRange(key) ? null : new SubMapKeyIterator(entry, fromStart ? null : findLowest(), toEnd ? null : findHighest()); - } + public KEY_TYPE getDefaultMaxValue() { return map.getDefaultMaxValue(); } @Override - VALUE_LIST_ITERATOR VALUE_GENERIC_TYPE valueIterator() { return new SubMapValueIterator(false); } + public void setDefaultMinValue(KEY_TYPE e) { map.setDefaultMinValue(e); } @Override - ObjectListIterator entryIterator() { return new SubMapEntrySetIterator(false); } - + public KEY_TYPE getDefaultMinValue() { return map.getDefaultMinValue(); } +#endif @Override - public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE descendingMap() { - if(descendingMap == null) descendingMap = new DescendingSubMapKV_BRACES(m, fromStart, low, loInclusive, toEnd, high, hiInclusive); - return descendingMap; - } - - AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE next(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry) { - return entry.next(); - } - - AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE previous(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry) { - return entry.previous(); - } - + public KEY_TYPE lower(KEY_TYPE e) { return map.lowerKey(e); } @Override - public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE subMap(KEY_TYPE fromKey, boolean fromInclusive, KEY_TYPE toKey, boolean toInclusive) { - if (!inRange(fromKey, fromInclusive)) throw new IllegalArgumentException("fromKey out of range"); - if (!inRange(toKey, toInclusive)) throw new IllegalArgumentException("toKey out of range"); - return new AscendingSubMapKV_BRACES(m, false, fromKey, fromInclusive, false, toKey, toInclusive); - } - + public KEY_TYPE floor(KEY_TYPE e) { return map.floorKey(e); } @Override - public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE headMap(KEY_TYPE toKey, boolean inclusive) { - if (!inRange(toKey, inclusive)) throw new IllegalArgumentException("toKey out of range"); - return new AscendingSubMapKV_BRACES(m, fromStart, low, loInclusive, false, toKey, inclusive); - } - + public KEY_TYPE ceiling(KEY_TYPE e) { return map.ceilingKey(e); } @Override - public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE tailMap(KEY_TYPE fromKey, boolean inclusive) { - if (!inRange(fromKey, inclusive)) throw new IllegalArgumentException("fromKey out of range"); - return new AscendingSubMapKV_BRACES(m, false, fromKey, inclusive, toEnd, high, hiInclusive); - } - } - - static final class DescendingSubMap KEY_VALUE_GENERIC_TYPE extends NavigableSubMap KEY_VALUE_GENERIC_TYPE { - - public DescendingSubMap(AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE m, boolean fromStart, KEY_TYPE low, boolean loInclusive, boolean toEnd, KEY_TYPE high, boolean hiInclusive) { - super(m, fromStart, low, loInclusive, toEnd, high, hiInclusive); - } - + public KEY_TYPE higher(KEY_TYPE e) { return map.higherKey(e); } @Override - public KEY_TYPE FIRST_ENTRY_KEY() { return super.LAST_ENTRY_KEY(); } - + public KEY_TYPE POLL_FIRST_KEY() { return map.POLL_FIRST_ENTRY_KEY(); } @Override - public KEY_TYPE POLL_FIRST_ENTRY_KEY() { return super.POLL_LAST_ENTRY_KEY(); } - + public KEY_TYPE POLL_LAST_KEY() { return map.POLL_LAST_ENTRY_KEY(); } @Override - public KEY_TYPE LAST_ENTRY_KEY() { return super.FIRST_ENTRY_KEY(); } - + public COMPARATOR KEY_GENERIC_TYPE comparator() { return map.comparator(); } @Override - public KEY_TYPE POLL_LAST_ENTRY_KEY() { return super.POLL_FIRST_ENTRY_KEY(); } - + public KEY_TYPE FIRST_KEY() { return map.FIRST_ENTRY_KEY(); } @Override - public VALUE_TYPE FIRST_ENTRY_VALUE() { return super.LAST_ENTRY_VALUE(); } - + public KEY_TYPE LAST_KEY() { return map.LAST_ENTRY_KEY(); } @Override - public VALUE_TYPE LAST_ENTRY_VALUE() { return super.FIRST_ENTRY_VALUE(); } - - @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE firstEntry() { return super.lastEntry(); } - - @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE lastEntry() { return super.firstEntry(); } - - @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE pollFirstEntry() { return super.pollLastEntry(); } - - @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE pollLastEntry() { return super.pollFirstEntry(); } - - @Override - public KEY_TYPE lowerKey(KEY_TYPE e) { return super.higherKey(e); } - - @Override - public KEY_TYPE floorKey(KEY_TYPE e) { return super.ceilingKey(e); } - - @Override - public KEY_TYPE ceilingKey(KEY_TYPE e) { return super.floorKey(e); } - - @Override - public KEY_TYPE higherKey(KEY_TYPE e) { return super.lowerKey(e); } - - @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE lowerEntry(KEY_TYPE e) { return super.higherEntry(e); } - - @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE floorEntry(KEY_TYPE e) { return super.ceilingEntry(e); } - - @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE ceilingEntry(KEY_TYPE e) { return super.floorEntry(e); } - - @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE higherEntry(KEY_TYPE e) { return super.lowerEntry(e); } - - protected AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE findLowest() { - if(fromStart) return m.first; - AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = loInclusive ? m.findFloorNode(high) : m.findLowerNode(high); - return entry == null || tooHigh(entry.key) ? null : entry; - } - - protected AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE findHighest() { - if(toEnd) return m.last; - AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = hiInclusive ? m.findCeilingNode(low) : m.findHigherNode(low); - return entry == null || tooLow(entry.key) ? null : entry; - } - - @Override - LIST_ITERATOR KEY_GENERIC_TYPE keyIterator(boolean descending) { - LIST_ITERATOR KEY_GENERIC_TYPE iter = new SubMapKeyIterator(!descending); - return descending ? iter : ITERATORS.invert(iter); - } - - @Override - LIST_ITERATOR KEY_GENERIC_TYPE keyIterator(KEY_TYPE key) { - AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = m.findNode(key); - return entry == null || !inClosedRange(key) ? null : ITERATORS.invert(new SubMapKeyIterator(entry, fromStart ? null : findLowest(), toEnd ? null : findHighest())); - } - - AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE next(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry) { - return entry.previous(); - } - - AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE previous(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry) { - return entry.next(); - } - - @Override - public COMPARATOR KEY_GENERIC_TYPE comparator() { return m.comparator() == null ? null : m.comparator().reversed(); } - - @Override - VALUE_LIST_ITERATOR VALUE_GENERIC_TYPE valueIterator() { return VALUE_ITERATORS.invert(new SubMapValueIterator(true)); } - - @Override - ObjectListIterator entryIterator() { return ObjectIterators.invert(new SubMapEntrySetIterator(true)); } - - @Override - public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE descendingMap() { - if(descendingMap == null) descendingMap = new AscendingSubMapKV_BRACES(m, fromStart, low, loInclusive, toEnd, high, hiInclusive); - return descendingMap; - } - - @Override - public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE subMap(KEY_TYPE fromKey, boolean fromInclusive, KEY_TYPE toKey, boolean toInclusive) { - if (!inRange(fromKey, fromInclusive)) throw new IllegalArgumentException("fromKey out of range"); - if (!inRange(toKey, toInclusive)) throw new IllegalArgumentException("toKey out of range"); - return new DescendingSubMapKV_BRACES(m, false, fromKey, fromInclusive, false, toKey, toInclusive); - } - - @Override - public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE headMap(KEY_TYPE toKey, boolean inclusive) { - if (!inRange(toKey, inclusive)) throw new IllegalArgumentException("toKey out of range"); - return new DescendingSubMapKV_BRACES(m, fromStart, low, loInclusive, false, toKey, inclusive); - } - - @Override - public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE tailMap(KEY_TYPE fromKey, boolean inclusive) { - if (!inRange(fromKey, inclusive)) throw new IllegalArgumentException("fromKey out of range"); - return new DescendingSubMapKV_BRACES(m, false, fromKey, inclusive, toEnd, high, hiInclusive); - } - } - - static abstract class NavigableSubMap KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE implements NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE { - final AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE m; - final KEY_TYPE low; - final KEY_TYPE high; - final boolean fromStart; - final boolean toEnd; - final boolean loInclusive; - final boolean hiInclusive; - transient NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE descendingMap; - transient SubMapEntrySet entrySet; - transient KeySet KEY_VALUE_GENERIC_TYPE keySet; - transient SubMapValues values; - - NavigableSubMap(AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE m, boolean fromStart, KEY_TYPE low, boolean loInclusive, boolean toEnd, KEY_TYPE high, boolean hiInclusive) { - this.m = m; - this.low = low; - this.high = high; - this.fromStart = fromStart; - this.toEnd = toEnd; - this.loInclusive = loInclusive; - this.hiInclusive = hiInclusive; - } - - abstract LIST_ITERATOR KEY_GENERIC_TYPE keyIterator(boolean descending); - abstract LIST_ITERATOR KEY_GENERIC_TYPE keyIterator(KEY_TYPE key); - abstract VALUE_LIST_ITERATOR VALUE_GENERIC_TYPE valueIterator(); - abstract ObjectListIterator entryIterator(); - abstract AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE next(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry); - abstract AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE previous(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry); - - @Override - public NavigableSubMap KEY_VALUE_GENERIC_TYPE copy() { throw new UnsupportedOperationException(); } - @Override - public abstract NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE descendingMap(); - @Override - public abstract NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE subMap(KEY_TYPE fromKey, boolean fromInclusive, KEY_TYPE toKey, boolean toInclusive); - @Override - public abstract NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE headMap(KEY_TYPE toKey, boolean inclusive); - @Override - public abstract NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE tailMap(KEY_TYPE fromKey, boolean inclusive); - - boolean tooLow(KEY_TYPE key) { return !fromStart && (loInclusive ? m.compare(key, low) < 0 : m.compare(key, low) <= 0); } - boolean tooHigh(KEY_TYPE key) { return !toEnd && (hiInclusive ? m.compare(key, high) > 0 : m.compare(key, high) >= 0); } - boolean inRange(KEY_TYPE key) { return !tooLow(key) && !tooHigh(key); } - boolean inClosedRange(KEY_TYPE key) { return (fromStart || m.compare(key, low) >= 0) && (toEnd || m.compare(high, key) >= 0); } - boolean inRange(KEY_TYPE key, boolean inclusive) { return inclusive ? inRange(key) : inClosedRange(key); } + public void clear() { map.clear(); } #if TYPE_OBJECT - public KEY_TYPE getDefaultMaxValue() { return m.getDefaultMaxValue(); } - public KEY_TYPE getDefaultMinValue() { return m.getDefaultMinValue(); } + @Override + public boolean remove(Object o) { + int oldSize = map.size(); + map.remove(o); + return oldSize != map.size(); + } + #else @Override - public void setDefaultMaxValue(KEY_TYPE e) { m.setDefaultMaxValue(e); } + public boolean remove(KEY_TYPE o) { + int oldSize = map.size(); + map.remove(o); + return oldSize != map.size(); + } + +#endif @Override - public KEY_TYPE getDefaultMaxValue() { return m.getDefaultMaxValue(); } + public boolean add(KEY_TYPE e) { throw new UnsupportedOperationException(); } @Override - public void setDefaultMinValue(KEY_TYPE e) { m.setDefaultMinValue(e); } + public boolean addAndMoveToFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); } @Override - public KEY_TYPE getDefaultMinValue() { return m.getDefaultMinValue(); } + 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 BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement) { + if(map instanceof AVL_TREE_MAP) return ((AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE)map).keyIterator(fromElement); + return ((NavigableSubMap KEY_VALUE_GENERIC_TYPE)map).keyIterator(fromElement); + } + + @Override + public NAVIGABLE_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, boolean fromInclusive, KEY_TYPE toElement, boolean toInclusive) { return new KeySetKV_BRACES(map.subMap(fromElement, fromInclusive, toElement, toInclusive)); } + @Override + public NAVIGABLE_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement, boolean inclusive) { return new KeySetKV_BRACES(map.headMap(toElement, inclusive)); } + @Override + public NAVIGABLE_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement, boolean inclusive) { return new KeySetKV_BRACES(map.tailMap(fromElement, inclusive)); } + + @Override + public BI_ITERATOR KEY_GENERIC_TYPE iterator() { + if(map instanceof AVL_TREE_MAP) return ((AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE)map).keyIterator(); + return ((NavigableSubMap KEY_VALUE_GENERIC_TYPE)map).keyIterator(); + } + + @Override + public BI_ITERATOR KEY_GENERIC_TYPE descendingIterator() { + if(map instanceof AVL_TREE_MAP) return ((AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE)map).descendingKeyIterator(); + return ((NavigableSubMap KEY_VALUE_GENERIC_TYPE)map).descendingKeyIterator(); + } + + protected Node KEY_VALUE_GENERIC_TYPE start() { + if(map instanceof AVL_TREE_MAP) return ((AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE)map).first; + return ((NavigableSubMap KEY_VALUE_GENERIC_TYPE)map).subLowest(); + } + + protected Node KEY_VALUE_GENERIC_TYPE end() { + if(map instanceof AVL_TREE_MAP) return null; + return ((NavigableSubMap KEY_VALUE_GENERIC_TYPE)map).subHighest(); + } + + protected Node KEY_VALUE_GENERIC_TYPE next(Node KEY_VALUE_GENERIC_TYPE entry) { + if(map instanceof AVL_TREE_MAP) return entry.next(); + return ((NavigableSubMap KEY_VALUE_GENERIC_TYPE)map).next(entry); + } + + protected Node KEY_VALUE_GENERIC_TYPE previous(Node KEY_VALUE_GENERIC_TYPE entry) { + if(map instanceof AVL_TREE_MAP) return entry.previous(); + return ((NavigableSubMap KEY_VALUE_GENERIC_TYPE)map).previous(entry); + } + + @Override + public NAVIGABLE_SET KEY_GENERIC_TYPE descendingSet() { return new KeySetKV_BRACES(map.descendingMap()); } + @Override + public KeySet KEY_VALUE_GENERIC_TYPE copy() { throw new UnsupportedOperationException(); } + @Override + public boolean isEmpty() { return map.isEmpty(); } + @Override + public int size() { return map.size(); } + + @Override + public void forEach(CONSUMER KEY_SUPER_GENERIC_TYPE action) { + Objects.requireNonNull(action); + for(Node KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) + action.accept(entry.key); + } + + @Override + public void forEach(E input, BI_FROM_OBJECT_CONSUMER KSK_GENERIC_TYPE action) { + Objects.requireNonNull(action); + for(Node KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) + action.accept(input, entry.key); + } + + @Override + public boolean matchesAny(PREDICATE KEY_GENERIC_TYPE filter) { + Objects.requireNonNull(filter); + for(Node KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) + if(filter.TEST_VALUE(entry.key)) return true; + return false; + } + + @Override + public boolean matchesNone(PREDICATE KEY_GENERIC_TYPE filter) { + Objects.requireNonNull(filter); + for(Node KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) + if(filter.TEST_VALUE(entry.key)) return false; + return true; + } + + @Override + public boolean matchesAll(PREDICATE KEY_GENERIC_TYPE filter) { + Objects.requireNonNull(filter); + for(Node KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) + if(!filter.TEST_VALUE(entry.key)) return false; + return true; + } + +#if !TYPE_OBJECT + @Override + public KEY_TYPE reduce(KEY_TYPE identity, SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + KEY_TYPE state = identity; + for(Node KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) + state = operator.APPLY_KEY_VALUE(state, entry.key); + return state; + } + +#else + @Override + public KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction operator) { + Objects.requireNonNull(operator); + KEY_SPECIAL_TYPE state = identity; + for(Node KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) + state = operator.apply(state, entry.key); + return state; + } #endif + @Override + public KEY_TYPE reduce(SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + KEY_TYPE state = EMPTY_KEY_VALUE; + boolean empty = true; + for(Node KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) { + if(empty) { + empty = false; + state = entry.key; + continue; + } + state = operator.APPLY_KEY_VALUE(state, entry.key); + } + return state; + } + + @Override + public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { + Objects.requireNonNull(filter); + for(Node KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) + if(filter.TEST_VALUE(entry.key)) return entry.key; + return EMPTY_KEY_VALUE; + } + + @Override + public int count(PREDICATE KEY_GENERIC_TYPE filter) { + Objects.requireNonNull(filter); + int result = 0; + for(Node KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) + if(filter.TEST_VALUE(entry.key)) result++; + return result; + } + } + + static class AscendingNaivgableSubMap KEY_VALUE_GENERIC_TYPE extends NavigableSubMap KEY_VALUE_GENERIC_TYPE + { + AscendingNaivgableSubMap(AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE map, boolean fromStart, KEY_TYPE lo, boolean loInclusive, boolean toEnd, KEY_TYPE hi, boolean hiInclusive) { + super(map, fromStart, lo, loInclusive, toEnd, hi, hiInclusive); + } + + @Override + public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE descendingMap() { + if(inverse == null) inverse = new DescendingNaivgableSubMapKV_BRACES(map, fromStart, lo, loInclusive, toEnd, hi, hiInclusive); + return inverse; + } + + @Override + public ObjectSet ENTRY_SET() { + if(entrySet == null) entrySet = new AscendingSubEntrySet(); + return entrySet; + } + + @Override + public NAVIGABLE_SET KEY_GENERIC_TYPE navigableKeySet() { + if(keySet == null) keySet = new KeySetKV_BRACES(this); + return keySet; + } + + @Override + public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE subMap(KEY_TYPE fromKey, boolean fromInclusive, KEY_TYPE toKey, boolean toInclusive) { + if (!inRange(fromKey, fromInclusive)) throw new IllegalArgumentException("fromKey out of range"); + if (!inRange(toKey, toInclusive)) throw new IllegalArgumentException("toKey out of range"); + return new AscendingNaivgableSubMapKV_BRACES(map, false, fromKey, fromInclusive, false, toKey, toInclusive); + } + + @Override + public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE headMap(KEY_TYPE toKey, boolean inclusive) { + if (!inRange(toKey, inclusive)) throw new IllegalArgumentException("toKey out of range"); + return new AscendingNaivgableSubMapKV_BRACES(map, fromStart, lo, loInclusive, false, toKey, inclusive); + } + + @Override + public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE tailMap(KEY_TYPE fromKey, boolean inclusive) { + if (!inRange(fromKey, inclusive)) throw new IllegalArgumentException("fromKey out of range"); + return new AscendingNaivgableSubMapKV_BRACES(map, false, fromKey, inclusive, toEnd, hi, hiInclusive); + } + + @Override + protected Node KEY_VALUE_GENERIC_TYPE subLowest() { return absLowest(); } + @Override + protected Node KEY_VALUE_GENERIC_TYPE subHighest() { return absHighest(); } + @Override + protected Node KEY_VALUE_GENERIC_TYPE subCeiling(KEY_TYPE key) { return absCeiling(key); } + @Override + protected Node KEY_VALUE_GENERIC_TYPE subHigher(KEY_TYPE key) { return absHigher(key); } + @Override + protected Node KEY_VALUE_GENERIC_TYPE subFloor(KEY_TYPE key) { return absFloor(key); } + @Override + protected Node KEY_VALUE_GENERIC_TYPE subLower(KEY_TYPE key) { return absLower(key); } + + @Override + protected BI_ITERATOR KEY_GENERIC_TYPE keyIterator() { + return new AcsendingSubKeyIterator(absLowest(), absHighFence(), absLowFence()); + } + @Override + protected BI_ITERATOR KEY_GENERIC_TYPE keyIterator(KEY_TYPE element) { + return new AcsendingSubKeyIterator(absLower(element), absHighFence(), absLowFence()); + } + + @Override + protected VALUE_BI_ITERATOR VALUE_GENERIC_TYPE valueIterator() { + return new AcsendingSubValueIterator(absLowest(), absHighFence(), absLowFence()); + } + + @Override + protected BI_ITERATOR KEY_GENERIC_TYPE descendingKeyIterator() { + return new DecsendingSubKeyIterator(absHighest(), absLowFence(), absHighFence()); + } + + class AscendingSubEntrySet extends SubEntrySet { + @Override + public ObjectIterator iterator() { + return new AcsendingSubEntryIterator(absLowest(), absHighFence(), absLowFence()); + } + } + } + + static class DescendingNaivgableSubMap KEY_VALUE_GENERIC_TYPE extends NavigableSubMap KEY_VALUE_GENERIC_TYPE + { + COMPARATOR KEY_GENERIC_TYPE comparator; + DescendingNaivgableSubMap(AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE map, boolean fromStart, KEY_TYPE lo, boolean loInclusive, boolean toEnd, KEY_TYPE hi, boolean hiInclusive) { + super(map, fromStart, lo, loInclusive, toEnd, hi, hiInclusive); +#if TYPE_OBJECT + comparator = Collections.reverseOrder(map.comparator()); +#else + comparator = map.comparator() == null ? COMPARATOR.of(Collections.reverseOrder()) : map.comparator().reversed(); +#endif + } + + @Override + public COMPARATOR KEY_GENERIC_TYPE comparator() { return comparator; } + + @Override + public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE descendingMap() { + if(inverse == null) inverse = new AscendingNaivgableSubMapKV_BRACES(map, fromStart, lo, loInclusive, toEnd, hi, hiInclusive); + return inverse; + } + + @Override + public NAVIGABLE_SET KEY_GENERIC_TYPE navigableKeySet() { + if(keySet == null) keySet = new KeySetKV_BRACES(this); + return keySet; + } + + @Override + public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE subMap(KEY_TYPE fromKey, boolean fromInclusive, KEY_TYPE toKey, boolean toInclusive) { + if (!inRange(fromKey, fromInclusive)) throw new IllegalArgumentException("fromKey out of range"); + if (!inRange(toKey, toInclusive)) throw new IllegalArgumentException("toKey out of range"); + return new DescendingNaivgableSubMapKV_BRACES(map, false, toKey, toInclusive, false, fromKey, fromInclusive); + } + + @Override + public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE headMap(KEY_TYPE toKey, boolean inclusive) { + if (!inRange(toKey, inclusive)) throw new IllegalArgumentException("toKey out of range"); + return new DescendingNaivgableSubMapKV_BRACES(map, false, toKey, inclusive, toEnd, hi, hiInclusive); + } + + @Override + public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE tailMap(KEY_TYPE fromKey, boolean inclusive) { + if (!inRange(fromKey, inclusive)) throw new IllegalArgumentException("fromKey out of range"); + return new DescendingNaivgableSubMapKV_BRACES(map, fromStart, lo, loInclusive, false, fromKey, inclusive); + } + + @Override + public ObjectSet ENTRY_SET() { + if(entrySet == null) entrySet = new DescendingSubEntrySet(); + return entrySet; + } + + @Override + protected Node KEY_VALUE_GENERIC_TYPE subLowest() { return absHighest(); } + @Override + protected Node KEY_VALUE_GENERIC_TYPE subHighest() { return absLowest(); } + @Override + protected Node KEY_VALUE_GENERIC_TYPE subCeiling(KEY_TYPE key) { return absFloor(key); } + @Override + protected Node KEY_VALUE_GENERIC_TYPE subHigher(KEY_TYPE key) { return absLower(key); } + @Override + protected Node KEY_VALUE_GENERIC_TYPE subFloor(KEY_TYPE key) { return absCeiling(key); } + @Override + protected Node KEY_VALUE_GENERIC_TYPE subLower(KEY_TYPE key) { return absHigher(key); } + @Override + protected Node KEY_VALUE_GENERIC_TYPE next(Node KEY_VALUE_GENERIC_TYPE entry) { return entry.previous(); } + @Override + protected Node KEY_VALUE_GENERIC_TYPE previous(Node KEY_VALUE_GENERIC_TYPE entry) { return entry.next(); } + + @Override + protected BI_ITERATOR KEY_GENERIC_TYPE keyIterator() { + return new DecsendingSubKeyIterator(absHighest(), absLowFence(), absHighFence()); + } + + @Override + protected BI_ITERATOR KEY_GENERIC_TYPE keyIterator(KEY_TYPE element) { + return new DecsendingSubKeyIterator(absHigher(element), absLowFence(), absHighFence()); + } + + @Override + protected VALUE_BI_ITERATOR VALUE_GENERIC_TYPE valueIterator() { + return new DecsendingSubValueIterator(absHighest(), absLowFence(), absHighFence()); + } + + @Override + protected BI_ITERATOR KEY_GENERIC_TYPE descendingKeyIterator() { + return new AcsendingSubKeyIterator(absLowest(), absHighFence(), absLowFence()); + } + + class DescendingSubEntrySet extends SubEntrySet { + @Override + public ObjectIterator iterator() { + return new DecsendingSubEntryIterator(absHighest(), absLowFence(), absHighFence()); + } + } + } + + static abstract class NavigableSubMap KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE implements NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE + { + final AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE map; + final KEY_TYPE lo, hi; + final boolean fromStart, toEnd; + final boolean loInclusive, hiInclusive; + + NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE inverse; + NAVIGABLE_SET KEY_GENERIC_TYPE keySet; + ObjectSet entrySet; + VALUE_COLLECTION VALUE_GENERIC_TYPE values; + + NavigableSubMap(AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE map, boolean fromStart, KEY_TYPE lo, boolean loInclusive, boolean toEnd, KEY_TYPE hi, boolean hiInclusive) { + if (!fromStart && !toEnd) { + if (map.compare(lo, hi) > 0) throw new IllegalArgumentException("fromKey > toKey"); + } + else { + if (!fromStart) map.validate(lo); + if (!toEnd) map.validate(hi); + } + this.map = map; + this.fromStart = fromStart; + this.lo = lo; + this.loInclusive = loInclusive; + this.toEnd = toEnd; + this.hi = hi; + this.hiInclusive = hiInclusive; + } + +#if TYPE_OBJECT + public KEY_TYPE getDefaultMaxValue() { return map.getDefaultMaxValue(); } + public KEY_TYPE getDefaultMinValue() { return map.getDefaultMinValue(); } +#else + @Override + public void setDefaultMaxValue(KEY_TYPE e) { map.setDefaultMaxValue(e); } + @Override + public KEY_TYPE getDefaultMaxValue() { return map.getDefaultMaxValue(); } + @Override + public void setDefaultMinValue(KEY_TYPE e) { map.setDefaultMinValue(e); } + @Override + public KEY_TYPE getDefaultMinValue() { return map.getDefaultMinValue(); } +#endif + protected boolean isNullComparator() { return map.comparator() == null; } + @Override public ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE setDefaultReturnValue(VALUE_TYPE v) { - m.setDefaultReturnValue(v); + map.setDefaultReturnValue(v); return this; } @Override - public VALUE_TYPE getDefaultReturnValue() { return m.getDefaultReturnValue(); } + public VALUE_TYPE getDefaultReturnValue() { return map.getDefaultReturnValue(); } + + @Override + public VALUE_COLLECTION VALUE_GENERIC_TYPE values() { + if(values == null) values = new SubMapValues(); + return values; + } + + @Override + public NAVIGABLE_SET KEY_GENERIC_TYPE descendingKeySet() { + return descendingMap().navigableKeySet(); + } + + @Override + public SET KEY_GENERIC_TYPE keySet() { + return navigableKeySet(); + } + + protected abstract Node KEY_VALUE_GENERIC_TYPE subLowest(); + protected abstract Node KEY_VALUE_GENERIC_TYPE subHighest(); + protected abstract Node KEY_VALUE_GENERIC_TYPE subCeiling(KEY_TYPE key); + protected abstract Node KEY_VALUE_GENERIC_TYPE subHigher(KEY_TYPE key); + protected abstract Node KEY_VALUE_GENERIC_TYPE subFloor(KEY_TYPE key); + protected abstract Node KEY_VALUE_GENERIC_TYPE subLower(KEY_TYPE key); + protected abstract BI_ITERATOR KEY_GENERIC_TYPE keyIterator(); + protected abstract BI_ITERATOR KEY_GENERIC_TYPE keyIterator(KEY_TYPE element); + protected abstract VALUE_BI_ITERATOR VALUE_GENERIC_TYPE valueIterator(); + protected abstract BI_ITERATOR KEY_GENERIC_TYPE descendingKeyIterator(); + protected KEY_TYPE lowKeyOrNull(Node KEY_VALUE_GENERIC_TYPE entry) { return entry == null ? EMPTY_KEY_VALUE : entry.key; } + protected KEY_TYPE highKeyOrNull(Node KEY_VALUE_GENERIC_TYPE entry) { return entry == null ? EMPTY_KEY_VALUE : entry.key; } + protected Node KEY_VALUE_GENERIC_TYPE next(Node KEY_VALUE_GENERIC_TYPE entry) { return entry.next(); } + protected Node KEY_VALUE_GENERIC_TYPE previous(Node KEY_VALUE_GENERIC_TYPE entry) { return entry.previous(); } + + protected boolean tooLow(KEY_TYPE key) { + if (!fromStart) { + int c = map.compare(key, lo); + if (c < 0 || (c == 0 && !loInclusive)) return true; + } + return false; + } + + protected boolean tooHigh(KEY_TYPE key) { + if (!toEnd) { + int c = map.compare(key, hi); + if (c > 0 || (c == 0 && !hiInclusive)) return true; + } + return false; + } + protected boolean inRange(KEY_TYPE key) { return !tooLow(key) && !tooHigh(key); } + protected boolean inClosedRange(KEY_TYPE key) { return (fromStart || map.compare(key, lo) >= 0) && (toEnd || map.compare(hi, key) >= 0); } + protected boolean inRange(KEY_TYPE key, boolean inclusive) { return inclusive ? inRange(key) : inClosedRange(key); } + + protected Node KEY_VALUE_GENERIC_TYPE absLowest() { + Node KEY_VALUE_GENERIC_TYPE e = (fromStart ? map.first : (loInclusive ? map.findCeilingNode(lo) : map.findHigherNode(lo))); + return (e == null || tooHigh(e.key)) ? null : e; + } + + protected Node KEY_VALUE_GENERIC_TYPE absHighest() { + Node KEY_VALUE_GENERIC_TYPE e = (toEnd ? map.last : (hiInclusive ? map.findFloorNode(hi) : map.findLowerNode(hi))); + return (e == null || tooLow(e.key)) ? null : e; + } + + protected Node KEY_VALUE_GENERIC_TYPE absCeiling(KEY_TYPE key) { + if (tooLow(key)) return absLowest(); + Node KEY_VALUE_GENERIC_TYPE e = map.findCeilingNode(key); + return (e == null || tooHigh(e.key)) ? null : e; + } + + protected Node KEY_VALUE_GENERIC_TYPE absHigher(KEY_TYPE key) { + if (tooLow(key)) return absLowest(); + Node KEY_VALUE_GENERIC_TYPE e = map.findHigherNode(key); + return (e == null || tooHigh(e.key)) ? null : e; + } + + protected Node KEY_VALUE_GENERIC_TYPE absFloor(KEY_TYPE key) { + if (tooHigh(key)) return absHighest(); + Node KEY_VALUE_GENERIC_TYPE e = map.findFloorNode(key); + return (e == null || tooLow(e.key)) ? null : e; + } + + protected Node KEY_VALUE_GENERIC_TYPE absLower(KEY_TYPE key) { + if (tooHigh(key)) return absHighest(); + Node KEY_VALUE_GENERIC_TYPE e = map.findLowerNode(key); + return (e == null || tooLow(e.key)) ? null : e; + } + + protected Node KEY_VALUE_GENERIC_TYPE absHighFence() { return (toEnd ? null : (hiInclusive ? map.findHigherNode(hi) : map.findCeilingNode(hi))); } + protected Node KEY_VALUE_GENERIC_TYPE absLowFence() { return (fromStart ? null : (loInclusive ? map.findLowerNode(lo) : map.findFloorNode(lo))); } @Override public VALUE_TYPE putAndMoveToFirst(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); } @@ -1249,338 +1535,283 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ @Override public VALUE_TYPE getAndMoveToLast(KEY_TYPE key) { throw new UnsupportedOperationException(); } @Override - public COMPARATOR KEY_GENERIC_TYPE comparator() { return m.comparator(); } -#if TYPE_OBJECT - @Override - public boolean containsKey(Object key) { return inRange((CLASS_TYPE)key) && m.containsKey(key); } -#else - @Override - public boolean containsKey(KEY_TYPE key) { return inRange(key) && m.containsKey(key); } - -#endif - @Override - public KEY_TYPE FIRST_ENTRY_KEY() { - AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(); - return entry == null ? getDefaultMaxValue() : entry.key; - } + public COMPARATOR KEY_GENERIC_TYPE comparator() { return map.comparator(); } @Override public KEY_TYPE POLL_FIRST_ENTRY_KEY() { - if(fromStart) return m.POLL_FIRST_ENTRY_KEY(); - AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = loInclusive ? m.findCeilingNode(low) : m.findHigherNode(low); - if(entry != null && !tooHigh(entry.key)) { - KEY_TYPE value = entry.key; - m.removeNode(entry); - return value; + Node KEY_VALUE_GENERIC_TYPE entry = subLowest(); + if(entry != null) { + KEY_TYPE result = entry.key; + map.removeNode(entry); + return result; } - return getDefaultMaxValue(); - } - - @Override - public KEY_TYPE LAST_ENTRY_KEY() { - AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findHighest(); - return entry == null ? getDefaultMinValue() : entry.key; + return EMPTY_KEY_VALUE; } @Override public KEY_TYPE POLL_LAST_ENTRY_KEY() { - if(toEnd) return m.POLL_LAST_ENTRY_KEY(); - AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = hiInclusive ? m.findFloorNode(high) : m.findLowerNode(high); - if(entry != null && !tooLow(entry.key)) { - KEY_TYPE value = entry.key; - m.removeNode(entry); - return value; + Node KEY_VALUE_GENERIC_TYPE entry = subHighest(); + if(entry != null) { + KEY_TYPE result = entry.key; + map.removeNode(entry); + return result; } - return getDefaultMinValue(); + return EMPTY_KEY_VALUE; } @Override public VALUE_TYPE FIRST_ENTRY_VALUE() { - AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(); - return entry == null ? getDefaultReturnValue() : entry.value; + Node KEY_VALUE_GENERIC_TYPE entry = subLowest(); + return entry == null ? map.getDefaultReturnValue() : entry.value; } @Override public VALUE_TYPE LAST_ENTRY_VALUE() { - AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findHighest(); - return entry == null ? getDefaultReturnValue() : entry.value; + Node KEY_VALUE_GENERIC_TYPE entry = subHighest(); + return entry == null ? map.getDefaultReturnValue() : entry.value; } - protected AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE findLowest() { - if(fromStart) return m.first; - AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = loInclusive ? m.findCeilingNode(low) : m.findHigherNode(low); - return entry == null || tooHigh(entry.key) ? null : entry; + @Override + public KEY_TYPE FIRST_ENTRY_KEY() { + Node KEY_VALUE_GENERIC_TYPE entry = subLowest(); + if(entry == null) throw new NoSuchElementException(); + return entry.key; } - protected AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE findHighest() { - if(toEnd) return m.last; - AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = hiInclusive ? m.findFloorNode(high) : m.findLowerNode(high); - return entry == null || tooLow(entry.key) ? null : entry; + @Override + public KEY_TYPE LAST_ENTRY_KEY() { + Node KEY_VALUE_GENERIC_TYPE entry = subHighest(); + if(entry == null) throw new NoSuchElementException(); + return entry.key; } @Override public VALUE_TYPE put(KEY_TYPE key, VALUE_TYPE value) { - if(!inRange(key)) throw new IllegalArgumentException("key out of range"); - return m.put(key, value); + if (!inRange(key)) throw new IllegalArgumentException("key out of range"); + return map.put(key, value); } @Override public VALUE_TYPE putIfAbsent(KEY_TYPE key, VALUE_TYPE value) { - if(!inRange(key)) throw new IllegalArgumentException("key out of range"); - return m.putIfAbsent(key, value); + if (!inRange(key)) throw new IllegalArgumentException("key out of range"); + return map.putIfAbsent(key, value); } #if VALUE_PRIMITIVES @Override public VALUE_TYPE addTo(KEY_TYPE key, VALUE_TYPE value) { if(!inRange(key)) throw new IllegalArgumentException("key out of range"); - return m.addTo(key, value); + return map.addTo(key, value); } #endif +#if TYPE_OBJECT @Override - public VALUE_TYPE GET_VALUE(KEY_TYPE key) { - return inRange(key) ? m.GET_VALUE(key) : getDefaultReturnValue(); - } - -#if TYPE_OBJECT && VALUE_OBJECT - @Override - public VALUE_TYPE getOrDefault(Object key, VALUE_TYPE defaultValue) { - return inRange((KEY_TYPE)key) ? m.getOrDefault(key, defaultValue) : getDefaultReturnValue(); - } - + public boolean containsKey(Object key) { return inRange((CLASS_TYPE)key) && map.containsKey(key); } #else @Override - public VALUE_TYPE getOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue) { - return inRange(key) ? m.getOrDefault(key, defaultValue) : getDefaultReturnValue(); - } + public boolean containsKey(KEY_TYPE key) { return inRange(key) && map.containsKey(key); } #endif + @Override + public VALUE_TYPE COMPUTE_IF_PRESENT(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { + Objects.requireNonNull(mappingFunction); +#if TYPE_OBJECT + map.validate(key); +#endif + if(!inRange(key)) return getDefaultReturnValue(); + Node KEY_VALUE_GENERIC_TYPE entry = map.findNode(key); + if(entry == null || VALUE_EQUALS(entry.value, getDefaultReturnValue())) return getDefaultReturnValue(); + VALUE_TYPE newValue = mappingFunction.apply(key, entry.value); + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) { + map.removeNode(entry); + return newValue; + } + entry.value = newValue; + return newValue; + } + @Override public VALUE_TYPE REMOVE_VALUE(KEY_TYPE key) { - return inRange(key) ? m.REMOVE_VALUE(key) : getDefaultReturnValue(); + return inRange(key) ? map.REMOVE_VALUE(key) : getDefaultReturnValue(); } @Override public VALUE_TYPE REMOVE_VALUEOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue) { - return inRange(key) ? m.REMOVE_VALUE(key) : defaultValue; + return inRange(key) ? map.REMOVE_VALUE(key) : defaultValue; } #if TYPE_OBJECT && VALUE_OBJECT @Override public boolean remove(Object key, Object value) { - return inRange((CLASS_TYPE)key) && m.remove(key, value); + return inRange((CLASS_TYPE)key) && map.remove(key, value); } #else @Override public boolean remove(KEY_TYPE key, VALUE_TYPE value) { - return inRange(key) && m.remove(key, value); + return inRange(key) && map.remove(key, value); } #endif + @Override - public ObjectSet ENTRY_SET() { - if(entrySet == null) entrySet = new SubMapEntrySet(); - return entrySet; + public VALUE_TYPE GET_VALUE(KEY_TYPE key) { + return inRange(key) ? map.GET_VALUE(key) : getDefaultReturnValue(); + } + +#if TYPE_OBJECT && VALUE_OBJECT + @Override + public VALUE_TYPE getOrDefault(Object key, VALUE_TYPE defaultValue) { + return inRange((CLASS_TYPE)key) ? map.getOrDefault(key, defaultValue) : getDefaultReturnValue(); + } + +#else + @Override + public VALUE_TYPE getOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue) { + return inRange(key) ? map.getOrDefault(key, defaultValue) : getDefaultReturnValue(); + } + +#endif + + @Override + public KEY_TYPE lowerKey(KEY_TYPE key) { return lowKeyOrNull(subLower(key)); } + @Override + public KEY_TYPE floorKey(KEY_TYPE key) { return lowKeyOrNull(subFloor(key)); } + @Override + public KEY_TYPE ceilingKey(KEY_TYPE key) { return highKeyOrNull(subCeiling(key)); } + @Override + public KEY_TYPE higherKey(KEY_TYPE key) { return highKeyOrNull(subHigher(key)); } + @Override + public MAP.Entry KEY_VALUE_GENERIC_TYPE lowerEntry(KEY_TYPE key) { return subLower(key); } + @Override + public MAP.Entry KEY_VALUE_GENERIC_TYPE floorEntry(KEY_TYPE key) { return subFloor(key); } + @Override + public MAP.Entry KEY_VALUE_GENERIC_TYPE ceilingEntry(KEY_TYPE key) { return subCeiling(key); } + @Override + public MAP.Entry KEY_VALUE_GENERIC_TYPE higherEntry(KEY_TYPE key) { return subHigher(key); } + + @Override + public boolean isEmpty() { + if(fromStart && toEnd) return map.isEmpty(); + Node KEY_VALUE_GENERIC_TYPE n = absLowest(); + return n == null || tooHigh(n.key); } @Override - public SET KEY_GENERIC_TYPE keySet() { return navigableKeySet(); } + public int size() { return fromStart && toEnd ? map.size() : entrySet().size(); } @Override - public NAVIGABLE_SET KEY_GENERIC_TYPE navigableKeySet() { - if(keySet == null) keySet = new KeySetKV_BRACES(this); - return keySet; - } - - @Override - public NAVIGABLE_SET KEY_GENERIC_TYPE descendingKeySet() { - return descendingMap().navigableKeySet(); - } - - @Override - public VALUE_COLLECTION VALUE_GENERIC_TYPE values() { - if(values == null) values = new SubMapValues(); - return values; - } - - @Override - public void forEach(BI_CONSUMER KEY_VALUE_GENERIC_TYPE action) { - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - action.accept(entry.key, entry.value); - } - - @Override - public int size() { - return (fromStart && toEnd) ? m.size() : entrySet().size(); - } + public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE copy() { throw new UnsupportedOperationException(); } @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE firstEntry() { - AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(); - return entry == null ? null : new BasicEntryKV_BRACES(entry.key, entry.value); + Node KEY_VALUE_GENERIC_TYPE entry = subLowest(); + return entry == null ? null : entry.export(); } - + @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE lastEntry() { - AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findHighest(); - return entry == null ? null : new BasicEntryKV_BRACES(entry.key, entry.value); + Node KEY_VALUE_GENERIC_TYPE entry = subHighest(); + return entry == null ? null : entry.export(); } - + @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE pollFirstEntry() { - AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(); - if(entry == null) return null; - BasicEntry KEY_VALUE_GENERIC_TYPE result = new BasicEntryKV_BRACES(entry.key, entry.value); - m.removeNode(entry); - return result; + Node KEY_VALUE_GENERIC_TYPE entry = subLowest(); + if(entry != null) { + MAP.Entry KEY_VALUE_GENERIC_TYPE result = entry.export(); + map.removeNode(entry); + return result; + } + return null; } - + @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE pollLastEntry() { - AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findHighest(); - if(entry == null) return null; - BasicEntry KEY_VALUE_GENERIC_TYPE result = new BasicEntryKV_BRACES(entry.key, entry.value); - m.removeNode(entry); - return result; - } - - @Override - public KEY_TYPE lowerKey(KEY_TYPE e) { - if(tooHigh(e)) { - AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findHighest(); - return entry == null ? getDefaultMinValue() : entry.key; + Node KEY_VALUE_GENERIC_TYPE entry = subHighest(); + if(entry != null) { + MAP.Entry KEY_VALUE_GENERIC_TYPE result = entry.export(); + map.removeNode(entry); + return result; } - AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = m.findLowerNode(e); - return entry == null || tooHigh(entry.key) ? getDefaultMaxValue() : entry.key; + return null; } - @Override - public KEY_TYPE floorKey(KEY_TYPE e) { - if(tooHigh(e)) { - AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findHighest(); - return entry == null ? getDefaultMinValue() : entry.key; - } - AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = m.findFloorNode(e); - return entry == null || tooHigh(entry.key) ? getDefaultMaxValue() : entry.key; - } - - @Override - public KEY_TYPE ceilingKey(KEY_TYPE e) { - if(tooLow(e)) { - AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(); - return entry == null ? getDefaultMaxValue() : entry.key; - } - AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = m.findCeilingNode(e); - return entry == null || tooLow(entry.key) ? getDefaultMinValue() : entry.key; - } - - @Override - public KEY_TYPE higherKey(KEY_TYPE e) { - if(tooLow(e)) { - AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(); - return entry == null ? getDefaultMaxValue() : entry.key; - } - AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = m.findHigherNode(e); - return entry == null || tooLow(entry.key) ? getDefaultMinValue() : entry.key; - } - - @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE lowerEntry(KEY_TYPE e) { - if(tooHigh(e)) { - AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findHighest(); - return entry == null ? null : new BasicEntryKV_BRACES(entry.key, entry.value); - } - AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = m.findLowerNode(e); - return entry == null || tooHigh(entry.key) ? null : new BasicEntryKV_BRACES(entry.key, entry.value); - } - - @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE floorEntry(KEY_TYPE e) { - if(tooHigh(e)) { - AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findHighest(); - return entry == null ? null : new BasicEntryKV_BRACES(entry.key, entry.value); - } - AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = m.findFloorNode(e); - return entry == null || tooHigh(entry.key) ? null : new BasicEntryKV_BRACES(entry.key, entry.value); - } - - @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE ceilingEntry(KEY_TYPE e) { - if(tooLow(e)) { - AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(); - return entry == null ? null : new BasicEntryKV_BRACES(entry.key, entry.value); - } - AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = m.findCeilingNode(e); - return entry == null || tooLow(entry.key) ? null : new BasicEntryKV_BRACES(entry.key, entry.value); - } - - @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE higherEntry(KEY_TYPE e) { - if(tooLow(e)) { - AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(); - return entry == null ? null : new BasicEntryKV_BRACES(entry.key, entry.value); - } - AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = m.findHigherNode(e); - return entry == null || tooLow(entry.key) ? null : new BasicEntryKV_BRACES(entry.key, entry.value); - } - - class SubMapEntrySet extends AbstractObjectSet implements ObjectSet { + abstract class SubEntrySet extends AbstractObjectSet { @Override - @Deprecated - public boolean contains(Object o) { - if(o instanceof Map.Entry) { - if(o instanceof MAP.Entry) { - MAP.Entry KEY_VALUE_GENERIC_TYPE entry = (MAP.Entry KEY_VALUE_GENERIC_TYPE)o; - if(entry.getKey() == null && comparator() == null) return false; - AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE subEntry = m.findNode(entry.ENTRY_KEY()); - if(subEntry != null) return VALUE_EQUALS(entry.ENTRY_VALUE(), subEntry.value); - } - else { - Map.Entry entry = (Map.Entry)o; - if(entry.getKey() == null && comparator() == null) return false; -#if !TYPE_OBJECT - if(!(entry.getKey() instanceof CLASS_TYPE)) return false; -#endif - AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE subEntry = m.findNode((CLASS_TYPE)entry.getKey()); - if(subEntry != null) return Objects.equals(entry.getValue(), VALUE_TO_OBJ(subEntry.value)); - } - } - return false; + public int size() { + if (fromStart && toEnd) return map.size(); + int size = 0; + for(ObjectIterator iter = iterator();iter.hasNext();iter.next(),size++); + return size; + } + + @Override + public boolean isEmpty() { + Node KEY_VALUE_GENERIC_TYPE n = absLowest(); + return n == null || tooHigh(n.key); + } + + @Override + public boolean contains(Object o) { + if (!(o instanceof Map.Entry)) return false; + if(o instanceof MAP.Entry) + { + MAP.Entry KEY_VALUE_GENERIC_TYPE entry = (MAP.Entry KEY_VALUE_GENERIC_TYPE) o; +#if TYPE_OBJECT + if(entry.ENTRY_KEY() == null && isNullComparator()) return false; +#endif + KEY_TYPE key = entry.ENTRY_KEY(); + if (!inRange(key)) return false; + Node KEY_VALUE_GENERIC_TYPE node = map.findNode(key); + return node != null && VALUE_EQUALS(entry.ENTRY_VALUE(), node.ENTRY_VALUE()); + } + Map.Entry entry = (Map.Entry) o; + if(entry.getKey() == null && isNullComparator()) return false; + CLASS_TYPE key = (CLASS_TYPE)entry.getKey(); + if (!inRange(key)) return false; + Node KEY_VALUE_GENERIC_TYPE node = map.findNode(key); + return node != null && Objects.equals(entry.getValue(), VALUE_TO_OBJ(node.ENTRY_VALUE())); } @Override - @Deprecated public boolean remove(Object o) { - if(o instanceof Map.Entry) { - if(o instanceof MAP.Entry) { - MAP.Entry KEY_VALUE_GENERIC_TYPE entry = (MAP.Entry KEY_VALUE_GENERIC_TYPE)o; - return NavigableSubMap.this.remove(entry.ENTRY_KEY(), entry.ENTRY_VALUE()); + if (!(o instanceof Map.Entry)) return false; + if(o instanceof MAP.Entry) + { + MAP.Entry KEY_VALUE_GENERIC_TYPE entry = (MAP.Entry KEY_VALUE_GENERIC_TYPE) o; + KEY_TYPE key = entry.ENTRY_KEY(); + if (!inRange(key)) return false; + Node KEY_VALUE_GENERIC_TYPE node = map.findNode(key); + if (node != null && VALUE_EQUALS(node.getValue(), entry.getValue())) { + map.removeNode(node); + return true; } - Map.Entry entry = (Map.Entry)o; - return NavigableSubMap.this.remove(entry.getKey(), entry.getValue()); + return false; + } + Map.Entry entry = (Map.Entry) o; + CLASS_TYPE key = (CLASS_TYPE)entry.getKey(); + if (!inRange(key)) return false; + Node KEY_VALUE_GENERIC_TYPE node = map.findNode(key); + if (node != null && Objects.equals(node.getValue(), entry.getValue())) { + map.removeNode(node); + return true; } return false; } - @Override - public ObjectIterator iterator() { return entryIterator(); } - @Override - public int size() { return NavigableSubMap.this.size(); } - @Override - public void clear() { NavigableSubMap.this.clear(); } @Override public void forEach(Consumer action) { Objects.requireNonNull(action); - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) + for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) action.accept(new BasicEntryKV_BRACES(entry.key, entry.value)); } @Override public void forEach(E input, ObjectObjectConsumer action) { Objects.requireNonNull(action); - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) + for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) action.accept(input, new BasicEntryKV_BRACES(entry.key, entry.value)); } @@ -1589,7 +1820,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ Objects.requireNonNull(filter); if(size() <= 0) return false; BasicEntry KEY_VALUE_GENERIC_TYPE subEntry = new BasicEntryKV_BRACES(); - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) { + for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) { subEntry.set(entry.key, entry.value); if(filter.getBoolean(subEntry)) return true; } @@ -1601,7 +1832,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ Objects.requireNonNull(filter); if(size() <= 0) return true; BasicEntry KEY_VALUE_GENERIC_TYPE subEntry = new BasicEntryKV_BRACES(); - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) { + for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) { subEntry.set(entry.key, entry.value); if(filter.getBoolean(subEntry)) return false; } @@ -1613,7 +1844,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ Objects.requireNonNull(filter); if(size() <= 0) return true; BasicEntry KEY_VALUE_GENERIC_TYPE subEntry = new BasicEntryKV_BRACES(); - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) { + for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) { subEntry.set(entry.key, entry.value); if(!filter.getBoolean(subEntry)) return false; } @@ -1624,7 +1855,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ public E reduce(E identity, BiFunction operator) { Objects.requireNonNull(operator); E state = identity; - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) { + for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) { state = operator.apply(state, new BasicEntryKV_BRACES(entry.key, entry.value)); } return state; @@ -1635,7 +1866,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ Objects.requireNonNull(operator); MAP.Entry KEY_VALUE_GENERIC_TYPE state = null; boolean empty = true; - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) { + for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) { if(empty) { empty = false; state = new BasicEntryKV_BRACES(entry.key, entry.value); @@ -1651,7 +1882,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ Objects.requireNonNull(filter); if(size() <= 0) return null; BasicEntry KEY_VALUE_GENERIC_TYPE subEntry = new BasicEntryKV_BRACES(); - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) { + for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) { subEntry.set(entry.key, entry.value); if(filter.getBoolean(subEntry)) return subEntry; } @@ -1664,7 +1895,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ if(size() <= 0) return 0; int result = 0; BasicEntry KEY_VALUE_GENERIC_TYPE subEntry = new BasicEntryKV_BRACES(); - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) { + for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) { subEntry.set(entry.key, entry.value); if(filter.getBoolean(subEntry)) result++; } @@ -1705,21 +1936,21 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ @Override public void forEach(VALUE_CONSUMER VALUE_SUPER_GENERIC_TYPE action) { Objects.requireNonNull(action); - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) + for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) action.accept(entry.value); } @Override public void forEach(E input, VALUE_BI_FROM_OBJECT_CONSUMER VSV_GENERIC_TYPE action) { Objects.requireNonNull(action); - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) + for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) action.accept(input, entry.value); } @Override public boolean matchesAny(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { Objects.requireNonNull(filter); - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) + for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) if(filter.VALUE_TEST_VALUE(entry.value)) return true; return false; } @@ -1727,7 +1958,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ @Override public boolean matchesNone(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { Objects.requireNonNull(filter); - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) + for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) if(filter.VALUE_TEST_VALUE(entry.value)) return false; return true; } @@ -1735,7 +1966,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ @Override public boolean matchesAll(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { Objects.requireNonNull(filter); - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) + for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) if(!filter.VALUE_TEST_VALUE(entry.value)) return false; return true; } @@ -1745,7 +1976,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ public VALUE_TYPE reduce(VALUE_TYPE identity, VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { Objects.requireNonNull(operator); VALUE_TYPE state = identity; - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) + for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) state = operator.APPLY_VALUE(state, entry.value); return state; } @@ -1755,7 +1986,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ public VALUE_SPECIAL_TYPE reduce(VALUE_SPECIAL_TYPE identity, BiFunction operator) { Objects.requireNonNull(operator); VALUE_SPECIAL_TYPE state = identity; - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) + for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) state = operator.apply(state, entry.value); return state; } @@ -1766,7 +1997,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ Objects.requireNonNull(operator); VALUE_TYPE state = EMPTY_VALUE; boolean empty = true; - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) { + for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) { if(empty) { empty = false; state = entry.value; @@ -1780,7 +2011,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ @Override public VALUE_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { Objects.requireNonNull(filter); - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) + for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) if(filter.VALUE_TEST_VALUE(entry.value)) return entry.value; return EMPTY_VALUE; } @@ -1789,517 +2020,198 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ public int count(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { Objects.requireNonNull(filter); int result = 0; - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) + for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) if(filter.VALUE_TEST_VALUE(entry.value)) result++; return result; } } - final class SubMapEntrySetIterator extends SubMapEntryIterator implements ObjectListIterator { - public SubMapEntrySetIterator(boolean descending) { - super(descending); - } - - public SubMapEntrySetIterator(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry, AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE lowerFence, AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE upperFence) { - super(entry, lowerFence, upperFence); + class DecsendingSubEntryIterator extends SubMapEntryIterator implements ObjectBidirectionalIterator + { + public DecsendingSubEntryIterator(Node KEY_VALUE_GENERIC_TYPE first, Node KEY_VALUE_GENERIC_TYPE forwardFence, Node KEY_VALUE_GENERIC_TYPE backwardFence) { + super(first, forwardFence, backwardFence); } @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE next() { return nextEntry(); } + public MAP.Entry KEY_VALUE_GENERIC_TYPE previous() { + if(!hasPrevious()) throw new NoSuchElementException(); + return nextEntry(); + } + @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE previous() { return previousEntry(); } - - @Override - public void set(MAP.Entry KEY_VALUE_GENERIC_TYPE e) { throw new UnsupportedOperationException(); } - @Override - public void add(MAP.Entry KEY_VALUE_GENERIC_TYPE e) { throw new UnsupportedOperationException(); } + public MAP.Entry KEY_VALUE_GENERIC_TYPE next() { + if(!hasNext()) throw new NoSuchElementException(); + return previousEntry(); + } } - final class SubMapKeyIterator extends SubMapEntryIterator implements LIST_ITERATOR KEY_GENERIC_TYPE { - public SubMapKeyIterator(boolean descending) { - super(descending); + class AcsendingSubEntryIterator extends SubMapEntryIterator implements ObjectBidirectionalIterator + { + public AcsendingSubEntryIterator(Node KEY_VALUE_GENERIC_TYPE first, Node KEY_VALUE_GENERIC_TYPE forwardFence, Node KEY_VALUE_GENERIC_TYPE backwardFence) { + super(first, forwardFence, backwardFence); } - - public SubMapKeyIterator(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry, AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE lowerFence, AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE upperFence) { - super(entry, lowerFence, upperFence); + + @Override + public MAP.Entry KEY_VALUE_GENERIC_TYPE previous() { + if(!hasPrevious()) throw new NoSuchElementException(); + return previousEntry(); } - + @Override - public KEY_TYPE NEXT() { return nextEntry().key; } - @Override - public KEY_TYPE PREVIOUS() { return previousEntry().key; } - - @Override - public void set(KEY_TYPE e) { throw new UnsupportedOperationException(); } - @Override - public void add(KEY_TYPE e) { throw new UnsupportedOperationException(); } + public MAP.Entry KEY_VALUE_GENERIC_TYPE next() { + if(!hasNext()) throw new NoSuchElementException(); + return nextEntry(); + } } - final class SubMapValueIterator extends SubMapEntryIterator implements VALUE_LIST_ITERATOR VALUE_GENERIC_TYPE { - public SubMapValueIterator(boolean descending) { - super(descending); - } - - public SubMapValueIterator(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry, AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE lowerFence, AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE upperFence) { - super(entry, lowerFence, upperFence); + class DecsendingSubKeyIterator extends SubMapEntryIterator implements BI_ITERATOR KEY_GENERIC_TYPE + { + public DecsendingSubKeyIterator(Node KEY_VALUE_GENERIC_TYPE first, Node KEY_VALUE_GENERIC_TYPE forwardFence, Node KEY_VALUE_GENERIC_TYPE backwardFence) { + super(first, forwardFence, backwardFence); } @Override - public VALUE_TYPE VALUE_NEXT() { return nextEntry().value; } + public KEY_TYPE PREVIOUS() { + if(!hasPrevious()) throw new NoSuchElementException(); + return nextEntry().key; + } + @Override - public VALUE_TYPE VALUE_PREVIOUS() { return previousEntry().value; } - - @Override - public void set(VALUE_TYPE e) { throw new UnsupportedOperationException(); } - @Override - public void add(VALUE_TYPE e) { throw new UnsupportedOperationException(); } + public KEY_TYPE NEXT() { + if(!hasNext()) throw new NoSuchElementException(); + return previousEntry().key; + } } - abstract class SubMapEntryIterator { - CLASS_TYPE lowerFence; - CLASS_TYPE upperFence; - AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE next; - AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE previous; - AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE current; - int index = 0; - - public SubMapEntryIterator(boolean descending) { - this(descending ? findHighest() : findLowest(), fromStart ? null : findLowest(), toEnd ? null : findHighest()); + class AcsendingSubKeyIterator extends SubMapEntryIterator implements BI_ITERATOR KEY_GENERIC_TYPE + { + public AcsendingSubKeyIterator(Node KEY_VALUE_GENERIC_TYPE first, Node KEY_VALUE_GENERIC_TYPE forwardFence, Node KEY_VALUE_GENERIC_TYPE backwardFence) { + super(first, forwardFence, backwardFence); + } + + @Override + public KEY_TYPE PREVIOUS() { + if(!hasPrevious()) throw new NoSuchElementException(); + return previousEntry().key; + } + + @Override + public KEY_TYPE NEXT() { + if(!hasNext()) throw new NoSuchElementException(); + return nextEntry().key; + } + } + + class AcsendingSubValueIterator extends SubMapEntryIterator implements VALUE_BI_ITERATOR VALUE_GENERIC_TYPE + { + public AcsendingSubValueIterator(Node KEY_VALUE_GENERIC_TYPE first, Node KEY_VALUE_GENERIC_TYPE forwardFence, Node KEY_VALUE_GENERIC_TYPE backwardFence) { + super(first, forwardFence, backwardFence); + } + + @Override + public VALUE_TYPE VALUE_PREVIOUS() { + if(!hasPrevious()) throw new NoSuchElementException(); + return previousEntry().value; + } + + @Override + public VALUE_TYPE VALUE_NEXT() { + if(!hasNext()) throw new NoSuchElementException(); + return nextEntry().value; + } + } + + class DecsendingSubValueIterator extends SubMapEntryIterator implements VALUE_BI_ITERATOR VALUE_GENERIC_TYPE + { + public DecsendingSubValueIterator(Node KEY_VALUE_GENERIC_TYPE first, Node KEY_VALUE_GENERIC_TYPE forwardFence, Node KEY_VALUE_GENERIC_TYPE backwardFence) { + super(first, forwardFence, backwardFence); } - public SubMapEntryIterator(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry, AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE lowerFence, AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE upperFence) { - next = entry; - previous = entry.previous(); - this.lowerFence = lowerFence != null ? KEY_TO_OBJ(lowerFence.key) : null; - this.upperFence = upperFence != null ? KEY_TO_OBJ(upperFence.key) : null; + @Override + public VALUE_TYPE VALUE_PREVIOUS() { + if(!hasPrevious()) throw new NoSuchElementException(); + return nextEntry().value; + } + + @Override + public VALUE_TYPE VALUE_NEXT() { + if(!hasNext()) throw new NoSuchElementException(); + return previousEntry().value; + } + } + + abstract class SubMapEntryIterator + { + boolean wasForward; + Node KEY_VALUE_GENERIC_TYPE lastReturned; + Node KEY_VALUE_GENERIC_TYPE next; + boolean unboundForwardFence; + boolean unboundBackwardFence; + KEY_TYPE forwardFence; + KEY_TYPE backwardFence; + + public SubMapEntryIterator(Node KEY_VALUE_GENERIC_TYPE first, Node KEY_VALUE_GENERIC_TYPE forwardFence, Node KEY_VALUE_GENERIC_TYPE backwardFence) + { + next = first; + this.forwardFence = forwardFence == null ? null : forwardFence.key; + this.backwardFence = backwardFence == null ? null : backwardFence.key; + unboundForwardFence = forwardFence == null; + unboundBackwardFence = backwardFence == null; } public boolean hasNext() { - return next != null && (upperFence == null || KEY_EQUALS(next.key, OBJ_TO_KEY(upperFence))); + return next != null && (unboundForwardFence || next.key != forwardFence); + } + + protected Node KEY_VALUE_GENERIC_TYPE nextEntry() { + lastReturned = next; + Node KEY_VALUE_GENERIC_TYPE result = next; + next = next.next(); + wasForward = true; + return result; } public boolean hasPrevious() { - return previous != null && (lowerFence == null || KEY_EQUALS(next.key, OBJ_TO_KEY(lowerFence))); + return next != null && (unboundBackwardFence || next.key != backwardFence); } - public int nextIndex() { - return index; - } - - public int previousIndex() { - return index - 1; - } - - protected void updateNext() { - next = current.next(); - } - - protected void updatePrevious() { - previous = current.previous(); - } - - public AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE nextEntry() { - if(!hasNext()) throw new NoSuchElementException(); - current = previous = next; - updateNext(); - index++; - return current; - } - - public AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE previousEntry() { - if(!hasPrevious()) throw new NoSuchElementException(); - current = next = previous; - updatePrevious(); - index--; - return current; + protected Node KEY_VALUE_GENERIC_TYPE previousEntry() { + lastReturned = next; + Node KEY_VALUE_GENERIC_TYPE result = next; + next = next.previous(); + wasForward = false; + return result; } public void remove() { - if(current == null) throw new IllegalStateException(); - if(current == previous) index--; - updateNext(); - updatePrevious(); - if(current.needsSuccessor()) next = current; - m.removeNode(current); - current = null; + if(lastReturned == null) throw new IllegalStateException(); + if(wasForward && lastReturned.needsSuccessor()) next = lastReturned; + map.removeNode(lastReturned); + lastReturned = null; } } } - private class EntrySet extends AbstractObjectSet { - + class Values extends VALUE_ABSTRACT_COLLECTION VALUE_GENERIC_TYPE + { @Override - public ObjectIterator iterator() { - return new EntryIterator(false); + public VALUE_ITERATOR VALUE_GENERIC_TYPE iterator() { + return new AscendingValueIterator(first); } @Override - @Deprecated - public boolean contains(Object o) { - if(o instanceof Map.Entry) { - if(o instanceof MAP.Entry) { - MAP.Entry KEY_VALUE_GENERIC_TYPE entry = (MAP.Entry KEY_VALUE_GENERIC_TYPE)o; - if(entry.getKey() == null && comparator() == null) return false; - AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE subEntry = AVL_TREE_MAP.this.findNode(entry.ENTRY_KEY()); - if(subEntry != null) return VALUE_EQUALS(entry.ENTRY_VALUE(), subEntry.value); - } - else { - Map.Entry entry = (Map.Entry)o; - if(entry.getKey() == null && comparator() == null) return false; -#if !TYPE_OBJECT - if(!(entry.getKey() instanceof CLASS_TYPE)) return false; -#endif - AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE subEntry = AVL_TREE_MAP.this.findNode((CLASS_TYPE)entry.getKey()); - if(subEntry != null) return Objects.equals(entry.getValue(), VALUE_TO_OBJ(subEntry.value)); - } - } - return false; - } - - @Override - @Deprecated - public boolean remove(Object o) { - if(o instanceof Map.Entry) { - if(o instanceof MAP.Entry) { - MAP.Entry KEY_VALUE_GENERIC_TYPE entry = (MAP.Entry KEY_VALUE_GENERIC_TYPE)o; - return AVL_TREE_MAP.this.remove(entry.ENTRY_KEY(), entry.ENTRY_VALUE()); - } - Map.Entry entry = (Map.Entry)o; - return AVL_TREE_MAP.this.remove(entry.getKey(), entry.getValue()); - } - return false; - } - - @Override - public int size() { - return AVL_TREE_MAP.this.size(); - } + public boolean add(VALUE_TYPE e) { throw new UnsupportedOperationException(); } @Override public void clear() { AVL_TREE_MAP.this.clear(); } - @Override - public void forEach(Consumer action) { - Objects.requireNonNull(action); - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) - action.accept(new BasicEntryKV_BRACES(entry.key, entry.value)); - } - - @Override - public void forEach(E input, ObjectObjectConsumer action) { - Objects.requireNonNull(action); - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) - action.accept(input, new BasicEntryKV_BRACES(entry.key, entry.value)); - } - - @Override - public boolean matchesAny(Object2BooleanFunction filter) { - Objects.requireNonNull(filter); - if(size() <= 0) return false; - BasicEntry KEY_VALUE_GENERIC_TYPE subEntry = new BasicEntryKV_BRACES(); - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { - subEntry.set(entry.key, entry.value); - if(filter.getBoolean(subEntry)) return true; - } - return false; - } - - @Override - public boolean matchesNone(Object2BooleanFunction filter) { - Objects.requireNonNull(filter); - if(size() <= 0) return true; - BasicEntry KEY_VALUE_GENERIC_TYPE subEntry = new BasicEntryKV_BRACES(); - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { - subEntry.set(entry.key, entry.value); - if(filter.getBoolean(subEntry)) return false; - } - return true; - } - - @Override - public boolean matchesAll(Object2BooleanFunction filter) { - Objects.requireNonNull(filter); - if(size() <= 0) return true; - BasicEntry KEY_VALUE_GENERIC_TYPE subEntry = new BasicEntryKV_BRACES(); - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { - subEntry.set(entry.key, entry.value); - if(!filter.getBoolean(subEntry)) return false; - } - return true; - } - - @Override - public E reduce(E identity, BiFunction operator) { - Objects.requireNonNull(operator); - E state = identity; - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { - state = operator.apply(state, new BasicEntryKV_BRACES(entry.key, entry.value)); - } - return state; - } - - @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE reduce(ObjectObjectUnaryOperator operator) { - Objects.requireNonNull(operator); - MAP.Entry KEY_VALUE_GENERIC_TYPE state = null; - boolean empty = true; - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { - if(empty) { - empty = false; - state = new BasicEntryKV_BRACES(entry.key, entry.value); - continue; - } - state = operator.apply(state, new BasicEntryKV_BRACES(entry.key, entry.value)); - } - return state; - } - - @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE findFirst(Object2BooleanFunction filter) { - Objects.requireNonNull(filter); - if(size() <= 0) return null; - BasicEntry KEY_VALUE_GENERIC_TYPE subEntry = new BasicEntryKV_BRACES(); - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { - subEntry.set(entry.key, entry.value); - if(filter.getBoolean(subEntry)) return subEntry; - } - return null; - } - } - - static final class KeySet KEY_VALUE_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE implements NAVIGABLE_SET KEY_GENERIC_TYPE { - NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE m; - - KeySet(NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE m) { - this.m = m; - } - -#if !TYPE_OBJECT - @Override - public void setDefaultMaxValue(KEY_TYPE e) { m.setDefaultMaxValue(e); } - @Override - public KEY_TYPE getDefaultMaxValue() { return m.getDefaultMaxValue(); } - @Override - public void setDefaultMinValue(KEY_TYPE e) { m.setDefaultMinValue(e); } - @Override - public KEY_TYPE getDefaultMinValue() { return m.getDefaultMinValue(); } -#endif - @Override - public boolean add(KEY_TYPE o) { throw new UnsupportedOperationException(); } - @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 m.comparator(); } - @Override - public KEY_TYPE lower(KEY_TYPE e) { return m.lowerKey(e); } - @Override - public KEY_TYPE floor(KEY_TYPE e) { return m.floorKey(e); } - @Override - public KEY_TYPE ceiling(KEY_TYPE e) { return m.ceilingKey(e); } - @Override - public KEY_TYPE higher(KEY_TYPE e) { return m.higherKey(e); } - @Override - public KEY_TYPE FIRST_KEY() { return m.FIRST_ENTRY_KEY(); } - @Override - public KEY_TYPE POLL_FIRST_KEY() { return m.POLL_FIRST_ENTRY_KEY(); } - @Override - public KEY_TYPE LAST_KEY() { return m.LAST_ENTRY_KEY(); } - @Override - public KEY_TYPE POLL_LAST_KEY() { return m.POLL_LAST_ENTRY_KEY(); } -#if TYPE_OBJECT - @Override - public boolean remove(Object o) { - int oldSize = m.size(); - m.remove(o); - return oldSize != m.size(); - } - -#else - @Override - public boolean remove(KEY_TYPE o) { - int oldSize = m.size(); - m.remove(o); - return oldSize != m.size(); - } - -#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)); - } - - @Override - public NAVIGABLE_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement, boolean inclusive) { - return new KeySetKV_BRACES(m.headMap(toElement, inclusive)); - } - - @Override - public NAVIGABLE_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement, boolean inclusive) { - return new KeySetKV_BRACES(m.tailMap(fromElement, inclusive)); - } - - @Override - public NAVIGABLE_SET KEY_GENERIC_TYPE descendingSet() { - return new KeySetKV_BRACES(m.descendingMap()); - } - - @Override - public BI_ITERATOR KEY_GENERIC_TYPE iterator() { - if(m instanceof AVL_TREE_MAP) return ((AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE)m).keyIterator(false); - return ((NavigableSubMap KEY_VALUE_GENERIC_TYPE)m).keyIterator(false); - } - - @Override - public BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement) { - if(m instanceof AVL_TREE_MAP) return ((AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE)m).keyIterator(fromElement); - return ((NavigableSubMap KEY_VALUE_GENERIC_TYPE)m).keyIterator(fromElement); - } - - @Override - public BI_ITERATOR KEY_GENERIC_TYPE descendingIterator() { - if(m instanceof AVL_TREE_MAP) return ((AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE)m).keyIterator(true); - return ((NavigableSubMap KEY_VALUE_GENERIC_TYPE)m).keyIterator(true); - } - @Override public int size() { - return m.size(); + return AVL_TREE_MAP.this.size; } - @Override - public void clear() { - m.clear(); - } - - protected AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE start() { - if(m instanceof AVL_TREE_MAP) return ((AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE)m).first; - return ((NavigableSubMap KEY_VALUE_GENERIC_TYPE)m).findLowest(); - } - - protected AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE end() { - if(m instanceof AVL_TREE_MAP) return null; - return ((NavigableSubMap KEY_VALUE_GENERIC_TYPE)m).findLowest(); - } - - protected AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE next(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry) { - if(m instanceof AVL_TREE_MAP) return entry.next(); - return ((NavigableSubMap KEY_VALUE_GENERIC_TYPE)m).next(entry); - } - - protected AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE previous(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry) { - if(m instanceof AVL_TREE_MAP) return entry.previous(); - return ((NavigableSubMap KEY_VALUE_GENERIC_TYPE)m).previous(entry); - } - - @Override - public void forEach(CONSUMER KEY_SUPER_GENERIC_TYPE action) { - Objects.requireNonNull(action); - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - action.accept(entry.key); - } - - @Override - public void forEach(E input, BI_FROM_OBJECT_CONSUMER KSK_GENERIC_TYPE action) { - Objects.requireNonNull(action); - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - action.accept(input, entry.key); - } - - @Override - public boolean matchesAny(PREDICATE KEY_GENERIC_TYPE filter) { - Objects.requireNonNull(filter); - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.TEST_VALUE(entry.key)) return true; - return false; - } - - @Override - public boolean matchesNone(PREDICATE KEY_GENERIC_TYPE filter) { - Objects.requireNonNull(filter); - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.TEST_VALUE(entry.key)) return false; - return true; - } - - @Override - public boolean matchesAll(PREDICATE KEY_GENERIC_TYPE filter) { - Objects.requireNonNull(filter); - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(!filter.TEST_VALUE(entry.key)) return false; - return true; - } - -#if !TYPE_OBJECT - @Override - public KEY_TYPE reduce(KEY_TYPE identity, SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { - Objects.requireNonNull(operator); - KEY_TYPE state = identity; - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - state = operator.APPLY_KEY_VALUE(state, entry.key); - return state; - } - -#else - @Override - public KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction operator) { - Objects.requireNonNull(operator); - KEY_SPECIAL_TYPE state = identity; - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - state = operator.apply(state, entry.key); - return state; - } - -#endif - @Override - public KEY_TYPE reduce(SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { - Objects.requireNonNull(operator); - KEY_TYPE state = EMPTY_KEY_VALUE; - boolean empty = true; - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) { - if(empty) { - empty = false; - state = entry.key; - continue; - } - state = operator.APPLY_KEY_VALUE(state, entry.key); - } - return state; - } - - @Override - public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { - Objects.requireNonNull(filter); - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.TEST_VALUE(entry.key)) return entry.key; - return EMPTY_KEY_VALUE; - } - - @Override - public int count(PREDICATE KEY_GENERIC_TYPE filter) { - Objects.requireNonNull(filter); - int result = 0; - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.TEST_VALUE(entry.key)) result++; - return result; - } - } - - final class Values extends VALUE_ABSTRACT_COLLECTION VALUE_GENERIC_TYPE { - - @Override - public boolean add(VALUE_TYPE o) { throw new UnsupportedOperationException(); } - #if VALUE_OBJECT @Override public boolean contains(Object e) { @@ -2314,36 +2226,34 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ #endif @Override - public VALUE_ITERATOR VALUE_GENERIC_TYPE iterator() { return new ValueIterator(false); } - - @Override - public int size() { - return AVL_TREE_MAP.this.size(); - } - - @Override - public void clear() { - AVL_TREE_MAP.this.clear(); + public boolean remove(Object o) { + for(Node KEY_VALUE_GENERIC_TYPE entry = first; entry != null; entry = entry.next()) { + if(Objects.equals(entry.getValue(), o)) { + removeNode(entry); + return true; + } + } + return false; } @Override public void forEach(VALUE_CONSUMER VALUE_SUPER_GENERIC_TYPE action) { Objects.requireNonNull(action); - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) + for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) action.accept(entry.value); } @Override public void forEach(E input, VALUE_BI_FROM_OBJECT_CONSUMER VSV_GENERIC_TYPE action) { Objects.requireNonNull(action); - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) + for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) action.accept(input, entry.value); } @Override public boolean matchesAny(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { Objects.requireNonNull(filter); - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) + for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) if(filter.VALUE_TEST_VALUE(entry.value)) return true; return false; } @@ -2351,7 +2261,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ @Override public boolean matchesNone(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { Objects.requireNonNull(filter); - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) + for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) if(filter.VALUE_TEST_VALUE(entry.value)) return false; return true; } @@ -2359,38 +2269,38 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ @Override public boolean matchesAll(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { Objects.requireNonNull(filter); - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) + for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) if(!filter.VALUE_TEST_VALUE(entry.value)) return false; return true; } -#if !VALUE_OBJECT + #if !VALUE_OBJECT @Override public VALUE_TYPE reduce(VALUE_TYPE identity, VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { Objects.requireNonNull(operator); VALUE_TYPE state = identity; - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) + for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) state = operator.APPLY_VALUE(state, entry.value); return state; } -#else + #else @Override public VALUE_SPECIAL_TYPE reduce(VALUE_SPECIAL_TYPE identity, BiFunction operator) { Objects.requireNonNull(operator); VALUE_SPECIAL_TYPE state = identity; - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) + for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) state = operator.apply(state, entry.value); return state; } -#endif + #endif @Override public VALUE_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { Objects.requireNonNull(operator); VALUE_TYPE state = EMPTY_VALUE; boolean empty = true; - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { + for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { if(empty) { empty = false; state = entry.value; @@ -2404,7 +2314,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ @Override public VALUE_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { Objects.requireNonNull(filter); - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) + for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) if(filter.VALUE_TEST_VALUE(entry.value)) return entry.value; return EMPTY_VALUE; } @@ -2413,185 +2323,333 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ public int count(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { Objects.requireNonNull(filter); int result = 0; - for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) + for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) if(filter.VALUE_TEST_VALUE(entry.value)) result++; return result; } } - final class EntryIterator extends MapEntryIterator implements ObjectListIterator { + class EntrySet extends AbstractObjectSet { - public EntryIterator(boolean descending) { - super(descending); - } - - public EntryIterator(Entry KEY_VALUE_GENERIC_TYPE entry) { - super(entry); + @Override + public ObjectIterator iterator() { + return new AscendingMapEntryIterator(first); } @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE next() { - return nextEntry(); + public void clear() { + AVL_TREE_MAP.this.clear(); } + @Override + public int size() { + return AVL_TREE_MAP.this.size; + } + + @Override + public boolean contains(Object o) { + if (!(o instanceof Map.Entry)) return false; + if(o instanceof MAP.Entry) + { + MAP.Entry KEY_VALUE_GENERIC_TYPE entry = (MAP.Entry KEY_VALUE_GENERIC_TYPE) o; +#if TYPE_OBJECT + if(entry.getKey() == null && comparator() == null) return false; +#endif + KEY_TYPE key = entry.ENTRY_KEY(); + Node KEY_VALUE_GENERIC_TYPE node = findNode(key); + return node != null && VALUE_EQUALS(entry.ENTRY_VALUE(), node.ENTRY_VALUE()); + } + Map.Entry entry = (Map.Entry) o; + if(entry.getKey() == null && comparator() == null) return false; +#if !TYPE_OBJECT + if(!(entry.getKey() instanceof CLASS_TYPE)) return false; +#endif + CLASS_TYPE key = (CLASS_TYPE)entry.getKey(); + Node KEY_VALUE_GENERIC_TYPE node = findNode(key); + return node != null && Objects.equals(entry.getValue(), VALUE_TO_OBJ(node.ENTRY_VALUE())); + } + + @Override + public boolean remove(Object o) { + if (!(o instanceof Map.Entry)) return false; + if(o instanceof MAP.Entry) + { + MAP.Entry KEY_VALUE_GENERIC_TYPE entry = (MAP.Entry KEY_VALUE_GENERIC_TYPE) o; + KEY_TYPE key = entry.ENTRY_KEY(); + Node KEY_VALUE_GENERIC_TYPE node = findNode(key); + if (node != null && VALUE_EQUALS(entry.ENTRY_VALUE(), node.ENTRY_VALUE())) { + removeNode(node); + return true; + } + return false; + } + Map.Entry entry = (Map.Entry) o; + CLASS_TYPE key = (CLASS_TYPE)entry.getKey(); + Node KEY_VALUE_GENERIC_TYPE node = findNode(key); + if (node != null && Objects.equals(entry.getValue(), VALUE_TO_OBJ(node.ENTRY_VALUE()))) { + removeNode(node); + return true; + } + return false; + } + + @Override + public void forEach(Consumer action) { + Objects.requireNonNull(action); + for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) + action.accept(new BasicEntryKV_BRACES(entry.key, entry.value)); + } + + @Override + public void forEach(E input, ObjectObjectConsumer action) { + Objects.requireNonNull(action); + for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) + action.accept(input, new BasicEntryKV_BRACES(entry.key, entry.value)); + } + + @Override + public boolean matchesAny(Object2BooleanFunction filter) { + Objects.requireNonNull(filter); + if(size() <= 0) return false; + BasicEntry KEY_VALUE_GENERIC_TYPE subEntry = new BasicEntryKV_BRACES(); + for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { + subEntry.set(entry.key, entry.value); + if(filter.getBoolean(subEntry)) return true; + } + return false; + } + + @Override + public boolean matchesNone(Object2BooleanFunction filter) { + Objects.requireNonNull(filter); + if(size() <= 0) return true; + BasicEntry KEY_VALUE_GENERIC_TYPE subEntry = new BasicEntryKV_BRACES(); + for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { + subEntry.set(entry.key, entry.value); + if(filter.getBoolean(subEntry)) return false; + } + return true; + } + + @Override + public boolean matchesAll(Object2BooleanFunction filter) { + Objects.requireNonNull(filter); + if(size() <= 0) return true; + BasicEntry KEY_VALUE_GENERIC_TYPE subEntry = new BasicEntryKV_BRACES(); + for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { + subEntry.set(entry.key, entry.value); + if(!filter.getBoolean(subEntry)) return false; + } + return true; + } + + @Override + public E reduce(E identity, BiFunction operator) { + Objects.requireNonNull(operator); + E state = identity; + for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { + state = operator.apply(state, new BasicEntryKV_BRACES(entry.key, entry.value)); + } + return state; + } + + @Override + public MAP.Entry KEY_VALUE_GENERIC_TYPE reduce(ObjectObjectUnaryOperator operator) { + Objects.requireNonNull(operator); + MAP.Entry KEY_VALUE_GENERIC_TYPE state = null; + boolean empty = true; + for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { + if(empty) { + empty = false; + state = new BasicEntryKV_BRACES(entry.key, entry.value); + continue; + } + state = operator.apply(state, new BasicEntryKV_BRACES(entry.key, entry.value)); + } + return state; + } + + @Override + public MAP.Entry KEY_VALUE_GENERIC_TYPE findFirst(Object2BooleanFunction filter) { + Objects.requireNonNull(filter); + if(size() <= 0) return null; + BasicEntry KEY_VALUE_GENERIC_TYPE subEntry = new BasicEntryKV_BRACES(); + for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { + subEntry.set(entry.key, entry.value); + if(filter.getBoolean(subEntry)) return subEntry; + } + return null; + } + + @Override + public int count(Object2BooleanFunction filter) { + Objects.requireNonNull(filter); + if(size() <= 0) return 0; + int result = 0; + BasicEntry KEY_VALUE_GENERIC_TYPE subEntry = new BasicEntryKV_BRACES(); + for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { + subEntry.set(entry.key, entry.value); + if(filter.getBoolean(subEntry)) result++; + } + return result; + } + } + + class DescendingKeyIterator extends MapEntryIterator implements BI_ITERATOR KEY_GENERIC_TYPE + { + public DescendingKeyIterator(Node KEY_VALUE_GENERIC_TYPE first) { + super(first); + } + + @Override + public KEY_TYPE PREVIOUS() { + if(!hasPrevious()) throw new NoSuchElementException(); + return nextEntry().key; + } + + @Override + public KEY_TYPE NEXT() { + if(!hasNext()) throw new NoSuchElementException(); + return previousEntry().key; + } + } + + class AscendingMapEntryIterator extends MapEntryIterator implements ObjectBidirectionalIterator + { + public AscendingMapEntryIterator(Node KEY_VALUE_GENERIC_TYPE first) + { + super(first); + } + @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE previous() { + if(!hasPrevious()) throw new NoSuchElementException(); return previousEntry(); } - + @Override - public void set(MAP.Entry KEY_VALUE_GENERIC_TYPE e) { throw new UnsupportedOperationException(); } - - @Override - public void add(MAP.Entry KEY_VALUE_GENERIC_TYPE e) { throw new UnsupportedOperationException(); } + public MAP.Entry KEY_VALUE_GENERIC_TYPE next() { + if(!hasNext()) throw new NoSuchElementException(); + return nextEntry(); + } } - final class KeyIterator extends MapEntryIterator implements LIST_ITERATOR KEY_GENERIC_TYPE { - - public KeyIterator(boolean descending) { - super(descending); + class AscendingValueIterator extends MapEntryIterator implements VALUE_BI_ITERATOR VALUE_GENERIC_TYPE + { + public AscendingValueIterator(Node KEY_VALUE_GENERIC_TYPE first) { + super(first); } - - public KeyIterator(Entry KEY_VALUE_GENERIC_TYPE entry) { - super(entry); - } - - @Override - public KEY_TYPE PREVIOUS() { return previousEntry().key; } - @Override - public KEY_TYPE NEXT() { return nextEntry().key; } - - @Override - public void set(KEY_TYPE e) { throw new UnsupportedOperationException(); } - @Override - public void add(KEY_TYPE e) { throw new UnsupportedOperationException(); } - } - - final class ValueIterator extends MapEntryIterator implements VALUE_LIST_ITERATOR VALUE_GENERIC_TYPE { - - public ValueIterator(boolean descending) { - super(descending); - } - - public ValueIterator(Entry KEY_VALUE_GENERIC_TYPE entry) { - super(entry); - } - + @Override public VALUE_TYPE VALUE_PREVIOUS() { + if(!hasPrevious()) throw new NoSuchElementException(); return previousEntry().value; } - + @Override public VALUE_TYPE VALUE_NEXT() { + if(!hasNext()) throw new NoSuchElementException(); return nextEntry().value; } - - @Override - public void set(VALUE_TYPE e) { throw new UnsupportedOperationException(); } - - @Override - public void add(VALUE_TYPE e) { throw new UnsupportedOperationException(); } } - abstract class MapEntryIterator { - Entry KEY_VALUE_GENERIC_TYPE next; - Entry KEY_VALUE_GENERIC_TYPE previous; - Entry KEY_VALUE_GENERIC_TYPE current; - int index = 0; - - public MapEntryIterator(boolean descending) { - if(descending) previous = last; - else next = first; + class AscendingKeyIterator extends MapEntryIterator implements BI_ITERATOR KEY_GENERIC_TYPE + { + public AscendingKeyIterator(Node KEY_VALUE_GENERIC_TYPE first) { + super(first); } + + @Override + public KEY_TYPE PREVIOUS() { + if(!hasPrevious()) throw new NoSuchElementException(); + return previousEntry().key; + } + + @Override + public KEY_TYPE NEXT() { + if(!hasNext()) throw new NoSuchElementException(); + return nextEntry().key; + } + } + + abstract class MapEntryIterator + { + boolean wasMoved = false; + Node KEY_VALUE_GENERIC_TYPE lastReturned; + Node KEY_VALUE_GENERIC_TYPE next; - public MapEntryIterator(Entry KEY_VALUE_GENERIC_TYPE entry) { - next = entry; - previous = entry.previous(); + public MapEntryIterator(Node KEY_VALUE_GENERIC_TYPE first) + { + next = first; } public boolean hasNext() { - return next != null; + return next != null; + } + + protected Node KEY_VALUE_GENERIC_TYPE nextEntry() { + lastReturned = next; + Node KEY_VALUE_GENERIC_TYPE result = next; + next = next.next(); + wasMoved = true; + return result; } public boolean hasPrevious() { - return previous != null; + return next != null; } - public int nextIndex() { - return index; - } - - public int previousIndex() { - return index - 1; - } - - protected void updateNext() { - next = current.next(); - } - - protected void updatePrevious() { - previous = current.previous(); - } - - public Entry KEY_VALUE_GENERIC_TYPE nextEntry() { - if(!hasNext()) throw new NoSuchElementException(); - current = previous = next; - updateNext(); - index++; - return current; - } - - public Entry KEY_VALUE_GENERIC_TYPE previousEntry() { - if(!hasPrevious()) throw new NoSuchElementException(); - current = next = previous; - updatePrevious(); - index--; - return current; + protected Node KEY_VALUE_GENERIC_TYPE previousEntry() { + lastReturned = next; + Node KEY_VALUE_GENERIC_TYPE result = next; + next = next.previous(); + wasMoved = false; + return result; } public void remove() { - if(current == null) throw new IllegalStateException(); - if(current == previous) index--; - updateNext(); - updatePrevious(); - if(current.needsSuccessor()) next = current; - removeNode(current); - current = null; + if(lastReturned == null) throw new IllegalStateException(); + if(wasMoved && lastReturned.needsSuccessor()) next = lastReturned; + removeNode(lastReturned); + lastReturned = null; } } - private static final class Entry KEY_VALUE_GENERIC_TYPE implements MAP.Entry KEY_VALUE_GENERIC_TYPE + private static final class Node KEY_VALUE_GENERIC_TYPE implements MAP.Entry KEY_VALUE_GENERIC_TYPE { KEY_TYPE key; VALUE_TYPE value; int state; - Entry KEY_VALUE_GENERIC_TYPE parent; - Entry KEY_VALUE_GENERIC_TYPE left; - Entry KEY_VALUE_GENERIC_TYPE right; + Node KEY_VALUE_GENERIC_TYPE parent; + Node KEY_VALUE_GENERIC_TYPE left; + Node KEY_VALUE_GENERIC_TYPE right; - Entry(KEY_TYPE key, VALUE_TYPE value, Entry KEY_VALUE_GENERIC_TYPE parent) { + Node(KEY_TYPE key, VALUE_TYPE value, Node KEY_VALUE_GENERIC_TYPE parent) { this.key = key; this.value = value; this.parent = parent; } - Entry KEY_VALUE_GENERIC_TYPE copy() { - Entry KEY_VALUE_GENERIC_TYPE entry = new EntryKV_BRACES(key, value, null); + Node KEY_VALUE_GENERIC_TYPE copy() { + Node KEY_VALUE_GENERIC_TYPE entry = new NodeKV_BRACES(key, value, null); entry.state = state; if(left != null) { - Entry KEY_VALUE_GENERIC_TYPE newLeft = left.copy(); + Node KEY_VALUE_GENERIC_TYPE newLeft = left.copy(); entry.left = newLeft; newLeft.parent = entry; } if(right != null) { - Entry KEY_VALUE_GENERIC_TYPE newRight = right.copy(); + Node KEY_VALUE_GENERIC_TYPE newRight = right.copy(); entry.right = newRight; newRight.parent = entry; } return entry; } + public BasicEntry KEY_VALUE_GENERIC_TYPE export() { + return new BasicEntryKV_BRACES(key, value); + } + @Override public KEY_TYPE ENTRY_KEY() { return key; @@ -2662,7 +2720,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ boolean needsSuccessor() { return left != null && right != null; } - boolean replace(Entry KEY_VALUE_GENERIC_TYPE entry) { + boolean replace(Node KEY_VALUE_GENERIC_TYPE entry) { if(entry != null) entry.parent = parent; if(parent != null) { if(parent.left == this) parent.left = entry; @@ -2671,14 +2729,14 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ return parent == null; } - Entry KEY_VALUE_GENERIC_TYPE next() { + Node KEY_VALUE_GENERIC_TYPE next() { if(right != null) { - Entry KEY_VALUE_GENERIC_TYPE parent = right; + Node KEY_VALUE_GENERIC_TYPE parent = right; while(parent.left != null) parent = parent.left; return parent; } - Entry KEY_VALUE_GENERIC_TYPE parent = this.parent; - Entry KEY_VALUE_GENERIC_TYPE control = this; + Node KEY_VALUE_GENERIC_TYPE parent = this.parent; + Node KEY_VALUE_GENERIC_TYPE control = this; while(parent != null && control == parent.right) { control = parent; parent = parent.parent; @@ -2686,14 +2744,14 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ return parent; } - Entry KEY_VALUE_GENERIC_TYPE previous() { + Node KEY_VALUE_GENERIC_TYPE previous() { if(left != null) { - Entry KEY_VALUE_GENERIC_TYPE parent = left; + Node KEY_VALUE_GENERIC_TYPE parent = left; while(parent.right != null) parent = parent.right; return parent; } - Entry KEY_VALUE_GENERIC_TYPE parent = this.parent; - Entry KEY_VALUE_GENERIC_TYPE control = this; + Node KEY_VALUE_GENERIC_TYPE parent = this.parent; + Node KEY_VALUE_GENERIC_TYPE control = this; while(parent != null && control == parent.left) { control = parent; parent = parent.parent; diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/impl/tree/RBTreeMap.template b/src/builder/resources/speiger/assets/collections/templates/maps/impl/tree/RBTreeMap.template index 7def1a73..4dd32168 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/impl/tree/RBTreeMap.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/impl/tree/RBTreeMap.template @@ -1,5 +1,6 @@ package speiger.src.collections.PACKAGE.maps.impl.tree; +import java.util.Collections; import java.util.Map; #if TYPE_OBJECT import java.util.Comparator; @@ -24,7 +25,6 @@ import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR; #if !SAME_TYPE import speiger.src.collections.PACKAGE.functions.function.SINGLE_UNARY_OPERATOR; #endif -import speiger.src.collections.PACKAGE.lists.LIST_ITERATOR; import speiger.src.collections.PACKAGE.maps.abstracts.ABSTRACT_MAP; import speiger.src.collections.PACKAGE.maps.interfaces.MAP; import speiger.src.collections.PACKAGE.maps.interfaces.NAVIGABLE_MAP; @@ -32,16 +32,14 @@ import speiger.src.collections.PACKAGE.sets.ABSTRACT_SET; 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.ITERATORS; import speiger.src.collections.PACKAGE.utils.maps.MAPS; import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_ABSTRACT_COLLECTION; import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_COLLECTION; import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_ITERATOR; import speiger.src.collections.VALUE_PACKAGE.functions.VALUE_SUPPLIER; #if !SAME_TYPE +import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_BI_ITERATOR; import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_UNARY_OPERATOR; -import speiger.src.collections.VALUE_PACKAGE.lists.VALUE_LIST_ITERATOR; -import speiger.src.collections.VALUE_PACKAGE.utils.VALUE_ITERATORS; #endif #if !VALUE_OBJECT && !SAME_TYPE import speiger.src.collections.VALUE_PACKAGE.functions.VALUE_CONSUMER; @@ -62,8 +60,7 @@ import speiger.src.collections.objects.functions.consumer.VALUE_BI_FROM_OBJECT_C import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_PREDICATE; #endif #if !TYPE_OBJECT && !VALUE_OBJECT -import speiger.src.collections.objects.lists.ObjectListIterator; -import speiger.src.collections.objects.utils.ObjectIterators; +import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; #endif #if !VALUE_OBJECT import speiger.src.collections.objects.collections.ObjectIterator; @@ -88,11 +85,11 @@ import speiger.src.collections.objects.sets.ObjectSet; public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE implements NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE { /** The center of the Tree */ - protected transient Entry KEY_VALUE_GENERIC_TYPE tree; + protected transient Node KEY_VALUE_GENERIC_TYPE tree; /** The Lowest possible Node */ - protected transient Entry KEY_VALUE_GENERIC_TYPE first; + protected transient Node KEY_VALUE_GENERIC_TYPE first; /** The Highest possible Node */ - protected transient Entry KEY_VALUE_GENERIC_TYPE last; + protected transient Node KEY_VALUE_GENERIC_TYPE last; /** The amount of elements stored in the Map */ protected int size = 0; /** The Sorter of the Tree */ @@ -209,7 +206,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G comparator = comp; putAll(map); } - + #if TYPE_OBJECT /** only used for primitives * @return null @@ -231,18 +228,19 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G public KEY_TYPE getDefaultMinValue() { return defaultMinNotFound; } #endif + @Override public VALUE_TYPE put(KEY_TYPE key, VALUE_TYPE value) { #if TYPE_OBJECT validate(key); #endif if(tree == null) { - tree = first = last = new EntryKV_BRACES(key, value, null); + tree = first = last = new NodeKV_BRACES(key, value, null); size++; return getDefaultReturnValue(); } int compare = 0; - Entry KEY_VALUE_GENERIC_TYPE parent = tree; + Node KEY_VALUE_GENERIC_TYPE parent = tree; while(true) { if((compare = compare(key, parent.key)) == 0) return parent.setValue(value); if(compare < 0) { @@ -254,7 +252,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G parent = parent.right; } } - Entry KEY_VALUE_GENERIC_TYPE adding = new EntryKV_BRACES(key, value, parent); + Node KEY_VALUE_GENERIC_TYPE adding = new NodeKV_BRACES(key, value, parent); if(compare < 0) { parent.left = adding; if(parent == first) first = adding; @@ -273,14 +271,13 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G #if TYPE_OBJECT validate(key); #endif - if(tree == null) { - tree = first = last = new EntryKV_BRACES(key, value, null); + tree = first = last = new NodeKV_BRACES(key, value, null); size++; return getDefaultReturnValue(); } int compare = 0; - Entry KEY_VALUE_GENERIC_TYPE parent = tree; + Node KEY_VALUE_GENERIC_TYPE parent = tree; while(true) { if((compare = compare(key, parent.key)) == 0) return parent.value; if(compare < 0) { @@ -292,7 +289,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G parent = parent.right; } } - Entry KEY_VALUE_GENERIC_TYPE adding = new EntryKV_BRACES(key, value, parent); + Node KEY_VALUE_GENERIC_TYPE adding = new NodeKV_BRACES(key, value, parent); if(compare < 0) { parent.left = adding; if(parent == first) first = adding; @@ -313,12 +310,12 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G validate(key); #endif if(tree == null) { - tree = first = last = new EntryKV_BRACES(key, value, null); + tree = first = last = new NodeKV_BRACES(key, value, null); size++; return getDefaultReturnValue(); } int compare = 0; - Entry KEY_VALUE_GENERIC_TYPE parent = tree; + Node KEY_VALUE_GENERIC_TYPE parent = tree; while(true) { if((compare = compare(key, parent.key)) == 0) return parent.addTo(value); if(compare < 0) { @@ -330,7 +327,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G parent = parent.right; } } - Entry KEY_VALUE_GENERIC_TYPE adding = new EntryKV_BRACES(key, value, parent); + Node KEY_VALUE_GENERIC_TYPE adding = new NodeKV_BRACES(key, value, parent); if(compare < 0) { parent.left = adding; if(parent == first) first = adding; @@ -360,7 +357,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G @Override public COMPARATOR KEY_GENERIC_TYPE comparator() { return comparator; } - + #if TYPE_OBJECT @Override public boolean containsKey(Object key) { @@ -376,21 +373,21 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G #endif @Override public VALUE_TYPE GET_VALUE(KEY_TYPE key) { - Entry KEY_VALUE_GENERIC_TYPE node = findNode(key); + Node KEY_VALUE_GENERIC_TYPE node = findNode(key); return node == null ? getDefaultReturnValue() : node.value; } #if TYPE_OBJECT && VALUE_OBJECT @Override public VALUE_TYPE getOrDefault(Object key, VALUE_TYPE defaultValue) { - Entry KEY_VALUE_GENERIC_TYPE node = findNode((CLASS_TYPE)key); + Node KEY_VALUE_GENERIC_TYPE node = findNode((CLASS_TYPE)key); return node == null ? defaultValue : node.value; } #else @Override public VALUE_TYPE getOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue) { - Entry KEY_VALUE_GENERIC_TYPE node = findNode(key); + Node KEY_VALUE_GENERIC_TYPE node = findNode(key); return node == null ? defaultValue : node.value; } @@ -403,7 +400,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G @Override public KEY_TYPE POLL_FIRST_ENTRY_KEY() { - if(tree == null) throw new NoSuchElementException(); + if(tree == null) return EMPTY_KEY_VALUE; KEY_TYPE result = first.key; removeNode(first); return result; @@ -417,7 +414,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G @Override public KEY_TYPE POLL_LAST_ENTRY_KEY() { - if(tree == null) throw new NoSuchElementException(); + if(tree == null) return EMPTY_KEY_VALUE; KEY_TYPE result = last.key; removeNode(last); return result; @@ -425,28 +422,28 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE firstEntry() { - if(tree == null) throw new NoSuchElementException(); - return new BasicEntryKV_BRACES(first.key, first.value); + if(tree == null) return null; + return first.export(); } @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE lastEntry() { - if(tree == null) throw new NoSuchElementException(); - return new BasicEntryKV_BRACES(last.key, last.value); + if(tree == null) return null; + return last.export(); } @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE pollFirstEntry() { - if(tree == null) throw new NoSuchElementException(); - BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES(first.key, first.value); + if(tree == null) return null; + BasicEntry KEY_VALUE_GENERIC_TYPE entry = first.export(); removeNode(first); return entry; } @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE pollLastEntry() { - if(tree == null) throw new NoSuchElementException(); - BasicEntry KEY_VALUE_GENERIC_TYPE entry = new BasicEntryKV_BRACES(last.key, last.value); + if(tree == null) return null; + BasicEntry KEY_VALUE_GENERIC_TYPE entry = last.export(); removeNode(last); return entry; } @@ -465,7 +462,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G @Override public VALUE_TYPE REMOVE_VALUE(KEY_TYPE key) { - Entry KEY_VALUE_GENERIC_TYPE entry = findNode(key); + Node KEY_VALUE_GENERIC_TYPE entry = findNode(key); if(entry == null) return getDefaultReturnValue(); VALUE_TYPE value = entry.value; removeNode(entry); @@ -474,7 +471,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G @Override public VALUE_TYPE REMOVE_VALUEOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue) { - Entry KEY_VALUE_GENERIC_TYPE entry = findNode(key); + Node KEY_VALUE_GENERIC_TYPE entry = findNode(key); if(entry == null) return defaultValue; VALUE_TYPE value = entry.value; removeNode(entry); @@ -484,7 +481,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G #if TYPE_OBJECT && VALUE_OBJECT @Override public boolean remove(Object key, Object value) { - Entry KEY_VALUE_GENERIC_TYPE entry = findNode((CLASS_TYPE)key); + Node KEY_VALUE_GENERIC_TYPE entry = findNode((CLASS_TYPE)key); if(entry == null || !Objects.equals(value, entry.value)) return false; removeNode(entry); return true; @@ -493,7 +490,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G #else @Override public boolean remove(KEY_TYPE key, VALUE_TYPE value) { - Entry KEY_VALUE_GENERIC_TYPE entry = findNode(key); + Node KEY_VALUE_GENERIC_TYPE entry = findNode(key); if(entry == null || entry.value != value) return false; removeNode(entry); return true; @@ -502,7 +499,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G #endif @Override public boolean replace(KEY_TYPE key, VALUE_TYPE oldValue, VALUE_TYPE newValue) { - Entry KEY_VALUE_GENERIC_TYPE entry = findNode(key); + Node KEY_VALUE_GENERIC_TYPE entry = findNode(key); if(entry == null || entry.value != oldValue) return false; entry.value = newValue; return true; @@ -510,7 +507,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G @Override public VALUE_TYPE replace(KEY_TYPE key, VALUE_TYPE value) { - Entry KEY_VALUE_GENERIC_TYPE entry = findNode(key); + Node KEY_VALUE_GENERIC_TYPE entry = findNode(key); if(entry == null) return getDefaultReturnValue(); VALUE_TYPE oldValue = entry.value; entry.value = value; @@ -523,7 +520,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G #if TYPE_OBJECT validate(key); #endif - Entry KEY_VALUE_GENERIC_TYPE entry = findNode(key); + Node KEY_VALUE_GENERIC_TYPE entry = findNode(key); if(entry == null) { VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, getDefaultReturnValue()); if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; @@ -545,14 +542,14 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G #if TYPE_OBJECT validate(key); #endif - Entry KEY_VALUE_GENERIC_TYPE entry = findNode(key); + Node KEY_VALUE_GENERIC_TYPE entry = findNode(key); if(entry == null) { VALUE_TYPE newValue = mappingFunction.GET_VALUE(key); if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; put(key, newValue); return newValue; } - if(VALUE_EQUALS(entry.value, getDefaultReturnValue())) { + if(Objects.equals(entry.value, getDefaultReturnValue())) { VALUE_TYPE newValue = mappingFunction.GET_VALUE(key); if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; entry.value = newValue; @@ -566,7 +563,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G #if TYPE_OBJECT validate(key); #endif - Entry KEY_VALUE_GENERIC_TYPE entry = findNode(key); + Node KEY_VALUE_GENERIC_TYPE entry = findNode(key); if(entry == null) { VALUE_TYPE newValue = valueProvider.VALUE_GET_KEY(); if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; @@ -587,7 +584,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G #if TYPE_OBJECT validate(key); #endif - Entry KEY_VALUE_GENERIC_TYPE entry = findNode(key); + Node KEY_VALUE_GENERIC_TYPE entry = findNode(key); if(entry == null || VALUE_EQUALS(entry.value, getDefaultReturnValue())) return getDefaultReturnValue(); VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, entry.value); if(VALUE_EQUALS(newValue, getDefaultReturnValue())) { @@ -604,7 +601,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G #if TYPE_OBJECT validate(key); #endif - Entry KEY_VALUE_GENERIC_TYPE entry = findNode(key); + Node KEY_VALUE_GENERIC_TYPE entry = findNode(key); VALUE_TYPE newValue = entry == null || VALUE_EQUALS(entry.value, getDefaultReturnValue()) ? value : mappingFunction.APPLY_VALUE(entry.value, value); if(VALUE_EQUALS(newValue, getDefaultReturnValue())) { if(entry != null) @@ -620,7 +617,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G Objects.requireNonNull(mappingFunction); for(MAP.Entry KEY_VALUE_GENERIC_TYPE entry : MAPS.fastIterable(m)) { KEY_TYPE key = entry.ENTRY_KEY(); - Entry KEY_VALUE_GENERIC_TYPE subEntry = findNode(key); + Node KEY_VALUE_GENERIC_TYPE subEntry = findNode(key); VALUE_TYPE newValue = subEntry == null || VALUE_EQUALS(subEntry.value, getDefaultReturnValue()) ? entry.ENTRY_VALUE() : mappingFunction.APPLY_VALUE(subEntry.value, entry.ENTRY_VALUE()); if(VALUE_EQUALS(newValue, getDefaultReturnValue())) { if(subEntry != null) @@ -633,7 +630,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G @Override public void forEach(BI_CONSUMER KEY_VALUE_GENERIC_TYPE action) { - for(Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) + for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) action.accept(entry.key, entry.value); } @@ -648,25 +645,29 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G tree = null; } - LIST_ITERATOR KEY_GENERIC_TYPE keyIterator(boolean descending) { - LIST_ITERATOR KEY_GENERIC_TYPE iter = new KeyIterator(descending); - return descending ? ITERATORS.invert(iter) : iter; + protected BI_ITERATOR KEY_GENERIC_TYPE keyIterator() { + return new AscendingKeyIterator(first); } - LIST_ITERATOR KEY_GENERIC_TYPE keyIterator(KEY_TYPE key) { - return new KeyIterator(findNode(key)); + protected BI_ITERATOR KEY_GENERIC_TYPE keyIterator(KEY_TYPE element) { + return null; } + protected BI_ITERATOR KEY_GENERIC_TYPE descendingKeyIterator() { + return new DescendingKeyIterator(last); + } + + @Override public RB_TREE_MAP KEY_VALUE_GENERIC_TYPE copy() { RB_TREE_MAP KEY_VALUE_GENERIC_TYPE set = new RB_TREE_MAPKV_BRACES(); set.size = size; if(tree != null) { set.tree = tree.copy(); - Entry KEY_VALUE_GENERIC_TYPE lastFound = null; - for(Entry KEY_VALUE_GENERIC_TYPE entry = tree;entry != null;entry = entry.left) lastFound = entry; + Node KEY_VALUE_GENERIC_TYPE lastFound = null; + for(Node KEY_VALUE_GENERIC_TYPE entry = tree;entry != null;entry = entry.left) lastFound = entry; set.first = lastFound; lastFound = null; - for(Entry KEY_VALUE_GENERIC_TYPE entry = tree;entry != null;entry = entry.right) lastFound = entry; + for(Node KEY_VALUE_GENERIC_TYPE entry = tree;entry != null;entry = entry.right) lastFound = entry; set.last = lastFound; } return set; @@ -697,7 +698,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G @Override public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE descendingMap() { - return new DescendingSubMapKV_BRACES(this, true, EMPTY_KEY_VALUE, true, true, EMPTY_KEY_VALUE, true); + return new DescendingNaivgableSubMapKV_BRACES(this, true, EMPTY_KEY_VALUE, true, true, EMPTY_KEY_VALUE, true); } @Override @@ -707,69 +708,69 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G @Override public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE subMap(KEY_TYPE fromKey, boolean fromInclusive, KEY_TYPE toKey, boolean toInclusive) { - return new AscendingSubMapKV_BRACES(this, false, fromKey, fromInclusive, false, toKey, toInclusive); + return new AscendingNaivgableSubMapKV_BRACES(this, false, fromKey, fromInclusive, false, toKey, toInclusive); } @Override public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE headMap(KEY_TYPE toKey, boolean inclusive) { - return new AscendingSubMapKV_BRACES(this, true, EMPTY_KEY_VALUE, true, false, toKey, inclusive); + return new AscendingNaivgableSubMapKV_BRACES(this, true, EMPTY_KEY_VALUE, true, false, toKey, inclusive); } @Override public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE tailMap(KEY_TYPE fromKey, boolean inclusive) { - return new AscendingSubMapKV_BRACES(this, false, fromKey, inclusive, true, EMPTY_KEY_VALUE, true); + return new AscendingNaivgableSubMapKV_BRACES(this, false, fromKey, inclusive, true, EMPTY_KEY_VALUE, true); } @Override public KEY_TYPE lowerKey(KEY_TYPE e) { - Entry KEY_VALUE_GENERIC_TYPE node = findLowerNode(e); + Node KEY_VALUE_GENERIC_TYPE node = findLowerNode(e); return node != null ? node.key : getDefaultMinValue(); } @Override public KEY_TYPE floorKey(KEY_TYPE e) { - Entry KEY_VALUE_GENERIC_TYPE node = findFloorNode(e); + Node KEY_VALUE_GENERIC_TYPE node = findFloorNode(e); return node != null ? node.key : getDefaultMinValue(); } @Override public KEY_TYPE higherKey(KEY_TYPE e) { - Entry KEY_VALUE_GENERIC_TYPE node = findHigherNode(e); + Node KEY_VALUE_GENERIC_TYPE node = findHigherNode(e); return node != null ? node.key : getDefaultMaxValue(); } @Override public KEY_TYPE ceilingKey(KEY_TYPE e) { - Entry KEY_VALUE_GENERIC_TYPE node = findCeilingNode(e); + Node KEY_VALUE_GENERIC_TYPE node = findCeilingNode(e); return node != null ? node.key : getDefaultMaxValue(); } @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE lowerEntry(KEY_TYPE key) { - Entry KEY_VALUE_GENERIC_TYPE node = findLowerNode(key); - return node != null ? new BasicEntryKV_BRACES(node.key, node.value) : null; + Node KEY_VALUE_GENERIC_TYPE node = findLowerNode(key); + return node != null ? node.export() : null; } @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE higherEntry(KEY_TYPE key) { - Entry KEY_VALUE_GENERIC_TYPE node = findHigherNode(key); - return node != null ? new BasicEntryKV_BRACES(node.key, node.value) : null; + Node KEY_VALUE_GENERIC_TYPE node = findHigherNode(key); + return node != null ? node.export() : null; } @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE floorEntry(KEY_TYPE key) { - Entry KEY_VALUE_GENERIC_TYPE node = findFloorNode(key); - return node != null ? new BasicEntryKV_BRACES(node.key, node.value) : null; + Node KEY_VALUE_GENERIC_TYPE node = findFloorNode(key); + return node != null ? node.export() : null; } @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE ceilingEntry(KEY_TYPE key) { - Entry KEY_VALUE_GENERIC_TYPE node = findCeilingNode(key); - return node != null ? new BasicEntryKV_BRACES(node.key, node.value) : null; + Node KEY_VALUE_GENERIC_TYPE node = findCeilingNode(key); + return node != null ? node.export() : null; } - protected Entry KEY_VALUE_GENERIC_TYPE findLowerNode(KEY_TYPE key) { - Entry KEY_VALUE_GENERIC_TYPE entry = tree; + protected Node KEY_VALUE_GENERIC_TYPE findLowerNode(KEY_TYPE key) { + Node KEY_VALUE_GENERIC_TYPE entry = tree; while(entry != null) { if(compare(key, entry.key) > 0) { if(entry.right != null) entry = entry.right; @@ -778,7 +779,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G else { if(entry.left != null) entry = entry.left; else { - Entry KEY_VALUE_GENERIC_TYPE parent = entry.parent; + Node KEY_VALUE_GENERIC_TYPE parent = entry.parent; while(parent != null && parent.left == entry) { entry = parent; parent = parent.parent; @@ -790,8 +791,8 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G return null; } - protected Entry KEY_VALUE_GENERIC_TYPE findFloorNode(KEY_TYPE key) { - Entry KEY_VALUE_GENERIC_TYPE entry = tree; + protected Node KEY_VALUE_GENERIC_TYPE findFloorNode(KEY_TYPE key) { + Node KEY_VALUE_GENERIC_TYPE entry = tree; int compare; while(entry != null) { if((compare = compare(key, entry.key)) > 0) { @@ -802,7 +803,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G else if(compare < 0) { if(entry.left != null) entry = entry.left; else { - Entry KEY_VALUE_GENERIC_TYPE parent = entry.parent; + Node KEY_VALUE_GENERIC_TYPE parent = entry.parent; while(parent != null && parent.left == entry) { entry = parent; parent = parent.parent; @@ -816,8 +817,8 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G return entry; } - protected Entry KEY_VALUE_GENERIC_TYPE findCeilingNode(KEY_TYPE key) { - Entry KEY_VALUE_GENERIC_TYPE entry = tree; + protected Node KEY_VALUE_GENERIC_TYPE findCeilingNode(KEY_TYPE key) { + Node KEY_VALUE_GENERIC_TYPE entry = tree; int compare; while(entry != null) { if((compare = compare(key, entry.key)) < 0) { @@ -828,7 +829,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G else if(compare > 0) { if(entry.right != null) entry = entry.right; else { - Entry KEY_VALUE_GENERIC_TYPE parent = entry.parent; + Node KEY_VALUE_GENERIC_TYPE parent = entry.parent; while(parent != null && parent.right == entry) { entry = parent; parent = parent.parent; @@ -842,8 +843,8 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G return entry; } - protected Entry KEY_VALUE_GENERIC_TYPE findHigherNode(KEY_TYPE key) { - Entry KEY_VALUE_GENERIC_TYPE entry = tree; + protected Node KEY_VALUE_GENERIC_TYPE findHigherNode(KEY_TYPE key) { + Node KEY_VALUE_GENERIC_TYPE entry = tree; while(entry != null) { if(compare(key, entry.key) < 0) { if(entry.left != null) entry = entry.left; @@ -852,7 +853,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G else { if(entry.right != null) entry = entry.right; else { - Entry KEY_VALUE_GENERIC_TYPE parent = entry.parent; + Node KEY_VALUE_GENERIC_TYPE parent = entry.parent; while(parent != null && parent.right == entry) { entry = parent; parent = parent.parent; @@ -864,8 +865,8 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G return null; } - protected Entry KEY_VALUE_GENERIC_TYPE findNode(KEY_TYPE key) { - Entry KEY_VALUE_GENERIC_TYPE node = tree; + protected Node KEY_VALUE_GENERIC_TYPE findNode(KEY_TYPE key) { + Node KEY_VALUE_GENERIC_TYPE node = tree; int compare; while(node != null) { if((compare = compare(key, node.key)) == 0) return node; @@ -875,15 +876,15 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G return null; } - protected void removeNode(Entry KEY_VALUE_GENERIC_TYPE entry) { + protected void removeNode(Node KEY_VALUE_GENERIC_TYPE entry) { size--; if(entry.needsSuccessor()) { - Entry KEY_VALUE_GENERIC_TYPE successor = entry.next(); + Node KEY_VALUE_GENERIC_TYPE successor = entry.next(); entry.key = successor.key; entry.value = successor.value; entry = successor; } - Entry KEY_VALUE_GENERIC_TYPE replacement = entry.left != null ? entry.left : entry.right; + Node KEY_VALUE_GENERIC_TYPE replacement = entry.left != null ? entry.left : entry.right; if(replacement != null) { if(entry.replace(replacement)) tree = replacement; if(entry == first) first = replacement; @@ -897,7 +898,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G fixAfterDeletion(entry); entry.replace(null); if(entry.parent != null) { - Entry KEY_VALUE_GENERIC_TYPE parent = entry.parent; + Node KEY_VALUE_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; } @@ -907,15 +908,15 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G protected void validate(KEY_TYPE k) { compare(k, k); } protected int compare(KEY_TYPE k, KEY_TYPE v) { return comparator != null ? comparator.compare(k, v) : COMPAREABLE_TO_KEY(k, v);} - protected static GENERIC_KEY_VALUE_BRACES boolean isBlack(Entry KEY_VALUE_GENERIC_TYPE p) { return p == null || p.isBlack(); } - protected static GENERIC_KEY_VALUE_BRACES Entry KEY_VALUE_GENERIC_TYPE parentOf(Entry KEY_VALUE_GENERIC_TYPE p) { return (p == null ? null : p.parent); } - protected static GENERIC_KEY_VALUE_BRACES void setBlack(Entry KEY_VALUE_GENERIC_TYPE p, boolean c) { if(p != null) p.setBlack(c); } - protected static GENERIC_KEY_VALUE_BRACES Entry KEY_VALUE_GENERIC_TYPE leftOf(Entry KEY_VALUE_GENERIC_TYPE p) { return p == null ? null : p.left; } - protected static GENERIC_KEY_VALUE_BRACES Entry KEY_VALUE_GENERIC_TYPE rightOf(Entry KEY_VALUE_GENERIC_TYPE p) { return (p == null) ? null : p.right; } + protected static GENERIC_KEY_VALUE_BRACES boolean isBlack(Node KEY_VALUE_GENERIC_TYPE p) { return p == null || p.isBlack(); } + protected static GENERIC_KEY_VALUE_BRACES Node KEY_VALUE_GENERIC_TYPE parentOf(Node KEY_VALUE_GENERIC_TYPE p) { return (p == null ? null : p.parent); } + protected static GENERIC_KEY_VALUE_BRACES void setBlack(Node KEY_VALUE_GENERIC_TYPE p, boolean c) { if(p != null) p.setBlack(c); } + protected static GENERIC_KEY_VALUE_BRACES Node KEY_VALUE_GENERIC_TYPE leftOf(Node KEY_VALUE_GENERIC_TYPE p) { return p == null ? null : p.left; } + protected static GENERIC_KEY_VALUE_BRACES Node KEY_VALUE_GENERIC_TYPE rightOf(Node KEY_VALUE_GENERIC_TYPE p) { return (p == null) ? null : p.right; } - protected void rotateLeft(Entry KEY_VALUE_GENERIC_TYPE entry) { + protected void rotateLeft(Node KEY_VALUE_GENERIC_TYPE entry) { if(entry != null) { - Entry KEY_VALUE_GENERIC_TYPE right = entry.right; + Node KEY_VALUE_GENERIC_TYPE right = entry.right; entry.right = right.left; if(right.left != null) right.left.parent = entry; right.parent = entry.parent; @@ -927,9 +928,9 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G } } - protected void rotateRight(Entry KEY_VALUE_GENERIC_TYPE entry) { + protected void rotateRight(Node KEY_VALUE_GENERIC_TYPE entry) { if(entry != null) { - Entry KEY_VALUE_GENERIC_TYPE left = entry.left; + Node KEY_VALUE_GENERIC_TYPE left = entry.left; entry.left = left.right; if(left.right != null) left.right.parent = entry; left.parent = entry.parent; @@ -941,11 +942,11 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G } } - protected void fixAfterInsertion(Entry KEY_VALUE_GENERIC_TYPE entry) { + protected void fixAfterInsertion(Node KEY_VALUE_GENERIC_TYPE entry) { entry.setBlack(false); while(entry != null && entry != tree && !entry.parent.isBlack()) { if(parentOf(entry) == leftOf(parentOf(parentOf(entry)))) { - Entry KEY_VALUE_GENERIC_TYPE y = rightOf(parentOf(parentOf(entry))); + Node KEY_VALUE_GENERIC_TYPE y = rightOf(parentOf(parentOf(entry))); if(!isBlack(y)) { setBlack(parentOf(entry), true); setBlack(y, true); @@ -963,7 +964,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G } } else { - Entry KEY_VALUE_GENERIC_TYPE y = leftOf(parentOf(parentOf(entry))); + Node KEY_VALUE_GENERIC_TYPE y = leftOf(parentOf(parentOf(entry))); if(!isBlack(y)) { setBlack(parentOf(entry), true); setBlack(y, true); @@ -984,10 +985,10 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G tree.setBlack(true); } - protected void fixAfterDeletion(Entry KEY_VALUE_GENERIC_TYPE entry) { + protected void fixAfterDeletion(Node KEY_VALUE_GENERIC_TYPE entry) { while(entry != tree && isBlack(entry)) { if(entry == leftOf(parentOf(entry))) { - Entry KEY_VALUE_GENERIC_TYPE sib = rightOf(parentOf(entry)); + Node KEY_VALUE_GENERIC_TYPE sib = rightOf(parentOf(entry)); if(!isBlack(sib)) { setBlack(sib, true); setBlack(parentOf(entry), false); @@ -1013,7 +1014,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G } } else { - Entry KEY_VALUE_GENERIC_TYPE sib = leftOf(parentOf(entry)); + Node KEY_VALUE_GENERIC_TYPE sib = leftOf(parentOf(entry)); if(!isBlack(sib)) { setBlack(sib, true); setBlack(parentOf(entry), false); @@ -1042,254 +1043,539 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G setBlack(entry, true); } - static final class AscendingSubMap KEY_VALUE_GENERIC_TYPE extends NavigableSubMap KEY_VALUE_GENERIC_TYPE { + static class KeySet KEY_VALUE_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE implements NAVIGABLE_SET KEY_GENERIC_TYPE + { + NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE map; - public AscendingSubMap(RB_TREE_MAP KEY_VALUE_GENERIC_TYPE m, boolean fromStart, KEY_TYPE low, boolean loInclusive, boolean toEnd, KEY_TYPE high, boolean hiInclusive) { - super(m, fromStart, low, loInclusive, toEnd, high, hiInclusive); + public KeySet(NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE map) { + this.map = map; } +#if !TYPE_OBJECT @Override - LIST_ITERATOR KEY_GENERIC_TYPE keyIterator(boolean descending) { return new SubMapKeyIterator(descending); } + public void setDefaultMaxValue(KEY_TYPE e) { map.setDefaultMaxValue(e); } @Override - LIST_ITERATOR KEY_GENERIC_TYPE keyIterator(KEY_TYPE key) { - RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = m.findNode(key); - return entry == null || !inClosedRange(key) ? null : new SubMapKeyIterator(entry, fromStart ? null : findLowest(), toEnd ? null : findHighest()); - } + public KEY_TYPE getDefaultMaxValue() { return map.getDefaultMaxValue(); } @Override - VALUE_LIST_ITERATOR VALUE_GENERIC_TYPE valueIterator() { return new SubMapValueIterator(false); } + public void setDefaultMinValue(KEY_TYPE e) { map.setDefaultMinValue(e); } @Override - ObjectListIterator entryIterator() { return new SubMapEntrySetIterator(false); } - + public KEY_TYPE getDefaultMinValue() { return map.getDefaultMinValue(); } +#endif @Override - public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE descendingMap() { - if(descendingMap == null) descendingMap = new DescendingSubMapKV_BRACES(m, fromStart, low, loInclusive, toEnd, high, hiInclusive); - return descendingMap; - } - - RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE next(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry) { - return entry.next(); - } - - RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE previous(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry) { - return entry.previous(); - } - + public KEY_TYPE lower(KEY_TYPE e) { return map.lowerKey(e); } @Override - public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE subMap(KEY_TYPE fromKey, boolean fromInclusive, KEY_TYPE toKey, boolean toInclusive) { - if (!inRange(fromKey, fromInclusive)) throw new IllegalArgumentException("fromKey out of range"); - if (!inRange(toKey, toInclusive)) throw new IllegalArgumentException("toKey out of range"); - return new AscendingSubMapKV_BRACES(m, false, fromKey, fromInclusive, false, toKey, toInclusive); - } - + public KEY_TYPE floor(KEY_TYPE e) { return map.floorKey(e); } @Override - public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE headMap(KEY_TYPE toKey, boolean inclusive) { - if (!inRange(toKey, inclusive)) throw new IllegalArgumentException("toKey out of range"); - return new AscendingSubMapKV_BRACES(m, fromStart, low, loInclusive, false, toKey, inclusive); - } - + public KEY_TYPE ceiling(KEY_TYPE e) { return map.ceilingKey(e); } @Override - public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE tailMap(KEY_TYPE fromKey, boolean inclusive) { - if (!inRange(fromKey, inclusive)) throw new IllegalArgumentException("fromKey out of range"); - return new AscendingSubMapKV_BRACES(m, false, fromKey, inclusive, toEnd, high, hiInclusive); - } - } - - static final class DescendingSubMap KEY_VALUE_GENERIC_TYPE extends NavigableSubMap KEY_VALUE_GENERIC_TYPE { - - public DescendingSubMap(RB_TREE_MAP KEY_VALUE_GENERIC_TYPE m, boolean fromStart, KEY_TYPE low, boolean loInclusive, boolean toEnd, KEY_TYPE high, boolean hiInclusive) { - super(m, fromStart, low, loInclusive, toEnd, high, hiInclusive); - } - + public KEY_TYPE higher(KEY_TYPE e) { return map.higherKey(e); } @Override - public KEY_TYPE FIRST_ENTRY_KEY() { return super.LAST_ENTRY_KEY(); } - + public KEY_TYPE POLL_FIRST_KEY() { return map.POLL_FIRST_ENTRY_KEY(); } @Override - public KEY_TYPE POLL_FIRST_ENTRY_KEY() { return super.POLL_LAST_ENTRY_KEY(); } - + public KEY_TYPE POLL_LAST_KEY() { return map.POLL_LAST_ENTRY_KEY(); } @Override - public KEY_TYPE LAST_ENTRY_KEY() { return super.FIRST_ENTRY_KEY(); } - + public COMPARATOR KEY_GENERIC_TYPE comparator() { return map.comparator(); } @Override - public KEY_TYPE POLL_LAST_ENTRY_KEY() { return super.POLL_FIRST_ENTRY_KEY(); } - + public KEY_TYPE FIRST_KEY() { return map.FIRST_ENTRY_KEY(); } @Override - public VALUE_TYPE FIRST_ENTRY_VALUE() { return super.LAST_ENTRY_VALUE(); } - + public KEY_TYPE LAST_KEY() { return map.LAST_ENTRY_KEY(); } @Override - public VALUE_TYPE LAST_ENTRY_VALUE() { return super.FIRST_ENTRY_VALUE(); } - - @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE firstEntry() { return super.lastEntry(); } - - @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE lastEntry() { return super.firstEntry(); } - - @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE pollFirstEntry() { return super.pollLastEntry(); } - - @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE pollLastEntry() { return super.pollFirstEntry(); } - - @Override - public KEY_TYPE lowerKey(KEY_TYPE e) { return super.higherKey(e); } - - @Override - public KEY_TYPE floorKey(KEY_TYPE e) { return super.ceilingKey(e); } - - @Override - public KEY_TYPE ceilingKey(KEY_TYPE e) { return super.floorKey(e); } - - @Override - public KEY_TYPE higherKey(KEY_TYPE e) { return super.lowerKey(e); } - - @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE lowerEntry(KEY_TYPE e) { return super.higherEntry(e); } - - @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE floorEntry(KEY_TYPE e) { return super.ceilingEntry(e); } - - @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE ceilingEntry(KEY_TYPE e) { return super.floorEntry(e); } - - @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE higherEntry(KEY_TYPE e) { return super.lowerEntry(e); } - - protected RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE findLowest() { - if(fromStart) return m.first; - RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = loInclusive ? m.findCeilingNode(low) : m.findHigherNode(low); - return entry == null || tooHigh(entry.key) ? null : entry; - } - - protected RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE findHighest() { - if(toEnd) return m.last; - RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = hiInclusive ? m.findFloorNode(high) : m.findLowerNode(high); - return entry == null || tooLow(entry.key) ? null : entry; - } - - @Override - LIST_ITERATOR KEY_GENERIC_TYPE keyIterator(boolean descending) { - LIST_ITERATOR KEY_GENERIC_TYPE iter = new SubMapKeyIterator(!descending); - return descending ? iter : ITERATORS.invert(iter); - } - - @Override - LIST_ITERATOR KEY_GENERIC_TYPE keyIterator(KEY_TYPE key) { - RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = m.findNode(key); - return entry == null || !inClosedRange(key) ? null : ITERATORS.invert(new SubMapKeyIterator(entry, fromStart ? null : findLowest(), toEnd ? null : findHighest())); - } - - RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE next(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry) { - return entry.previous(); - } - - RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE previous(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry) { - return entry.next(); - } - - @Override - public COMPARATOR KEY_GENERIC_TYPE comparator() { return m.comparator() == null ? null : m.comparator().reversed(); } - - @Override - VALUE_LIST_ITERATOR VALUE_GENERIC_TYPE valueIterator() { return VALUE_ITERATORS.invert(new SubMapValueIterator(true)); } - - @Override - ObjectListIterator entryIterator() { return ObjectIterators.invert(new SubMapEntrySetIterator(true)); } - - @Override - public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE descendingMap() { - if(descendingMap == null) descendingMap = new AscendingSubMapKV_BRACES(m, fromStart, low, loInclusive, toEnd, high, hiInclusive); - return descendingMap; - } - - @Override - public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE subMap(KEY_TYPE fromKey, boolean fromInclusive, KEY_TYPE toKey, boolean toInclusive) { - if (!inRange(fromKey, fromInclusive)) throw new IllegalArgumentException("fromKey out of range"); - if (!inRange(toKey, toInclusive)) throw new IllegalArgumentException("toKey out of range"); - return new DescendingSubMapKV_BRACES(m, false, fromKey, fromInclusive, false, toKey, toInclusive); - } - - @Override - public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE headMap(KEY_TYPE toKey, boolean inclusive) { - if (!inRange(toKey, inclusive)) throw new IllegalArgumentException("toKey out of range"); - return new DescendingSubMapKV_BRACES(m, fromStart, low, loInclusive, false, toKey, inclusive); - } - - @Override - public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE tailMap(KEY_TYPE fromKey, boolean inclusive) { - if (!inRange(fromKey, inclusive)) throw new IllegalArgumentException("fromKey out of range"); - return new DescendingSubMapKV_BRACES(m, false, fromKey, inclusive, toEnd, high, hiInclusive); - } - } - - static abstract class NavigableSubMap KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE implements NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE { - final RB_TREE_MAP KEY_VALUE_GENERIC_TYPE m; - final KEY_TYPE low; - final KEY_TYPE high; - final boolean fromStart; - final boolean toEnd; - final boolean loInclusive; - final boolean hiInclusive; - transient NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE descendingMap; - transient SubMapEntrySet entrySet; - transient KeySet KEY_VALUE_GENERIC_TYPE keySet; - transient SubMapValues values; - - NavigableSubMap(RB_TREE_MAP KEY_VALUE_GENERIC_TYPE m, boolean fromStart, KEY_TYPE low, boolean loInclusive, boolean toEnd, KEY_TYPE high, boolean hiInclusive) { - this.m = m; - this.low = low; - this.high = high; - this.fromStart = fromStart; - this.toEnd = toEnd; - this.loInclusive = loInclusive; - this.hiInclusive = hiInclusive; - } - - abstract LIST_ITERATOR KEY_GENERIC_TYPE keyIterator(boolean descending); - abstract LIST_ITERATOR KEY_GENERIC_TYPE keyIterator(KEY_TYPE key); - abstract VALUE_LIST_ITERATOR VALUE_GENERIC_TYPE valueIterator(); - abstract ObjectListIterator entryIterator(); - abstract RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE next(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry); - abstract RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE previous(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry); - - @Override - public NavigableSubMap KEY_VALUE_GENERIC_TYPE copy() { throw new UnsupportedOperationException(); } - @Override - public abstract NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE descendingMap(); - @Override - public abstract NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE subMap(KEY_TYPE fromKey, boolean fromInclusive, KEY_TYPE toKey, boolean toInclusive); - @Override - public abstract NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE headMap(KEY_TYPE toKey, boolean inclusive); - @Override - public abstract NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE tailMap(KEY_TYPE fromKey, boolean inclusive); - - boolean tooLow(KEY_TYPE key) { return !fromStart && (loInclusive ? m.compare(key, low) < 0 : m.compare(key, low) <= 0); } - boolean tooHigh(KEY_TYPE key) { return !toEnd && (hiInclusive ? m.compare(key, high) > 0 : m.compare(key, high) >= 0); } - boolean inRange(KEY_TYPE key) { return !tooLow(key) && !tooHigh(key); } - boolean inClosedRange(KEY_TYPE key) { return (fromStart || m.compare(key, low) >= 0) && (toEnd || m.compare(high, key) >= 0); } - boolean inRange(KEY_TYPE key, boolean inclusive) { return inclusive ? inRange(key) : inClosedRange(key); } + public void clear() { map.clear(); } #if TYPE_OBJECT - public KEY_TYPE getDefaultMaxValue() { return m.getDefaultMaxValue(); } - public KEY_TYPE getDefaultMinValue() { return m.getDefaultMinValue(); } + @Override + public boolean remove(Object o) { + int oldSize = map.size(); + map.remove(o); + return oldSize != map.size(); + } + #else @Override - public void setDefaultMaxValue(KEY_TYPE e) { m.setDefaultMaxValue(e); } + public boolean remove(KEY_TYPE o) { + int oldSize = map.size(); + map.remove(o); + return oldSize != map.size(); + } + +#endif @Override - public KEY_TYPE getDefaultMaxValue() { return m.getDefaultMaxValue(); } + public boolean add(KEY_TYPE e) { throw new UnsupportedOperationException(); } @Override - public void setDefaultMinValue(KEY_TYPE e) { m.setDefaultMinValue(e); } + public boolean addAndMoveToFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); } @Override - public KEY_TYPE getDefaultMinValue() { return m.getDefaultMinValue(); } + 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 BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement) { + if(map instanceof RB_TREE_MAP) return ((RB_TREE_MAP KEY_VALUE_GENERIC_TYPE)map).keyIterator(fromElement); + return ((NavigableSubMap KEY_VALUE_GENERIC_TYPE)map).keyIterator(fromElement); + } + + @Override + public NAVIGABLE_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, boolean fromInclusive, KEY_TYPE toElement, boolean toInclusive) { return new KeySetKV_BRACES(map.subMap(fromElement, fromInclusive, toElement, toInclusive)); } + @Override + public NAVIGABLE_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement, boolean inclusive) { return new KeySetKV_BRACES(map.headMap(toElement, inclusive)); } + @Override + public NAVIGABLE_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement, boolean inclusive) { return new KeySetKV_BRACES(map.tailMap(fromElement, inclusive)); } + + @Override + public BI_ITERATOR KEY_GENERIC_TYPE iterator() { + if(map instanceof RB_TREE_MAP) return ((RB_TREE_MAP KEY_VALUE_GENERIC_TYPE)map).keyIterator(); + return ((NavigableSubMap KEY_VALUE_GENERIC_TYPE)map).keyIterator(); + } + + @Override + public BI_ITERATOR KEY_GENERIC_TYPE descendingIterator() { + if(map instanceof RB_TREE_MAP) return ((RB_TREE_MAP KEY_VALUE_GENERIC_TYPE)map).descendingKeyIterator(); + return ((NavigableSubMap KEY_VALUE_GENERIC_TYPE)map).descendingKeyIterator(); + } + + protected Node KEY_VALUE_GENERIC_TYPE start() { + if(map instanceof RB_TREE_MAP) return ((RB_TREE_MAP KEY_VALUE_GENERIC_TYPE)map).first; + return ((NavigableSubMap KEY_VALUE_GENERIC_TYPE)map).subLowest(); + } + + protected Node KEY_VALUE_GENERIC_TYPE end() { + if(map instanceof RB_TREE_MAP) return null; + return ((NavigableSubMap KEY_VALUE_GENERIC_TYPE)map).subHighest(); + } + + protected Node KEY_VALUE_GENERIC_TYPE next(Node KEY_VALUE_GENERIC_TYPE entry) { + if(map instanceof RB_TREE_MAP) return entry.next(); + return ((NavigableSubMap KEY_VALUE_GENERIC_TYPE)map).next(entry); + } + + protected Node KEY_VALUE_GENERIC_TYPE previous(Node KEY_VALUE_GENERIC_TYPE entry) { + if(map instanceof RB_TREE_MAP) return entry.previous(); + return ((NavigableSubMap KEY_VALUE_GENERIC_TYPE)map).previous(entry); + } + + @Override + public NAVIGABLE_SET KEY_GENERIC_TYPE descendingSet() { return new KeySetKV_BRACES(map.descendingMap()); } + @Override + public KeySet KEY_VALUE_GENERIC_TYPE copy() { throw new UnsupportedOperationException(); } + @Override + public boolean isEmpty() { return map.isEmpty(); } + @Override + public int size() { return map.size(); } + + @Override + public void forEach(CONSUMER KEY_SUPER_GENERIC_TYPE action) { + Objects.requireNonNull(action); + for(Node KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) + action.accept(entry.key); + } + + @Override + public void forEach(E input, BI_FROM_OBJECT_CONSUMER KSK_GENERIC_TYPE action) { + Objects.requireNonNull(action); + for(Node KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) + action.accept(input, entry.key); + } + + @Override + public boolean matchesAny(PREDICATE KEY_GENERIC_TYPE filter) { + Objects.requireNonNull(filter); + for(Node KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) + if(filter.TEST_VALUE(entry.key)) return true; + return false; + } + + @Override + public boolean matchesNone(PREDICATE KEY_GENERIC_TYPE filter) { + Objects.requireNonNull(filter); + for(Node KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) + if(filter.TEST_VALUE(entry.key)) return false; + return true; + } + + @Override + public boolean matchesAll(PREDICATE KEY_GENERIC_TYPE filter) { + Objects.requireNonNull(filter); + for(Node KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) + if(!filter.TEST_VALUE(entry.key)) return false; + return true; + } + +#if !TYPE_OBJECT + @Override + public KEY_TYPE reduce(KEY_TYPE identity, SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + KEY_TYPE state = identity; + for(Node KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) + state = operator.APPLY_KEY_VALUE(state, entry.key); + return state; + } + +#else + @Override + public KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction operator) { + Objects.requireNonNull(operator); + KEY_SPECIAL_TYPE state = identity; + for(Node KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) + state = operator.apply(state, entry.key); + return state; + } #endif + @Override + public KEY_TYPE reduce(SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + KEY_TYPE state = EMPTY_KEY_VALUE; + boolean empty = true; + for(Node KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) { + if(empty) { + empty = false; + state = entry.key; + continue; + } + state = operator.APPLY_KEY_VALUE(state, entry.key); + } + return state; + } + + @Override + public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { + Objects.requireNonNull(filter); + for(Node KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) + if(filter.TEST_VALUE(entry.key)) return entry.key; + return EMPTY_KEY_VALUE; + } + + @Override + public int count(PREDICATE KEY_GENERIC_TYPE filter) { + Objects.requireNonNull(filter); + int result = 0; + for(Node KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) + if(filter.TEST_VALUE(entry.key)) result++; + return result; + } + } + + static class AscendingNaivgableSubMap KEY_VALUE_GENERIC_TYPE extends NavigableSubMap KEY_VALUE_GENERIC_TYPE + { + AscendingNaivgableSubMap(RB_TREE_MAP KEY_VALUE_GENERIC_TYPE map, boolean fromStart, KEY_TYPE lo, boolean loInclusive, boolean toEnd, KEY_TYPE hi, boolean hiInclusive) { + super(map, fromStart, lo, loInclusive, toEnd, hi, hiInclusive); + } + + @Override + public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE descendingMap() { + if(inverse == null) inverse = new DescendingNaivgableSubMapKV_BRACES(map, fromStart, lo, loInclusive, toEnd, hi, hiInclusive); + return inverse; + } + + @Override + public ObjectSet ENTRY_SET() { + if(entrySet == null) entrySet = new AscendingSubEntrySet(); + return entrySet; + } + + @Override + public NAVIGABLE_SET KEY_GENERIC_TYPE navigableKeySet() { + if(keySet == null) keySet = new KeySetKV_BRACES(this); + return keySet; + } + + @Override + public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE subMap(KEY_TYPE fromKey, boolean fromInclusive, KEY_TYPE toKey, boolean toInclusive) { + if (!inRange(fromKey, fromInclusive)) throw new IllegalArgumentException("fromKey out of range"); + if (!inRange(toKey, toInclusive)) throw new IllegalArgumentException("toKey out of range"); + return new AscendingNaivgableSubMapKV_BRACES(map, false, fromKey, fromInclusive, false, toKey, toInclusive); + } + + @Override + public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE headMap(KEY_TYPE toKey, boolean inclusive) { + if (!inRange(toKey, inclusive)) throw new IllegalArgumentException("toKey out of range"); + return new AscendingNaivgableSubMapKV_BRACES(map, fromStart, lo, loInclusive, false, toKey, inclusive); + } + + @Override + public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE tailMap(KEY_TYPE fromKey, boolean inclusive) { + if (!inRange(fromKey, inclusive)) throw new IllegalArgumentException("fromKey out of range"); + return new AscendingNaivgableSubMapKV_BRACES(map, false, fromKey, inclusive, toEnd, hi, hiInclusive); + } + + @Override + protected Node KEY_VALUE_GENERIC_TYPE subLowest() { return absLowest(); } + @Override + protected Node KEY_VALUE_GENERIC_TYPE subHighest() { return absHighest(); } + @Override + protected Node KEY_VALUE_GENERIC_TYPE subCeiling(KEY_TYPE key) { return absCeiling(key); } + @Override + protected Node KEY_VALUE_GENERIC_TYPE subHigher(KEY_TYPE key) { return absHigher(key); } + @Override + protected Node KEY_VALUE_GENERIC_TYPE subFloor(KEY_TYPE key) { return absFloor(key); } + @Override + protected Node KEY_VALUE_GENERIC_TYPE subLower(KEY_TYPE key) { return absLower(key); } + + @Override + protected BI_ITERATOR KEY_GENERIC_TYPE keyIterator() { + return new AcsendingSubKeyIterator(absLowest(), absHighFence(), absLowFence()); + } + @Override + protected BI_ITERATOR KEY_GENERIC_TYPE keyIterator(KEY_TYPE element) { + return new AcsendingSubKeyIterator(absLower(element), absHighFence(), absLowFence()); + } + + @Override + protected VALUE_BI_ITERATOR VALUE_GENERIC_TYPE valueIterator() { + return new AcsendingSubValueIterator(absLowest(), absHighFence(), absLowFence()); + } + + @Override + protected BI_ITERATOR KEY_GENERIC_TYPE descendingKeyIterator() { + return new DecsendingSubKeyIterator(absHighest(), absLowFence(), absHighFence()); + } + + class AscendingSubEntrySet extends SubEntrySet { + @Override + public ObjectIterator iterator() { + return new AcsendingSubEntryIterator(absLowest(), absHighFence(), absLowFence()); + } + } + } + + static class DescendingNaivgableSubMap KEY_VALUE_GENERIC_TYPE extends NavigableSubMap KEY_VALUE_GENERIC_TYPE + { + COMPARATOR KEY_GENERIC_TYPE comparator; + DescendingNaivgableSubMap(RB_TREE_MAP KEY_VALUE_GENERIC_TYPE map, boolean fromStart, KEY_TYPE lo, boolean loInclusive, boolean toEnd, KEY_TYPE hi, boolean hiInclusive) { + super(map, fromStart, lo, loInclusive, toEnd, hi, hiInclusive); +#if TYPE_OBJECT + comparator = Collections.reverseOrder(map.comparator()); +#else + comparator = map.comparator() == null ? COMPARATOR.of(Collections.reverseOrder()) : map.comparator().reversed(); +#endif + } + + @Override + public COMPARATOR KEY_GENERIC_TYPE comparator() { return comparator; } + + @Override + public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE descendingMap() { + if(inverse == null) inverse = new AscendingNaivgableSubMapKV_BRACES(map, fromStart, lo, loInclusive, toEnd, hi, hiInclusive); + return inverse; + } + + @Override + public NAVIGABLE_SET KEY_GENERIC_TYPE navigableKeySet() { + if(keySet == null) keySet = new KeySetKV_BRACES(this); + return keySet; + } + + @Override + public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE subMap(KEY_TYPE fromKey, boolean fromInclusive, KEY_TYPE toKey, boolean toInclusive) { + if (!inRange(fromKey, fromInclusive)) throw new IllegalArgumentException("fromKey out of range"); + if (!inRange(toKey, toInclusive)) throw new IllegalArgumentException("toKey out of range"); + return new DescendingNaivgableSubMapKV_BRACES(map, false, toKey, toInclusive, false, fromKey, fromInclusive); + } + + @Override + public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE headMap(KEY_TYPE toKey, boolean inclusive) { + if (!inRange(toKey, inclusive)) throw new IllegalArgumentException("toKey out of range"); + return new DescendingNaivgableSubMapKV_BRACES(map, false, toKey, inclusive, toEnd, hi, hiInclusive); + } + + @Override + public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE tailMap(KEY_TYPE fromKey, boolean inclusive) { + if (!inRange(fromKey, inclusive)) throw new IllegalArgumentException("fromKey out of range"); + return new DescendingNaivgableSubMapKV_BRACES(map, fromStart, lo, loInclusive, false, fromKey, inclusive); + } + + @Override + public ObjectSet ENTRY_SET() { + if(entrySet == null) entrySet = new DescendingSubEntrySet(); + return entrySet; + } + + @Override + protected Node KEY_VALUE_GENERIC_TYPE subLowest() { return absHighest(); } + @Override + protected Node KEY_VALUE_GENERIC_TYPE subHighest() { return absLowest(); } + @Override + protected Node KEY_VALUE_GENERIC_TYPE subCeiling(KEY_TYPE key) { return absFloor(key); } + @Override + protected Node KEY_VALUE_GENERIC_TYPE subHigher(KEY_TYPE key) { return absLower(key); } + @Override + protected Node KEY_VALUE_GENERIC_TYPE subFloor(KEY_TYPE key) { return absCeiling(key); } + @Override + protected Node KEY_VALUE_GENERIC_TYPE subLower(KEY_TYPE key) { return absHigher(key); } + @Override + protected Node KEY_VALUE_GENERIC_TYPE next(Node KEY_VALUE_GENERIC_TYPE entry) { return entry.previous(); } + @Override + protected Node KEY_VALUE_GENERIC_TYPE previous(Node KEY_VALUE_GENERIC_TYPE entry) { return entry.next(); } + + @Override + protected BI_ITERATOR KEY_GENERIC_TYPE keyIterator() { + return new DecsendingSubKeyIterator(absHighest(), absLowFence(), absHighFence()); + } + + @Override + protected BI_ITERATOR KEY_GENERIC_TYPE keyIterator(KEY_TYPE element) { + return new DecsendingSubKeyIterator(absHigher(element), absLowFence(), absHighFence()); + } + + @Override + protected VALUE_BI_ITERATOR VALUE_GENERIC_TYPE valueIterator() { + return new DecsendingSubValueIterator(absHighest(), absLowFence(), absHighFence()); + } + + @Override + protected BI_ITERATOR KEY_GENERIC_TYPE descendingKeyIterator() { + return new AcsendingSubKeyIterator(absLowest(), absHighFence(), absLowFence()); + } + + class DescendingSubEntrySet extends SubEntrySet { + @Override + public ObjectIterator iterator() { + return new DecsendingSubEntryIterator(absHighest(), absLowFence(), absHighFence()); + } + } + } + + static abstract class NavigableSubMap KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE implements NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE + { + final RB_TREE_MAP KEY_VALUE_GENERIC_TYPE map; + final KEY_TYPE lo, hi; + final boolean fromStart, toEnd; + final boolean loInclusive, hiInclusive; + + NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE inverse; + NAVIGABLE_SET KEY_GENERIC_TYPE keySet; + ObjectSet entrySet; + VALUE_COLLECTION VALUE_GENERIC_TYPE values; + + NavigableSubMap(RB_TREE_MAP KEY_VALUE_GENERIC_TYPE map, boolean fromStart, KEY_TYPE lo, boolean loInclusive, boolean toEnd, KEY_TYPE hi, boolean hiInclusive) { + if (!fromStart && !toEnd) { + if (map.compare(lo, hi) > 0) throw new IllegalArgumentException("fromKey > toKey"); + } + else { + if (!fromStart) map.validate(lo); + if (!toEnd) map.validate(hi); + } + this.map = map; + this.fromStart = fromStart; + this.lo = lo; + this.loInclusive = loInclusive; + this.toEnd = toEnd; + this.hi = hi; + this.hiInclusive = hiInclusive; + } + +#if TYPE_OBJECT + public KEY_TYPE getDefaultMaxValue() { return map.getDefaultMaxValue(); } + public KEY_TYPE getDefaultMinValue() { return map.getDefaultMinValue(); } +#else + @Override + public void setDefaultMaxValue(KEY_TYPE e) { map.setDefaultMaxValue(e); } + @Override + public KEY_TYPE getDefaultMaxValue() { return map.getDefaultMaxValue(); } + @Override + public void setDefaultMinValue(KEY_TYPE e) { map.setDefaultMinValue(e); } + @Override + public KEY_TYPE getDefaultMinValue() { return map.getDefaultMinValue(); } +#endif + protected boolean isNullComparator() { return map.comparator() == null; } + @Override public ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE setDefaultReturnValue(VALUE_TYPE v) { - m.setDefaultReturnValue(v); + map.setDefaultReturnValue(v); return this; } @Override - public VALUE_TYPE getDefaultReturnValue() { return m.getDefaultReturnValue(); } + public VALUE_TYPE getDefaultReturnValue() { return map.getDefaultReturnValue(); } + + @Override + public VALUE_COLLECTION VALUE_GENERIC_TYPE values() { + if(values == null) values = new SubMapValues(); + return values; + } + + @Override + public NAVIGABLE_SET KEY_GENERIC_TYPE descendingKeySet() { + return descendingMap().navigableKeySet(); + } + + @Override + public SET KEY_GENERIC_TYPE keySet() { + return navigableKeySet(); + } + + protected abstract Node KEY_VALUE_GENERIC_TYPE subLowest(); + protected abstract Node KEY_VALUE_GENERIC_TYPE subHighest(); + protected abstract Node KEY_VALUE_GENERIC_TYPE subCeiling(KEY_TYPE key); + protected abstract Node KEY_VALUE_GENERIC_TYPE subHigher(KEY_TYPE key); + protected abstract Node KEY_VALUE_GENERIC_TYPE subFloor(KEY_TYPE key); + protected abstract Node KEY_VALUE_GENERIC_TYPE subLower(KEY_TYPE key); + protected abstract BI_ITERATOR KEY_GENERIC_TYPE keyIterator(); + protected abstract BI_ITERATOR KEY_GENERIC_TYPE keyIterator(KEY_TYPE element); + protected abstract VALUE_BI_ITERATOR VALUE_GENERIC_TYPE valueIterator(); + protected abstract BI_ITERATOR KEY_GENERIC_TYPE descendingKeyIterator(); + protected KEY_TYPE lowKeyOrNull(Node KEY_VALUE_GENERIC_TYPE entry) { return entry == null ? EMPTY_KEY_VALUE : entry.key; } + protected KEY_TYPE highKeyOrNull(Node KEY_VALUE_GENERIC_TYPE entry) { return entry == null ? EMPTY_KEY_VALUE : entry.key; } + protected Node KEY_VALUE_GENERIC_TYPE next(Node KEY_VALUE_GENERIC_TYPE entry) { return entry.next(); } + protected Node KEY_VALUE_GENERIC_TYPE previous(Node KEY_VALUE_GENERIC_TYPE entry) { return entry.previous(); } + + protected boolean tooLow(KEY_TYPE key) { + if (!fromStart) { + int c = map.compare(key, lo); + if (c < 0 || (c == 0 && !loInclusive)) return true; + } + return false; + } + + protected boolean tooHigh(KEY_TYPE key) { + if (!toEnd) { + int c = map.compare(key, hi); + if (c > 0 || (c == 0 && !hiInclusive)) return true; + } + return false; + } + protected boolean inRange(KEY_TYPE key) { return !tooLow(key) && !tooHigh(key); } + protected boolean inClosedRange(KEY_TYPE key) { return (fromStart || map.compare(key, lo) >= 0) && (toEnd || map.compare(hi, key) >= 0); } + protected boolean inRange(KEY_TYPE key, boolean inclusive) { return inclusive ? inRange(key) : inClosedRange(key); } + + protected Node KEY_VALUE_GENERIC_TYPE absLowest() { + Node KEY_VALUE_GENERIC_TYPE e = (fromStart ? map.first : (loInclusive ? map.findCeilingNode(lo) : map.findHigherNode(lo))); + return (e == null || tooHigh(e.key)) ? null : e; + } + + protected Node KEY_VALUE_GENERIC_TYPE absHighest() { + Node KEY_VALUE_GENERIC_TYPE e = (toEnd ? map.last : (hiInclusive ? map.findFloorNode(hi) : map.findLowerNode(hi))); + return (e == null || tooLow(e.key)) ? null : e; + } + + protected Node KEY_VALUE_GENERIC_TYPE absCeiling(KEY_TYPE key) { + if (tooLow(key)) return absLowest(); + Node KEY_VALUE_GENERIC_TYPE e = map.findCeilingNode(key); + return (e == null || tooHigh(e.key)) ? null : e; + } + + protected Node KEY_VALUE_GENERIC_TYPE absHigher(KEY_TYPE key) { + if (tooLow(key)) return absLowest(); + Node KEY_VALUE_GENERIC_TYPE e = map.findHigherNode(key); + return (e == null || tooHigh(e.key)) ? null : e; + } + + protected Node KEY_VALUE_GENERIC_TYPE absFloor(KEY_TYPE key) { + if (tooHigh(key)) return absHighest(); + Node KEY_VALUE_GENERIC_TYPE e = map.findFloorNode(key); + return (e == null || tooLow(e.key)) ? null : e; + } + + protected Node KEY_VALUE_GENERIC_TYPE absLower(KEY_TYPE key) { + if (tooHigh(key)) return absHighest(); + Node KEY_VALUE_GENERIC_TYPE e = map.findLowerNode(key); + return (e == null || tooLow(e.key)) ? null : e; + } + + protected Node KEY_VALUE_GENERIC_TYPE absHighFence() { return (toEnd ? null : (hiInclusive ? map.findHigherNode(hi) : map.findCeilingNode(hi))); } + protected Node KEY_VALUE_GENERIC_TYPE absLowFence() { return (fromStart ? null : (loInclusive ? map.findLowerNode(lo) : map.findFloorNode(lo))); } @Override public VALUE_TYPE putAndMoveToFirst(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); } @@ -1304,338 +1590,283 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G @Override public VALUE_TYPE getAndMoveToLast(KEY_TYPE key) { throw new UnsupportedOperationException(); } @Override - public COMPARATOR KEY_GENERIC_TYPE comparator() { return m.comparator(); } -#if TYPE_OBJECT - @Override - public boolean containsKey(Object key) { return inRange((CLASS_TYPE)key) && m.containsKey(key); } -#else - @Override - public boolean containsKey(KEY_TYPE key) { return inRange(key) && m.containsKey(key); } - -#endif - @Override - public KEY_TYPE FIRST_ENTRY_KEY() { - RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(); - return entry == null ? getDefaultMaxValue() : entry.key; - } + public COMPARATOR KEY_GENERIC_TYPE comparator() { return map.comparator(); } @Override public KEY_TYPE POLL_FIRST_ENTRY_KEY() { - if(fromStart) return m.POLL_FIRST_ENTRY_KEY(); - RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = loInclusive ? m.findCeilingNode(low) : m.findHigherNode(low); - if(entry != null && !tooHigh(entry.key)) { - KEY_TYPE value = entry.key; - m.removeNode(entry); - return value; + Node KEY_VALUE_GENERIC_TYPE entry = subLowest(); + if(entry != null) { + KEY_TYPE result = entry.key; + map.removeNode(entry); + return result; } - return getDefaultMaxValue(); - } - - @Override - public KEY_TYPE LAST_ENTRY_KEY() { - RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findHighest(); - return entry == null ? getDefaultMinValue() : entry.key; + return EMPTY_KEY_VALUE; } @Override public KEY_TYPE POLL_LAST_ENTRY_KEY() { - if(toEnd) return m.POLL_LAST_ENTRY_KEY(); - RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = hiInclusive ? m.findFloorNode(high) : m.findLowerNode(high); - if(entry != null && !tooLow(entry.key)) { - KEY_TYPE value = entry.key; - m.removeNode(entry); - return value; + Node KEY_VALUE_GENERIC_TYPE entry = subHighest(); + if(entry != null) { + KEY_TYPE result = entry.key; + map.removeNode(entry); + return result; } - return getDefaultMinValue(); + return EMPTY_KEY_VALUE; } @Override public VALUE_TYPE FIRST_ENTRY_VALUE() { - RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(); - return entry == null ? getDefaultReturnValue() : entry.value; + Node KEY_VALUE_GENERIC_TYPE entry = subLowest(); + return entry == null ? map.getDefaultReturnValue() : entry.value; } @Override public VALUE_TYPE LAST_ENTRY_VALUE() { - RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findHighest(); - return entry == null ? getDefaultReturnValue() : entry.value; + Node KEY_VALUE_GENERIC_TYPE entry = subHighest(); + return entry == null ? map.getDefaultReturnValue() : entry.value; } - protected RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE findLowest() { - if(fromStart) return m.first; - RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = loInclusive ? m.findCeilingNode(low) : m.findHigherNode(low); - return entry == null || tooHigh(entry.key) ? null : entry; + @Override + public KEY_TYPE FIRST_ENTRY_KEY() { + Node KEY_VALUE_GENERIC_TYPE entry = subLowest(); + if(entry == null) throw new NoSuchElementException(); + return entry.key; } - protected RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE findHighest() { - if(toEnd) return m.last; - RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = hiInclusive ? m.findFloorNode(high) : m.findLowerNode(high); - return entry == null || tooLow(entry.key) ? null : entry; + @Override + public KEY_TYPE LAST_ENTRY_KEY() { + Node KEY_VALUE_GENERIC_TYPE entry = subHighest(); + if(entry == null) throw new NoSuchElementException(); + return entry.key; } @Override public VALUE_TYPE put(KEY_TYPE key, VALUE_TYPE value) { - if(!inRange(key)) throw new IllegalArgumentException("key out of range"); - return m.put(key, value); + if (!inRange(key)) throw new IllegalArgumentException("key out of range"); + return map.put(key, value); } @Override public VALUE_TYPE putIfAbsent(KEY_TYPE key, VALUE_TYPE value) { - if(!inRange(key)) throw new IllegalArgumentException("key out of range"); - return m.putIfAbsent(key, value); + if (!inRange(key)) throw new IllegalArgumentException("key out of range"); + return map.putIfAbsent(key, value); } #if VALUE_PRIMITIVES @Override public VALUE_TYPE addTo(KEY_TYPE key, VALUE_TYPE value) { if(!inRange(key)) throw new IllegalArgumentException("key out of range"); - return m.addTo(key, value); + return map.addTo(key, value); } #endif +#if TYPE_OBJECT @Override - public VALUE_TYPE GET_VALUE(KEY_TYPE key) { - return inRange(key) ? m.GET_VALUE(key) : getDefaultReturnValue(); - } - -#if TYPE_OBJECT && VALUE_OBJECT - @Override - public VALUE_TYPE getOrDefault(Object key, VALUE_TYPE defaultValue) { - return inRange((KEY_TYPE)key) ? m.getOrDefault(key, defaultValue) : getDefaultReturnValue(); - } - + public boolean containsKey(Object key) { return inRange((CLASS_TYPE)key) && map.containsKey(key); } #else @Override - public VALUE_TYPE getOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue) { - return inRange(key) ? m.getOrDefault(key, defaultValue) : getDefaultReturnValue(); - } + public boolean containsKey(KEY_TYPE key) { return inRange(key) && map.containsKey(key); } #endif + @Override + public VALUE_TYPE COMPUTE_IF_PRESENT(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { + Objects.requireNonNull(mappingFunction); +#if TYPE_OBJECT + map.validate(key); +#endif + if(!inRange(key)) return getDefaultReturnValue(); + Node KEY_VALUE_GENERIC_TYPE entry = map.findNode(key); + if(entry == null || VALUE_EQUALS(entry.value, getDefaultReturnValue())) return getDefaultReturnValue(); + VALUE_TYPE newValue = mappingFunction.apply(key, entry.value); + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) { + map.removeNode(entry); + return newValue; + } + entry.value = newValue; + return newValue; + } + @Override public VALUE_TYPE REMOVE_VALUE(KEY_TYPE key) { - return inRange(key) ? m.REMOVE_VALUE(key) : getDefaultReturnValue(); + return inRange(key) ? map.REMOVE_VALUE(key) : getDefaultReturnValue(); } @Override public VALUE_TYPE REMOVE_VALUEOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue) { - return inRange(key) ? m.REMOVE_VALUE(key) : defaultValue; + return inRange(key) ? map.REMOVE_VALUE(key) : defaultValue; } #if TYPE_OBJECT && VALUE_OBJECT @Override public boolean remove(Object key, Object value) { - return inRange((CLASS_TYPE)key) && m.remove(key, value); + return inRange((CLASS_TYPE)key) && map.remove(key, value); } #else @Override public boolean remove(KEY_TYPE key, VALUE_TYPE value) { - return inRange(key) && m.remove(key, value); + return inRange(key) && map.remove(key, value); } #endif + @Override - public ObjectSet ENTRY_SET() { - if(entrySet == null) entrySet = new SubMapEntrySet(); - return entrySet; + public VALUE_TYPE GET_VALUE(KEY_TYPE key) { + return inRange(key) ? map.GET_VALUE(key) : getDefaultReturnValue(); + } + +#if TYPE_OBJECT && VALUE_OBJECT + @Override + public VALUE_TYPE getOrDefault(Object key, VALUE_TYPE defaultValue) { + return inRange((CLASS_TYPE)key) ? map.getOrDefault(key, defaultValue) : getDefaultReturnValue(); + } + +#else + @Override + public VALUE_TYPE getOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue) { + return inRange(key) ? map.getOrDefault(key, defaultValue) : getDefaultReturnValue(); + } + +#endif + + @Override + public KEY_TYPE lowerKey(KEY_TYPE key) { return lowKeyOrNull(subLower(key)); } + @Override + public KEY_TYPE floorKey(KEY_TYPE key) { return lowKeyOrNull(subFloor(key)); } + @Override + public KEY_TYPE ceilingKey(KEY_TYPE key) { return highKeyOrNull(subCeiling(key)); } + @Override + public KEY_TYPE higherKey(KEY_TYPE key) { return highKeyOrNull(subHigher(key)); } + @Override + public MAP.Entry KEY_VALUE_GENERIC_TYPE lowerEntry(KEY_TYPE key) { return subLower(key); } + @Override + public MAP.Entry KEY_VALUE_GENERIC_TYPE floorEntry(KEY_TYPE key) { return subFloor(key); } + @Override + public MAP.Entry KEY_VALUE_GENERIC_TYPE ceilingEntry(KEY_TYPE key) { return subCeiling(key); } + @Override + public MAP.Entry KEY_VALUE_GENERIC_TYPE higherEntry(KEY_TYPE key) { return subHigher(key); } + + @Override + public boolean isEmpty() { + if(fromStart && toEnd) return map.isEmpty(); + Node KEY_VALUE_GENERIC_TYPE n = absLowest(); + return n == null || tooHigh(n.key); } @Override - public SET KEY_GENERIC_TYPE keySet() { return navigableKeySet(); } + public int size() { return fromStart && toEnd ? map.size() : entrySet().size(); } @Override - public NAVIGABLE_SET KEY_GENERIC_TYPE navigableKeySet() { - if(keySet == null) keySet = new KeySetKV_BRACES(this); - return keySet; - } - - @Override - public NAVIGABLE_SET KEY_GENERIC_TYPE descendingKeySet() { - return descendingMap().navigableKeySet(); - } - - @Override - public VALUE_COLLECTION VALUE_GENERIC_TYPE values() { - if(values == null) values = new SubMapValues(); - return values; - } - - @Override - public void forEach(BI_CONSUMER KEY_VALUE_GENERIC_TYPE action) { - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) - action.accept(entry.key, entry.value); - } - - @Override - public int size() { - return (fromStart && toEnd) ? m.size() : entrySet().size(); - } + public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE copy() { throw new UnsupportedOperationException(); } @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE firstEntry() { - RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(); - return entry == null ? null : new BasicEntryKV_BRACES(entry.key, entry.value); + Node KEY_VALUE_GENERIC_TYPE entry = subLowest(); + return entry == null ? null : entry.export(); } - + @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE lastEntry() { - RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findHighest(); - return entry == null ? null : new BasicEntryKV_BRACES(entry.key, entry.value); + Node KEY_VALUE_GENERIC_TYPE entry = subHighest(); + return entry == null ? null : entry.export(); } - + @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE pollFirstEntry() { - RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(); - if(entry == null) return null; - BasicEntry KEY_VALUE_GENERIC_TYPE result = new BasicEntryKV_BRACES(entry.key, entry.value); - m.removeNode(entry); - return result; + Node KEY_VALUE_GENERIC_TYPE entry = subLowest(); + if(entry != null) { + MAP.Entry KEY_VALUE_GENERIC_TYPE result = entry.export(); + map.removeNode(entry); + return result; + } + return null; } - + @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE pollLastEntry() { - RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findHighest(); - if(entry == null) return null; - BasicEntry KEY_VALUE_GENERIC_TYPE result = new BasicEntryKV_BRACES(entry.key, entry.value); - m.removeNode(entry); - return result; - } - - @Override - public KEY_TYPE lowerKey(KEY_TYPE e) { - if(tooHigh(e)) { - RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findHighest(); - return entry == null ? getDefaultMinValue() : entry.key; + Node KEY_VALUE_GENERIC_TYPE entry = subHighest(); + if(entry != null) { + MAP.Entry KEY_VALUE_GENERIC_TYPE result = entry.export(); + map.removeNode(entry); + return result; } - RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = m.findLowerNode(e); - return entry == null || tooHigh(entry.key) ? getDefaultMaxValue() : entry.key; + return null; } - @Override - public KEY_TYPE floorKey(KEY_TYPE e) { - if(tooHigh(e)) { - RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findHighest(); - return entry == null ? getDefaultMinValue() : entry.key; - } - RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = m.findFloorNode(e); - return entry == null || tooHigh(entry.key) ? getDefaultMaxValue() : entry.key; - } - - @Override - public KEY_TYPE ceilingKey(KEY_TYPE e) { - if(tooLow(e)) { - RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(); - return entry == null ? getDefaultMaxValue() : entry.key; - } - RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = m.findCeilingNode(e); - return entry == null || tooLow(entry.key) ? getDefaultMinValue() : entry.key; - } - - @Override - public KEY_TYPE higherKey(KEY_TYPE e) { - if(tooLow(e)) { - RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(); - return entry == null ? getDefaultMaxValue() : entry.key; - } - RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = m.findHigherNode(e); - return entry == null || tooLow(entry.key) ? getDefaultMinValue() : entry.key; - } - - @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE lowerEntry(KEY_TYPE e) { - if(tooHigh(e)) { - RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findHighest(); - return entry == null ? null : new BasicEntryKV_BRACES(entry.key, entry.value); - } - RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = m.findLowerNode(e); - return entry == null || tooHigh(entry.key) ? null : new BasicEntryKV_BRACES(entry.key, entry.value); - } - - @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE floorEntry(KEY_TYPE e) { - if(tooHigh(e)) { - RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findHighest(); - return entry == null ? null : new BasicEntryKV_BRACES(entry.key, entry.value); - } - RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = m.findFloorNode(e); - return entry == null || tooHigh(entry.key) ? null : new BasicEntryKV_BRACES(entry.key, entry.value); - } - - @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE ceilingEntry(KEY_TYPE e) { - if(tooLow(e)) { - RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(); - return entry == null ? null : new BasicEntryKV_BRACES(entry.key, entry.value); - } - RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = m.findCeilingNode(e); - return entry == null || tooLow(entry.key) ? null : new BasicEntryKV_BRACES(entry.key, entry.value); - } - - @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE higherEntry(KEY_TYPE e) { - if(tooLow(e)) { - RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(); - return entry == null ? null : new BasicEntryKV_BRACES(entry.key, entry.value); - } - RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = m.findHigherNode(e); - return entry == null || tooLow(entry.key) ? null : new BasicEntryKV_BRACES(entry.key, entry.value); - } - - class SubMapEntrySet extends AbstractObjectSet implements ObjectSet { + abstract class SubEntrySet extends AbstractObjectSet { @Override - @Deprecated - public boolean contains(Object o) { - if(o instanceof Map.Entry) { - if(o instanceof MAP.Entry) { - MAP.Entry KEY_VALUE_GENERIC_TYPE entry = (MAP.Entry KEY_VALUE_GENERIC_TYPE)o; - if(entry.getKey() == null && comparator() == null) return false; - RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE subEntry = m.findNode(entry.ENTRY_KEY()); - if(subEntry != null) return VALUE_EQUALS(entry.ENTRY_VALUE(), subEntry.value); - } - else { - Map.Entry entry = (Map.Entry)o; - if(entry.getKey() == null && comparator() == null) return false; -#if !TYPE_OBJECT - if(!(entry.getKey() instanceof CLASS_TYPE)) return false; -#endif - RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE subEntry = m.findNode((CLASS_TYPE)entry.getKey()); - if(subEntry != null) return Objects.equals(entry.getValue(), VALUE_TO_OBJ(subEntry.value)); - } - } - return false; + public int size() { + if (fromStart && toEnd) return map.size(); + int size = 0; + for(ObjectIterator iter = iterator();iter.hasNext();iter.next(),size++); + return size; + } + + @Override + public boolean isEmpty() { + Node KEY_VALUE_GENERIC_TYPE n = absLowest(); + return n == null || tooHigh(n.key); + } + + @Override + public boolean contains(Object o) { + if (!(o instanceof Map.Entry)) return false; + if(o instanceof MAP.Entry) + { + MAP.Entry KEY_VALUE_GENERIC_TYPE entry = (MAP.Entry KEY_VALUE_GENERIC_TYPE) o; +#if TYPE_OBJECT + if(entry.ENTRY_KEY() == null && isNullComparator()) return false; +#endif + KEY_TYPE key = entry.ENTRY_KEY(); + if (!inRange(key)) return false; + Node KEY_VALUE_GENERIC_TYPE node = map.findNode(key); + return node != null && VALUE_EQUALS(entry.ENTRY_VALUE(), node.ENTRY_VALUE()); + } + Map.Entry entry = (Map.Entry) o; + if(entry.getKey() == null && isNullComparator()) return false; + CLASS_TYPE key = (CLASS_TYPE)entry.getKey(); + if (!inRange(key)) return false; + Node KEY_VALUE_GENERIC_TYPE node = map.findNode(key); + return node != null && Objects.equals(entry.getValue(), VALUE_TO_OBJ(node.ENTRY_VALUE())); } @Override - @Deprecated public boolean remove(Object o) { - if(o instanceof Map.Entry) { - if(o instanceof MAP.Entry) { - MAP.Entry KEY_VALUE_GENERIC_TYPE entry = (MAP.Entry KEY_VALUE_GENERIC_TYPE)o; - return NavigableSubMap.this.remove(entry.ENTRY_KEY(), entry.ENTRY_VALUE()); + if (!(o instanceof Map.Entry)) return false; + if(o instanceof MAP.Entry) + { + MAP.Entry KEY_VALUE_GENERIC_TYPE entry = (MAP.Entry KEY_VALUE_GENERIC_TYPE) o; + KEY_TYPE key = entry.ENTRY_KEY(); + if (!inRange(key)) return false; + Node KEY_VALUE_GENERIC_TYPE node = map.findNode(key); + if (node != null && VALUE_EQUALS(node.getValue(), entry.getValue())) { + map.removeNode(node); + return true; } - Map.Entry entry = (Map.Entry)o; - return NavigableSubMap.this.remove(entry.getKey(), entry.getValue()); + return false; + } + Map.Entry entry = (Map.Entry) o; + CLASS_TYPE key = (CLASS_TYPE)entry.getKey(); + if (!inRange(key)) return false; + Node KEY_VALUE_GENERIC_TYPE node = map.findNode(key); + if (node != null && Objects.equals(node.getValue(), entry.getValue())) { + map.removeNode(node); + return true; } return false; } - @Override - public ObjectIterator iterator() { return entryIterator(); } - @Override - public int size() { return NavigableSubMap.this.size(); } - @Override - public void clear() { NavigableSubMap.this.clear(); } @Override public void forEach(Consumer action) { Objects.requireNonNull(action); - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) + for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) action.accept(new BasicEntryKV_BRACES(entry.key, entry.value)); } @Override public void forEach(E input, ObjectObjectConsumer action) { Objects.requireNonNull(action); - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) + for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) action.accept(input, new BasicEntryKV_BRACES(entry.key, entry.value)); } @@ -1644,7 +1875,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G Objects.requireNonNull(filter); if(size() <= 0) return false; BasicEntry KEY_VALUE_GENERIC_TYPE subEntry = new BasicEntryKV_BRACES(); - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) { + for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) { subEntry.set(entry.key, entry.value); if(filter.getBoolean(subEntry)) return true; } @@ -1656,7 +1887,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G Objects.requireNonNull(filter); if(size() <= 0) return true; BasicEntry KEY_VALUE_GENERIC_TYPE subEntry = new BasicEntryKV_BRACES(); - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) { + for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) { subEntry.set(entry.key, entry.value); if(filter.getBoolean(subEntry)) return false; } @@ -1668,7 +1899,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G Objects.requireNonNull(filter); if(size() <= 0) return true; BasicEntry KEY_VALUE_GENERIC_TYPE subEntry = new BasicEntryKV_BRACES(); - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) { + for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) { subEntry.set(entry.key, entry.value); if(!filter.getBoolean(subEntry)) return false; } @@ -1679,7 +1910,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G public E reduce(E identity, BiFunction operator) { Objects.requireNonNull(operator); E state = identity; - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) { + for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) { state = operator.apply(state, new BasicEntryKV_BRACES(entry.key, entry.value)); } return state; @@ -1690,7 +1921,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G Objects.requireNonNull(operator); MAP.Entry KEY_VALUE_GENERIC_TYPE state = null; boolean empty = true; - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) { + for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) { if(empty) { empty = false; state = new BasicEntryKV_BRACES(entry.key, entry.value); @@ -1706,7 +1937,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G Objects.requireNonNull(filter); if(size() <= 0) return null; BasicEntry KEY_VALUE_GENERIC_TYPE subEntry = new BasicEntryKV_BRACES(); - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) { + for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) { subEntry.set(entry.key, entry.value); if(filter.getBoolean(subEntry)) return subEntry; } @@ -1719,7 +1950,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G if(size() <= 0) return 0; int result = 0; BasicEntry KEY_VALUE_GENERIC_TYPE subEntry = new BasicEntryKV_BRACES(); - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) { + for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) { subEntry.set(entry.key, entry.value); if(filter.getBoolean(subEntry)) result++; } @@ -1760,21 +1991,21 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G @Override public void forEach(VALUE_CONSUMER VALUE_SUPER_GENERIC_TYPE action) { Objects.requireNonNull(action); - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) + for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) action.accept(entry.value); } @Override public void forEach(E input, VALUE_BI_FROM_OBJECT_CONSUMER VSV_GENERIC_TYPE action) { Objects.requireNonNull(action); - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) + for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) action.accept(input, entry.value); } @Override public boolean matchesAny(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { Objects.requireNonNull(filter); - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) + for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) if(filter.VALUE_TEST_VALUE(entry.value)) return true; return false; } @@ -1782,7 +2013,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G @Override public boolean matchesNone(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { Objects.requireNonNull(filter); - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) + for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) if(filter.VALUE_TEST_VALUE(entry.value)) return false; return true; } @@ -1790,7 +2021,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G @Override public boolean matchesAll(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { Objects.requireNonNull(filter); - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) + for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) if(!filter.VALUE_TEST_VALUE(entry.value)) return false; return true; } @@ -1800,7 +2031,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G public VALUE_TYPE reduce(VALUE_TYPE identity, VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { Objects.requireNonNull(operator); VALUE_TYPE state = identity; - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) + for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) state = operator.APPLY_VALUE(state, entry.value); return state; } @@ -1810,7 +2041,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G public VALUE_SPECIAL_TYPE reduce(VALUE_SPECIAL_TYPE identity, BiFunction operator) { Objects.requireNonNull(operator); VALUE_SPECIAL_TYPE state = identity; - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) + for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) state = operator.apply(state, entry.value); return state; } @@ -1821,7 +2052,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G Objects.requireNonNull(operator); VALUE_TYPE state = EMPTY_VALUE; boolean empty = true; - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) { + for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) { if(empty) { empty = false; state = entry.value; @@ -1835,7 +2066,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G @Override public VALUE_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { Objects.requireNonNull(filter); - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) + for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) if(filter.VALUE_TEST_VALUE(entry.value)) return entry.value; return EMPTY_VALUE; } @@ -1844,530 +2075,198 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G public int count(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { Objects.requireNonNull(filter); int result = 0; - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) + for(Node KEY_VALUE_GENERIC_TYPE entry = subLowest(), last = subHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) if(filter.VALUE_TEST_VALUE(entry.value)) result++; return result; } } - final class SubMapEntrySetIterator extends SubMapEntryIterator implements ObjectListIterator { - public SubMapEntrySetIterator(boolean descending) { - super(descending); - } - - public SubMapEntrySetIterator(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry, RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE lowerFence, RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE upperFence) { - super(entry, lowerFence, upperFence); + class DecsendingSubEntryIterator extends SubMapEntryIterator implements ObjectBidirectionalIterator + { + public DecsendingSubEntryIterator(Node KEY_VALUE_GENERIC_TYPE first, Node KEY_VALUE_GENERIC_TYPE forwardFence, Node KEY_VALUE_GENERIC_TYPE backwardFence) { + super(first, forwardFence, backwardFence); } @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE next() { return nextEntry(); } + public MAP.Entry KEY_VALUE_GENERIC_TYPE previous() { + if(!hasPrevious()) throw new NoSuchElementException(); + return nextEntry(); + } + @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE previous() { return previousEntry(); } - - @Override - public void set(MAP.Entry KEY_VALUE_GENERIC_TYPE e) { throw new UnsupportedOperationException(); } - @Override - public void add(MAP.Entry KEY_VALUE_GENERIC_TYPE e) { throw new UnsupportedOperationException(); } + public MAP.Entry KEY_VALUE_GENERIC_TYPE next() { + if(!hasNext()) throw new NoSuchElementException(); + return previousEntry(); + } } - final class SubMapKeyIterator extends SubMapEntryIterator implements LIST_ITERATOR KEY_GENERIC_TYPE { - public SubMapKeyIterator(boolean descending) { - super(descending); + class AcsendingSubEntryIterator extends SubMapEntryIterator implements ObjectBidirectionalIterator + { + public AcsendingSubEntryIterator(Node KEY_VALUE_GENERIC_TYPE first, Node KEY_VALUE_GENERIC_TYPE forwardFence, Node KEY_VALUE_GENERIC_TYPE backwardFence) { + super(first, forwardFence, backwardFence); } - - public SubMapKeyIterator(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry, RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE lowerFence, RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE upperFence) { - super(entry, lowerFence, upperFence); + + @Override + public MAP.Entry KEY_VALUE_GENERIC_TYPE previous() { + if(!hasPrevious()) throw new NoSuchElementException(); + return previousEntry(); } - + @Override - public KEY_TYPE NEXT() { return nextEntry().key; } - @Override - public KEY_TYPE PREVIOUS() { return previousEntry().key; } - - @Override - public void set(KEY_TYPE e) { throw new UnsupportedOperationException(); } - @Override - public void add(KEY_TYPE e) { throw new UnsupportedOperationException(); } + public MAP.Entry KEY_VALUE_GENERIC_TYPE next() { + if(!hasNext()) throw new NoSuchElementException(); + return nextEntry(); + } } - final class SubMapValueIterator extends SubMapEntryIterator implements VALUE_LIST_ITERATOR VALUE_GENERIC_TYPE { - public SubMapValueIterator(boolean descending) { - super(descending); - } - - public SubMapValueIterator(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry, RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE lowerFence, RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE upperFence) { - super(entry, lowerFence, upperFence); + class DecsendingSubKeyIterator extends SubMapEntryIterator implements BI_ITERATOR KEY_GENERIC_TYPE + { + public DecsendingSubKeyIterator(Node KEY_VALUE_GENERIC_TYPE first, Node KEY_VALUE_GENERIC_TYPE forwardFence, Node KEY_VALUE_GENERIC_TYPE backwardFence) { + super(first, forwardFence, backwardFence); } @Override - public VALUE_TYPE VALUE_NEXT() { return nextEntry().value; } + public KEY_TYPE PREVIOUS() { + if(!hasPrevious()) throw new NoSuchElementException(); + return nextEntry().key; + } + @Override - public VALUE_TYPE VALUE_PREVIOUS() { return previousEntry().value; } - - @Override - public void set(VALUE_TYPE e) { throw new UnsupportedOperationException(); } - @Override - public void add(VALUE_TYPE e) { throw new UnsupportedOperationException(); } + public KEY_TYPE NEXT() { + if(!hasNext()) throw new NoSuchElementException(); + return previousEntry().key; + } } - abstract class SubMapEntryIterator { - CLASS_TYPE lowerFence; - CLASS_TYPE upperFence; - RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE next; - RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE previous; - RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE current; - int index = 0; - - public SubMapEntryIterator(boolean descending) { - this(descending ? findHighest() : findLowest(), fromStart ? null : findLowest(), toEnd ? null : findHighest()); + class AcsendingSubKeyIterator extends SubMapEntryIterator implements BI_ITERATOR KEY_GENERIC_TYPE + { + public AcsendingSubKeyIterator(Node KEY_VALUE_GENERIC_TYPE first, Node KEY_VALUE_GENERIC_TYPE forwardFence, Node KEY_VALUE_GENERIC_TYPE backwardFence) { + super(first, forwardFence, backwardFence); + } + + @Override + public KEY_TYPE PREVIOUS() { + if(!hasPrevious()) throw new NoSuchElementException(); + return previousEntry().key; + } + + @Override + public KEY_TYPE NEXT() { + if(!hasNext()) throw new NoSuchElementException(); + return nextEntry().key; + } + } + + class AcsendingSubValueIterator extends SubMapEntryIterator implements VALUE_BI_ITERATOR VALUE_GENERIC_TYPE + { + public AcsendingSubValueIterator(Node KEY_VALUE_GENERIC_TYPE first, Node KEY_VALUE_GENERIC_TYPE forwardFence, Node KEY_VALUE_GENERIC_TYPE backwardFence) { + super(first, forwardFence, backwardFence); + } + + @Override + public VALUE_TYPE VALUE_PREVIOUS() { + if(!hasPrevious()) throw new NoSuchElementException(); + return previousEntry().value; + } + + @Override + public VALUE_TYPE VALUE_NEXT() { + if(!hasNext()) throw new NoSuchElementException(); + return nextEntry().value; + } + } + + class DecsendingSubValueIterator extends SubMapEntryIterator implements VALUE_BI_ITERATOR VALUE_GENERIC_TYPE + { + public DecsendingSubValueIterator(Node KEY_VALUE_GENERIC_TYPE first, Node KEY_VALUE_GENERIC_TYPE forwardFence, Node KEY_VALUE_GENERIC_TYPE backwardFence) { + super(first, forwardFence, backwardFence); } - public SubMapEntryIterator(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry, RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE lowerFence, RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE upperFence) { - next = entry; - previous = entry.previous(); - this.lowerFence = lowerFence != null ? KEY_TO_OBJ(lowerFence.key) : null; - this.upperFence = upperFence != null ? KEY_TO_OBJ(upperFence.key) : null; + @Override + public VALUE_TYPE VALUE_PREVIOUS() { + if(!hasPrevious()) throw new NoSuchElementException(); + return nextEntry().value; + } + + @Override + public VALUE_TYPE VALUE_NEXT() { + if(!hasNext()) throw new NoSuchElementException(); + return previousEntry().value; + } + } + + abstract class SubMapEntryIterator + { + boolean wasForward; + Node KEY_VALUE_GENERIC_TYPE lastReturned; + Node KEY_VALUE_GENERIC_TYPE next; + boolean unboundForwardFence; + boolean unboundBackwardFence; + KEY_TYPE forwardFence; + KEY_TYPE backwardFence; + + public SubMapEntryIterator(Node KEY_VALUE_GENERIC_TYPE first, Node KEY_VALUE_GENERIC_TYPE forwardFence, Node KEY_VALUE_GENERIC_TYPE backwardFence) + { + next = first; + this.forwardFence = forwardFence == null ? null : forwardFence.key; + this.backwardFence = backwardFence == null ? null : backwardFence.key; + unboundForwardFence = forwardFence == null; + unboundBackwardFence = backwardFence == null; } public boolean hasNext() { - return next != null && (upperFence == null || KEY_EQUALS(next.key, OBJ_TO_KEY(upperFence))); + return next != null && (unboundForwardFence || next.key != forwardFence); + } + + protected Node KEY_VALUE_GENERIC_TYPE nextEntry() { + lastReturned = next; + Node KEY_VALUE_GENERIC_TYPE result = next; + next = next.next(); + wasForward = true; + return result; } public boolean hasPrevious() { - return previous != null && (lowerFence == null || KEY_EQUALS(next.key, OBJ_TO_KEY(lowerFence))); + return next != null && (unboundBackwardFence || next.key != backwardFence); } - public int nextIndex() { - return index; - } - - public int previousIndex() { - return index - 1; - } - - protected void updateNext() { - next = current.next(); - } - - protected void updatePrevious() { - previous = current.previous(); - } - - public RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE nextEntry() { - if(!hasNext()) throw new NoSuchElementException(); - current = previous = next; - updateNext(); - index++; - return current; - } - - public RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE previousEntry() { - if(!hasPrevious()) throw new NoSuchElementException(); - current = next = previous; - updatePrevious(); - index--; - return current; + protected Node KEY_VALUE_GENERIC_TYPE previousEntry() { + lastReturned = next; + Node KEY_VALUE_GENERIC_TYPE result = next; + next = next.previous(); + wasForward = false; + return result; } public void remove() { - if(current == null) throw new IllegalStateException(); - if(current == previous) index--; - updateNext(); - updatePrevious(); - if(current.needsSuccessor()) next = current; - m.removeNode(current); - current = null; + if(lastReturned == null) throw new IllegalStateException(); + if(wasForward && lastReturned.needsSuccessor()) next = lastReturned; + map.removeNode(lastReturned); + lastReturned = null; } } } - private class EntrySet extends AbstractObjectSet { - + class Values extends VALUE_ABSTRACT_COLLECTION VALUE_GENERIC_TYPE + { @Override - public ObjectIterator iterator() { - return new EntryIterator(false); + public VALUE_ITERATOR VALUE_GENERIC_TYPE iterator() { + return new AscendingValueIterator(first); } @Override - @Deprecated - public boolean contains(Object o) { - if(o instanceof Map.Entry) { - if(o instanceof MAP.Entry) { - MAP.Entry KEY_VALUE_GENERIC_TYPE entry = (MAP.Entry KEY_VALUE_GENERIC_TYPE)o; - if(entry.getKey() == null && comparator() == null) return false; - RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE subEntry = RB_TREE_MAP.this.findNode(entry.ENTRY_KEY()); - if(subEntry != null) return VALUE_EQUALS(entry.ENTRY_VALUE(), subEntry.value); - } - else { - Map.Entry entry = (Map.Entry)o; - if(entry.getKey() == null && comparator() == null) return false; -#if !TYPE_OBJECT - if(!(entry.getKey() instanceof CLASS_TYPE)) return false; -#endif - RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE subEntry = RB_TREE_MAP.this.findNode((CLASS_TYPE)entry.getKey()); - if(subEntry != null) return Objects.equals(entry.getValue(), VALUE_TO_OBJ(subEntry.value)); - } - } - return false; - } - - @Override - @Deprecated - public boolean remove(Object o) { - if(o instanceof Map.Entry) { - if(o instanceof MAP.Entry) { - MAP.Entry KEY_VALUE_GENERIC_TYPE entry = (MAP.Entry KEY_VALUE_GENERIC_TYPE)o; - return RB_TREE_MAP.this.remove(entry.ENTRY_KEY(), entry.ENTRY_VALUE()); - } - Map.Entry entry = (Map.Entry)o; - return RB_TREE_MAP.this.remove(entry.getKey(), entry.getValue()); - } - return false; - } - - @Override - public int size() { - return RB_TREE_MAP.this.size(); - } + public boolean add(VALUE_TYPE e) { throw new UnsupportedOperationException(); } @Override public void clear() { RB_TREE_MAP.this.clear(); } - @Override - public void forEach(Consumer action) { - Objects.requireNonNull(action); - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) - action.accept(new BasicEntryKV_BRACES(entry.key, entry.value)); - } - - @Override - public void forEach(E input, ObjectObjectConsumer action) { - Objects.requireNonNull(action); - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) - action.accept(input, new BasicEntryKV_BRACES(entry.key, entry.value)); - } - - @Override - public boolean matchesAny(Object2BooleanFunction filter) { - Objects.requireNonNull(filter); - if(size() <= 0) return false; - BasicEntry KEY_VALUE_GENERIC_TYPE subEntry = new BasicEntryKV_BRACES(); - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { - subEntry.set(entry.key, entry.value); - if(filter.getBoolean(subEntry)) return true; - } - return false; - } - - @Override - public boolean matchesNone(Object2BooleanFunction filter) { - Objects.requireNonNull(filter); - if(size() <= 0) return true; - BasicEntry KEY_VALUE_GENERIC_TYPE subEntry = new BasicEntryKV_BRACES(); - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { - subEntry.set(entry.key, entry.value); - if(filter.getBoolean(subEntry)) return false; - } - return true; - } - - @Override - public boolean matchesAll(Object2BooleanFunction filter) { - Objects.requireNonNull(filter); - if(size() <= 0) return true; - BasicEntry KEY_VALUE_GENERIC_TYPE subEntry = new BasicEntryKV_BRACES(); - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { - subEntry.set(entry.key, entry.value); - if(!filter.getBoolean(subEntry)) return false; - } - return true; - } - - @Override - public E reduce(E identity, BiFunction operator) { - Objects.requireNonNull(operator); - E state = identity; - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { - state = operator.apply(state, new BasicEntryKV_BRACES(entry.key, entry.value)); - } - return state; - } - - @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE reduce(ObjectObjectUnaryOperator operator) { - Objects.requireNonNull(operator); - MAP.Entry KEY_VALUE_GENERIC_TYPE state = null; - boolean empty = true; - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { - if(empty) { - empty = false; - state = new BasicEntryKV_BRACES(entry.key, entry.value); - continue; - } - state = operator.apply(state, new BasicEntryKV_BRACES(entry.key, entry.value)); - } - return state; - } - - @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE findFirst(Object2BooleanFunction filter) { - Objects.requireNonNull(filter); - if(size() <= 0) return null; - BasicEntry KEY_VALUE_GENERIC_TYPE subEntry = new BasicEntryKV_BRACES(); - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { - subEntry.set(entry.key, entry.value); - if(filter.getBoolean(subEntry)) return subEntry; - } - return null; - } - - @Override - public int count(Object2BooleanFunction filter) { - Objects.requireNonNull(filter); - if(size() <= 0) return 0; - int result = 0; - BasicEntry KEY_VALUE_GENERIC_TYPE subEntry = new BasicEntryKV_BRACES(); - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { - subEntry.set(entry.key, entry.value); - if(filter.getBoolean(subEntry)) result++; - } - return result; - } - } - - static final class KeySet KEY_VALUE_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE implements NAVIGABLE_SET KEY_GENERIC_TYPE { - NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE m; - - KeySet(NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE m) { - this.m = m; - } - -#if !TYPE_OBJECT - @Override - public void setDefaultMaxValue(KEY_TYPE e) { m.setDefaultMaxValue(e); } - @Override - public KEY_TYPE getDefaultMaxValue() { return m.getDefaultMaxValue(); } - @Override - public void setDefaultMinValue(KEY_TYPE e) { m.setDefaultMinValue(e); } - @Override - public KEY_TYPE getDefaultMinValue() { return m.getDefaultMinValue(); } -#endif - @Override - public boolean add(KEY_TYPE o) { throw new UnsupportedOperationException(); } - @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 m.comparator(); } - @Override - public KEY_TYPE lower(KEY_TYPE e) { return m.lowerKey(e); } - @Override - public KEY_TYPE floor(KEY_TYPE e) { return m.floorKey(e); } - @Override - public KEY_TYPE ceiling(KEY_TYPE e) { return m.ceilingKey(e); } - @Override - public KEY_TYPE higher(KEY_TYPE e) { return m.higherKey(e); } - @Override - public KEY_TYPE FIRST_KEY() { return m.FIRST_ENTRY_KEY(); } - @Override - public KEY_TYPE POLL_FIRST_KEY() { return m.POLL_FIRST_ENTRY_KEY(); } - @Override - public KEY_TYPE LAST_KEY() { return m.LAST_ENTRY_KEY(); } - @Override - public KEY_TYPE POLL_LAST_KEY() { return m.POLL_LAST_ENTRY_KEY(); } -#if TYPE_OBJECT - @Override - public boolean remove(Object o) { - int oldSize = m.size(); - m.remove(o); - return oldSize != m.size(); - } - -#else - @Override - public boolean remove(KEY_TYPE o) { - int oldSize = m.size(); - m.remove(o); - return oldSize != m.size(); - } - -#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)); - } - - @Override - public NAVIGABLE_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement, boolean inclusive) { - return new KeySetKV_BRACES(m.headMap(toElement, inclusive)); - } - - @Override - public NAVIGABLE_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement, boolean inclusive) { - return new KeySetKV_BRACES(m.tailMap(fromElement, inclusive)); - } - - @Override - public NAVIGABLE_SET KEY_GENERIC_TYPE descendingSet() { - return new KeySetKV_BRACES(m.descendingMap()); - } - - @Override - public BI_ITERATOR KEY_GENERIC_TYPE iterator() { - if(m instanceof RB_TREE_MAP) return ((RB_TREE_MAP KEY_VALUE_GENERIC_TYPE)m).keyIterator(false); - return ((NavigableSubMap KEY_VALUE_GENERIC_TYPE)m).keyIterator(false); - } - - @Override - public BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement) { - if(m instanceof RB_TREE_MAP) return ((RB_TREE_MAP KEY_VALUE_GENERIC_TYPE)m).keyIterator(fromElement); - return ((NavigableSubMap KEY_VALUE_GENERIC_TYPE)m).keyIterator(fromElement); - } - - @Override - public BI_ITERATOR KEY_GENERIC_TYPE descendingIterator() { - if(m instanceof RB_TREE_MAP) return ((RB_TREE_MAP KEY_VALUE_GENERIC_TYPE)m).keyIterator(true); - return ((NavigableSubMap KEY_VALUE_GENERIC_TYPE)m).keyIterator(true); - } - @Override public int size() { - return m.size(); + return RB_TREE_MAP.this.size; } - @Override - public void clear() { - m.clear(); - } - - protected RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE start() { - if(m instanceof RB_TREE_MAP) return ((RB_TREE_MAP KEY_VALUE_GENERIC_TYPE)m).first; - return ((NavigableSubMap KEY_VALUE_GENERIC_TYPE)m).findLowest(); - } - - protected RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE end() { - if(m instanceof RB_TREE_MAP) return null; - return ((NavigableSubMap KEY_VALUE_GENERIC_TYPE)m).findLowest(); - } - - protected RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE next(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry) { - if(m instanceof RB_TREE_MAP) return entry.next(); - return ((NavigableSubMap KEY_VALUE_GENERIC_TYPE)m).next(entry); - } - - protected RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE previous(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry) { - if(m instanceof RB_TREE_MAP) return entry.previous(); - return ((NavigableSubMap KEY_VALUE_GENERIC_TYPE)m).previous(entry); - } - - @Override - public void forEach(CONSUMER KEY_SUPER_GENERIC_TYPE action) { - Objects.requireNonNull(action); - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - action.accept(entry.key); - } - - @Override - public void forEach(E input, BI_FROM_OBJECT_CONSUMER KSK_GENERIC_TYPE action) { - Objects.requireNonNull(action); - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - action.accept(input, entry.key); - } - - @Override - public boolean matchesAny(PREDICATE KEY_GENERIC_TYPE filter) { - Objects.requireNonNull(filter); - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.TEST_VALUE(entry.key)) return true; - return false; - } - - @Override - public boolean matchesNone(PREDICATE KEY_GENERIC_TYPE filter) { - Objects.requireNonNull(filter); - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.TEST_VALUE(entry.key)) return false; - return true; - } - - @Override - public boolean matchesAll(PREDICATE KEY_GENERIC_TYPE filter) { - Objects.requireNonNull(filter); - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(!filter.TEST_VALUE(entry.key)) return false; - return true; - } - -#if !TYPE_OBJECT - @Override - public KEY_TYPE reduce(KEY_TYPE identity, SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { - Objects.requireNonNull(operator); - KEY_TYPE state = identity; - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - state = operator.APPLY_KEY_VALUE(state, entry.key); - return state; - } - -#else - @Override - public KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction operator) { - Objects.requireNonNull(operator); - KEY_SPECIAL_TYPE state = identity; - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - state = operator.apply(state, entry.key); - return state; - } - -#endif - @Override - public KEY_TYPE reduce(SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { - Objects.requireNonNull(operator); - KEY_TYPE state = EMPTY_KEY_VALUE; - boolean empty = true; - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) { - if(empty) { - empty = false; - state = entry.key; - continue; - } - state = operator.APPLY_KEY_VALUE(state, entry.key); - } - return state; - } - - @Override - public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { - Objects.requireNonNull(filter); - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.TEST_VALUE(entry.key)) return entry.key; - return EMPTY_KEY_VALUE; - } - - @Override - public int count(PREDICATE KEY_GENERIC_TYPE filter) { - Objects.requireNonNull(filter); - int result = 0; - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) - if(filter.TEST_VALUE(entry.key)) result++; - return result; - } - } - - final class Values extends VALUE_ABSTRACT_COLLECTION VALUE_GENERIC_TYPE { - - @Override - public boolean add(VALUE_TYPE o) { throw new UnsupportedOperationException(); } - #if VALUE_OBJECT @Override public boolean contains(Object e) { @@ -2382,36 +2281,34 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G #endif @Override - public VALUE_ITERATOR VALUE_GENERIC_TYPE iterator() { return new ValueIterator(false); } - - @Override - public int size() { - return RB_TREE_MAP.this.size(); - } - - @Override - public void clear() { - RB_TREE_MAP.this.clear(); + public boolean remove(Object o) { + for(Node KEY_VALUE_GENERIC_TYPE entry = first; entry != null; entry = entry.next()) { + if(Objects.equals(entry.getValue(), o)) { + removeNode(entry); + return true; + } + } + return false; } @Override public void forEach(VALUE_CONSUMER VALUE_SUPER_GENERIC_TYPE action) { Objects.requireNonNull(action); - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) + for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) action.accept(entry.value); } @Override public void forEach(E input, VALUE_BI_FROM_OBJECT_CONSUMER VSV_GENERIC_TYPE action) { Objects.requireNonNull(action); - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) + for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) action.accept(input, entry.value); } @Override public boolean matchesAny(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { Objects.requireNonNull(filter); - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) + for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) if(filter.VALUE_TEST_VALUE(entry.value)) return true; return false; } @@ -2419,7 +2316,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G @Override public boolean matchesNone(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { Objects.requireNonNull(filter); - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) + for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) if(filter.VALUE_TEST_VALUE(entry.value)) return false; return true; } @@ -2427,38 +2324,38 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G @Override public boolean matchesAll(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { Objects.requireNonNull(filter); - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) + for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) if(!filter.VALUE_TEST_VALUE(entry.value)) return false; return true; } -#if !VALUE_OBJECT + #if !VALUE_OBJECT @Override public VALUE_TYPE reduce(VALUE_TYPE identity, VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { Objects.requireNonNull(operator); VALUE_TYPE state = identity; - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) + for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) state = operator.APPLY_VALUE(state, entry.value); return state; } -#else + #else @Override public VALUE_SPECIAL_TYPE reduce(VALUE_SPECIAL_TYPE identity, BiFunction operator) { Objects.requireNonNull(operator); VALUE_SPECIAL_TYPE state = identity; - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) + for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) state = operator.apply(state, entry.value); return state; } -#endif + #endif @Override public VALUE_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { Objects.requireNonNull(operator); VALUE_TYPE state = EMPTY_VALUE; boolean empty = true; - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { + for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { if(empty) { empty = false; state = entry.value; @@ -2472,7 +2369,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G @Override public VALUE_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { Objects.requireNonNull(filter); - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) + for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) if(filter.VALUE_TEST_VALUE(entry.value)) return entry.value; return EMPTY_VALUE; } @@ -2481,187 +2378,335 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G public int count(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { Objects.requireNonNull(filter); int result = 0; - for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) + for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) if(filter.VALUE_TEST_VALUE(entry.value)) result++; return result; } } - final class EntryIterator extends MapEntryIterator implements ObjectListIterator { + class EntrySet extends AbstractObjectSet { - public EntryIterator(boolean descending) { - super(descending); - } - - public EntryIterator(Entry KEY_VALUE_GENERIC_TYPE entry) { - super(entry); + @Override + public ObjectIterator iterator() { + return new AscendingMapEntryIterator(first); } @Override - public MAP.Entry KEY_VALUE_GENERIC_TYPE next() { - return nextEntry(); + public void clear() { + RB_TREE_MAP.this.clear(); } + @Override + public int size() { + return RB_TREE_MAP.this.size; + } + + @Override + public boolean contains(Object o) { + if (!(o instanceof Map.Entry)) return false; + if(o instanceof MAP.Entry) + { + MAP.Entry KEY_VALUE_GENERIC_TYPE entry = (MAP.Entry KEY_VALUE_GENERIC_TYPE) o; +#if TYPE_OBJECT + if(entry.getKey() == null && comparator() == null) return false; +#endif + KEY_TYPE key = entry.ENTRY_KEY(); + Node KEY_VALUE_GENERIC_TYPE node = findNode(key); + return node != null && VALUE_EQUALS(entry.ENTRY_VALUE(), node.ENTRY_VALUE()); + } + Map.Entry entry = (Map.Entry) o; + if(entry.getKey() == null && comparator() == null) return false; +#if !TYPE_OBJECT + if(!(entry.getKey() instanceof CLASS_TYPE)) return false; +#endif + CLASS_TYPE key = (CLASS_TYPE)entry.getKey(); + Node KEY_VALUE_GENERIC_TYPE node = findNode(key); + return node != null && Objects.equals(entry.getValue(), VALUE_TO_OBJ(node.ENTRY_VALUE())); + } + + @Override + public boolean remove(Object o) { + if (!(o instanceof Map.Entry)) return false; + if(o instanceof MAP.Entry) + { + MAP.Entry KEY_VALUE_GENERIC_TYPE entry = (MAP.Entry KEY_VALUE_GENERIC_TYPE) o; + KEY_TYPE key = entry.ENTRY_KEY(); + Node KEY_VALUE_GENERIC_TYPE node = findNode(key); + if (node != null && VALUE_EQUALS(entry.ENTRY_VALUE(), node.ENTRY_VALUE())) { + removeNode(node); + return true; + } + return false; + } + Map.Entry entry = (Map.Entry) o; + CLASS_TYPE key = (CLASS_TYPE)entry.getKey(); + Node KEY_VALUE_GENERIC_TYPE node = findNode(key); + if (node != null && Objects.equals(entry.getValue(), VALUE_TO_OBJ(node.ENTRY_VALUE()))) { + removeNode(node); + return true; + } + return false; + } + + @Override + public void forEach(Consumer action) { + Objects.requireNonNull(action); + for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) + action.accept(new BasicEntryKV_BRACES(entry.key, entry.value)); + } + + @Override + public void forEach(E input, ObjectObjectConsumer action) { + Objects.requireNonNull(action); + for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) + action.accept(input, new BasicEntryKV_BRACES(entry.key, entry.value)); + } + + @Override + public boolean matchesAny(Object2BooleanFunction filter) { + Objects.requireNonNull(filter); + if(size() <= 0) return false; + BasicEntry KEY_VALUE_GENERIC_TYPE subEntry = new BasicEntryKV_BRACES(); + for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { + subEntry.set(entry.key, entry.value); + if(filter.getBoolean(subEntry)) return true; + } + return false; + } + + @Override + public boolean matchesNone(Object2BooleanFunction filter) { + Objects.requireNonNull(filter); + if(size() <= 0) return true; + BasicEntry KEY_VALUE_GENERIC_TYPE subEntry = new BasicEntryKV_BRACES(); + for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { + subEntry.set(entry.key, entry.value); + if(filter.getBoolean(subEntry)) return false; + } + return true; + } + + @Override + public boolean matchesAll(Object2BooleanFunction filter) { + Objects.requireNonNull(filter); + if(size() <= 0) return true; + BasicEntry KEY_VALUE_GENERIC_TYPE subEntry = new BasicEntryKV_BRACES(); + for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { + subEntry.set(entry.key, entry.value); + if(!filter.getBoolean(subEntry)) return false; + } + return true; + } + + @Override + public E reduce(E identity, BiFunction operator) { + Objects.requireNonNull(operator); + E state = identity; + for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { + state = operator.apply(state, new BasicEntryKV_BRACES(entry.key, entry.value)); + } + return state; + } + + @Override + public MAP.Entry KEY_VALUE_GENERIC_TYPE reduce(ObjectObjectUnaryOperator operator) { + Objects.requireNonNull(operator); + MAP.Entry KEY_VALUE_GENERIC_TYPE state = null; + boolean empty = true; + for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { + if(empty) { + empty = false; + state = new BasicEntryKV_BRACES(entry.key, entry.value); + continue; + } + state = operator.apply(state, new BasicEntryKV_BRACES(entry.key, entry.value)); + } + return state; + } + + @Override + public MAP.Entry KEY_VALUE_GENERIC_TYPE findFirst(Object2BooleanFunction filter) { + Objects.requireNonNull(filter); + if(size() <= 0) return null; + BasicEntry KEY_VALUE_GENERIC_TYPE subEntry = new BasicEntryKV_BRACES(); + for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { + subEntry.set(entry.key, entry.value); + if(filter.getBoolean(subEntry)) return subEntry; + } + return null; + } + + @Override + public int count(Object2BooleanFunction filter) { + Objects.requireNonNull(filter); + if(size() <= 0) return 0; + int result = 0; + BasicEntry KEY_VALUE_GENERIC_TYPE subEntry = new BasicEntryKV_BRACES(); + for(Node KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { + subEntry.set(entry.key, entry.value); + if(filter.getBoolean(subEntry)) result++; + } + return result; + } + } + + class DescendingKeyIterator extends MapEntryIterator implements BI_ITERATOR KEY_GENERIC_TYPE + { + public DescendingKeyIterator(Node KEY_VALUE_GENERIC_TYPE first) { + super(first); + } + + @Override + public KEY_TYPE PREVIOUS() { + if(!hasPrevious()) throw new NoSuchElementException(); + return nextEntry().key; + } + + @Override + public KEY_TYPE NEXT() { + if(!hasNext()) throw new NoSuchElementException(); + return previousEntry().key; + } + } + + class AscendingMapEntryIterator extends MapEntryIterator implements ObjectBidirectionalIterator + { + public AscendingMapEntryIterator(Node KEY_VALUE_GENERIC_TYPE first) + { + super(first); + } + @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE previous() { + if(!hasPrevious()) throw new NoSuchElementException(); return previousEntry(); } - + @Override - public void set(MAP.Entry KEY_VALUE_GENERIC_TYPE e) { throw new UnsupportedOperationException(); } - - @Override - public void add(MAP.Entry KEY_VALUE_GENERIC_TYPE e) { throw new UnsupportedOperationException(); } + public MAP.Entry KEY_VALUE_GENERIC_TYPE next() { + if(!hasNext()) throw new NoSuchElementException(); + return nextEntry(); + } } - final class KeyIterator extends MapEntryIterator implements LIST_ITERATOR KEY_GENERIC_TYPE { - - public KeyIterator(boolean descending) { - super(descending); + class AscendingValueIterator extends MapEntryIterator implements VALUE_BI_ITERATOR VALUE_GENERIC_TYPE + { + public AscendingValueIterator(Node KEY_VALUE_GENERIC_TYPE first) { + super(first); } - - public KeyIterator(Entry KEY_VALUE_GENERIC_TYPE entry) { - super(entry); - } - - @Override - public KEY_TYPE PREVIOUS() { return previousEntry().key; } - @Override - public KEY_TYPE NEXT() { return nextEntry().key; } - - @Override - public void set(KEY_TYPE e) { throw new UnsupportedOperationException(); } - @Override - public void add(KEY_TYPE e) { throw new UnsupportedOperationException(); } - } - - final class ValueIterator extends MapEntryIterator implements VALUE_LIST_ITERATOR VALUE_GENERIC_TYPE { - - public ValueIterator(boolean descending) { - super(descending); - } - - public ValueIterator(Entry KEY_VALUE_GENERIC_TYPE entry) { - super(entry); - } - + @Override public VALUE_TYPE VALUE_PREVIOUS() { + if(!hasPrevious()) throw new NoSuchElementException(); return previousEntry().value; } - + @Override public VALUE_TYPE VALUE_NEXT() { + if(!hasNext()) throw new NoSuchElementException(); return nextEntry().value; } - - @Override - public void set(VALUE_TYPE e) { throw new UnsupportedOperationException(); } - - @Override - public void add(VALUE_TYPE e) { throw new UnsupportedOperationException(); } } - abstract class MapEntryIterator { - Entry KEY_VALUE_GENERIC_TYPE next; - Entry KEY_VALUE_GENERIC_TYPE previous; - Entry KEY_VALUE_GENERIC_TYPE current; - int index = 0; - - public MapEntryIterator(boolean descending) { - if(descending) previous = last; - else next = first; + class AscendingKeyIterator extends MapEntryIterator implements BI_ITERATOR KEY_GENERIC_TYPE + { + public AscendingKeyIterator(Node KEY_VALUE_GENERIC_TYPE first) { + super(first); } + + @Override + public KEY_TYPE PREVIOUS() { + if(!hasPrevious()) throw new NoSuchElementException(); + return previousEntry().key; + } + + @Override + public KEY_TYPE NEXT() { + if(!hasNext()) throw new NoSuchElementException(); + return nextEntry().key; + } + } + + abstract class MapEntryIterator + { + boolean wasMoved = false; + Node KEY_VALUE_GENERIC_TYPE lastReturned; + Node KEY_VALUE_GENERIC_TYPE next; - public MapEntryIterator(Entry KEY_VALUE_GENERIC_TYPE entry) { - next = entry; - previous = entry.previous(); + public MapEntryIterator(Node KEY_VALUE_GENERIC_TYPE first) + { + next = first; } public boolean hasNext() { - return next != null; + return next != null; + } + + protected Node KEY_VALUE_GENERIC_TYPE nextEntry() { + lastReturned = next; + Node KEY_VALUE_GENERIC_TYPE result = next; + next = next.next(); + wasMoved = true; + return result; } public boolean hasPrevious() { - return previous != null; + return next != null; } - public int nextIndex() { - return index; - } - - public int previousIndex() { - return index - 1; - } - - protected void updateNext() { - next = current.next(); - } - - protected void updatePrevious() { - previous = current.previous(); - } - - public Entry KEY_VALUE_GENERIC_TYPE nextEntry() { - if(!hasNext()) throw new NoSuchElementException(); - current = previous = next; - updateNext(); - index++; - return current; - } - - public Entry KEY_VALUE_GENERIC_TYPE previousEntry() { - if(!hasPrevious()) throw new NoSuchElementException(); - current = next = previous; - updatePrevious(); - index--; - return current; + protected Node KEY_VALUE_GENERIC_TYPE previousEntry() { + lastReturned = next; + Node KEY_VALUE_GENERIC_TYPE result = next; + next = next.previous(); + wasMoved = false; + return result; } public void remove() { - if(current == null) throw new IllegalStateException(); - if(current == previous) index--; - updateNext(); - updatePrevious(); - if(current.needsSuccessor()) next = current; - removeNode(current); - current = null; + if(lastReturned == null) throw new IllegalStateException(); + if(wasMoved && lastReturned.needsSuccessor()) next = lastReturned; + removeNode(lastReturned); + lastReturned = null; } } - private static final class Entry KEY_VALUE_GENERIC_TYPE implements MAP.Entry KEY_VALUE_GENERIC_TYPE + private static final class Node KEY_VALUE_GENERIC_TYPE implements MAP.Entry KEY_VALUE_GENERIC_TYPE { static final int BLACK = 1; KEY_TYPE key; VALUE_TYPE value; int state; - Entry KEY_VALUE_GENERIC_TYPE parent; - Entry KEY_VALUE_GENERIC_TYPE left; - Entry KEY_VALUE_GENERIC_TYPE right; + Node KEY_VALUE_GENERIC_TYPE parent; + Node KEY_VALUE_GENERIC_TYPE left; + Node KEY_VALUE_GENERIC_TYPE right; - Entry(KEY_TYPE key, VALUE_TYPE value, Entry KEY_VALUE_GENERIC_TYPE parent) { + Node(KEY_TYPE key, VALUE_TYPE value, Node KEY_VALUE_GENERIC_TYPE parent) { this.key = key; this.value = value; this.parent = parent; } - Entry KEY_VALUE_GENERIC_TYPE copy() { - Entry KEY_VALUE_GENERIC_TYPE entry = new EntryKV_BRACES(key, value, null); + Node KEY_VALUE_GENERIC_TYPE copy() { + Node KEY_VALUE_GENERIC_TYPE entry = new NodeKV_BRACES(key, value, null); entry.state = state; if(left != null) { - Entry KEY_VALUE_GENERIC_TYPE newLeft = left.copy(); + Node KEY_VALUE_GENERIC_TYPE newLeft = left.copy(); entry.left = newLeft; newLeft.parent = entry; } if(right != null) { - Entry KEY_VALUE_GENERIC_TYPE newRight = right.copy(); + Node KEY_VALUE_GENERIC_TYPE newRight = right.copy(); entry.right = newRight; newRight.parent = entry; } return entry; } + public BasicEntry KEY_VALUE_GENERIC_TYPE export() { + return new BasicEntryKV_BRACES(key, value); + } + @Override public KEY_TYPE ENTRY_KEY() { return key; @@ -2735,7 +2780,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G boolean needsSuccessor() { return left != null && right != null; } - boolean replace(Entry KEY_VALUE_GENERIC_TYPE entry) { + boolean replace(Node KEY_VALUE_GENERIC_TYPE entry) { if(entry != null) entry.parent = parent; if(parent != null) { if(parent.left == this) parent.left = entry; @@ -2744,14 +2789,14 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G return parent == null; } - Entry KEY_VALUE_GENERIC_TYPE next() { + Node KEY_VALUE_GENERIC_TYPE next() { if(right != null) { - Entry KEY_VALUE_GENERIC_TYPE parent = right; + Node KEY_VALUE_GENERIC_TYPE parent = right; while(parent.left != null) parent = parent.left; return parent; } - Entry KEY_VALUE_GENERIC_TYPE parent = this.parent; - Entry KEY_VALUE_GENERIC_TYPE control = this; + Node KEY_VALUE_GENERIC_TYPE parent = this.parent; + Node KEY_VALUE_GENERIC_TYPE control = this; while(parent != null && control == parent.right) { control = parent; parent = parent.parent; @@ -2759,14 +2804,14 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G return parent; } - Entry KEY_VALUE_GENERIC_TYPE previous() { + Node KEY_VALUE_GENERIC_TYPE previous() { if(left != null) { - Entry KEY_VALUE_GENERIC_TYPE parent = left; + Node KEY_VALUE_GENERIC_TYPE parent = left; while(parent.right != null) parent = parent.right; return parent; } - Entry KEY_VALUE_GENERIC_TYPE parent = this.parent; - Entry KEY_VALUE_GENERIC_TYPE control = this; + Node KEY_VALUE_GENERIC_TYPE parent = this.parent; + Node KEY_VALUE_GENERIC_TYPE control = this; while(parent != null && control == parent.left) { control = parent; parent = parent.parent; diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/interfaces/NavigableMap.template b/src/builder/resources/speiger/assets/collections/templates/maps/interfaces/NavigableMap.template index 5e6bfb14..644ff566 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/interfaces/NavigableMap.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/interfaces/NavigableMap.template @@ -207,13 +207,13 @@ public interface NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE extends SORTED_MAP KEY_VAL default NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE tailMap(CLASS_TYPE fromKey) { return tailMap(OBJ_TO_KEY(fromKey), true); } #else @Override - default MAP.Entry KEY_VALUE_GENERIC_TYPE lowerEntry(CLASS_TYPE key) { return lowerEntry(OBJ_TO_KEY(key)); } + MAP.Entry KEY_VALUE_GENERIC_TYPE lowerEntry(CLASS_TYPE key); @Override - default MAP.Entry KEY_VALUE_GENERIC_TYPE floorEntry(CLASS_TYPE key) { return floorEntry(OBJ_TO_KEY(key)); } + MAP.Entry KEY_VALUE_GENERIC_TYPE floorEntry(CLASS_TYPE key); @Override - default MAP.Entry KEY_VALUE_GENERIC_TYPE ceilingEntry(CLASS_TYPE key) { return ceilingEntry(OBJ_TO_KEY(key)); } + MAP.Entry KEY_VALUE_GENERIC_TYPE ceilingEntry(CLASS_TYPE key); @Override - default MAP.Entry KEY_VALUE_GENERIC_TYPE higherEntry(CLASS_TYPE key) { return higherEntry(OBJ_TO_KEY(key)); } + MAP.Entry KEY_VALUE_GENERIC_TYPE higherEntry(CLASS_TYPE key); @Override public NAVIGABLE_MAP KEY_VALUE_GENERIC_TYPE subMap(CLASS_TYPE fromKey, boolean fromInclusive, CLASS_TYPE toKey, boolean toInclusive); diff --git a/src/builder/resources/speiger/assets/collections/templates/sets/AVLTreeSet.template b/src/builder/resources/speiger/assets/collections/templates/sets/AVLTreeSet.template index 36029657..5488d208 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/AVLTreeSet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/AVLTreeSet.template @@ -1,6 +1,7 @@ package speiger.src.collections.PACKAGE.sets; import java.util.Collection; +import java.util.Collections; #if TYPE_OBJECT import java.util.Comparator; import java.util.function.Consumer; @@ -19,7 +20,6 @@ import speiger.src.collections.PACKAGE.functions.function.PREDICATE; import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR; import speiger.src.collections.PACKAGE.collections.COLLECTION; import speiger.src.collections.PACKAGE.collections.ITERATOR; -import speiger.src.collections.PACKAGE.lists.LIST_ITERATOR; #if !TYPE_OBJECT import speiger.src.collections.PACKAGE.utils.ITERATORS; #endif @@ -557,7 +557,11 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE @Override public KEY_TYPE POLL_FIRST_KEY() { - if(tree == null) throw new NoSuchElementException(); +#if TYPE_OBJECT + if(tree == null) return null; +#else + if(tree == null) return getDefaultMinValue(); +#endif KEY_TYPE value = first.key; removeNode(first); return value; @@ -565,7 +569,11 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE @Override public KEY_TYPE POLL_LAST_KEY() { - if(tree == null) throw new NoSuchElementException(); +#if TYPE_OBJECT + if(tree == null) return null; +#else + if(tree == null) return getDefaultMaxValue(); +#endif KEY_TYPE value = last.key; removeNode(last); return value; @@ -601,16 +609,16 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE 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 AscendingSetIterator(first); } @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); + return entry == null ? null : new AscendingSetIterator(entry); } @Override - public BI_ITERATOR KEY_GENERIC_TYPE descendingIterator() { return new SetIterator(true); } + public BI_ITERATOR KEY_GENERIC_TYPE descendingIterator() { return new DescendingSetIterator(last); } @Override public NAVIGABLE_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, boolean fromInclusive, KEY_TYPE toElement, boolean toInclusive) { @@ -743,158 +751,174 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE } } - private static class AscendingSubSet KEY_GENERIC_TYPE extends SubSet KEY_GENERIC_TYPE + 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); + public AscendingSubSet(AVL_TREE_SET KEY_GENERIC_TYPE set, boolean fromStart, KEY_TYPE lo, boolean loInclusive, boolean toEnd, KEY_TYPE hi, boolean hiInclusive) + { + super(set, fromStart, lo, loInclusive, toEnd, hi, 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"); + public COMPARATOR KEY_GENERIC_TYPE comparator() { return set.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 out of range"); + if(!inRange(toElement, toInclusive)) throw new IllegalArgumentException("toElement out of 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); + public NAVIGABLE_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement, boolean inclusive) + { + if(!inRange(toElement, inclusive)) throw new IllegalArgumentException("toElement out of range"); + return new AscendingSubSetBRACES(set, fromStart, lo, 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 out of range"); + return new AscendingSubSetBRACES(set, false, fromElement, inclusive, toEnd, hi, hiInclusive); } @Override - public BI_ITERATOR KEY_GENERIC_TYPE descendingIterator() { return new SubSetIterator(findHighest()); } + public BI_ITERATOR KEY_GENERIC_TYPE iterator() + { + return new AscendingSubSetIterator(absLowest(), absHighFence(), absLowFence()); + } + + @Override + public BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement) + { + return new AscendingSubSetIterator(absLower(fromElement), absHighFence(), absLowFence()); + } + + @Override + public BI_ITERATOR KEY_GENERIC_TYPE descendingIterator() + { + return new DescendingSubSetIterator(absHighest(), absLowFence(), absHighFence()); + } @Override - public NAVIGABLE_SET KEY_GENERIC_TYPE descendingSet() { - return new DescendingSubSetBRACES(set, fromStart, start, loInclusive, toEnd, end, hiInclusive); + public NAVIGABLE_SET KEY_GENERIC_TYPE descendingSet() + { + if(inverse == null) inverse = new DescendingSubSetBRACES(set, fromStart, lo, loInclusive, toEnd, hi, hiInclusive); + return inverse; } + + @Override + protected Entry KEY_GENERIC_TYPE subLowest() { return absLowest(); } + @Override + protected Entry KEY_GENERIC_TYPE subHighest() { return absHighest(); } + @Override + protected Entry KEY_GENERIC_TYPE subCeiling(KEY_TYPE key) { return absCeiling(key); } + @Override + protected Entry KEY_GENERIC_TYPE subHigher(KEY_TYPE key) { return absHigher(key); } + @Override + protected Entry KEY_GENERIC_TYPE subFloor(KEY_TYPE key) { return absFloor(key); } + @Override + protected Entry KEY_GENERIC_TYPE subLower(KEY_TYPE key) { return absLower(key); } + @Override + protected Entry KEY_GENERIC_TYPE start() { return absLowest(); } + @Override + protected Entry KEY_GENERIC_TYPE next(Entry KEY_GENERIC_TYPE entry) { return entry.next(); } } - private static class DescendingSubSet KEY_GENERIC_TYPE extends SubSet KEY_GENERIC_TYPE + 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(); - if(comparator != null) comparator = comparator.reversed(); + + public DescendingSubSet(AVL_TREE_SET KEY_GENERIC_TYPE set, boolean fromStart, KEY_TYPE lo, boolean loInclusive, boolean toEnd, KEY_TYPE hi, boolean hiInclusive) + { + super(set, fromStart, lo, loInclusive, toEnd, hi, hiInclusive); +#if TYPE_OBJECT + comparator = Collections.reverseOrder(set.comparator()); +#else + comparator = set.comparator() == null ? COMPARATOR.of(Collections.reverseOrder()) : set.comparator().reversed(); +#endif } @Override public COMPARATOR KEY_GENERIC_TYPE comparator() { return comparator; } + public KEY_TYPE getDefaultMaxValue() { return super.getDefaultMinValue(); } + + public KEY_TYPE getDefaultMinValue() { return super.getDefaultMaxValue(); } + @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); + if(!inRange(fromElement, fromInclusive)) throw new IllegalArgumentException("fromElement out of range"); + if(!inRange(toElement, toInclusive)) throw new IllegalArgumentException("toElement out of range"); + return new DescendingSubSetBRACES(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 DescendingSubSetBRACES(set, false, toElement, inclusive, toEnd, end, hiInclusive); + public NAVIGABLE_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement, boolean inclusive) + { + if(!inRange(toElement, inclusive)) throw new IllegalArgumentException("toElement out of range"); + return new DescendingSubSetBRACES(set, false, toElement, inclusive, toEnd, hi, 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); + if(!inRange(fromElement, inclusive)) throw new IllegalArgumentException("fromElement out of range"); + return new DescendingSubSetBRACES(set, fromStart, lo, loInclusive, false, fromElement, inclusive); } - - protected Entry KEY_GENERIC_TYPE next(Entry KEY_GENERIC_TYPE entry) { return entry.previous(); } - protected Entry KEY_GENERIC_TYPE start() { return findHighest(); } - - @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); + public BI_ITERATOR KEY_GENERIC_TYPE iterator() { + return new DescendingSubSetIterator(absHighest(), absLowFence(), absHighFence()); } - + @Override - public BI_ITERATOR KEY_GENERIC_TYPE descendingIterator() { return new DescendingSubIterator(findLowest()); } - + public BI_ITERATOR KEY_GENERIC_TYPE descendingIterator() { + return new AscendingSubSetIterator(absLowest(), absHighFence(), absLowFence()); + } + @Override public NAVIGABLE_SET KEY_GENERIC_TYPE descendingSet() { - return new AscendingSubSetBRACES(set, fromStart, start, loInclusive, toEnd, end, hiInclusive); + if(inverse == null) inverse = new AscendingSubSetBRACES(set, fromStart, lo, loInclusive, toEnd, hi, hiInclusive); + return inverse; } - 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; - } + @Override + public BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement) { + return new DescendingSubSetIterator(absHigher(fromElement), absLowFence(), absHighFence()); } + + @Override + protected Entry KEY_GENERIC_TYPE subLowest() { return absHighest(); } + @Override + protected Entry KEY_GENERIC_TYPE subHighest() { return absLowest(); } + @Override + protected Entry KEY_GENERIC_TYPE subCeiling(KEY_TYPE key) { return absFloor(key); } + @Override + protected Entry KEY_GENERIC_TYPE subHigher(KEY_TYPE key) { return absLower(key); } + @Override + protected Entry KEY_GENERIC_TYPE subFloor(KEY_TYPE key) { return absCeiling(key); } + @Override + protected Entry KEY_GENERIC_TYPE subLower(KEY_TYPE key) { return absHigher(key); } + @Override + protected Entry KEY_GENERIC_TYPE start() { return absHighest(); } + @Override + protected Entry KEY_GENERIC_TYPE next(Entry KEY_GENERIC_TYPE entry) { return entry.previous(); } } - private static abstract class SubSet KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE implements NAVIGABLE_SET KEY_GENERIC_TYPE + 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; + final AVL_TREE_SET KEY_GENERIC_TYPE set; + final KEY_TYPE lo, hi; + final boolean fromStart, toEnd; + final boolean loInclusive, hiInclusive; + NAVIGABLE_SET KEY_GENERIC_TYPE inverse; - SubSet(AVL_TREE_SET KEY_GENERIC_TYPE set, boolean fromStart, KEY_TYPE start, boolean loInclusive, boolean toEnd, KEY_TYPE end, boolean hiInclusive) { + public SubSet(AVL_TREE_SET KEY_GENERIC_TYPE set, boolean fromStart, KEY_TYPE lo, boolean loInclusive, boolean toEnd, KEY_TYPE hi, boolean hiInclusive) + { this.set = set; - this.start = start; - this.end = end; + this.lo = lo; + this.hi = hi; this.fromStart = fromStart; this.toEnd = toEnd; this.loInclusive = loInclusive; @@ -903,13 +927,13 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE #if !TYPE_OBJECT @Override - public void setDefaultMaxValue(KEY_TYPE value) { throw new UnsupportedOperationException(); } + public void setDefaultMaxValue(KEY_TYPE value) { set.setDefaultMaxValue(value); } @Override public KEY_TYPE getDefaultMaxValue() { return set.getDefaultMaxValue(); } @Override - public void setDefaultMinValue(KEY_TYPE value) { throw new UnsupportedOperationException(); } + public void setDefaultMinValue(KEY_TYPE value) { set.setDefaultMinValue(value); } @Override public KEY_TYPE getDefaultMinValue() { return set.getDefaultMinValue(); } @@ -920,80 +944,75 @@ public class AVL_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 tooLow(KEY_TYPE key) { + if (!fromStart) { + int c = set.compare(key, lo); + if (c < 0 || (c == 0 && !loInclusive)) return true; + } + return false; + } + + boolean tooHigh(KEY_TYPE key) { + if (!toEnd) { + int c = set.compare(key, hi); + if (c > 0 || (c == 0 && !hiInclusive)) return true; + } + return false; + } + 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 inClosedRange(KEY_TYPE key) { return (fromStart || set.compare(key, lo) >= 0) && (toEnd || set.compare(hi, key) >= 0); } boolean inRange(KEY_TYPE key, boolean inclusive) { return inclusive ? inRange(key) : inClosedRange(key); } - protected Entry KEY_GENERIC_TYPE next(Entry KEY_GENERIC_TYPE entry) { return entry.next(); } - protected Entry KEY_GENERIC_TYPE start() { return findLowest(); } - @Override - public boolean addAndMoveToFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); } + protected abstract Entry KEY_GENERIC_TYPE subLowest(); + protected abstract Entry KEY_GENERIC_TYPE subHighest(); + protected abstract Entry KEY_GENERIC_TYPE subCeiling(KEY_TYPE key); + protected abstract Entry KEY_GENERIC_TYPE subHigher(KEY_TYPE key); + protected abstract Entry KEY_GENERIC_TYPE subFloor(KEY_TYPE key); + protected abstract Entry KEY_GENERIC_TYPE subLower(KEY_TYPE key); + protected KEY_TYPE lowKeyOrNull(Entry KEY_GENERIC_TYPE entry) { return entry == null ? getDefaultMinValue() : entry.key; } + protected KEY_TYPE highKeyOrNull(Entry KEY_GENERIC_TYPE entry) { return entry == null ? getDefaultMaxValue() : entry.key; } + protected abstract Entry KEY_GENERIC_TYPE start(); + protected abstract Entry KEY_GENERIC_TYPE next(Entry KEY_GENERIC_TYPE entry); - @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 abstract BI_ITERATOR KEY_GENERIC_TYPE iterator(); - - @Override - public KEY_TYPE FIRST_KEY() { - Entry KEY_GENERIC_TYPE entry = findLowest(); - return entry == null ? getDefaultMaxValue() : entry.key; + final Entry KEY_GENERIC_TYPE absLowest() { + Entry KEY_GENERIC_TYPE e = (fromStart ? set.first : (loInclusive ? set.findCeilingNode(lo) : set.findHigherNode(lo))); + return (e == null || tooHigh(e.key)) ? null : e; } - 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; + final Entry KEY_GENERIC_TYPE absHighest() { + Entry KEY_GENERIC_TYPE e = (toEnd ? set.last : (hiInclusive ? set.findFloorNode(hi) : set.findLowerNode(hi))); + return (e == null || tooLow(e.key)) ? null : e; } - @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; + final Entry KEY_GENERIC_TYPE absCeiling(KEY_TYPE key) { + if (tooLow(key)) return absLowest(); + Entry KEY_GENERIC_TYPE e = set.findCeilingNode(key); + return (e == null || tooHigh(e.key)) ? null : e; } - 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; + final Entry KEY_GENERIC_TYPE absHigher(KEY_TYPE key) { + if (tooLow(key)) return absLowest(); + Entry KEY_GENERIC_TYPE e = set.findHigherNode(key); + return (e == null || tooHigh(e.key)) ? null : e; } - - @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(); + + final Entry KEY_GENERIC_TYPE absFloor(KEY_TYPE key) { + if (tooHigh(key)) return absHighest(); + Entry KEY_GENERIC_TYPE e = set.findFloorNode(key); + return (e == null || tooLow(e.key)) ? null : e; } + final Entry KEY_GENERIC_TYPE absLower(KEY_TYPE key) { + if (tooHigh(key)) return absHighest(); + Entry KEY_GENERIC_TYPE e = set.findLowerNode(key); + return (e == null || tooLow(e.key)) ? null : e; + } + + final Entry KEY_GENERIC_TYPE absHighFence() { return (toEnd ? null : (hiInclusive ? set.findHigherNode(hi) : set.findCeilingNode(hi))); } + final Entry KEY_GENERIC_TYPE absLowFence() { return (fromStart ? null : (loInclusive ? set.findLowerNode(lo) : set.findFloorNode(lo))); } + @Override public boolean add(KEY_TYPE o) { if(!inRange(o)) throw new IllegalArgumentException("Key is out of range"); @@ -1017,7 +1036,7 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE KEY_TYPE o = OBJ_TO_KEY(((CLASS_TYPE)e)); return inRange(o) && set.contains(o); } - + @Override public boolean remove(Object e) { KEY_TYPE o = OBJ_TO_KEY(((CLASS_TYPE)e)); @@ -1025,50 +1044,85 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE } @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; + public boolean isEmpty() { + if(fromStart && toEnd) return set.isEmpty(); + Entry KEY_GENERIC_TYPE n = absLowest(); + return n == null || tooHigh(n.key); } @Override public int size() { - return fromStart && toEnd ? set.size() : iterator().skip(Integer.MAX_VALUE); + if(fromStart && toEnd) return set.size(); + int i = 0; + for(ITERATOR KEY_GENERIC_TYPE iter = iterator();iter.hasNext();iter.NEXT(),i++); + return i; } + @Override + public KEY_TYPE lower(KEY_TYPE e) { + return lowKeyOrNull(subLower(e)); + } + + @Override + public KEY_TYPE floor(KEY_TYPE e) { + return lowKeyOrNull(subFloor(e)); + } + + @Override + public KEY_TYPE ceiling(KEY_TYPE e) { + return highKeyOrNull(subCeiling(e)); + } + + @Override + public KEY_TYPE higher(KEY_TYPE e) { + return highKeyOrNull(subHigher(e)); + } + + @Override + public KEY_TYPE POLL_FIRST_KEY() { + Entry KEY_GENERIC_TYPE entry = subLowest(); + if(entry != null) { + KEY_TYPE result = entry.key; + set.removeNode(entry); + return result; + } + return getDefaultMinValue(); + } + + @Override + public KEY_TYPE POLL_LAST_KEY() { + Entry KEY_GENERIC_TYPE entry = subHighest(); + if(entry != null) { + KEY_TYPE result = entry.key; + set.removeNode(entry); + return result; + } + return getDefaultMaxValue(); + } + + @Override + public KEY_TYPE FIRST_KEY() { + Entry KEY_GENERIC_TYPE entry = subLowest(); + if(entry == null) throw new NoSuchElementException(); + return entry.key; + } + + @Override + public KEY_TYPE LAST_KEY() { + Entry KEY_GENERIC_TYPE entry = subHighest(); + if(entry == null) throw new NoSuchElementException(); + return entry.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 SubSet KEY_GENERIC_TYPE copy() { throw new UnsupportedOperationException(); } @@ -1172,171 +1226,207 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE return result; } - class SubSetIterator implements LIST_ITERATOR KEY_GENERIC_TYPE + class AscendingSubSetIterator implements BI_ITERATOR KEY_GENERIC_TYPE { - Entry KEY_GENERIC_TYPE previous; + Entry KEY_GENERIC_TYPE lastReturned; Entry KEY_GENERIC_TYPE next; - Entry KEY_GENERIC_TYPE current; - int index = 0; + boolean unboundForwardFence; + boolean unboundBackwardFence; + KEY_TYPE forwardFence; + KEY_TYPE backwardFence; - public SubSetIterator(Entry KEY_GENERIC_TYPE entry) { - next = entry; - previous = entry == null ? null : entry.previous(); + public AscendingSubSetIterator(Entry KEY_GENERIC_TYPE first, Entry KEY_GENERIC_TYPE forwardFence, Entry KEY_GENERIC_TYPE backwardFence) + { + next = first; + this.forwardFence = forwardFence == null ? EMPTY_KEY_VALUE : forwardFence.key; + this.backwardFence = backwardFence == null ? EMPTY_KEY_VALUE : backwardFence.key; + unboundForwardFence = forwardFence == null; + unboundBackwardFence = backwardFence == null; } @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; + return next != null && (unboundForwardFence || next.key != forwardFence); } @Override public KEY_TYPE NEXT() { if(!hasNext()) throw new NoSuchElementException(); - current = previous = next; - updateNext(); - index++; - return current.key; + lastReturned = next; + KEY_TYPE result = next.key; + next = next.next(); + return result; + } + + @Override + public boolean hasPrevious() { + return next != null && (unboundBackwardFence || next.key != backwardFence); } @Override public KEY_TYPE PREVIOUS() { if(!hasPrevious()) throw new NoSuchElementException(); - current = next = previous; - updatePrevious(); - index--; - return current.key; + lastReturned = next; + KEY_TYPE result = next.key; + next = next.previous(); + return result; } @Override public void remove() { - if(current == null) throw new IllegalStateException(); - if(current == previous) index--; - updateNext(); - updatePrevious(); - set.removeNode(current); - current = null; + if(lastReturned == null) throw new IllegalStateException(); + if(lastReturned.needsSuccessor()) next = lastReturned; + set.removeNode(lastReturned); + lastReturned = null; + } + } + + class DescendingSubSetIterator implements BI_ITERATOR KEY_GENERIC_TYPE + { + Entry KEY_GENERIC_TYPE lastReturned; + Entry KEY_GENERIC_TYPE next; + boolean unboundForwardFence; + boolean unboundBackwardFence; + KEY_TYPE forwardFence; + KEY_TYPE backwardFence; + + public DescendingSubSetIterator(Entry KEY_GENERIC_TYPE first, Entry KEY_GENERIC_TYPE forwardFence, Entry KEY_GENERIC_TYPE backwardFence) + { + next = first; + this.forwardFence = forwardFence == null ? EMPTY_KEY_VALUE : forwardFence.key; + this.backwardFence = backwardFence == null ? EMPTY_KEY_VALUE : backwardFence.key; + unboundForwardFence = forwardFence == null; + unboundBackwardFence = backwardFence == null; } @Override - public void set(KEY_TYPE e){ throw new UnsupportedOperationException(); } + public boolean hasNext() { + return next != null && (unboundForwardFence || next.key != forwardFence); + } @Override - public void add(KEY_TYPE e) { throw new UnsupportedOperationException(); } + public KEY_TYPE NEXT() { + if(!hasNext()) throw new NoSuchElementException(); + lastReturned = next; + KEY_TYPE result = next.key; + next = next.previous(); + return result; + } + + @Override + public boolean hasPrevious() { + return next != null && (unboundBackwardFence || next.key != backwardFence); + } + + @Override + public KEY_TYPE PREVIOUS() { + if(!hasPrevious()) throw new NoSuchElementException(); + lastReturned = next; + KEY_TYPE result = next.key; + next = next.next(); + return result; + } + + @Override + public void remove() { + if(lastReturned == null) throw new IllegalStateException(); + set.removeNode(lastReturned); + lastReturned = null; + } } } - private class SetIterator implements LIST_ITERATOR KEY_GENERIC_TYPE + class AscendingSetIterator implements BI_ITERATOR KEY_GENERIC_TYPE { - Entry KEY_GENERIC_TYPE previous; + Entry KEY_GENERIC_TYPE lastReturned; 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(); + public AscendingSetIterator(Entry KEY_GENERIC_TYPE first) + { + next = first; } @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(); + return next != null; } @Override public KEY_TYPE NEXT() { if(!hasNext()) throw new NoSuchElementException(); - current = previous = next; - updateNext(); - index++; - return current.key; + lastReturned = next; + KEY_TYPE result = next.key; + next = next.next(); + return result; + } + + @Override + public boolean hasPrevious() { + return next != null; } @Override public KEY_TYPE PREVIOUS() { if(!hasPrevious()) throw new NoSuchElementException(); - current = next = previous; - updatePrevious(); - index--; - return current.key; + lastReturned = next; + KEY_TYPE result = next.key; + next = next.previous(); + return result; } @Override public void remove() { - if(current == null) throw new IllegalStateException(); - if(current == previous) index--; - updateNext(); - updatePrevious(); - if(current.needsSuccessor()) next = current; - removeNode(current); - current = null; + if(lastReturned == null) throw new IllegalStateException(); + if(lastReturned.needsSuccessor()) next = lastReturned; + removeNode(lastReturned); + lastReturned = null; + } + } + + class DescendingSetIterator implements BI_ITERATOR KEY_GENERIC_TYPE + { + Entry KEY_GENERIC_TYPE lastReturned; + Entry KEY_GENERIC_TYPE next; + + public DescendingSetIterator(Entry KEY_GENERIC_TYPE first) + { + next = first; } - - @Override - public void set(KEY_TYPE e){ throw new UnsupportedOperationException(); } @Override - public void add(KEY_TYPE e) { throw new UnsupportedOperationException(); } + public boolean hasNext() { + return next != null; + } + + @Override + public KEY_TYPE NEXT() { + if(!hasNext()) throw new NoSuchElementException(); + lastReturned = next; + KEY_TYPE result = next.key; + next = next.previous(); + return result; + } + + @Override + public boolean hasPrevious() { + return next != null; + } + + @Override + public KEY_TYPE PREVIOUS() { + if(!hasPrevious()) throw new NoSuchElementException(); + lastReturned = next; + KEY_TYPE result = next.key; + next = next.next(); + return result; + } + + @Override + public void remove() { + if(lastReturned == null) throw new IllegalStateException(); + removeNode(lastReturned); + lastReturned = null; + } } private static final class Entry KEY_GENERIC_TYPE diff --git a/src/builder/resources/speiger/assets/collections/templates/sets/ArraySet.template b/src/builder/resources/speiger/assets/collections/templates/sets/ArraySet.template index 59512546..e078dd6c 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/ArraySet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/ArraySet.template @@ -499,27 +499,30 @@ public class ARRAY_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE im throw new NoSuchElementException(); } + /** + * Unsupported for now. Implementation is buggy and does not support the Java Standard with these functions. + * It is a Unsorted Sorted Set. Thats why the SubSet implementation will be disabled until a better solution is found. + * To give a simple reason: LinkedHashSets are also not SortedSets even so they could be. + * @throws UnsupportedOperationException + */ @Override - public SORTED_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, KEY_TYPE toElement) { - int fromIndex = findIndex(fromElement); - int toIndex = findIndex(toElement); - if(fromIndex == -1 || toIndex == -1) throw new NoSuchElementException(); - return new SubSet(fromIndex, toIndex - fromIndex + 1); - } - + public SORTED_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, KEY_TYPE toElement) { throw new UnsupportedOperationException(); } + /** + * Unsupported for now. Implementation is buggy and does not support the Java Standard with these functions. + * It is a Unsorted Sorted Set. Thats why the SubSet implementation will be disabled until a better solution is found. + * To give a simple reason: LinkedHashSets are also not SortedSets even so they could be. + * @throws UnsupportedOperationException + */ @Override - public SORTED_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement) { - int toIndex = findIndex(toElement); - if(toIndex == -1) throw new NoSuchElementException(); - return new SubSet(0, toIndex+1); - } - + public SORTED_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement) { throw new UnsupportedOperationException(); } + /** + * Unsupported for now. Implementation is buggy and does not support the Java Standard with these functions. + * It is a Unsorted Sorted Set. Thats why the SubSet implementation will be disabled until a better solution is found. + * To give a simple reason: LinkedHashSets are also not SortedSets even so they could be. + * @throws UnsupportedOperationException + */ @Override - public SORTED_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement) { - int fromIndex = findIndex(fromElement); - if(fromIndex == -1) throw new NoSuchElementException(); - return new SubSet(fromIndex, size - fromIndex); - } + public SORTED_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement) { throw new UnsupportedOperationException(); } public ARRAY_SET KEY_GENERIC_TYPE copy() { ARRAY_SET KEY_GENERIC_TYPE set = new ARRAY_SETBRACES(); @@ -573,345 +576,346 @@ public class ARRAY_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE im return a; } - private class SubSet extends ABSTRACT_SET KEY_GENERIC_TYPE implements SORTED_SET KEY_GENERIC_TYPE { - int offset; - int length; - - SubSet(int offset, int length) { - this.offset = offset; - this.length = length; - } - - int end() { return offset+length; } - - @Override - public boolean add(KEY_TYPE o) { - int index = findIndex(o); - if(index == -1) { - if(data.length == size) data = Arrays.copyOf(data, size == 0 ? 2 : size * 2); - if(end() != size) System.arraycopy(data, end(), data, end()+1, size-(offset+length)); - data[end()] = o; - size++; - length++; - return true; - } - return false; - } - - @Override - public boolean addAndMoveToFirst(KEY_TYPE o) { - int index = findIndex(o); - if(index == -1) { - if(data.length == size) data = Arrays.copyOf(data, size == 0 ? 2 : size * 2); - System.arraycopy(data, offset, data, offset+1, size-offset); - data[offset] = o; - size++; - length++; - return true; - } - else if(index != 0) { - o = data[index]; - System.arraycopy(data, offset, data, offset+1, index-offset); - data[offset] = o; - } - return false; - } - - @Override - public boolean addAndMoveToLast(KEY_TYPE o) { - int index = findIndex(o); - if(index == -1) { - if(data.length == size) data = Arrays.copyOf(data, size == 0 ? 2 : size * 2); - System.arraycopy(data, end()+1, data, end(), size-end()); - data[end()] = o; - size++; - length++; - return true; - } - else if(index != 0) { - o = data[index]; - System.arraycopy(data, offset+1, data, offset, index-offset); - data[offset+length] = o; - } - return false; - } - - @Override - public boolean moveToFirst(KEY_TYPE o) { - int index = findIndex(o); - if(index > offset) { - o = data[index]; - System.arraycopy(data, offset, data, offset+1, index-offset); - data[offset] = o; - return true; - } - return false; - } - - @Override - public boolean moveToLast(KEY_TYPE o) { - int index = findIndex(o); - if(index != -1 && index < end()-1) { - o = data[index]; - System.arraycopy(data, index+1, data, index, end()-index-1); - data[end()-1] = o; - return true; - } - return false; - } - -#if !TYPE_OBJECT - @Override - public boolean contains(KEY_TYPE e) { - return findIndex(e) != -1; - } - -#endif - @Override - public boolean contains(Object e) { - return findIndex(e) != -1; - } - - @Override - public KEY_TYPE FIRST_KEY() { - if(length == 0) throw new NoSuchElementException(); - return data[offset]; - } - - @Override - public KEY_TYPE LAST_KEY() { - if(length == 0) throw new NoSuchElementException(); - return data[end()-1]; - } - -#if !TYPE_OBJECT - @Override - public boolean remove(KEY_TYPE o) { - int index = findIndex(o); - if(index != -1) { - size--; - length--; - if(index != size) System.arraycopy(data, index+1, data, index, size - index); - return true; - } - return false; - } - -#endif - @Override - public boolean remove(Object o) { - int index = findIndex(o); - if(index != -1) { - size--; - length--; - if(index != size) System.arraycopy(data, index+1, data, index, size - index); -#if TYPE_OBJECT - data[size] = EMPTY_KEY_VALUE; -#endif - return true; - } - return false; - } - - @Override - public KEY_TYPE POLL_FIRST_KEY() { - if(length == 0) throw new NoSuchElementException(); - size--; - length--; - KEY_TYPE result = data[offset]; - System.arraycopy(data, offset+1, data, offset, size-offset); -#if TYPE_OBJECT - data[size] = EMPTY_KEY_VALUE; -#endif - return result; - } - - @Override - public KEY_TYPE POLL_LAST_KEY() { - if(length == 0) throw new NoSuchElementException(); - KEY_TYPE result = data[offset+length]; - size--; - length--; - System.arraycopy(data, end()+1, data, end(), size-end()); -#if TYPE_OBJECT - data[size] = EMPTY_KEY_VALUE; -#endif - return result; - } - - @Override - public COMPARATOR KEY_GENERIC_TYPE comparator() { - return null; - } - - @Override - public BI_ITERATOR KEY_GENERIC_TYPE iterator() { - return new SetIterator(offset); - } - - @Override - public BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement) { - int index = findIndex(fromElement); - 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); - int toIndex = findIndex(toElement); - if(fromIndex == -1 || toIndex == -1) throw new NoSuchElementException(); - return new SubSet(fromIndex, toIndex - fromIndex + 1); - } - - @Override - public SORTED_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement) { - int toIndex = findIndex(toElement); - if(toIndex == -1) throw new NoSuchElementException(); - return new SubSet(0, toIndex+1); - } - - @Override - public SORTED_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement) { - int fromIndex = findIndex(fromElement); - if(fromIndex == -1) throw new NoSuchElementException(); - return new SubSet(fromIndex, size - fromIndex); - } - - @Override - public int size() { - return length; - } - -#if !TYPE_OBJECT - @Override - public KEY_TYPE[] TO_ARRAY(KEY_TYPE[] a) { - if(a == null || a.length < size()) return Arrays.copyOfRange(data, offset, end()); - System.arraycopy(data, offset, a, 0, size()); - return a; - } - -#endif - @Override - @Deprecated - public Object[] toArray() { - Object[] obj = new Object[size()]; - for(int i = 0;i E[] toArray(E[] a) { - if(a == null) a = (E[])new Object[size()]; - else if(a.length < size()) a = (E[])ObjectArrays.newArray(a.getClass().getComponentType(), size()); - for(int i = 0;i=0;i--) - if(KEY_EQUALS(data[offset+i], o)) return i + offset; - return -1; - } - -#endif - protected int findIndex(Object o) { - for(int i = length-1;i>=0;i--) - if(EQUALS_KEY_TYPE(data[offset+i], o)) return i + offset; - return -1; - } - - private class SetIterator implements LIST_ITERATOR KEY_GENERIC_TYPE { - int index; - int lastReturned = -1; - - public SetIterator(int index) { - this.index = index; - } - - @Override - public boolean hasNext() { - return index < size(); - } - - @Override - public KEY_TYPE NEXT() { - if(!hasNext()) throw new NoSuchElementException(); - lastReturned = index; - return data[index++]; - } - - @Override - public boolean hasPrevious() { - return index > 0; - } - - @Override - public KEY_TYPE PREVIOUS() { - if(!hasPrevious()) throw new NoSuchElementException(); - lastReturned = index; - return data[index--]; - } - - @Override - public int nextIndex() { - return index; - } - - @Override - public int previousIndex() { - return index-1; - } - - @Override - public void remove() { - if(lastReturned == -1) - throw new IllegalStateException(); - SubSet.this.remove(data[lastReturned]); - if(lastReturned < index) - index--; - lastReturned = -1; - } - - #if TYPE_OBJECT - @Override - public void set(Object e) { throw new UnsupportedOperationException(); } - - @Override - public void add(Object e) { throw new UnsupportedOperationException(); } - - #else - @Override - public void set(KEY_TYPE e) { throw new UnsupportedOperationException(); } - - @Override - public void add(KEY_TYPE e) { throw new UnsupportedOperationException(); } - - #endif - @Override - public int skip(int amount) { - if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed"); - int steps = Math.min(amount, (size() - 1) - index); - index += steps; - return steps; - } - - @Override - public int back(int amount) { - if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed"); - int steps = Math.min(amount, index); - index -= steps; - return steps; - } - } - } +// Disabled until a Proper Implementation can be thought out or it is decided that the interface is thrown out. +// private class SubSet extends ABSTRACT_SET KEY_GENERIC_TYPE implements SORTED_SET KEY_GENERIC_TYPE { +// int offset; +// int length; +// +// SubSet(int offset, int length) { +// this.offset = offset; +// this.length = length; +// } +// +// int end() { return offset+length; } +// +// @Override +// public boolean add(KEY_TYPE o) { +// int index = findIndex(o); +// if(index == -1) { +// if(data.length == size) data = Arrays.copyOf(data, size == 0 ? 2 : size * 2); +// if(end() != size) System.arraycopy(data, end(), data, end()+1, size-(offset+length)); +// data[end()] = o; +// size++; +// length++; +// return true; +// } +// return false; +// } +// +// @Override +// public boolean addAndMoveToFirst(KEY_TYPE o) { +// int index = findIndex(o); +// if(index == -1) { +// if(data.length == size) data = Arrays.copyOf(data, size == 0 ? 2 : size * 2); +// System.arraycopy(data, offset, data, offset+1, size-offset); +// data[offset] = o; +// size++; +// length++; +// return true; +// } +// else if(index != 0) { +// o = data[index]; +// System.arraycopy(data, offset, data, offset+1, index-offset); +// data[offset] = o; +// } +// return false; +// } +// +// @Override +// public boolean addAndMoveToLast(KEY_TYPE o) { +// int index = findIndex(o); +// if(index == -1) { +// if(data.length == size) data = Arrays.copyOf(data, size == 0 ? 2 : size * 2); +// System.arraycopy(data, end()+1, data, end(), size-end()); +// data[end()] = o; +// size++; +// length++; +// return true; +// } +// else if(index != 0) { +// o = data[index]; +// System.arraycopy(data, offset+1, data, offset, index-offset); +// data[offset+length] = o; +// } +// return false; +// } +// +// @Override +// public boolean moveToFirst(KEY_TYPE o) { +// int index = findIndex(o); +// if(index > offset) { +// o = data[index]; +// System.arraycopy(data, offset, data, offset+1, index-offset); +// data[offset] = o; +// return true; +// } +// return false; +// } +// +// @Override +// public boolean moveToLast(KEY_TYPE o) { +// int index = findIndex(o); +// if(index != -1 && index < end()-1) { +// o = data[index]; +// System.arraycopy(data, index+1, data, index, end()-index-1); +// data[end()-1] = o; +// return true; +// } +// return false; +// } +// +//#if !TYPE_OBJECT +// @Override +// public boolean contains(KEY_TYPE e) { +// return findIndex(e) != -1; +// } +// +//#endif +// @Override +// public boolean contains(Object e) { +// return findIndex(e) != -1; +// } +// +// @Override +// public KEY_TYPE FIRST_KEY() { +// if(length == 0) throw new NoSuchElementException(); +// return data[offset]; +// } +// +// @Override +// public KEY_TYPE LAST_KEY() { +// if(length == 0) throw new NoSuchElementException(); +// return data[end()-1]; +// } +// +//#if !TYPE_OBJECT +// @Override +// public boolean remove(KEY_TYPE o) { +// int index = findIndex(o); +// if(index != -1) { +// size--; +// length--; +// if(index != size) System.arraycopy(data, index+1, data, index, size - index); +// return true; +// } +// return false; +// } +// +//#endif +// @Override +// public boolean remove(Object o) { +// int index = findIndex(o); +// if(index != -1) { +// size--; +// length--; +// if(index != size) System.arraycopy(data, index+1, data, index, size - index); +//#if TYPE_OBJECT +// data[size] = EMPTY_KEY_VALUE; +//#endif +// return true; +// } +// return false; +// } +// +// @Override +// public KEY_TYPE POLL_FIRST_KEY() { +// if(length == 0) throw new NoSuchElementException(); +// size--; +// length--; +// KEY_TYPE result = data[offset]; +// System.arraycopy(data, offset+1, data, offset, size-offset); +//#if TYPE_OBJECT +// data[size] = EMPTY_KEY_VALUE; +//#endif +// return result; +// } +// +// @Override +// public KEY_TYPE POLL_LAST_KEY() { +// if(length == 0) throw new NoSuchElementException(); +// KEY_TYPE result = data[offset+length]; +// size--; +// length--; +// System.arraycopy(data, end()+1, data, end(), size-end()); +//#if TYPE_OBJECT +// data[size] = EMPTY_KEY_VALUE; +//#endif +// return result; +// } +// +// @Override +// public COMPARATOR KEY_GENERIC_TYPE comparator() { +// return null; +// } +// +// @Override +// public BI_ITERATOR KEY_GENERIC_TYPE iterator() { +// return new SetIterator(offset); +// } +// +// @Override +// public BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement) { +// int index = findIndex(fromElement); +// 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); +// int toIndex = findIndex(toElement); +// if(fromIndex == -1 || toIndex == -1) throw new NoSuchElementException(); +// return new SubSet(fromIndex, toIndex - fromIndex + 1); +// } +// +// @Override +// public SORTED_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement) { +// int toIndex = findIndex(toElement); +// if(toIndex == -1) throw new NoSuchElementException(); +// return new SubSet(0, toIndex+1); +// } +// +// @Override +// public SORTED_SET KEY_GENERIC_TYPE tailSet(KEY_TYPE fromElement) { +// int fromIndex = findIndex(fromElement); +// if(fromIndex == -1) throw new NoSuchElementException(); +// return new SubSet(fromIndex, size - fromIndex); +// } +// +// @Override +// public int size() { +// return length; +// } +// +//#if !TYPE_OBJECT +// @Override +// public KEY_TYPE[] TO_ARRAY(KEY_TYPE[] a) { +// if(a == null || a.length < size()) return Arrays.copyOfRange(data, offset, end()); +// System.arraycopy(data, offset, a, 0, size()); +// return a; +// } +// +//#endif +// @Override +// @Deprecated +// public Object[] toArray() { +// Object[] obj = new Object[size()]; +// for(int i = 0;i E[] toArray(E[] a) { +// if(a == null) a = (E[])new Object[size()]; +// else if(a.length < size()) a = (E[])ObjectArrays.newArray(a.getClass().getComponentType(), size()); +// for(int i = 0;i=0;i--) +// if(KEY_EQUALS(data[offset+i], o)) return i + offset; +// return -1; +// } +// +//#endif +// protected int findIndex(Object o) { +// for(int i = length-1;i>=0;i--) +// if(EQUALS_KEY_TYPE(data[offset+i], o)) return i + offset; +// return -1; +// } +// +// private class SetIterator implements LIST_ITERATOR KEY_GENERIC_TYPE { +// int index; +// int lastReturned = -1; +// +// public SetIterator(int index) { +// this.index = index; +// } +// +// @Override +// public boolean hasNext() { +// return index < size(); +// } +// +// @Override +// public KEY_TYPE NEXT() { +// if(!hasNext()) throw new NoSuchElementException(); +// lastReturned = index; +// return data[index++]; +// } +// +// @Override +// public boolean hasPrevious() { +// return index > 0; +// } +// +// @Override +// public KEY_TYPE PREVIOUS() { +// if(!hasPrevious()) throw new NoSuchElementException(); +// lastReturned = index; +// return data[index--]; +// } +// +// @Override +// public int nextIndex() { +// return index; +// } +// +// @Override +// public int previousIndex() { +// return index-1; +// } +// +// @Override +// public void remove() { +// if(lastReturned == -1) +// throw new IllegalStateException(); +// SubSet.this.remove(data[lastReturned]); +// if(lastReturned < index) +// index--; +// lastReturned = -1; +// } +// +// #if TYPE_OBJECT +// @Override +// public void set(Object e) { throw new UnsupportedOperationException(); } +// +// @Override +// public void add(Object e) { throw new UnsupportedOperationException(); } +// +// #else +// @Override +// public void set(KEY_TYPE e) { throw new UnsupportedOperationException(); } +// +// @Override +// public void add(KEY_TYPE e) { throw new UnsupportedOperationException(); } +// +// #endif +// @Override +// public int skip(int amount) { +// if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed"); +// int steps = Math.min(amount, (size() - 1) - index); +// index += steps; +// return steps; +// } +// +// @Override +// public int back(int amount) { +// if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed"); +// int steps = Math.min(amount, index); +// index -= steps; +// return steps; +// } +// } +// } private class SetIterator implements LIST_ITERATOR KEY_GENERIC_TYPE { int index; diff --git a/src/builder/resources/speiger/assets/collections/templates/sets/RBTreeSet.template b/src/builder/resources/speiger/assets/collections/templates/sets/RBTreeSet.template index 83872ffd..69dbf7ab 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/RBTreeSet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/RBTreeSet.template @@ -1,6 +1,7 @@ package speiger.src.collections.PACKAGE.sets; import java.util.Collection; +import java.util.Collections; #if TYPE_OBJECT import java.util.Comparator; import java.util.function.Consumer; @@ -19,7 +20,6 @@ import speiger.src.collections.PACKAGE.functions.function.PREDICATE; import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR; import speiger.src.collections.PACKAGE.collections.COLLECTION; import speiger.src.collections.PACKAGE.collections.ITERATOR; -import speiger.src.collections.PACKAGE.lists.LIST_ITERATOR; #if !TYPE_OBJECT import speiger.src.collections.PACKAGE.utils.ITERATORS; #endif @@ -559,7 +559,11 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE @Override public KEY_TYPE POLL_FIRST_KEY() { - if(tree == null) throw new NoSuchElementException(); +#if TYPE_OBJECT + if(tree == null) return null; +#else + if(tree == null) return getDefaultMinValue(); +#endif KEY_TYPE value = first.key; removeNode(first); return value; @@ -567,7 +571,11 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE @Override public KEY_TYPE POLL_LAST_KEY() { - if(tree == null) throw new NoSuchElementException(); +#if TYPE_OBJECT + if(tree == null) return null; +#else + if(tree == null) return getDefaultMaxValue(); +#endif KEY_TYPE value = last.key; removeNode(last); return value; @@ -603,16 +611,16 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE 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 AscendingSetIterator(first); } @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); + return entry == null ? null : new AscendingSetIterator(entry); } @Override - public BI_ITERATOR KEY_GENERIC_TYPE descendingIterator() { return new SetIterator(true); } + public BI_ITERATOR KEY_GENERIC_TYPE descendingIterator() { return new DescendingSetIterator(last); } @Override public NAVIGABLE_SET KEY_GENERIC_TYPE subSet(KEY_TYPE fromElement, boolean fromInclusive, KEY_TYPE toElement, boolean toInclusive) { @@ -804,158 +812,174 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE setBlack(entry, true); } - private static class AscendingSubSet KEY_GENERIC_TYPE extends SubSet KEY_GENERIC_TYPE + 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) { - super(set, fromStart, start, loInclusive, toEnd, end, hiInclusive); + public AscendingSubSet(RB_TREE_SET KEY_GENERIC_TYPE set, boolean fromStart, KEY_TYPE lo, boolean loInclusive, boolean toEnd, KEY_TYPE hi, boolean hiInclusive) + { + super(set, fromStart, lo, loInclusive, toEnd, hi, 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"); + public COMPARATOR KEY_GENERIC_TYPE comparator() { return set.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 out of range"); + if(!inRange(toElement, toInclusive)) throw new IllegalArgumentException("toElement out of 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); + public NAVIGABLE_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement, boolean inclusive) + { + if(!inRange(toElement, inclusive)) throw new IllegalArgumentException("toElement out of range"); + return new AscendingSubSetBRACES(set, fromStart, lo, 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 out of range"); + return new AscendingSubSetBRACES(set, false, fromElement, inclusive, toEnd, hi, hiInclusive); } @Override - public BI_ITERATOR KEY_GENERIC_TYPE descendingIterator() { return new SubSetIterator(findHighest()); } + public BI_ITERATOR KEY_GENERIC_TYPE iterator() + { + return new AscendingSubSetIterator(absLowest(), absHighFence(), absLowFence()); + } + + @Override + public BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement) + { + return new AscendingSubSetIterator(absLower(fromElement), absHighFence(), absLowFence()); + } + + @Override + public BI_ITERATOR KEY_GENERIC_TYPE descendingIterator() + { + return new DescendingSubSetIterator(absHighest(), absLowFence(), absHighFence()); + } @Override - public NAVIGABLE_SET KEY_GENERIC_TYPE descendingSet() { - return new DescendingSubSetBRACES(set, fromStart, start, loInclusive, toEnd, end, hiInclusive); + public NAVIGABLE_SET KEY_GENERIC_TYPE descendingSet() + { + if(inverse == null) inverse = new DescendingSubSetBRACES(set, fromStart, lo, loInclusive, toEnd, hi, hiInclusive); + return inverse; } + + @Override + protected Entry KEY_GENERIC_TYPE subLowest() { return absLowest(); } + @Override + protected Entry KEY_GENERIC_TYPE subHighest() { return absHighest(); } + @Override + protected Entry KEY_GENERIC_TYPE subCeiling(KEY_TYPE key) { return absCeiling(key); } + @Override + protected Entry KEY_GENERIC_TYPE subHigher(KEY_TYPE key) { return absHigher(key); } + @Override + protected Entry KEY_GENERIC_TYPE subFloor(KEY_TYPE key) { return absFloor(key); } + @Override + protected Entry KEY_GENERIC_TYPE subLower(KEY_TYPE key) { return absLower(key); } + @Override + protected Entry KEY_GENERIC_TYPE start() { return absLowest(); } + @Override + protected Entry KEY_GENERIC_TYPE next(Entry KEY_GENERIC_TYPE entry) { return entry.next(); } } - private static class DescendingSubSet KEY_GENERIC_TYPE extends SubSet KEY_GENERIC_TYPE + 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) { - super(set, fromStart, start, loInclusive, toEnd, end, hiInclusive); - comparator = set.comparator(); - if(comparator != null) comparator = comparator.reversed(); + + public DescendingSubSet(RB_TREE_SET KEY_GENERIC_TYPE set, boolean fromStart, KEY_TYPE lo, boolean loInclusive, boolean toEnd, KEY_TYPE hi, boolean hiInclusive) + { + super(set, fromStart, lo, loInclusive, toEnd, hi, hiInclusive); +#if TYPE_OBJECT + comparator = Collections.reverseOrder(set.comparator()); +#else + comparator = set.comparator() == null ? COMPARATOR.of(Collections.reverseOrder()) : set.comparator().reversed(); +#endif } @Override public COMPARATOR KEY_GENERIC_TYPE comparator() { return comparator; } + public KEY_TYPE getDefaultMaxValue() { return super.getDefaultMinValue(); } + + public KEY_TYPE getDefaultMinValue() { return super.getDefaultMaxValue(); } + @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); + if(!inRange(fromElement, fromInclusive)) throw new IllegalArgumentException("fromElement out of range"); + if(!inRange(toElement, toInclusive)) throw new IllegalArgumentException("toElement out of range"); + return new DescendingSubSetBRACES(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 DescendingSubSetBRACES(set, false, toElement, inclusive, toEnd, end, hiInclusive); + public NAVIGABLE_SET KEY_GENERIC_TYPE headSet(KEY_TYPE toElement, boolean inclusive) + { + if(!inRange(toElement, inclusive)) throw new IllegalArgumentException("toElement out of range"); + return new DescendingSubSetBRACES(set, false, toElement, inclusive, toEnd, hi, 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); + if(!inRange(fromElement, inclusive)) throw new IllegalArgumentException("fromElement out of range"); + return new DescendingSubSetBRACES(set, fromStart, lo, loInclusive, false, fromElement, inclusive); } - - protected Entry KEY_GENERIC_TYPE next(Entry KEY_GENERIC_TYPE entry) { return entry.previous(); } - protected Entry KEY_GENERIC_TYPE start() { return findHighest(); } @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); + public BI_ITERATOR KEY_GENERIC_TYPE iterator() { + return new DescendingSubSetIterator(absHighest(), absLowFence(), absHighFence()); } - + @Override - public BI_ITERATOR KEY_GENERIC_TYPE descendingIterator() { return new DescendingSubIterator(findLowest()); } - + public BI_ITERATOR KEY_GENERIC_TYPE descendingIterator() { + return new AscendingSubSetIterator(absLowest(), absHighFence(), absLowFence()); + } + @Override public NAVIGABLE_SET KEY_GENERIC_TYPE descendingSet() { - return new AscendingSubSetBRACES(set, fromStart, start, loInclusive, toEnd, end, hiInclusive); + if(inverse == null) inverse = new AscendingSubSetBRACES(set, fromStart, lo, loInclusive, toEnd, hi, hiInclusive); + return inverse; } - 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; - } + @Override + public BI_ITERATOR KEY_GENERIC_TYPE iterator(KEY_TYPE fromElement) { + return new DescendingSubSetIterator(absHigher(fromElement), absLowFence(), absHighFence()); } + + @Override + protected Entry KEY_GENERIC_TYPE subLowest() { return absHighest(); } + @Override + protected Entry KEY_GENERIC_TYPE subHighest() { return absLowest(); } + @Override + protected Entry KEY_GENERIC_TYPE subCeiling(KEY_TYPE key) { return absFloor(key); } + @Override + protected Entry KEY_GENERIC_TYPE subHigher(KEY_TYPE key) { return absLower(key); } + @Override + protected Entry KEY_GENERIC_TYPE subFloor(KEY_TYPE key) { return absCeiling(key); } + @Override + protected Entry KEY_GENERIC_TYPE subLower(KEY_TYPE key) { return absHigher(key); } + @Override + protected Entry KEY_GENERIC_TYPE start() { return absHighest(); } + @Override + protected Entry KEY_GENERIC_TYPE next(Entry KEY_GENERIC_TYPE entry) { return entry.previous(); } } - private static abstract class SubSet KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE implements NAVIGABLE_SET KEY_GENERIC_TYPE + static abstract class SubSet KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE implements NAVIGABLE_SET KEY_GENERIC_TYPE { - RB_TREE_SET KEY_GENERIC_TYPE set; - KEY_TYPE start; - KEY_TYPE end; - boolean fromStart; - boolean toEnd; - boolean loInclusive; - boolean hiInclusive; + final RB_TREE_SET KEY_GENERIC_TYPE set; + final KEY_TYPE lo, hi; + final boolean fromStart, toEnd; + final boolean loInclusive, hiInclusive; + NAVIGABLE_SET KEY_GENERIC_TYPE inverse; - SubSet(RB_TREE_SET KEY_GENERIC_TYPE set, boolean fromStart, KEY_TYPE start, boolean loInclusive, boolean toEnd, KEY_TYPE end, boolean hiInclusive) { + public SubSet(RB_TREE_SET KEY_GENERIC_TYPE set, boolean fromStart, KEY_TYPE lo, boolean loInclusive, boolean toEnd, KEY_TYPE hi, boolean hiInclusive) + { this.set = set; - this.start = start; - this.end = end; + this.lo = lo; + this.hi = hi; this.fromStart = fromStart; this.toEnd = toEnd; this.loInclusive = loInclusive; @@ -981,80 +1005,75 @@ 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 tooLow(KEY_TYPE key) { + if (!fromStart) { + int c = set.compare(key, lo); + if (c < 0 || (c == 0 && !loInclusive)) return true; + } + return false; + } + + boolean tooHigh(KEY_TYPE key) { + if (!toEnd) { + int c = set.compare(key, hi); + if (c > 0 || (c == 0 && !hiInclusive)) return true; + } + return false; + } + 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 inClosedRange(KEY_TYPE key) { return (fromStart || set.compare(key, lo) >= 0) && (toEnd || set.compare(hi, key) >= 0); } boolean inRange(KEY_TYPE key, boolean inclusive) { return inclusive ? inRange(key) : inClosedRange(key); } - protected Entry KEY_GENERIC_TYPE next(Entry KEY_GENERIC_TYPE entry) { return entry.next(); } - protected Entry KEY_GENERIC_TYPE start() { return findLowest(); } - @Override - public boolean addAndMoveToFirst(KEY_TYPE o) { throw new UnsupportedOperationException(); } + protected abstract Entry KEY_GENERIC_TYPE subLowest(); + protected abstract Entry KEY_GENERIC_TYPE subHighest(); + protected abstract Entry KEY_GENERIC_TYPE subCeiling(KEY_TYPE key); + protected abstract Entry KEY_GENERIC_TYPE subHigher(KEY_TYPE key); + protected abstract Entry KEY_GENERIC_TYPE subFloor(KEY_TYPE key); + protected abstract Entry KEY_GENERIC_TYPE subLower(KEY_TYPE key); + protected KEY_TYPE lowKeyOrNull(Entry KEY_GENERIC_TYPE entry) { return entry == null ? getDefaultMinValue() : entry.key; } + protected KEY_TYPE highKeyOrNull(Entry KEY_GENERIC_TYPE entry) { return entry == null ? getDefaultMaxValue() : entry.key; } + protected abstract Entry KEY_GENERIC_TYPE start(); + protected abstract Entry KEY_GENERIC_TYPE next(Entry KEY_GENERIC_TYPE entry); - @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 abstract BI_ITERATOR KEY_GENERIC_TYPE iterator(); - - @Override - public KEY_TYPE FIRST_KEY() { - Entry KEY_GENERIC_TYPE entry = findLowest(); - return entry == null ? getDefaultMaxValue() : entry.key; + final Entry KEY_GENERIC_TYPE absLowest() { + Entry KEY_GENERIC_TYPE e = (fromStart ? set.first : (loInclusive ? set.findCeilingNode(lo) : set.findHigherNode(lo))); + return (e == null || tooHigh(e.key)) ? null : e; } - 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; + final Entry KEY_GENERIC_TYPE absHighest() { + Entry KEY_GENERIC_TYPE e = (toEnd ? set.last : (hiInclusive ? set.findFloorNode(hi) : set.findLowerNode(hi))); + return (e == null || tooLow(e.key)) ? null : e; } - @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; + final Entry KEY_GENERIC_TYPE absCeiling(KEY_TYPE key) { + if (tooLow(key)) return absLowest(); + Entry KEY_GENERIC_TYPE e = set.findCeilingNode(key); + return (e == null || tooHigh(e.key)) ? null : e; } - 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; + final Entry KEY_GENERIC_TYPE absHigher(KEY_TYPE key) { + if (tooLow(key)) return absLowest(); + Entry KEY_GENERIC_TYPE e = set.findHigherNode(key); + return (e == null || tooHigh(e.key)) ? null : e; } - - @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(); + + final Entry KEY_GENERIC_TYPE absFloor(KEY_TYPE key) { + if (tooHigh(key)) return absHighest(); + Entry KEY_GENERIC_TYPE e = set.findFloorNode(key); + return (e == null || tooLow(e.key)) ? null : e; } + final Entry KEY_GENERIC_TYPE absLower(KEY_TYPE key) { + if (tooHigh(key)) return absHighest(); + Entry KEY_GENERIC_TYPE e = set.findLowerNode(key); + return (e == null || tooLow(e.key)) ? null : e; + } + + final Entry KEY_GENERIC_TYPE absHighFence() { return (toEnd ? null : (hiInclusive ? set.findHigherNode(hi) : set.findCeilingNode(hi))); } + final Entry KEY_GENERIC_TYPE absLowFence() { return (fromStart ? null : (loInclusive ? set.findLowerNode(lo) : set.findFloorNode(lo))); } + @Override public boolean add(KEY_TYPE o) { if(!inRange(o)) throw new IllegalArgumentException("Key is out of range"); @@ -1086,50 +1105,85 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE } @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; + public boolean isEmpty() { + if(fromStart && toEnd) return set.isEmpty(); + Entry KEY_GENERIC_TYPE n = absLowest(); + return n == null || tooHigh(n.key); } @Override public int size() { - return fromStart && toEnd ? set.size() : iterator().skip(Integer.MAX_VALUE); + if(fromStart && toEnd) return set.size(); + int i = 0; + for(ITERATOR KEY_GENERIC_TYPE iter = iterator();iter.hasNext();iter.NEXT(),i++); + return i; } + @Override + public KEY_TYPE lower(KEY_TYPE e) { + return lowKeyOrNull(subLower(e)); + } + + @Override + public KEY_TYPE floor(KEY_TYPE e) { + return lowKeyOrNull(subFloor(e)); + } + + @Override + public KEY_TYPE ceiling(KEY_TYPE e) { + return highKeyOrNull(subCeiling(e)); + } + + @Override + public KEY_TYPE higher(KEY_TYPE e) { + return highKeyOrNull(subHigher(e)); + } + + @Override + public KEY_TYPE POLL_FIRST_KEY() { + Entry KEY_GENERIC_TYPE entry = subLowest(); + if(entry != null) { + KEY_TYPE result = entry.key; + set.removeNode(entry); + return result; + } + return getDefaultMinValue(); + } + + @Override + public KEY_TYPE POLL_LAST_KEY() { + Entry KEY_GENERIC_TYPE entry = subHighest(); + if(entry != null) { + KEY_TYPE result = entry.key; + set.removeNode(entry); + return result; + } + return getDefaultMaxValue(); + } + + @Override + public KEY_TYPE FIRST_KEY() { + Entry KEY_GENERIC_TYPE entry = subLowest(); + if(entry == null) throw new NoSuchElementException(); + return entry.key; + } + + @Override + public KEY_TYPE LAST_KEY() { + Entry KEY_GENERIC_TYPE entry = subHighest(); + if(entry == null) throw new NoSuchElementException(); + return entry.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 SubSet KEY_GENERIC_TYPE copy() { throw new UnsupportedOperationException(); } @@ -1233,172 +1287,207 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE return result; } - class SubSetIterator implements LIST_ITERATOR KEY_GENERIC_TYPE + class AscendingSubSetIterator implements BI_ITERATOR KEY_GENERIC_TYPE { - Entry KEY_GENERIC_TYPE previous; + Entry KEY_GENERIC_TYPE lastReturned; Entry KEY_GENERIC_TYPE next; - Entry KEY_GENERIC_TYPE current; - int index = 0; + boolean unboundForwardFence; + boolean unboundBackwardFence; + KEY_TYPE forwardFence; + KEY_TYPE backwardFence; - public SubSetIterator(Entry KEY_GENERIC_TYPE entry) { - next = entry; - previous = entry == null ? null : entry.previous(); + public AscendingSubSetIterator(Entry KEY_GENERIC_TYPE first, Entry KEY_GENERIC_TYPE forwardFence, Entry KEY_GENERIC_TYPE backwardFence) + { + next = first; + this.forwardFence = forwardFence == null ? EMPTY_KEY_VALUE : forwardFence.key; + this.backwardFence = backwardFence == null ? EMPTY_KEY_VALUE : backwardFence.key; + unboundForwardFence = forwardFence == null; + unboundBackwardFence = backwardFence == null; } @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; + return next != null && (unboundForwardFence || next.key != forwardFence); } @Override public KEY_TYPE NEXT() { if(!hasNext()) throw new NoSuchElementException(); - current = previous = next; - updateNext(); - index++; - return current.key; + lastReturned = next; + KEY_TYPE result = next.key; + next = next.next(); + return result; + } + + @Override + public boolean hasPrevious() { + return next != null && (unboundBackwardFence || next.key != backwardFence); } @Override public KEY_TYPE PREVIOUS() { if(!hasPrevious()) throw new NoSuchElementException(); - current = next = previous; - updatePrevious(); - index--; - return current.key; + lastReturned = next; + KEY_TYPE result = next.key; + next = next.previous(); + return result; } @Override public void remove() { - if(current == null) throw new IllegalStateException(); - if(current == previous) index--; - updateNext(); - updatePrevious(); - if(current.needsSuccessor()) next = current; - set.removeNode(current); - current = null; + if(lastReturned == null) throw new IllegalStateException(); + if(lastReturned.needsSuccessor()) next = lastReturned; + set.removeNode(lastReturned); + lastReturned = null; + } + } + + class DescendingSubSetIterator implements BI_ITERATOR KEY_GENERIC_TYPE + { + Entry KEY_GENERIC_TYPE lastReturned; + Entry KEY_GENERIC_TYPE next; + boolean unboundForwardFence; + boolean unboundBackwardFence; + KEY_TYPE forwardFence; + KEY_TYPE backwardFence; + + public DescendingSubSetIterator(Entry KEY_GENERIC_TYPE first, Entry KEY_GENERIC_TYPE forwardFence, Entry KEY_GENERIC_TYPE backwardFence) + { + next = first; + this.forwardFence = forwardFence == null ? EMPTY_KEY_VALUE : forwardFence.key; + this.backwardFence = backwardFence == null ? EMPTY_KEY_VALUE : backwardFence.key; + unboundForwardFence = forwardFence == null; + unboundBackwardFence = backwardFence == null; } @Override - public void set(KEY_TYPE e){ throw new UnsupportedOperationException(); } + public boolean hasNext() { + return next != null && (unboundForwardFence || next.key != forwardFence); + } @Override - public void add(KEY_TYPE e) { throw new UnsupportedOperationException(); } + public KEY_TYPE NEXT() { + if(!hasNext()) throw new NoSuchElementException(); + lastReturned = next; + KEY_TYPE result = next.key; + next = next.previous(); + return result; + } + + @Override + public boolean hasPrevious() { + return next != null && (unboundBackwardFence || next.key != backwardFence); + } + + @Override + public KEY_TYPE PREVIOUS() { + if(!hasPrevious()) throw new NoSuchElementException(); + lastReturned = next; + KEY_TYPE result = next.key; + next = next.next(); + return result; + } + + @Override + public void remove() { + if(lastReturned == null) throw new IllegalStateException(); + set.removeNode(lastReturned); + lastReturned = null; + } } } - private class SetIterator implements LIST_ITERATOR KEY_GENERIC_TYPE + class AscendingSetIterator implements BI_ITERATOR KEY_GENERIC_TYPE { - Entry KEY_GENERIC_TYPE previous; + Entry KEY_GENERIC_TYPE lastReturned; 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(); + public AscendingSetIterator(Entry KEY_GENERIC_TYPE first) + { + next = first; } @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(); + return next != null; } @Override public KEY_TYPE NEXT() { if(!hasNext()) throw new NoSuchElementException(); - current = previous = next; - updateNext(); - index++; - return current.key; + lastReturned = next; + KEY_TYPE result = next.key; + next = next.next(); + return result; + } + + @Override + public boolean hasPrevious() { + return next != null; } @Override public KEY_TYPE PREVIOUS() { if(!hasPrevious()) throw new NoSuchElementException(); - current = next = previous; - updatePrevious(); - index--; - return current.key; + lastReturned = next; + KEY_TYPE result = next.key; + next = next.previous(); + return result; } @Override public void remove() { - if(current == null) throw new IllegalStateException(); - if(current == previous) index--; - updateNext(); - updatePrevious(); - if(current.needsSuccessor()) next = current; - removeNode(current); - current = null; + if(lastReturned == null) throw new IllegalStateException(); + if(lastReturned.needsSuccessor()) next = lastReturned; + removeNode(lastReturned); + lastReturned = null; + } + } + + class DescendingSetIterator implements BI_ITERATOR KEY_GENERIC_TYPE + { + Entry KEY_GENERIC_TYPE lastReturned; + Entry KEY_GENERIC_TYPE next; + + public DescendingSetIterator(Entry KEY_GENERIC_TYPE first) + { + next = first; } - - @Override - public void set(KEY_TYPE e){ throw new UnsupportedOperationException(); } @Override - public void add(KEY_TYPE e) { throw new UnsupportedOperationException(); } + public boolean hasNext() { + return next != null; + } + + @Override + public KEY_TYPE NEXT() { + if(!hasNext()) throw new NoSuchElementException(); + lastReturned = next; + KEY_TYPE result = next.key; + next = next.previous(); + return result; + } + + @Override + public boolean hasPrevious() { + return next != null; + } + + @Override + public KEY_TYPE PREVIOUS() { + if(!hasPrevious()) throw new NoSuchElementException(); + lastReturned = next; + KEY_TYPE result = next.key; + next = next.next(); + return result; + } + + @Override + public void remove() { + if(lastReturned == null) throw new IllegalStateException(); + removeNode(lastReturned); + lastReturned = null; + } } private static final class Entry KEY_GENERIC_TYPE diff --git a/src/test/java/speiger/src/collections/ints/maps/Int2IntArrayMapTest.java b/src/test/java/speiger/src/collections/ints/maps/Int2IntArrayMapTest.java index a281bc64..edca9cbc 100644 --- a/src/test/java/speiger/src/collections/ints/maps/Int2IntArrayMapTest.java +++ b/src/test/java/speiger/src/collections/ints/maps/Int2IntArrayMapTest.java @@ -1,12 +1,18 @@ package speiger.src.collections.ints.maps; +import java.util.EnumSet; + import speiger.src.collections.ints.base.BaseInt2IntSortedMapTest; import speiger.src.collections.ints.maps.impl.misc.Int2IntArrayMap; import speiger.src.collections.ints.maps.interfaces.Int2IntSortedMap; +import speiger.src.collections.tests.SortedMapTests; @SuppressWarnings("javadoc") public class Int2IntArrayMapTest extends BaseInt2IntSortedMapTest { + @Override + public EnumSet getValidSortedMapTests() { return EnumSet.complementOf(EnumSet.of(SortedMapTests.SUB_MAP, SortedMapTests.HEAD_MAP, SortedMapTests.TAIL_MAP)); } + @Override public Int2IntSortedMap createMap(int[] keys, int[] values) { diff --git a/src/test/java/speiger/src/collections/ints/sets/IntArraySetTests.java b/src/test/java/speiger/src/collections/ints/sets/IntArraySetTests.java index c7fc570d..ca8330e6 100644 --- a/src/test/java/speiger/src/collections/ints/sets/IntArraySetTests.java +++ b/src/test/java/speiger/src/collections/ints/sets/IntArraySetTests.java @@ -1,10 +1,15 @@ package speiger.src.collections.ints.sets; +import java.util.EnumSet; + import speiger.src.collections.ints.base.BaseIntSortedSetTest; +import speiger.src.collections.tests.SortedSetTest; @SuppressWarnings("javadoc") public class IntArraySetTests extends BaseIntSortedSetTest { + @Override + protected EnumSet getValidSortedSetTests() { return EnumSet.complementOf(EnumSet.of(SortedSetTest.SUB_SET, SortedSetTest.HEAD_SET, SortedSetTest.TAIL_SET)); } @Override protected IntSortedSet create(int[] data) { return new IntArraySet(data.clone()); } } diff --git a/src/test/java/speiger/src/collections/objects/map/ObjectMapTests.java b/src/test/java/speiger/src/collections/objects/map/ObjectMapTests.java index b126e42d..e82d859f 100644 --- a/src/test/java/speiger/src/collections/objects/map/ObjectMapTests.java +++ b/src/test/java/speiger/src/collections/objects/map/ObjectMapTests.java @@ -2,14 +2,17 @@ package speiger.src.collections.objects.map; import java.util.Comparator; import java.util.Map; +import java.util.NavigableMap; import java.util.Objects; import java.util.function.BiFunction; import java.util.function.Supplier; import com.google.common.collect.testing.AnEnum; import com.google.common.collect.testing.MapTestSuiteBuilder; +import com.google.common.collect.testing.NavigableMapTestSuiteBuilder; import com.google.common.collect.testing.TestEnumMapGenerator; import com.google.common.collect.testing.TestStringMapGenerator; +import com.google.common.collect.testing.TestStringSortedMapGenerator; import com.google.common.collect.testing.features.CollectionFeature; import com.google.common.collect.testing.features.CollectionSize; import com.google.common.collect.testing.features.MapFeature; @@ -39,10 +42,10 @@ public class ObjectMapTests extends TestCase suite.addTest(suite("LinkedHashMap", Object2ObjectLinkedOpenHashMap::new, true)); suite.addTest(suite("CustomHashMap", () -> new Object2ObjectOpenCustomHashMap<>(Strategy.INSTANCE), true)); suite.addTest(suite("LinkedCustomHashMap", () -> new Object2ObjectLinkedOpenCustomHashMap<>(Strategy.INSTANCE), true)); - suite.addTest(suite("RBTreeMap_NonNull", Object2ObjectRBTreeMap::new, false)); - suite.addTest(suite("AVLTreeMap_NonNull", Object2ObjectAVLTreeMap::new, false)); - suite.addTest(suite("RBTreeMap_Null", () -> new Object2ObjectRBTreeMap(Comparator.nullsFirst(Comparator.naturalOrder())), true)); - suite.addTest(suite("AVLTreeMap_Null", () -> new Object2ObjectAVLTreeMap(Comparator.nullsFirst(Comparator.naturalOrder())), true)); + suite.addTest(navigableSuite("RBTreeMap_NonNull", Object2ObjectRBTreeMap::new, false)); + suite.addTest(navigableSuite("AVLTreeMap_NonNull", Object2ObjectAVLTreeMap::new, false)); + suite.addTest(navigableSuite("RBTreeMap_Null", () -> new Object2ObjectRBTreeMap<>(new NullFriendlyComparator()), true)); + suite.addTest(navigableSuite("AVLTreeMap_Null", () -> new Object2ObjectAVLTreeMap<>(new NullFriendlyComparator()), true)); suite.addTest(immutableSuit("ImmutableMap", ImmutableObject2ObjectOpenHashMap::new)); suite.addTest(suite("ArrayMap", Object2ObjectArrayMap::new, true)); suite.addTest(enumSuite("EnumMap", () -> new Enum2ObjectMap<>(AnEnum.class))); @@ -66,6 +69,22 @@ public class ObjectMapTests extends TestCase return builder.createTestSuite(); } + public static Test navigableSuite(String name, Supplier> factory, boolean allowNull) + { + MapTestSuiteBuilder builder = NavigableMapTestSuiteBuilder.using(new TestStringSortedMapGenerator() { + @Override + protected NavigableMap create(Map.Entry[] entries) { + NavigableMap map = factory.get(); + for(Map.Entry entry : entries) { + map.put(entry.getKey(), entry.getValue()); + } + return map; + } + }).named(name).withFeatures(CollectionSize.ANY, MapFeature.GENERAL_PURPOSE, MapFeature.ALLOWS_NULL_VALUES, CollectionFeature.SUPPORTS_ITERATOR_REMOVE); + if(allowNull) builder.withFeatures(MapFeature.ALLOWS_NULL_KEYS, MapFeature.ALLOWS_ANY_NULL_QUERIES); + return builder.createTestSuite(); + } + public static Test immutableSuit(String name, BiFunction> factory) { MapTestSuiteBuilder builder = MapTestSuiteBuilder.using(new TestStringMapGenerator() { @Override @@ -97,6 +116,14 @@ public class ObjectMapTests extends TestCase return builder.createTestSuite(); } + private static final class NullFriendlyComparator implements Comparator + { + @Override + public int compare(String left, String right) { + return String.valueOf(left).compareTo(String.valueOf(right)); + } + } + private static class Strategy implements ObjectStrategy { static final Strategy INSTANCE = new Strategy(); diff --git a/src/test/java/speiger/src/collections/objects/set/ObjectSetTests.java b/src/test/java/speiger/src/collections/objects/set/ObjectSetTests.java index 3c874c92..bea42ee2 100644 --- a/src/test/java/speiger/src/collections/objects/set/ObjectSetTests.java +++ b/src/test/java/speiger/src/collections/objects/set/ObjectSetTests.java @@ -1,12 +1,15 @@ package speiger.src.collections.objects.set; import java.util.Comparator; +import java.util.NavigableSet; import java.util.Objects; import java.util.Set; import java.util.function.Function; +import com.google.common.collect.testing.NavigableSetTestSuiteBuilder; import com.google.common.collect.testing.SetTestSuiteBuilder; import com.google.common.collect.testing.TestStringSetGenerator; +import com.google.common.collect.testing.TestStringSortedSetGenerator; import com.google.common.collect.testing.features.CollectionFeature; import com.google.common.collect.testing.features.CollectionSize; import com.google.common.collect.testing.features.SetFeature; @@ -36,15 +39,14 @@ public class ObjectSetTests extends TestCase suite.addTest(suite("LinkedCustomHashSet", T -> new ObjectLinkedOpenCustomHashSet<>(T, Strategy.INSTANCE), true)); suite.addTest(immutableSuite("ImmutableHashSet", ImmutableObjectOpenHashSet::new)); suite.addTest(suite("ArraySet", ObjectArraySet::new, true)); - suite.addTest(suite("RBTreeSet_NonNull", ObjectRBTreeSet::new, false)); - suite.addTest(suite("AVLTreeSet_NonNull", ObjectAVLTreeSet::new, false)); - suite.addTest(suite("RBTreeSet_Null", T -> new ObjectRBTreeSet<>(T, Comparator.nullsFirst(Comparator.naturalOrder())), true)); - suite.addTest(suite("AVLTreeSet_Null", T -> new ObjectAVLTreeSet<>(T, Comparator.nullsFirst(Comparator.naturalOrder())), true)); + suite.addTest(navigableSuite("RBTreeSet_NonNull", ObjectRBTreeSet::new, false)); + suite.addTest(navigableSuite("AVLTreeSet_NonNull", ObjectAVLTreeSet::new, false)); + suite.addTest(navigableSuite("RBTreeSet_Null", T -> new ObjectRBTreeSet<>(T, Comparator.nullsFirst(Comparator.naturalOrder())), true)); + suite.addTest(navigableSuite("AVLTreeSet_Null", T -> new ObjectAVLTreeSet<>(T, Comparator.nullsFirst(Comparator.naturalOrder())), true)); return suite; } - public static Test suite(String name, Function> factory, boolean allowNull) - { + public static Test suite(String name, Function> factory, boolean allowNull) { SetTestSuiteBuilder generator = SetTestSuiteBuilder.using(new TestStringSetGenerator() { @Override protected Set create(String[] elements) { return factory.apply(elements); } @@ -53,17 +55,23 @@ public class ObjectSetTests extends TestCase return generator.createTestSuite(); } + public static Test navigableSuite(String name, Function> factory, boolean allowNull) { + SetTestSuiteBuilder generator = NavigableSetTestSuiteBuilder.using(new TestStringSortedSetGenerator() { + @Override + protected NavigableSet create(String[] elements) { return factory.apply(elements); } + }).named(name).withFeatures(CollectionSize.ANY, SetFeature.GENERAL_PURPOSE); + if(allowNull) generator.withFeatures(CollectionFeature.ALLOWS_NULL_VALUES); + return generator.createTestSuite(); + } - public static Test immutableSuite(String name, Function> factory) - { + public static Test immutableSuite(String name, Function> factory) { return SetTestSuiteBuilder.using(new TestStringSetGenerator() { @Override protected Set create(String[] elements) { return factory.apply(elements); } }).named(name).withFeatures(CollectionSize.ANY, CollectionFeature.ALLOWS_NULL_VALUES).createTestSuite(); } - private static class Strategy implements ObjectStrategy - { + private static class Strategy implements ObjectStrategy { static final Strategy INSTANCE = new Strategy(); @Override @@ -75,6 +83,5 @@ public class ObjectSetTests extends TestCase public boolean equals(String key, String value) { return Objects.equals(key, value); } - } -} +} \ No newline at end of file