Fixing bugs found when implementing Bidirectional Iterator unit tests.

-Fixed: AbstractList/ImmutableList/ArraySet/ArrayMap skip/back implementation was causing crashes and didn't update the last returned value.
-Fixed: ArraySet/ArrayMap previous was not subtracting before returning value.
-Fixed: BidirectionalIterator back was calling the object variant instead of the TypeSpecific Variant.
-Fixed: TreeSets/Maps Iterator now fully supports backwards Iterating.
-Added: Specialized skip/back function to improve speed in ImmutableHashSet/LinkedHashSet/CustomLinkedHashSet
This commit is contained in:
2022-06-04 21:05:31 +02:00
parent c1862e6b05
commit 8b5e5a75c1
18 changed files with 502 additions and 175 deletions
@@ -22,7 +22,6 @@ import speiger.src.collections.PACKAGE.maps.interfaces.MAP;
import speiger.src.collections.PACKAGE.maps.interfaces.ORDERED_MAP;
import speiger.src.collections.PACKAGE.sets.ABSTRACT_SET;
import speiger.src.collections.PACKAGE.sets.ORDERED_SET;
import speiger.src.collections.PACKAGE.sets.SET;
import speiger.src.collections.PACKAGE.utils.STRATEGY;
import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_ABSTRACT_COLLECTION;
import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_COLLECTION;
@@ -57,7 +56,7 @@ import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOpera
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.ObjectSet;
import speiger.src.collections.objects.sets.ObjectOrderedSet;
#endif
import speiger.src.collections.utils.HashUtil;
@@ -285,7 +284,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
@Override
public boolean moveToFirst(KEY_TYPE key) {
if(strategy.equals(FIRST_ENTRY_KEY(), key)) return false;
if(isEmpty() || strategy.equals(FIRST_ENTRY_KEY(), key)) return false;
if(strategy.equals(key, EMPTY_KEY_VALUE)) {
if(containsNull) {
moveToFirstIndex(nullIndex);
@@ -307,7 +306,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
@Override
public boolean moveToLast(KEY_TYPE key) {
if(strategy.equals(LAST_ENTRY_KEY(), key)) return false;
if(isEmpty() || strategy.equals(LAST_ENTRY_KEY(), key)) return false;
if(strategy.equals(key, EMPTY_KEY_VALUE)) {
if(containsNull) {
moveToLastIndex(nullIndex);
@@ -421,15 +420,15 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M
}
@Override
public ObjectSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> ENTRY_SET() {
public ObjectOrderedSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> ENTRY_SET() {
if(entrySet == null) entrySet = new MapEntrySet();
return entrySet;
return (ObjectOrderedSet<MAP.Entry KEY_VALUE_GENERIC_TYPE>)entrySet;
}
@Override
public SET KEY_GENERIC_TYPE keySet() {
public ORDERED_SET KEY_GENERIC_TYPE keySet() {
if(keySet == null) keySet = new KeySet();
return keySet;
return (ORDERED_SET KEY_GENERIC_TYPE)keySet;
}
@Override
@@ -22,7 +22,6 @@ import speiger.src.collections.PACKAGE.maps.interfaces.MAP;
import speiger.src.collections.PACKAGE.maps.interfaces.ORDERED_MAP;
import speiger.src.collections.PACKAGE.sets.ABSTRACT_SET;
import speiger.src.collections.PACKAGE.sets.ORDERED_SET;
import speiger.src.collections.PACKAGE.sets.SET;
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;
@@ -56,7 +55,7 @@ import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOpera
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.ObjectSet;
import speiger.src.collections.objects.sets.ObjectOrderedSet;
#endif
import speiger.src.collections.utils.HashUtil;
@@ -262,7 +261,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
@Override
public boolean moveToFirst(KEY_TYPE key) {
if(KEY_EQUALS(FIRST_ENTRY_KEY(), key)) return false;
if(isEmpty() || KEY_EQUALS(FIRST_ENTRY_KEY(), key)) return false;
if(KEY_EQUALS_NULL(key)) {
if(containsNull) {
moveToFirstIndex(nullIndex);
@@ -284,7 +283,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
@Override
public boolean moveToLast(KEY_TYPE key) {
if(KEY_EQUALS(LAST_ENTRY_KEY(), key)) return false;
if(isEmpty() || KEY_EQUALS(LAST_ENTRY_KEY(), key)) return false;
if(KEY_EQUALS_NULL(key)) {
if(containsNull) {
moveToLastIndex(nullIndex);
@@ -425,15 +424,15 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G
}
@Override
public ObjectSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> ENTRY_SET() {
public ObjectOrderedSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> ENTRY_SET() {
if(entrySet == null) entrySet = new MapEntrySet();
return entrySet;
return (ObjectOrderedSet<MAP.Entry KEY_VALUE_GENERIC_TYPE>)entrySet;
}
@Override
public SET KEY_GENERIC_TYPE keySet() {
public ORDERED_SET KEY_GENERIC_TYPE keySet() {
if(keySet == null) keySet = new KeySet();
return keySet;
return (ORDERED_SET KEY_GENERIC_TYPE)keySet;
}
@Override
@@ -25,7 +25,9 @@ import speiger.src.collections.PACKAGE.lists.LIST_ITERATOR;
import speiger.src.collections.PACKAGE.maps.interfaces.ORDERED_MAP;
import speiger.src.collections.PACKAGE.maps.abstracts.ABSTRACT_MAP;
import speiger.src.collections.PACKAGE.maps.interfaces.MAP;
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.sets.ORDERED_SET;
#endif
import speiger.src.collections.PACKAGE.utils.maps.MAPS;
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.utils.ARRAYS;
@@ -47,7 +49,6 @@ import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_PREDICATE;
#endif
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.sets.ABSTRACT_SET;
import speiger.src.collections.PACKAGE.sets.SET;
#endif
import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_ABSTRACT_COLLECTION;
import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_COLLECTION;
@@ -70,7 +71,7 @@ import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOpera
import speiger.src.collections.objects.lists.ObjectListIterator;
#endif
import speiger.src.collections.objects.sets.AbstractObjectSet;
import speiger.src.collections.objects.sets.ObjectSet;
import speiger.src.collections.objects.sets.ObjectOrderedSet;
import speiger.src.collections.utils.HashUtil;
import speiger.src.collections.utils.SanityChecks;
@@ -100,9 +101,9 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_
/** The Last Index in the Map */
protected int lastIndex = -1;
/** EntrySet cache */
protected transient FastEntrySet KEY_VALUE_GENERIC_TYPE entrySet;
protected transient FastOrderedSet KEY_VALUE_GENERIC_TYPE entrySet;
/** KeySet cache */
protected transient SET KEY_GENERIC_TYPE keySet;
protected transient ORDERED_SET KEY_GENERIC_TYPE keySet;
/** Values cache */
protected transient VALUE_COLLECTION VALUE_GENERIC_TYPE valuesC;
@@ -429,13 +430,13 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_
}
@Override
public ObjectSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> ENTRY_SET() {
public ObjectOrderedSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> ENTRY_SET() {
if(entrySet == null) entrySet = new MapEntrySet();
return entrySet;
}
@Override
public SET KEY_GENERIC_TYPE keySet() {
public ORDERED_SET KEY_GENERIC_TYPE keySet() {
if(keySet == null) keySet = new KeySet();
return keySet;
}
@@ -27,7 +27,6 @@ import speiger.src.collections.PACKAGE.maps.interfaces.MAP;
import speiger.src.collections.PACKAGE.maps.interfaces.ORDERED_MAP;
import speiger.src.collections.PACKAGE.sets.ABSTRACT_SET;
import speiger.src.collections.PACKAGE.sets.ORDERED_SET;
import speiger.src.collections.PACKAGE.sets.SET;
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;
@@ -90,7 +89,7 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN
/** Amount of Elements stored in the ArrayMap */
protected int size = 0;
/** KeySet cache */
protected SET KEY_GENERIC_TYPE keySet;
protected ORDERED_SET KEY_GENERIC_TYPE keySet;
/** Values cache */
protected VALUE_COLLECTION VALUE_GENERIC_TYPE valuesC;
/** EntrySet cache */
@@ -270,7 +269,7 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN
@Override
public boolean moveToLast(KEY_TYPE key) {
int index = findIndex(key);
if(index < size-1) {
if(index >= 0 && index < size-1) {
moveIndexToLast(index);
return true;
}
@@ -437,7 +436,7 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN
}
@Override
public SET KEY_GENERIC_TYPE keySet() {
public ORDERED_SET KEY_GENERIC_TYPE keySet() {
if(keySet == null) keySet = new KeySet();
return keySet;
}
@@ -1249,7 +1248,9 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN
@Override
public void remove() {
super.remove();
entry.index = -1;
if(entry != null && entry.index != -1) {
entry.index = -1;
}
}
@Override
@@ -1263,6 +1264,7 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN
public KeyIterator(KEY_TYPE element) {
index = findIndex(element);
}
@Override
public KEY_TYPE PREVIOUS() {
return keys[previousEntry()];
@@ -1319,8 +1321,7 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN
}
public void remove() {
if(lastReturned == -1)
throw new IllegalStateException();
if(lastReturned == -1) throw new IllegalStateException();
removeIndex(lastReturned);
if(lastReturned < index)
index--;
@@ -1329,8 +1330,8 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN
public int previousEntry() {
if(!hasPrevious()) throw new NoSuchElementException();
lastReturned = index;
return index--;
index--;
return (lastReturned = index);
}
public int nextEntry() {
@@ -1341,8 +1342,9 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN
public int skip(int amount) {
if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed");
int steps = Math.min(amount, (size() - 1) - index);
int steps = Math.min(amount, size() - index);
index += steps;
if(steps > 0) lastReturned = Math.min(index-1, size()-1);
return steps;
}
@@ -1350,6 +1352,7 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN
if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed");
int steps = Math.min(amount, index);
index -= steps;
if(steps > 0) lastReturned = Math.min(index, size()-1);
return steps;
}
}
@@ -17,7 +17,6 @@ import speiger.src.collections.PACKAGE.maps.interfaces.MAP;
import speiger.src.collections.PACKAGE.maps.interfaces.ORDERED_MAP;
import speiger.src.collections.PACKAGE.sets.ABSTRACT_SET;
import speiger.src.collections.PACKAGE.sets.ORDERED_SET;
import speiger.src.collections.PACKAGE.sets.SET;
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;
@@ -29,7 +28,6 @@ import speiger.src.collections.VALUE_PACKAGE.lists.VALUE_LIST_ITERATOR;
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.ObjectSet;
#endif
/**
@@ -315,15 +313,15 @@ public class LINKED_ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ENUM_MAP KEY_VA
}
@Override
public ObjectSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> ENTRY_SET() {
public ObjectOrderedSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> ENTRY_SET() {
if(entrySet == null) entrySet = new MapEntrySet();
return entrySet;
return (ObjectOrderedSet<MAP.Entry KEY_VALUE_GENERIC_TYPE>)entrySet;
}
@Override
public SET KEY_GENERIC_TYPE keySet() {
public ORDERED_SET KEY_GENERIC_TYPE keySet() {
if(keySet == null) keySet = new KeySet();
return keySet;
return (ORDERED_SET KEY_GENERIC_TYPE)keySet;
}
@Override
@@ -2051,26 +2051,36 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_
class DecsendingSubEntryIterator extends SubMapEntryIterator implements ObjectBidirectionalIterator<MAP.Entry KEY_VALUE_GENERIC_TYPE>
{
public DecsendingSubEntryIterator(Node KEY_VALUE_GENERIC_TYPE first, Node KEY_VALUE_GENERIC_TYPE forwardFence, Node KEY_VALUE_GENERIC_TYPE backwardFence) {
super(first, forwardFence, backwardFence);
super(first, forwardFence, backwardFence, false);
}
@Override
protected Node KEY_VALUE_GENERIC_TYPE moveNext(Node KEY_VALUE_GENERIC_TYPE node) {
return node.previous();
}
@Override
protected Node KEY_VALUE_GENERIC_TYPE movePrevious(Node KEY_VALUE_GENERIC_TYPE node) {
return node.next();
}
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE previous() {
if(!hasPrevious()) throw new NoSuchElementException();
return nextEntry();
return previousEntry();
}
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE next() {
if(!hasNext()) throw new NoSuchElementException();
return previousEntry();
return nextEntry();
}
}
class AcsendingSubEntryIterator extends SubMapEntryIterator implements ObjectBidirectionalIterator<MAP.Entry KEY_VALUE_GENERIC_TYPE>
{
public AcsendingSubEntryIterator(Node KEY_VALUE_GENERIC_TYPE first, Node KEY_VALUE_GENERIC_TYPE forwardFence, Node KEY_VALUE_GENERIC_TYPE backwardFence) {
super(first, forwardFence, backwardFence);
super(first, forwardFence, backwardFence, true);
}
@Override
@@ -2089,26 +2099,36 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_
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);
super(first, forwardFence, backwardFence, false);
}
@Override
protected Node KEY_VALUE_GENERIC_TYPE moveNext(Node KEY_VALUE_GENERIC_TYPE node) {
return node.previous();
}
@Override
protected Node KEY_VALUE_GENERIC_TYPE movePrevious(Node KEY_VALUE_GENERIC_TYPE node) {
return node.next();
}
@Override
public KEY_TYPE PREVIOUS() {
if(!hasPrevious()) throw new NoSuchElementException();
return nextEntry().key;
return previousEntry().key;
}
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
return previousEntry().key;
return nextEntry().key;
}
}
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);
super(first, forwardFence, backwardFence, true);
}
@Override
@@ -2127,7 +2147,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_
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);
super(first, forwardFence, backwardFence, true);
}
@Override
@@ -2146,39 +2166,60 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_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);
super(first, forwardFence, backwardFence, false);
}
@Override
protected Node KEY_VALUE_GENERIC_TYPE moveNext(Node KEY_VALUE_GENERIC_TYPE node) {
return node.previous();
}
@Override
protected Node KEY_VALUE_GENERIC_TYPE movePrevious(Node KEY_VALUE_GENERIC_TYPE node) {
return node.next();
}
@Override
public VALUE_TYPE VALUE_PREVIOUS() {
if(!hasPrevious()) throw new NoSuchElementException();
return nextEntry().value;
return previousEntry().value;
}
@Override
public VALUE_TYPE VALUE_NEXT() {
if(!hasNext()) throw new NoSuchElementException();
return previousEntry().value;
return nextEntry().value;
}
}
abstract class SubMapEntryIterator
{
final boolean isForward;
boolean wasForward;
Node KEY_VALUE_GENERIC_TYPE lastReturned;
Node KEY_VALUE_GENERIC_TYPE next;
Node KEY_VALUE_GENERIC_TYPE previous;
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)
{
public SubMapEntryIterator(Node KEY_VALUE_GENERIC_TYPE first, Node KEY_VALUE_GENERIC_TYPE forwardFence, Node KEY_VALUE_GENERIC_TYPE backwardFence, boolean isForward) {
next = first;
previous = first == null ? null : movePrevious(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;
this.isForward = isForward;
}
protected Node KEY_VALUE_GENERIC_TYPE moveNext(Node KEY_VALUE_GENERIC_TYPE node) {
return node.next();
}
protected Node KEY_VALUE_GENERIC_TYPE movePrevious(Node KEY_VALUE_GENERIC_TYPE node) {
return node.previous();
}
public boolean hasNext() {
@@ -2187,26 +2228,30 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_
protected Node KEY_VALUE_GENERIC_TYPE nextEntry() {
lastReturned = next;
previous = next;
Node KEY_VALUE_GENERIC_TYPE result = next;
next = next.next();
wasForward = true;
next = moveNext(next);
wasForward = isForward;
return result;
}
public boolean hasPrevious() {
return next != null && (unboundBackwardFence || next.key != backwardFence);
return previous != null && (unboundBackwardFence || previous.key != backwardFence);
}
protected Node KEY_VALUE_GENERIC_TYPE previousEntry() {
lastReturned = next;
Node KEY_VALUE_GENERIC_TYPE result = next;
next = next.previous();
wasForward = false;
lastReturned = previous;
next = previous;
Node KEY_VALUE_GENERIC_TYPE result = previous;
previous = movePrevious(previous);
wasForward = !isForward;
return result;
}
public void remove() {
if(lastReturned == null) throw new IllegalStateException();
if(next == lastReturned) next = moveNext(next);
if(previous == lastReturned) previous = movePrevious(previous);
if(wasForward && lastReturned.needsSuccessor()) next = lastReturned;
map.removeNode(lastReturned);
lastReturned = null;
@@ -2520,19 +2565,29 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_
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;
super(first, false);
}
@Override
protected Node KEY_VALUE_GENERIC_TYPE moveNext(Node KEY_VALUE_GENERIC_TYPE node) {
return node.previous();
}
@Override
protected Node KEY_VALUE_GENERIC_TYPE movePrevious(Node KEY_VALUE_GENERIC_TYPE node) {
return node.next();
}
@Override
public KEY_TYPE PREVIOUS() {
if(!hasPrevious()) throw new NoSuchElementException();
return previousEntry().key;
}
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
return previousEntry().key;
return nextEntry().key;
}
}
@@ -2540,7 +2595,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_
{
public AscendingMapEntryIterator(Node KEY_VALUE_GENERIC_TYPE first)
{
super(first);
super(first, true);
}
@Override
@@ -2559,7 +2614,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_
class AscendingValueIterator extends MapEntryIterator implements VALUE_BI_ITERATOR VALUE_GENERIC_TYPE
{
public AscendingValueIterator(Node KEY_VALUE_GENERIC_TYPE first) {
super(first);
super(first, true);
}
@Override
@@ -2578,7 +2633,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_
class AscendingKeyIterator extends MapEntryIterator implements BI_ITERATOR KEY_GENERIC_TYPE
{
public AscendingKeyIterator(Node KEY_VALUE_GENERIC_TYPE first) {
super(first);
super(first, true);
}
@Override
@@ -2596,13 +2651,24 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_
abstract class MapEntryIterator
{
final boolean isForward;
boolean wasMoved = false;
Node KEY_VALUE_GENERIC_TYPE lastReturned;
Node KEY_VALUE_GENERIC_TYPE next;
Node KEY_VALUE_GENERIC_TYPE previous;
public MapEntryIterator(Node KEY_VALUE_GENERIC_TYPE first)
{
public MapEntryIterator(Node KEY_VALUE_GENERIC_TYPE first, boolean isForward) {
next = first;
previous = first == null ? null : movePrevious(first);
this.isForward = isForward;
}
protected Node KEY_VALUE_GENERIC_TYPE moveNext(Node KEY_VALUE_GENERIC_TYPE node) {
return node.next();
}
protected Node KEY_VALUE_GENERIC_TYPE movePrevious(Node KEY_VALUE_GENERIC_TYPE node) {
return node.previous();
}
public boolean hasNext() {
@@ -2611,26 +2677,30 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_
protected Node KEY_VALUE_GENERIC_TYPE nextEntry() {
lastReturned = next;
previous = next;
Node KEY_VALUE_GENERIC_TYPE result = next;
next = next.next();
wasMoved = true;
next = moveNext(next);
wasMoved = isForward;
return result;
}
public boolean hasPrevious() {
return next != null;
return previous != null;
}
protected Node KEY_VALUE_GENERIC_TYPE previousEntry() {
lastReturned = next;
Node KEY_VALUE_GENERIC_TYPE result = next;
next = next.previous();
wasMoved = false;
lastReturned = previous;
next = previous;
Node KEY_VALUE_GENERIC_TYPE result = previous;
previous = movePrevious(previous);
wasMoved = !isForward;
return result;
}
public void remove() {
if(lastReturned == null) throw new IllegalStateException();
if(next == lastReturned) next = moveNext(next);
if(previous == lastReturned) previous = movePrevious(previous);
if(wasMoved && lastReturned.needsSuccessor()) next = lastReturned;
removeNode(lastReturned);
lastReturned = null;
@@ -2118,26 +2118,36 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G
class DecsendingSubEntryIterator extends SubMapEntryIterator implements ObjectBidirectionalIterator<MAP.Entry KEY_VALUE_GENERIC_TYPE>
{
public DecsendingSubEntryIterator(Node KEY_VALUE_GENERIC_TYPE first, Node KEY_VALUE_GENERIC_TYPE forwardFence, Node KEY_VALUE_GENERIC_TYPE backwardFence) {
super(first, forwardFence, backwardFence);
super(first, forwardFence, backwardFence, false);
}
@Override
protected Node KEY_VALUE_GENERIC_TYPE moveNext(Node KEY_VALUE_GENERIC_TYPE node) {
return node.previous();
}
@Override
protected Node KEY_VALUE_GENERIC_TYPE movePrevious(Node KEY_VALUE_GENERIC_TYPE node) {
return node.next();
}
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE previous() {
if(!hasPrevious()) throw new NoSuchElementException();
return nextEntry();
return previousEntry();
}
@Override
public MAP.Entry KEY_VALUE_GENERIC_TYPE next() {
if(!hasNext()) throw new NoSuchElementException();
return previousEntry();
return nextEntry();
}
}
class AcsendingSubEntryIterator extends SubMapEntryIterator implements ObjectBidirectionalIterator<MAP.Entry KEY_VALUE_GENERIC_TYPE>
{
public AcsendingSubEntryIterator(Node KEY_VALUE_GENERIC_TYPE first, Node KEY_VALUE_GENERIC_TYPE forwardFence, Node KEY_VALUE_GENERIC_TYPE backwardFence) {
super(first, forwardFence, backwardFence);
super(first, forwardFence, backwardFence, true);
}
@Override
@@ -2156,26 +2166,36 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G
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);
super(first, forwardFence, backwardFence, false);
}
@Override
protected Node KEY_VALUE_GENERIC_TYPE moveNext(Node KEY_VALUE_GENERIC_TYPE node) {
return node.previous();
}
@Override
protected Node KEY_VALUE_GENERIC_TYPE movePrevious(Node KEY_VALUE_GENERIC_TYPE node) {
return node.next();
}
@Override
public KEY_TYPE PREVIOUS() {
if(!hasPrevious()) throw new NoSuchElementException();
return nextEntry().key;
return previousEntry().key;
}
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
return previousEntry().key;
return nextEntry().key;
}
}
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);
super(first, forwardFence, backwardFence, true);
}
@Override
@@ -2194,7 +2214,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G
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);
super(first, forwardFence, backwardFence, true);
}
@Override
@@ -2213,39 +2233,60 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G
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);
super(first, forwardFence, backwardFence, false);
}
@Override
protected Node KEY_VALUE_GENERIC_TYPE moveNext(Node KEY_VALUE_GENERIC_TYPE node) {
return node.previous();
}
@Override
protected Node KEY_VALUE_GENERIC_TYPE movePrevious(Node KEY_VALUE_GENERIC_TYPE node) {
return node.next();
}
@Override
public VALUE_TYPE VALUE_PREVIOUS() {
if(!hasPrevious()) throw new NoSuchElementException();
return nextEntry().value;
return previousEntry().value;
}
@Override
public VALUE_TYPE VALUE_NEXT() {
if(!hasNext()) throw new NoSuchElementException();
return previousEntry().value;
return nextEntry().value;
}
}
abstract class SubMapEntryIterator
{
final boolean isForward;
boolean wasForward;
Node KEY_VALUE_GENERIC_TYPE lastReturned;
Node KEY_VALUE_GENERIC_TYPE next;
Node KEY_VALUE_GENERIC_TYPE previous;
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)
{
public SubMapEntryIterator(Node KEY_VALUE_GENERIC_TYPE first, Node KEY_VALUE_GENERIC_TYPE forwardFence, Node KEY_VALUE_GENERIC_TYPE backwardFence, boolean isForward) {
next = first;
previous = first == null ? null : movePrevious(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;
this.isForward = isForward;
}
protected Node KEY_VALUE_GENERIC_TYPE moveNext(Node KEY_VALUE_GENERIC_TYPE node) {
return node.next();
}
protected Node KEY_VALUE_GENERIC_TYPE movePrevious(Node KEY_VALUE_GENERIC_TYPE node) {
return node.previous();
}
public boolean hasNext() {
@@ -2254,26 +2295,30 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G
protected Node KEY_VALUE_GENERIC_TYPE nextEntry() {
lastReturned = next;
previous = next;
Node KEY_VALUE_GENERIC_TYPE result = next;
next = next.next();
wasForward = true;
next = moveNext(next);
wasForward = isForward;
return result;
}
public boolean hasPrevious() {
return next != null && (unboundBackwardFence || next.key != backwardFence);
return previous != null && (unboundBackwardFence || previous.key != backwardFence);
}
protected Node KEY_VALUE_GENERIC_TYPE previousEntry() {
lastReturned = next;
Node KEY_VALUE_GENERIC_TYPE result = next;
next = next.previous();
wasForward = false;
lastReturned = previous;
next = previous;
Node KEY_VALUE_GENERIC_TYPE result = previous;
previous = movePrevious(previous);
wasForward = !isForward;
return result;
}
public void remove() {
if(lastReturned == null) throw new IllegalStateException();
if(next == lastReturned) next = moveNext(next);
if(previous == lastReturned) previous = movePrevious(previous);
if(wasForward && lastReturned.needsSuccessor()) next = lastReturned;
map.removeNode(lastReturned);
lastReturned = null;
@@ -2587,19 +2632,29 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G
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;
super(first, false);
}
@Override
protected Node KEY_VALUE_GENERIC_TYPE moveNext(Node KEY_VALUE_GENERIC_TYPE node) {
return node.previous();
}
@Override
protected Node KEY_VALUE_GENERIC_TYPE movePrevious(Node KEY_VALUE_GENERIC_TYPE node) {
return node.next();
}
@Override
public KEY_TYPE PREVIOUS() {
if(!hasPrevious()) throw new NoSuchElementException();
return previousEntry().key;
}
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
return previousEntry().key;
return nextEntry().key;
}
}
@@ -2607,7 +2662,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G
{
public AscendingMapEntryIterator(Node KEY_VALUE_GENERIC_TYPE first)
{
super(first);
super(first, true);
}
@Override
@@ -2626,7 +2681,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G
class AscendingValueIterator extends MapEntryIterator implements VALUE_BI_ITERATOR VALUE_GENERIC_TYPE
{
public AscendingValueIterator(Node KEY_VALUE_GENERIC_TYPE first) {
super(first);
super(first, true);
}
@Override
@@ -2645,7 +2700,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G
class AscendingKeyIterator extends MapEntryIterator implements BI_ITERATOR KEY_GENERIC_TYPE
{
public AscendingKeyIterator(Node KEY_VALUE_GENERIC_TYPE first) {
super(first);
super(first, true);
}
@Override
@@ -2663,13 +2718,24 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G
abstract class MapEntryIterator
{
final boolean isForward;
boolean wasMoved = false;
Node KEY_VALUE_GENERIC_TYPE lastReturned;
Node KEY_VALUE_GENERIC_TYPE next;
Node KEY_VALUE_GENERIC_TYPE previous;
public MapEntryIterator(Node KEY_VALUE_GENERIC_TYPE first)
{
public MapEntryIterator(Node KEY_VALUE_GENERIC_TYPE first, boolean isForward) {
next = first;
previous = first == null ? null : movePrevious(first);
this.isForward = isForward;
}
protected Node KEY_VALUE_GENERIC_TYPE moveNext(Node KEY_VALUE_GENERIC_TYPE node) {
return node.next();
}
protected Node KEY_VALUE_GENERIC_TYPE movePrevious(Node KEY_VALUE_GENERIC_TYPE node) {
return node.previous();
}
public boolean hasNext() {
@@ -2678,26 +2744,30 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G
protected Node KEY_VALUE_GENERIC_TYPE nextEntry() {
lastReturned = next;
previous = next;
Node KEY_VALUE_GENERIC_TYPE result = next;
next = next.next();
wasMoved = true;
next = moveNext(next);
wasMoved = isForward;
return result;
}
public boolean hasPrevious() {
return next != null;
return previous != null;
}
protected Node KEY_VALUE_GENERIC_TYPE previousEntry() {
lastReturned = next;
Node KEY_VALUE_GENERIC_TYPE result = next;
next = next.previous();
wasMoved = false;
lastReturned = previous;
next = previous;
Node KEY_VALUE_GENERIC_TYPE result = previous;
previous = movePrevious(previous);
wasMoved = !isForward;
return result;
}
public void remove() {
if(lastReturned == null) throw new IllegalStateException();
if(next == lastReturned) next = moveNext(next);
if(previous == lastReturned) previous = movePrevious(previous);
if(wasMoved && lastReturned.needsSuccessor()) next = lastReturned;
removeNode(lastReturned);
lastReturned = null;
@@ -1,6 +1,9 @@
package speiger.src.collections.PACKAGE.maps.interfaces;
import speiger.src.collections.PACKAGE.utils.maps.MAPS;
#if !TYPE_OBJECT
import speiger.src.collections.PACKAGE.sets.ORDERED_SET;
#endif
import speiger.src.collections.objects.collections.ObjectBidirectionalIterator;
import speiger.src.collections.objects.sets.ObjectOrderedSet;
/**
@@ -94,6 +97,10 @@ public interface ORDERED_MAP KEY_VALUE_GENERIC_TYPE extends MAP KEY_VALUE_GENERI
@Override
public ORDERED_MAP KEY_VALUE_GENERIC_TYPE copy();
@Override
public ORDERED_SET KEY_GENERIC_TYPE keySet();
@Override
public ObjectOrderedSet<MAP.Entry KEY_VALUE_GENERIC_TYPE> ENTRY_SET();
/**
* Creates a Wrapped SortedMap that is Synchronized