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
@@ -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;
}
}