Fixed a bunch of Iterator code and implemented Reverse Map/Set Tests
This commit is contained in:
+42
-17
@@ -460,7 +460,7 @@ public class IMMUTABLE_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERI
|
||||
|
||||
@Override
|
||||
public LIST_ITERATOR KEY_GENERIC_TYPE reverseIterator() {
|
||||
return new ReverseBiIteratorBRACES(new SetIterator(false));
|
||||
return new SetIterator(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -526,17 +526,20 @@ public class IMMUTABLE_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERI
|
||||
}
|
||||
|
||||
private class SetIterator implements LIST_ITERATOR KEY_GENERIC_TYPE {
|
||||
boolean forward;
|
||||
int previous = -1;
|
||||
int next = -1;
|
||||
int current = -1;
|
||||
int index = 0;
|
||||
int index = -1;
|
||||
|
||||
SetIterator(boolean start) {
|
||||
this.forward = start;
|
||||
if(start) next = firstIndex;
|
||||
else previous = lastIndex;
|
||||
}
|
||||
|
||||
SetIterator(KEY_TYPE from) {
|
||||
this.forward = true;
|
||||
if(KEY_EQUALS_NULL(from)) {
|
||||
if(containsNull) {
|
||||
next = (int) links[nullIndex];
|
||||
@@ -565,48 +568,60 @@ public class IMMUTABLE_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERI
|
||||
|
||||
@Override
|
||||
public int skip(int amount) {
|
||||
int result = 0;
|
||||
while(next != -1 && result != amount) {
|
||||
current = previous = next;
|
||||
next = (int)(links[current]);
|
||||
result++;
|
||||
}
|
||||
int result = forward ? moveForward(amount) : moveBackwards(amount);
|
||||
if(index >= 0) index+=result;
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int back(int amount) {
|
||||
int result = forward ? moveBackwards(amount) : moveForward(amount);
|
||||
if(index >= 0) index-=result;
|
||||
return result;
|
||||
}
|
||||
|
||||
private int moveForward(int amount) {
|
||||
int result = 0;
|
||||
while(next != -1 && result != amount) {
|
||||
current = previous = next;
|
||||
next = (int)(links[current]);
|
||||
result++;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private int moveBackwards(int amount) {
|
||||
int result = 0;
|
||||
while(previous != -1 && result != amount) {
|
||||
current = next = previous;
|
||||
previous = (int)(links[current] >> 32);
|
||||
result++;
|
||||
}
|
||||
if(index >= 0) index-=result;
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return next != -1;
|
||||
return (forward ? next : previous) != -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPrevious() {
|
||||
return previous != -1;
|
||||
return (forward ? previous : next) != -1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int nextIndex() {
|
||||
ensureIndexKnown();
|
||||
return index;
|
||||
if(forward) return index;
|
||||
return size - index;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int previousIndex() {
|
||||
ensureIndexKnown();
|
||||
return index - 1;
|
||||
if(forward) return index-1;
|
||||
return (size - index)-1;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -615,8 +630,8 @@ public class IMMUTABLE_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERI
|
||||
@Override
|
||||
public KEY_TYPE PREVIOUS() {
|
||||
if(!hasPrevious()) throw new NoSuchElementException();
|
||||
current = next = previous;
|
||||
previous = (int)(links[current] >> 32);
|
||||
if(forward) moveBackwards();
|
||||
else moveForwards();
|
||||
if(index >= 0) index--;
|
||||
return keys[current];
|
||||
}
|
||||
@@ -624,12 +639,22 @@ public class IMMUTABLE_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERI
|
||||
@Override
|
||||
public KEY_TYPE NEXT() {
|
||||
if(!hasNext()) throw new NoSuchElementException();
|
||||
current = previous = next;
|
||||
next = (int)(links[current]);
|
||||
if(forward) moveForwards();
|
||||
else moveBackwards();
|
||||
if(index >= 0) index++;
|
||||
return keys[current];
|
||||
}
|
||||
|
||||
private void moveBackwards() {
|
||||
current = next = previous;
|
||||
previous = (int)(links[current] >> 32);
|
||||
}
|
||||
|
||||
private void moveForwards() {
|
||||
current = previous = next;
|
||||
next = (int)(links[current]);
|
||||
}
|
||||
|
||||
private void ensureIndexKnown() {
|
||||
if(index == -1) {
|
||||
if(previous == -1) {
|
||||
|
||||
Reference in New Issue
Block a user