Fixed Bugs with Java Iterators not throwing the correct exception.

This commit is contained in:
Speiger 2022-05-17 10:45:05 +02:00
parent 31b34f5de1
commit 068cc4a4f7
9 changed files with 44 additions and 33 deletions

View File

@ -1263,7 +1263,7 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
@Override
public long estimateSize() {
return list.size - index;
return (long)list.size - (long)index;
}
@Override
@ -1329,7 +1329,7 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE
@Override
public long estimateSize() {
return list.size - index;
return (long)list.size - (long)index;
}
@Override

View File

@ -434,6 +434,7 @@ public class ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE extends ABSTRACT_PRIORITY_QUEUE K
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
KEY_TYPE value = array[index];
removeIndex(index);
index = ++index % array.length;

View File

@ -187,7 +187,7 @@ public class ARRAY_PRIORITY_QUEUE KEY_GENERIC_TYPE extends ABSTRACT_PRIORITY_QUE
@Override
public void enqueue(KEY_TYPE e) {
if(size == array.length) array = Arrays.copyOf(array, (int)Math.max(Math.min((long)array.length + (array.length >> 1), SanityChecks.MAX_ARRAY_SIZE), size+1));
if(size == array.length) array = Arrays.copyOf(array, (int)Math.max(Math.min((long)array.length + (long)(array.length >> 1), (long)SanityChecks.MAX_ARRAY_SIZE), size+1));
if(firstIndex != -1){
int compare = comparator == null ? COMPAREABLE_TO_KEY(e, array[firstIndex]) : comparator.compare(e, array[firstIndex]);
if(compare < 0) firstIndex = size;
@ -414,6 +414,7 @@ public class ARRAY_PRIORITY_QUEUE KEY_GENERIC_TYPE extends ABSTRACT_PRIORITY_QUE
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
return dequeue();
}
}

View File

@ -207,7 +207,7 @@ public class HEAP_PRIORITY_QUEUE KEY_GENERIC_TYPE extends ABSTRACT_PRIORITY_QUEU
@Override
public void enqueue(KEY_TYPE e) {
if(size == array.length) array = Arrays.copyOf(array, (int)Math.max(Math.min((long)array.length + (array.length >> 1), SanityChecks.MAX_ARRAY_SIZE), size+1));
if(size == array.length) array = Arrays.copyOf(array, (int)Math.max(Math.min((long)array.length + (long)(array.length >> 1), (long)SanityChecks.MAX_ARRAY_SIZE), size+1));
array[size++] = e;
ARRAYS.shiftUp(array, size-1, comparator);
}
@ -389,6 +389,7 @@ public class HEAP_PRIORITY_QUEUE KEY_GENERIC_TYPE extends ABSTRACT_PRIORITY_QUEU
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
return dequeue();
}
}

View File

@ -34,7 +34,7 @@ public class ITERATORS
/**
* Empty Iterator Reference
*/
public static final EmptyIterator NO_GENERIC_TYPE EMPTY = new EmptyIteratorBRACES();
private static final EmptyIterator NO_GENERIC_TYPE EMPTY = new EmptyIteratorBRACES();
/**
* Returns a Immutable EmptyIterator instance that is automatically casted.
@ -735,7 +735,7 @@ public class ITERATORS
@Override
public KEY_TYPE NEXT() {
return EMPTY_KEY_VALUE;
throw new NoSuchElementException();
}
@Override
@ -745,7 +745,7 @@ public class ITERATORS
@Override
public KEY_TYPE PREVIOUS() {
return EMPTY_KEY_VALUE;
throw new NoSuchElementException();
}
@Override
@ -787,6 +787,7 @@ public class ITERATORS
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
return a[from++];
}
@ -854,7 +855,7 @@ public class ITERATORS
@Override
public T next() {
if(!hasNext()) throw new IllegalStateException("End of Iterator");
if(!hasNext()) throw new NoSuchElementException();
T result = last.next();
foundNext = false;
return result;
@ -890,7 +891,7 @@ public class ITERATORS
@Override
public T next() {
if(!hasNext()) throw new IllegalStateException("End of Iterator");
if(!hasNext()) throw new NoSuchElementException();
T result = last.next();
foundNext = false;
return result;
@ -921,7 +922,7 @@ public class ITERATORS
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new IllegalStateException("End of Iterator");
if(!hasNext()) throw new NoSuchElementException();
return sortedElements.GET_KEY(index++);
}
}
@ -971,7 +972,7 @@ public class ITERATORS
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new IllegalStateException("End of Iterator");
if(!hasNext()) throw new NoSuchElementException();
foundNext = false;
return lastFound;
}
@ -1008,7 +1009,7 @@ public class ITERATORS
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new IllegalStateException("End of Iterator");
if(!hasNext()) throw new NoSuchElementException();
foundNext = false;
return lastFound;
}
@ -1031,7 +1032,7 @@ public class ITERATORS
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new IllegalStateException("End of Iterator");
if(!hasNext()) throw new NoSuchElementException();
limit--;
return iterator.NEXT();
}
@ -1054,7 +1055,7 @@ public class ITERATORS
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new IllegalStateException("End of Iterator");
if(!hasNext()) throw new NoSuchElementException();
KEY_TYPE result = iterator.NEXT();
action.accept(result);
return result;

View File

@ -27,7 +27,7 @@ public class LISTS
/**
* Empty List reference
*/
public static final EmptyList NO_GENERIC_TYPE EMPTY = new EmptyListBRACES();
private static final EmptyList NO_GENERIC_TYPE EMPTY = new EmptyListBRACES();
/**
* Returns a Immutable EmptyList instance that is automatically casted.

View File

@ -1,5 +1,6 @@
package speiger.src.collections.PACKAGE.utils;
import java.util.NoSuchElementException;
#if TYPE_BOOLEAN
import speiger.src.collections.booleans.collections.BooleanIterator;
import speiger.src.collections.booleans.sets.AbstractBooleanSet;
@ -34,7 +35,7 @@ public class SETS
/**
* Empty Set Variable
*/
public static final SET NO_GENERIC_TYPE EMPTY = new EmptySetBRACES();
private static final SET NO_GENERIC_TYPE EMPTY = new EmptySetBRACES();
/**
* EmptySet getter
@ -216,10 +217,10 @@ public class SETS
return new ITERATOR KEY_GENERIC_TYPE() {
boolean next = true;
@Override
public boolean hasNext() { return next = false; }
public boolean hasNext() { return next; }
@Override
public KEY_TYPE NEXT() {
if(!next) throw new IllegalStateException();
if(!hasNext()) throw new NoSuchElementException();
next = false;
return element;
}

View File

@ -1,6 +1,7 @@
package speiger.src.collections.PACKAGE.utils;
import java.util.Comparator;
import java.util.NoSuchElementException;
import java.util.Spliterator;
#if PRIMITIVES
import java.util.Spliterator.JAVA_SPLIT_ITERATOR;
@ -329,7 +330,11 @@ public class SPLIT_ITERATORS
}
@Override
public KEY_TYPE NEXT() { return array[index++]; }
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
return array[index++];
}
@Override
public boolean hasNext() { return index < fence; }
}

View File

@ -57,7 +57,21 @@ public class MAPS
/**
* Empty Map Variable
*/
public static final MAP NO_KV_GENERIC_TYPE EMPTY = new EmptyMapKV_BRACES();
private static final MAP NO_KV_GENERIC_TYPE EMPTY = new EmptyMapKV_BRACES();
/**
* Empty Map getter function that autocasts to the desired Key and Value
* @Type(T)
* @ValueType(V)
* @return empty map of desired type
*/
public static GENERIC_KEY_VALUE_BRACES MAP KEY_VALUE_GENERIC_TYPE empty() {
#if TYPE_OBJECT || VALUE_OBJECT
return (MAP KEY_VALUE_GENERIC_TYPE)EMPTY;
#else
return EMPTY;
#endif
}
#endif
/**
@ -104,19 +118,6 @@ public class MAPS
}
#if !TYPE_BOOLEAN
/**
* Empty Map getter function that autocasts to the desired Key and Value
* @Type(T)
* @ValueType(V)
* @return empty map of desired type
*/
public static GENERIC_KEY_VALUE_BRACES MAP KEY_VALUE_GENERIC_TYPE empty() {
#if TYPE_OBJECT || VALUE_OBJECT
return (MAP KEY_VALUE_GENERIC_TYPE)EMPTY;
#else
return EMPTY;
#endif
}
/**
* Helper function that creates a Helper wrapper to synchronize access into the map.