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:
+40
-16
@@ -1359,6 +1359,7 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
|
||||
{
|
||||
Entry KEY_GENERIC_TYPE lastReturned;
|
||||
Entry KEY_GENERIC_TYPE next;
|
||||
Entry KEY_GENERIC_TYPE previous;
|
||||
boolean forwards = false;
|
||||
boolean unboundForwardFence;
|
||||
boolean unboundBackwardFence;
|
||||
@@ -1368,6 +1369,7 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
|
||||
public AscendingSubSetIterator(Entry KEY_GENERIC_TYPE first, Entry KEY_GENERIC_TYPE forwardFence, Entry KEY_GENERIC_TYPE backwardFence)
|
||||
{
|
||||
next = first;
|
||||
previous = first == null ? null : first.previous();
|
||||
this.forwardFence = forwardFence == null ? EMPTY_KEY_VALUE : forwardFence.key;
|
||||
this.backwardFence = backwardFence == null ? EMPTY_KEY_VALUE : backwardFence.key;
|
||||
unboundForwardFence = forwardFence == null;
|
||||
@@ -1383,6 +1385,7 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
|
||||
public KEY_TYPE NEXT() {
|
||||
if(!hasNext()) throw new NoSuchElementException();
|
||||
lastReturned = next;
|
||||
previous = next;
|
||||
KEY_TYPE result = next.key;
|
||||
next = next.next();
|
||||
forwards = true;
|
||||
@@ -1391,15 +1394,16 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
|
||||
|
||||
@Override
|
||||
public boolean hasPrevious() {
|
||||
return next != null && (unboundBackwardFence || next.key != backwardFence);
|
||||
return previous != null && (unboundBackwardFence || previous.key != backwardFence);
|
||||
}
|
||||
|
||||
@Override
|
||||
public KEY_TYPE PREVIOUS() {
|
||||
if(!hasPrevious()) throw new NoSuchElementException();
|
||||
lastReturned = next;
|
||||
KEY_TYPE result = next.key;
|
||||
next = next.previous();
|
||||
lastReturned = previous;
|
||||
next = previous;
|
||||
KEY_TYPE result = previous.key;
|
||||
previous = previous.previous();
|
||||
forwards = false;
|
||||
return result;
|
||||
}
|
||||
@@ -1407,6 +1411,8 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
|
||||
@Override
|
||||
public void remove() {
|
||||
if(lastReturned == null) throw new IllegalStateException();
|
||||
if(previous == lastReturned) previous = previous.previous();
|
||||
if(next == lastReturned) next = next.next();
|
||||
if(forwards && lastReturned.needsSuccessor()) next = lastReturned;
|
||||
set.removeNode(lastReturned);
|
||||
lastReturned = null;
|
||||
@@ -1417,6 +1423,7 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
|
||||
{
|
||||
Entry KEY_GENERIC_TYPE lastReturned;
|
||||
Entry KEY_GENERIC_TYPE next;
|
||||
Entry KEY_GENERIC_TYPE previous;
|
||||
boolean forwards = false;
|
||||
boolean unboundForwardFence;
|
||||
boolean unboundBackwardFence;
|
||||
@@ -1426,6 +1433,7 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
|
||||
public DescendingSubSetIterator(Entry KEY_GENERIC_TYPE first, Entry KEY_GENERIC_TYPE forwardFence, Entry KEY_GENERIC_TYPE backwardFence)
|
||||
{
|
||||
next = first;
|
||||
previous = first == null ? null : first.next();
|
||||
this.forwardFence = forwardFence == null ? EMPTY_KEY_VALUE : forwardFence.key;
|
||||
this.backwardFence = backwardFence == null ? EMPTY_KEY_VALUE : backwardFence.key;
|
||||
unboundForwardFence = forwardFence == null;
|
||||
@@ -1441,6 +1449,7 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
|
||||
public KEY_TYPE NEXT() {
|
||||
if(!hasNext()) throw new NoSuchElementException();
|
||||
lastReturned = next;
|
||||
previous = next;
|
||||
KEY_TYPE result = next.key;
|
||||
next = next.previous();
|
||||
forwards = false;
|
||||
@@ -1449,15 +1458,16 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
|
||||
|
||||
@Override
|
||||
public boolean hasPrevious() {
|
||||
return next != null && (unboundBackwardFence || next.key != backwardFence);
|
||||
return previous != null && (unboundBackwardFence || previous.key != backwardFence);
|
||||
}
|
||||
|
||||
@Override
|
||||
public KEY_TYPE PREVIOUS() {
|
||||
if(!hasPrevious()) throw new NoSuchElementException();
|
||||
lastReturned = next;
|
||||
KEY_TYPE result = next.key;
|
||||
next = next.next();
|
||||
lastReturned = previous;
|
||||
next = previous;
|
||||
KEY_TYPE result = previous.key;
|
||||
previous = previous.next();
|
||||
forwards = true;
|
||||
return result;
|
||||
}
|
||||
@@ -1465,6 +1475,8 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
|
||||
@Override
|
||||
public void remove() {
|
||||
if(lastReturned == null) throw new IllegalStateException();
|
||||
if(previous == lastReturned) previous = previous.next();
|
||||
if(next == lastReturned) next = next.previous();
|
||||
if(forwards && lastReturned.needsSuccessor()) next = lastReturned;
|
||||
set.removeNode(lastReturned);
|
||||
lastReturned = null;
|
||||
@@ -1476,11 +1488,13 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
|
||||
{
|
||||
Entry KEY_GENERIC_TYPE lastReturned;
|
||||
Entry KEY_GENERIC_TYPE next;
|
||||
Entry KEY_GENERIC_TYPE previous;
|
||||
boolean forwards = false;
|
||||
|
||||
public AscendingSetIterator(Entry KEY_GENERIC_TYPE first)
|
||||
{
|
||||
next = first;
|
||||
previous = first == null ? null : first.previous();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -1492,6 +1506,7 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
|
||||
public KEY_TYPE NEXT() {
|
||||
if(!hasNext()) throw new NoSuchElementException();
|
||||
lastReturned = next;
|
||||
previous = next;
|
||||
KEY_TYPE result = next.key;
|
||||
next = next.next();
|
||||
forwards = true;
|
||||
@@ -1500,15 +1515,16 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
|
||||
|
||||
@Override
|
||||
public boolean hasPrevious() {
|
||||
return next != null;
|
||||
return previous != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public KEY_TYPE PREVIOUS() {
|
||||
if(!hasPrevious()) throw new NoSuchElementException();
|
||||
lastReturned = next;
|
||||
KEY_TYPE result = next.key;
|
||||
next = next.previous();
|
||||
lastReturned = previous;
|
||||
next = previous;
|
||||
KEY_TYPE result = previous.key;
|
||||
previous = previous.previous();
|
||||
forwards = false;
|
||||
return result;
|
||||
}
|
||||
@@ -1516,6 +1532,8 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
|
||||
@Override
|
||||
public void remove() {
|
||||
if(lastReturned == null) throw new IllegalStateException();
|
||||
if(lastReturned == previous) previous = previous.previous();
|
||||
if(lastReturned == next) next = next.next();
|
||||
if(forwards && lastReturned.needsSuccessor()) next = lastReturned;
|
||||
removeNode(lastReturned);
|
||||
lastReturned = null;
|
||||
@@ -1526,11 +1544,13 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
|
||||
{
|
||||
Entry KEY_GENERIC_TYPE lastReturned;
|
||||
Entry KEY_GENERIC_TYPE next;
|
||||
Entry KEY_GENERIC_TYPE previous;
|
||||
boolean forwards = false;
|
||||
|
||||
public DescendingSetIterator(Entry KEY_GENERIC_TYPE first)
|
||||
{
|
||||
next = first;
|
||||
previous = first == null ? null : first.next();
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -1542,6 +1562,7 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
|
||||
public KEY_TYPE NEXT() {
|
||||
if(!hasNext()) throw new NoSuchElementException();
|
||||
lastReturned = next;
|
||||
previous = next;
|
||||
KEY_TYPE result = next.key;
|
||||
next = next.previous();
|
||||
forwards = false;
|
||||
@@ -1550,15 +1571,16 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
|
||||
|
||||
@Override
|
||||
public boolean hasPrevious() {
|
||||
return next != null;
|
||||
return previous != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public KEY_TYPE PREVIOUS() {
|
||||
if(!hasPrevious()) throw new NoSuchElementException();
|
||||
lastReturned = next;
|
||||
KEY_TYPE result = next.key;
|
||||
next = next.next();
|
||||
lastReturned = previous;
|
||||
next = previous;
|
||||
KEY_TYPE result = previous.key;
|
||||
previous = previous.next();
|
||||
forwards = true;
|
||||
return result;
|
||||
}
|
||||
@@ -1566,6 +1588,8 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE
|
||||
@Override
|
||||
public void remove() {
|
||||
if(lastReturned == null) throw new IllegalStateException();
|
||||
if(lastReturned == previous) previous = previous.next();
|
||||
if(lastReturned == next) next = next.previous();
|
||||
if(forwards && lastReturned.needsSuccessor()) next = lastReturned;
|
||||
removeNode(lastReturned);
|
||||
lastReturned = null;
|
||||
|
||||
Reference in New Issue
Block a user