Primitive-Collections/src/builder/resources/speiger/assets/collections/templates/utils/Iterators.template

1266 lines
42 KiB
Plaintext

package speiger.src.collections.PACKAGE.utils;
import java.util.Iterator;
import java.util.NoSuchElementException;
#if TYPE_OBJECT
import java.util.Comparator;
#endif
import java.util.function.Consumer;
#if JDK_FUNCTION
import java.util.function.PREDICATE;
#endif
import speiger.src.collections.PACKAGE.collections.ITERATOR;
#if !TYPE_OBJECT
import speiger.src.collections.objects.collections.ObjectIterator;
import speiger.src.collections.objects.utils.ObjectIterators;
import speiger.src.collections.PACKAGE.functions.CONSUMER;
import speiger.src.collections.PACKAGE.functions.COMPARATOR;
#else
#if BOOLEAN_COLLECTION_MODULE
import speiger.src.collections.booleans.collections.BooleanIterator;
#endif
#iterate
#argument ITERATOR ByteIterator ShortIterator IntIterator LongIterator FloatIterator DoubleIterator
#argument PACKAGE bytes shorts ints longs floats doubles
#argument MAPPER ToByteFunction ToShortFunction ToIntFunction ToLongFunction ToFloatFunction ToDoubleFunction
#argument FILTER_TYPE BYTE_COLLECTION_MODULE SHORT_COLLECTION_MODULE INT_COLLECTION_MODULE LONG_COLLECTION_MODULE FLOAT_COLLECTION_MODULE DOUBLE_COLLECTION_MODULE
#if BOOLEAN_COLLECTION_MODULE
import speiger.src.collections.objects.functions.function.MAPPER;
import speiger.src.collections.PACKAGE.collections.ITERATOR;
#endif
#enditerate
#endif
import speiger.src.collections.PACKAGE.functions.function.TO_OBJECT_FUNCTION;
import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER;
#if !JDK_FUNCTION
import speiger.src.collections.PACKAGE.functions.function.PREDICATE;
#endif
#if ARRAY_LIST_FEATURE || LINKED_LIST_FEATURE
import speiger.src.collections.PACKAGE.lists.LIST;
#if ARRAY_LIST_FEATURE
import speiger.src.collections.PACKAGE.lists.ARRAY_LIST;
#else if LINKED_LIST_FEATURE
import speiger.src.collections.PACKAGE.lists.LINKED_LIST;
#endif
#endif
import speiger.src.collections.PACKAGE.lists.LIST_ITERATOR;
import speiger.src.collections.PACKAGE.collections.BI_ITERATOR;
import speiger.src.collections.PACKAGE.collections.COLLECTION;
import speiger.src.collections.PACKAGE.utils.COLLECTIONS.CollectionWrapper;
/**
* A Helper class for Iterators
*/
public class ITERATORS
{
/**
* Empty Iterator Reference
*/
private static final EmptyIterator NO_GENERIC_TYPE EMPTY = new EmptyIteratorBRACES();
/**
* Returns a Immutable EmptyIterator instance that is automatically casted.
* @Type(T)
* @return an empty iterator
*/
public static GENERIC_KEY_BRACES EmptyIterator KEY_GENERIC_TYPE empty() {
#if TYPE_OBJECT
return (EmptyIterator<KEY_TYPE>)EMPTY;
#else
return EMPTY;
#endif
}
/**
* Inverter function for Bidirectional Iterators
* @param it the iterator that should be inverted
* @Type(T)
* @return a Inverted Bidirectional Iterator. If it was inverted then it just gives back the original reference
*/
public static GENERIC_KEY_BRACES BI_ITERATOR KEY_GENERIC_TYPE invert(BI_ITERATOR KEY_GENERIC_TYPE it) {
return it instanceof ReverseBiIterator ? ((ReverseBiIterator KEY_GENERIC_TYPE)it).it : new ReverseBiIteratorBRACES(it);
}
/**
* Inverter function for List Iterators
* @param it the iterator that should be inverted
* @Type(T)
* @return a Inverted List Iterator. If it was inverted then it just gives back the original reference
*/
public static GENERIC_KEY_BRACES LIST_ITERATOR KEY_GENERIC_TYPE invert(LIST_ITERATOR KEY_GENERIC_TYPE it) {
return it instanceof ReverseListIterator ? ((ReverseListIterator KEY_GENERIC_TYPE)it).it : new ReverseListIteratorBRACES(it);
}
/**
* Returns a Immutable Iterator instance based on the instance given.
* @param iterator that should be made immutable/unmodifiable
* @Type(T)
* @return a unmodifiable iterator wrapper. If the Iterator already a unmodifiable wrapper then it just returns itself.
*/
public static GENERIC_KEY_BRACES ITERATOR KEY_GENERIC_TYPE unmodifiable(ITERATOR KEY_GENERIC_TYPE iterator) {
return iterator instanceof UnmodifiableIterator ? iterator : new UnmodifiableIteratorBRACES(iterator);
}
/**
* Returns a Immutable Iterator instance based on the instance given.
* @param iterator that should be made immutable/unmodifiable
* @Type(T)
* @return a unmodifiable iterator wrapper. If the Iterator already a unmodifiable wrapper then it just returns itself.
*/
public static GENERIC_KEY_BRACES BI_ITERATOR KEY_GENERIC_TYPE unmodifiable(BI_ITERATOR KEY_GENERIC_TYPE iterator) {
return iterator instanceof UnmodifiableBiIterator ? iterator : new UnmodifiableBiIteratorBRACES(iterator);
}
/**
* Returns a Immutable ListIterator instance based on the instance given.
* @param iterator that should be made immutable/unmodifiable
* @Type(T)
* @return a unmodifiable listiterator wrapper. If the ListIterator already a unmodifiable wrapper then it just returns itself.
*/
public static GENERIC_KEY_BRACES LIST_ITERATOR KEY_GENERIC_TYPE unmodifiable(LIST_ITERATOR KEY_GENERIC_TYPE iterator) {
return iterator instanceof UnmodifiableListIterator ? iterator : new UnmodifiableListIteratorBRACES(iterator);
}
#if OBJECT_COLLECTION_MODULE
/**
* A Helper function that maps a Java-Iterator into a new Type.
* @param iterator that should be mapped
* @param mapper the function that decides what the result turns into.
* @Type(T)
* @param <E> The return type.
* @return a iterator that is mapped to a new result
*/
public static GENERIC_KEY_SPECIAL_BRACES<E> ObjectIterator<E> map(Iterator<? extends CLASS_TYPE> iterator, TO_OBJECT_FUNCTION KKS_GENERIC_TYPE<E> mapper) {
return new MappedIterator<>(wrap(iterator), mapper);
}
/**
* A Helper function that maps a Iterator into a new Type.
* @param iterator that should be mapped
* @param mapper the function that decides what the result turns into.
* @Type(T)
* @param <E> The return type.
* @return a iterator that is mapped to a new result
*/
public static GENERIC_KEY_SPECIAL_BRACES<E> ObjectIterator<E> map(ITERATOR KEY_GENERIC_TYPE iterator, TO_OBJECT_FUNCTION KKS_GENERIC_TYPE<E> mapper) {
return new MappedIterator<>(iterator, mapper);
}
#endif
#if TYPE_OBJECT
#iterate
#argument MAPPED_TYPE MappedBoolean MappedByte MappedShort MappedInt MappedLong MappedFloat MappedDouble
#argument OUTPUT_ITERATOR BooleanIterator ByteIterator ShortIterator IntIterator LongIterator FloatIterator DoubleIterator
#argument MAPPER Predicate ToByteFunction ToShortFunction ToIntFunction ToLongFunction ToFloatFunction ToDoubleFunction
#argument DATA_TYPE Boolean Byte Short Int Long Float Double
#argument FILTER_TYPE BOOLEAN_COLLECTION_MODULE BYTE_COLLECTION_MODULE SHORT_COLLECTION_MODULE INT_COLLECTION_MODULE LONG_COLLECTION_MODULE FLOAT_COLLECTION_MODULE DOUBLE_COLLECTION_MODULE
#if FILTER_TYPE
/**
* A Helper function that maps a Java-Iterator into a new Type.
* @param iterator that should be mapped
* @param mapper the function that decides what the result turns into.
* @Type(T)
* @return a iterator that is mapped to a new result
*/
public static <T> OUTPUT_ITERATOR mapToDATA_TYPE(Iterator<? extends CLASS_TYPE> iterator, MAPPER<T> mapper) {
return new MAPPED_TYPEIterator<>(wrap(iterator), mapper);
}
/**
* A Helper function that maps a Iterator into a new Type.
* @param iterator that should be mapped
* @param mapper the function that decides what the result turns into.
* @Type(T)
* @return a iterator that is mapped to a new result
*/
public static <T> OUTPUT_ITERATOR mapToDATA_TYPE(ITERATOR KEY_GENERIC_TYPE iterator, MAPPER<T> mapper) {
return new MAPPED_TYPEIterator<>(iterator, mapper);
}
#endif
#enditerate
#endif
/**
* A Helper function that flatMaps a Java-Iterator into a new Type.
* @param iterator that should be flatMapped
* @param mapper the function that decides what the result turns into.
* @Type(T)
* @param <V> The return type supplier.
* @param <E> The return type.
* @return a iterator that is flatMapped to a new result
*/
public static GENERIC_KEY_SPECIAL_BRACES<E, V extends Iterable<E>> ObjectIterator<E> flatMap(Iterator<? extends CLASS_TYPE> iterator, TO_OBJECT_FUNCTION KKS_GENERIC_TYPE<V> mapper) {
return new FlatMappedIterator<>(wrap(iterator), mapper);
}
/**
* A Helper function that flatMaps a Iterator into a new Type.
* @param iterator that should be flatMapped
* @param mapper the function that decides what the result turns into.
* @Type(T)
* @param <V> The return type supplier.
* @param <E> The return type.
* @return a iterator that is flatMapped to a new result
*/
public static GENERIC_KEY_SPECIAL_BRACES<E, V extends Iterable<E>> ObjectIterator<E> flatMap(ITERATOR KEY_GENERIC_TYPE iterator, TO_OBJECT_FUNCTION KKS_GENERIC_TYPE<V> mapper) {
return new FlatMappedIterator<>(iterator, mapper);
}
/**
* A Helper function that flatMaps a Java-Iterator into a new Type.
* @param iterator that should be flatMapped
* @param mapper the function that decides what the result turns into.
* @Type(T)
* @param <E> The return type.
* @return a iterator that is flatMapped to a new result
*/
public static GENERIC_KEY_SPECIAL_BRACES<E> ObjectIterator<E> arrayFlatMap(Iterator<? extends CLASS_TYPE> iterator, TO_OBJECT_FUNCTION KKS_GENERIC_TYPE<E[]> mapper) {
return new FlatMappedArrayIterator<>(wrap(iterator), mapper);
}
/**
* A Helper function that flatMaps a Iterator into a new Type.
* @param iterator that should be flatMapped
* @param mapper the function that decides what the result turns into.
* @Type(T)
* @param <E> The return type.
* @return a iterator that is flatMapped to a new result
*/
public static GENERIC_KEY_SPECIAL_BRACES<E> ObjectIterator<E> arrayFlatMap(ITERATOR KEY_GENERIC_TYPE iterator, TO_OBJECT_FUNCTION KKS_GENERIC_TYPE<E[]> mapper) {
return new FlatMappedArrayIterator<>(iterator, mapper);
}
/**
* A Helper function that filters out all desired elements from a Java-Iterator
* @param iterator that should be filtered.
* @param filter the filter that decides that should be let through
* @Type(T)
* @return a filtered iterator
*/
public static GENERIC_KEY_BRACES ITERATOR KEY_GENERIC_TYPE filter(Iterator<? extends CLASS_TYPE> iterator, PREDICATE KEY_GENERIC_TYPE filter) {
return new FilteredIteratorBRACES(wrap(iterator), filter);
}
/**
* A Helper function that filters out all desired elements
* @param iterator that should be filtered.
* @param filter the filter that decides that should be let through
* @Type(T)
* @return a filtered iterator
*/
public static GENERIC_KEY_BRACES ITERATOR KEY_GENERIC_TYPE filter(ITERATOR KEY_GENERIC_TYPE iterator, PREDICATE KEY_GENERIC_TYPE filter) {
return new FilteredIteratorBRACES(iterator, filter);
}
/**
* A Helper function that filters out all duplicated elements.
* @param iterator that should be distinct
* @Type(T)
* @return a distinct iterator
*/
public static GENERIC_KEY_BRACES ITERATOR KEY_GENERIC_TYPE distinct(ITERATOR KEY_GENERIC_TYPE iterator) {
return new DistinctIteratorBRACES(iterator);
}
/**
* A Helper function that filters out all duplicated elements from a Java Iterator.
* @param iterator that should be distinct
* @Type(T)
* @return a distinct iterator
*/
public static GENERIC_KEY_BRACES ITERATOR KEY_GENERIC_TYPE distinct(Iterator<? extends CLASS_TYPE> iterator) {
return new DistinctIteratorBRACES(wrap(iterator));
}
/**
* A Helper function that repeats the Iterator a specific amount of times
* @param iterator that should be repeated
* @param repeats the amount of times the iterator should be repeated
* @Type(T)
* @return a repeating iterator
*/
public static GENERIC_KEY_BRACES ITERATOR KEY_GENERIC_TYPE repeat(ITERATOR KEY_GENERIC_TYPE iterator, int repeats) {
return new RepeatingIteratorBRACES(iterator, repeats);
}
/**
* A Helper function that repeats the Iterator a specific amount of times from a Java Iterator
* @param iterator that should be repeated
* @param repeats the amount of times the iterator should be repeated
* @Type(T)
* @return a repeating iterator
*/
public static GENERIC_KEY_BRACES ITERATOR KEY_GENERIC_TYPE repeat(Iterator<? extends CLASS_TYPE> iterator, int repeats) {
return new RepeatingIteratorBRACES(wrap(iterator), repeats);
}
/**
* A Helper function that creates a infinitely looping iterator
* @param iterator that should be looping infinitely
* @Type(T)
* @return a infinitely looping iterator
*/
public static GENERIC_KEY_BRACES ITERATOR KEY_GENERIC_TYPE infinite(ITERATOR KEY_GENERIC_TYPE iterator) {
return new InfiniteIteratorBRACES(iterator);
}
/**
* A Helper function that creates a infinitely looping iterator from a Java Iterator
* @param iterator that should be looping infinitely
* @Type(T)
* @return a infinitely looping iterator
*/
public static GENERIC_KEY_BRACES ITERATOR KEY_GENERIC_TYPE infinite(Iterator<? extends CLASS_TYPE> iterator) {
return new InfiniteIteratorBRACES(wrap(iterator));
}
/**
* A Helper function that hard limits the Iterator to a specific size
* @param iterator that should be limited
* @param limit the amount of elements it should be limited to
* @Type(T)
* @return a limited iterator
*/
public static GENERIC_KEY_BRACES ITERATOR KEY_GENERIC_TYPE limit(ITERATOR KEY_GENERIC_TYPE iterator, long limit) {
return new LimitedIteratorBRACES(iterator, limit);
}
/**
* A Helper function that hard limits the Iterator to a specific size from a Java Iterator
* @param iterator that should be limited
* @param limit the amount of elements it should be limited to
* @Type(T)
* @return a limited iterator
*/
public static GENERIC_KEY_BRACES ITERATOR KEY_GENERIC_TYPE limit(Iterator<? extends CLASS_TYPE> iterator, long limit) {
return new LimitedIteratorBRACES(wrap(iterator), limit);
}
/**
* A Helper function that sorts the Iterator beforehand.
* This operation is heavily hurting performance because it rebuilds the entire iterator and then sorts it.
* @param iterator that should be sorted.
* @param sorter the sorter of the iterator. Can be null.
* @Type(T)
* @return a new sorted iterator
*/
public static GENERIC_KEY_BRACES ITERATOR KEY_GENERIC_TYPE sorted(ITERATOR KEY_GENERIC_TYPE iterator, COMPARATOR KEY_GENERIC_TYPE sorter) {
return new SortedIteratorBRACES(iterator, sorter);
}
/**
* A Helper function that sorts the Iterator beforehand from a Java Iterator.
* This operation is heavily hurting performance because it rebuilds the entire iterator and then sorts it.
* @param iterator that should be sorted.
* @param sorter the sorter of the iterator. Can be null.
* @Type(T)
* @return a new sorted iterator
*/
public static GENERIC_KEY_BRACES ITERATOR KEY_GENERIC_TYPE sorted(Iterator<? extends CLASS_TYPE> iterator, COMPARATOR KEY_GENERIC_TYPE sorter) {
return new SortedIteratorBRACES(wrap(iterator), sorter);
}
/**
* A Helper function that allows to preview the result of a Iterator.
* @param iterator that should be peeked at
* @param action callback that receives the value before the iterator returns it
* @Type(T)
* @return a peeked iterator
*/
public static GENERIC_KEY_BRACES ITERATOR KEY_GENERIC_TYPE peek(ITERATOR KEY_GENERIC_TYPE iterator, CONSUMER KEY_GENERIC_TYPE action) {
return new PeekIteratorBRACES(iterator, action);
}
/**
* A Helper function that allows to preview the result of a Iterator from a Java Iterator
* @param iterator that should be peeked at
* @param action callback that receives the value before the iterator returns it
* @Type(T)
* @return a peeked iterator
*/
public static GENERIC_KEY_BRACES ITERATOR KEY_GENERIC_TYPE peek(Iterator<? extends CLASS_TYPE> iterator, CONSUMER KEY_GENERIC_TYPE action) {
return new PeekIteratorBRACES(wrap(iterator), action);
}
/**
* Helper function to convert a Object Iterator into a Primitive Iterator
* @param iterator that should be converted to a unboxing iterator
* @ArrayType(T)
* @return a primitive iterator
*/
public static GENERIC_KEY_BRACES ITERATOR KEY_GENERIC_TYPE wrap(Iterator<? extends CLASS_TYPE> iterator) {
return iterator instanceof ITERATOR ? (ITERATOR KEY_GENERIC_TYPE)iterator : new IteratorWrapperBRACES(iterator);
}
/**
* Returns a Array Wrapping iterator
* @param a the array that should be wrapped
* @ArrayType(T)
* @return a Iterator that is wrapping a array.
*/
public static GENERIC_KEY_BRACES ArrayIterator KEY_GENERIC_TYPE wrap(KEY_TYPE... a) {
return wrap(a, 0, a.length);
}
/**
* Returns a Array Wrapping iterator
* @param a the array that should be wrapped.
* @param start the index to be started from.
* @param end the index that should be ended.
* @ArrayType(T)
* @return a Iterator that is wrapping a array.
*/
public static GENERIC_KEY_BRACES ArrayIterator KEY_GENERIC_TYPE wrap(KEY_TYPE[] a, int start, int end) {
return new ArrayIteratorBRACES(a, start, end);
}
/**
* Iterates over a iterator and inserts the values into the array and returns the amount that was inserted
* @param a where the elements should be inserted
* @param i the source iterator
* @ArrayType(T)
* @return the amount of elements that were inserted into the array.
*/
public static GENERIC_KEY_BRACES int unwrap(KEY_TYPE[] a, Iterator<? extends CLASS_TYPE> i) {
return unwrap(a, i, 0, a.length);
}
/**
* Iterates over a iterator and inserts the values into the array and returns the amount that was inserted
* @param a where the elements should be inserted
* @param i the source iterator
* @param offset the array offset where the start should be
* @ArrayType(T)
* @return the amount of elements that were inserted into the array.
*/
public static GENERIC_KEY_BRACES int unwrap(KEY_TYPE[] a, Iterator<? extends CLASS_TYPE> i, int offset) {
return unwrap(a, i, offset, a.length - offset);
}
/**
* Iterates over a iterator and inserts the values into the array and returns the amount that was inserted
* @param a where the elements should be inserted
* @param i the source iterator
* @param offset the array offset where the start should be
* @param max the maximum values that should be extracted from the source
* @ArrayType(T)
* @return the amount of elements that were inserted into the array.
* @throws IllegalStateException if max is smaller the 0 or if the maximum index is larger then the array
*/
public static GENERIC_KEY_BRACES int unwrap(KEY_TYPE[] a, Iterator<? extends CLASS_TYPE> i, int offset, int max) {
if(max < 0) throw new IllegalStateException("The max size is smaller then 0");
if(offset + max > a.length) throw new IllegalStateException("largest array index exceeds array size");
int index = 0;
for(;index<max && i.hasNext();index++) a[index+offset] = OBJ_TO_KEY(i.next());
return index;
}
/**
* A Primitive iterator variant of the ITERATORS unwrap function
* Iterates over a iterator and inserts the values into the array and returns the amount that was inserted
* @param a where the elements should be inserted
* @param i the source iterator
* @ArrayType(T)
* @return the amount of elements that were inserted into the array.
*/
public static GENERIC_KEY_BRACES int unwrap(KEY_TYPE[] a, ITERATOR KEY_GENERIC_TYPE i) {
return unwrap(a, i, 0, a.length);
}
/**
* A Primitive iterator variant of the ITERATORS unwrap function
* Iterates over a iterator and inserts the values into the array and returns the amount that was inserted
* @param a where the elements should be inserted
* @param i the source iterator
* @param offset the array offset where the start should be
* @ArrayType(T)
* @return the amount of elements that were inserted into the array.
*/
public static GENERIC_KEY_BRACES int unwrap(KEY_TYPE[] a, ITERATOR KEY_GENERIC_TYPE i, int offset) {
return unwrap(a, i, offset, a.length - offset);
}
/**
* A Primitive iterator variant of the ITERATORS unwrap function
* Iterates over a iterator and inserts the values into the array and returns the amount that was inserted
* @param a where the elements should be inserted
* @param i the source iterator
* @param offset the array offset where the start should be
* @param max the maximum values that should be extracted from the source
* @ArrayType(T)
* @return the amount of elements that were inserted into the array.
* @throws IllegalStateException if max is smaller the 0 or if the maximum index is larger then the array
*/
public static GENERIC_KEY_BRACES int unwrap(KEY_TYPE[] a, ITERATOR KEY_GENERIC_TYPE i, int offset, int max) {
if(max < 0) throw new IllegalStateException("The max size is smaller then 0");
if(offset + max > a.length) throw new IllegalStateException("largest array index exceeds array size");
int index = 0;
for(;index<max && i.hasNext();index++) a[index+offset] = i.NEXT();
return index;
}
#if !TYPE_OBJECT
/**
* A Function to convert a Primitive Iterator to a Object array.
* Iterates over a iterator and inserts the values into the array and returns the amount that was inserted
* @param a where the elements should be inserted
* @param i the source iterator
* @ArrayType(T)
* @return the amount of elements that were inserted into the array.
*/
public static GENERIC_KEY_BRACES int unwrap(CLASS_TYPE[] a, ITERATOR KEY_GENERIC_TYPE i) {
return unwrap(a, i, 0, a.length);
}
/**
* A Function to convert a Primitive Iterator to a Object array.
* Iterates over a iterator and inserts the values into the array and returns the amount that was inserted
* @param a where the elements should be inserted
* @param i the source iterator
* @param offset the array offset where the start should be
* @ArrayType(T)
* @return the amount of elements that were inserted into the array.
*/
public static GENERIC_KEY_BRACES int unwrap(CLASS_TYPE[] a, ITERATOR KEY_GENERIC_TYPE i, int offset) {
return unwrap(a, i, offset, a.length - offset);
}
/**
* A Function to convert a Primitive Iterator to a Object array.
* Iterates over a iterator and inserts the values into the array and returns the amount that was inserted
* @param a where the elements should be inserted
* @param i the source iterator
* @param offset the array offset where the start should be
* @param max the maximum values that should be extracted from the source
* @ArrayType(T)
* @return the amount of elements that were inserted into the array.
* @throws IllegalStateException if max is smaller the 0 or if the maximum index is larger then the array
*/
public static GENERIC_KEY_BRACES int unwrap(CLASS_TYPE[] a, ITERATOR KEY_GENERIC_TYPE i, int offset, int max) {
if(max < 0) throw new IllegalStateException("The max size is smaller then 0");
if(offset + max > a.length) throw new IllegalStateException("largest array index exceeds array size");
int index = 0;
for(;index<max && i.hasNext();index++) a[index+offset] = KEY_TO_OBJ(i.NEXT());
return index;
}
#endif
#if LIST_MODULE
#if ARRAY_LIST_FEATURE || LINKED_LIST_FEATURE
/**
* A Helper function to pours all elements of a Iterator into a List
* @param iter the elements that should be poured into list.
* @ArrayType(T)
* @return A list of all elements of the Iterator
*/
public static GENERIC_KEY_BRACES LIST KEY_GENERIC_TYPE pour(ITERATOR KEY_GENERIC_TYPE iter) {
return pour(iter, Integer.MAX_VALUE);
}
/**
* A Helper function to pours all elements of a Iterator into a List
* @param iter the elements that should be poured into list.
* @param max the maximum amount of elements that should be collected
* @ArrayType(T)
* @return A list of all requested elements of the Iterator
*/
public static GENERIC_KEY_BRACES LIST KEY_GENERIC_TYPE pour(ITERATOR KEY_GENERIC_TYPE iter, int max) {
#if ARRAY_LIST_FEATURE
ARRAY_LIST KEY_GENERIC_TYPE list = new ARRAY_LISTBRACES();
#else
LINKED_LIST KEY_GENERIC_TYPE list = new LINKED_LISTBRACES();
#endif
pour(iter, list, max);
#if ARRAY_LIST_FEATURE
list.trim();
#endif
return list;
}
#endif
#endif
/**
* A Helper function to pours all elements of a Iterator into a Collection
* @param iter the elements that should be poured into list.
* @param c the collection where the elements should be poured into
* @ArrayType(T)
* @return the amount of elements that were added
*/
public static GENERIC_KEY_BRACES int pour(ITERATOR KEY_GENERIC_TYPE iter, COLLECTION KEY_GENERIC_TYPE c) {
return pour(iter, c, Integer.MAX_VALUE);
}
/**
* A Helper function to pours all elements of a Iterator into a Collection
* @param iter the elements that should be poured into list.
* @param c the collection where the elements should be poured into
* @param max the maximum amount of elements that should be collected
* @ArrayType(T)
* @return the amount of elements that were added
*/
public static GENERIC_KEY_BRACES int pour(ITERATOR KEY_GENERIC_TYPE iter, COLLECTION KEY_GENERIC_TYPE c, int max) {
if(max < 0) throw new IllegalStateException("Max is negative");
int done = 0;
for(;done<max && iter.hasNext();done++, c.add(iter.NEXT()));
return done;
}
/**
* Helper Iterator that concats other iterators together
* @param array the Iterators that should be concatenated
* @ArrayType(T)
* @return iterator of the inputted iterators
*/
public static GENERIC_KEY_BRACES ITERATOR KEY_GENERIC_TYPE concat(ITERATOR KEY_GENERIC_TYPE... array) {
return concat(array, 0, array.length);
}
/**
* Helper Iterator that concats other iterators together
* @param array the Iterators that should be concatenated
* @param offset where to start within the array
* @param length the length of the array
* @ArrayType(T)
* @return iterator of the inputted iterators
*/
public static GENERIC_KEY_BRACES ITERATOR KEY_GENERIC_TYPE concat(ITERATOR KEY_GENERIC_TYPE[] array, int offset, int length) {
return new ConcatIteratorBRACES(array, offset, length);
}
private static class IteratorWrapper KEY_GENERIC_TYPE implements ITERATOR KEY_GENERIC_TYPE
{
Iterator<? extends CLASS_TYPE> iter;
public IteratorWrapper(Iterator<? extends CLASS_TYPE> iter) {
this.iter = iter;
}
@Override
public boolean hasNext() {
return iter.hasNext();
}
@Override
public KEY_TYPE NEXT() {
return OBJ_TO_KEY(iter.next());
}
#if !TYPE_OBJECT
@Override
@Deprecated
public CLASS_TYPE next() {
return iter.next();
}
#endif
}
private static class ConcatIterator KEY_GENERIC_TYPE implements ITERATOR KEY_GENERIC_TYPE
{
ITERATOR KEY_GENERIC_TYPE[] iters;
int offset;
int lastOffset = -1;
int length;
public ConcatIterator(ITERATOR KEY_GENERIC_TYPE[] iters, int offset, int length) {
this.iters = iters;
this.offset = offset;
this.length = length;
find();
}
private void find() {
for(;length != 0 && !iters[offset].hasNext();length--, offset++);
}
@Override
public boolean hasNext() {
return length > 0;
}
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
KEY_TYPE result = iters[lastOffset = offset].NEXT();
find();
return result;
}
@Override
public void remove() {
if(lastOffset == -1) throw new IllegalStateException();
iters[lastOffset].remove();
lastOffset = -1;
}
}
private static class ReverseBiIterator KEY_GENERIC_TYPE implements BI_ITERATOR KEY_GENERIC_TYPE {
BI_ITERATOR KEY_GENERIC_TYPE it;
ReverseBiIterator(BI_ITERATOR KEY_GENERIC_TYPE it) {
this.it = it;
}
@Override
public KEY_TYPE NEXT() { return it.PREVIOUS(); }
@Override
public boolean hasNext() { return it.hasPrevious(); }
@Override
public boolean hasPrevious() { return it.hasNext(); }
@Override
public KEY_TYPE PREVIOUS() { return it.NEXT(); }
@Override
public void remove() { it.remove(); }
}
private static class ReverseListIterator KEY_GENERIC_TYPE implements LIST_ITERATOR KEY_GENERIC_TYPE {
LIST_ITERATOR KEY_GENERIC_TYPE it;
ReverseListIterator(LIST_ITERATOR KEY_GENERIC_TYPE it) {
this.it = it;
}
@Override
public KEY_TYPE NEXT() { return it.PREVIOUS(); }
@Override
public boolean hasNext() { return it.hasPrevious(); }
@Override
public boolean hasPrevious() { return it.hasNext(); }
@Override
public KEY_TYPE PREVIOUS() { return it.NEXT(); }
@Override
public void remove() { it.remove(); }
@Override
public int nextIndex() { return it.previousIndex(); }
@Override
public int previousIndex() { return it.nextIndex(); }
@Override
public void set(KEY_TYPE e) { it.set(e); }
@Override
public void add(KEY_TYPE e) { it.add(e); }
}
private static class UnmodifiableListIterator KEY_GENERIC_TYPE implements LIST_ITERATOR KEY_GENERIC_TYPE
{
LIST_ITERATOR KEY_GENERIC_TYPE iter;
UnmodifiableListIterator(LIST_ITERATOR KEY_GENERIC_TYPE iter) {
this.iter = iter;
}
@Override
public boolean hasNext() {
return iter.hasNext();
}
@Override
public boolean hasPrevious() {
return iter.hasPrevious();
}
@Override
public int nextIndex() {
return iter.nextIndex();
}
@Override
public int previousIndex() {
return iter.previousIndex();
}
@Override
public void remove() { throw new UnsupportedOperationException(); }
@Override
public KEY_TYPE PREVIOUS() {
return iter.PREVIOUS();
}
@Override
public KEY_TYPE NEXT() {
return iter.NEXT();
}
@Override
public void set(KEY_TYPE e) { throw new UnsupportedOperationException(); }
@Override
public void add(KEY_TYPE e) { throw new UnsupportedOperationException(); }
}
private static class UnmodifiableBiIterator KEY_GENERIC_TYPE implements BI_ITERATOR KEY_GENERIC_TYPE
{
BI_ITERATOR KEY_GENERIC_TYPE iter;
UnmodifiableBiIterator(BI_ITERATOR KEY_GENERIC_TYPE iter) {
this.iter = iter;
}
@Override
public KEY_TYPE NEXT() {
return iter.NEXT();
}
@Override
public boolean hasNext() {
return iter.hasNext();
}
@Override
public boolean hasPrevious() {
return iter.hasPrevious();
}
@Override
public KEY_TYPE PREVIOUS() {
return iter.PREVIOUS();
}
}
private static class UnmodifiableIterator KEY_GENERIC_TYPE implements ITERATOR KEY_GENERIC_TYPE
{
ITERATOR KEY_GENERIC_TYPE iterator;
UnmodifiableIterator(ITERATOR KEY_GENERIC_TYPE iterator) {
this.iterator = iterator;
}
@Override
public boolean hasNext() {
return iterator.hasNext();
}
@Override
public KEY_TYPE NEXT() {
return iterator.NEXT();
}
}
private static class EmptyIterator KEY_GENERIC_TYPE implements LIST_ITERATOR KEY_GENERIC_TYPE
{
@Override
public boolean hasNext() { return false; }
@Override
public KEY_TYPE NEXT() { throw new NoSuchElementException(); }
@Override
public boolean hasPrevious() { return false; }
@Override
public KEY_TYPE PREVIOUS() { throw new NoSuchElementException(); }
@Override
public int nextIndex() { return 0; }
@Override
public int previousIndex() { return -1; }
@Override
public void remove() { throw new UnsupportedOperationException(); }
@Override
public void set(KEY_TYPE e) { throw new UnsupportedOperationException(); }
@Override
public void add(KEY_TYPE e) { throw new UnsupportedOperationException(); }
}
private static class ArrayIterator KEY_GENERIC_TYPE implements ITERATOR KEY_GENERIC_TYPE
{
KEY_TYPE[] a;
int from;
int to;
ArrayIterator(KEY_TYPE[] a, int from, int to) {
this.a = a;
this.from = from;
this.to = to;
}
@Override
public boolean hasNext() {
return from < to;
}
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
return a[from++];
}
@Override
public int skip(int amount) {
if(amount < 0) throw new IllegalStateException("Negative Numbers are not allowed");
int left = Math.min(amount, to - from);
from += left;
return amount - left;
}
}
private static class MappedIterator KSS_GENERIC_TYPE<E, T> implements ObjectIterator<T>
{
ITERATOR KEY_SPECIAL_GENERIC_TYPE<E> iterator;
TO_OBJECT_FUNCTION KSS_GENERIC_TYPE<E, T> mapper;
MappedIterator(ITERATOR KEY_SPECIAL_GENERIC_TYPE<E> iterator, TO_OBJECT_FUNCTION KSS_GENERIC_TYPE<E, T> mapper) {
this.iterator = iterator;
this.mapper = mapper;
}
@Override
public boolean hasNext() {
return iterator.hasNext();
}
@Override
public T next() {
return mapper.apply(iterator.NEXT());
}
@Override
public int skip(int amount) {
return iterator.skip(amount);
}
}
#if TYPE_OBJECT
#iterate
#argument MAPPED_TYPE MappedBoolean MappedByte MappedShort MappedInt MappedLong MappedFloat MappedDouble
#argument NEXT_TYPE nextBoolean nextByte nextShort nextInt nextLong nextFloat nextDouble
#argument OUTPUT_ITERATOR BooleanIterator ByteIterator ShortIterator IntIterator LongIterator FloatIterator DoubleIterator
#argument MAPPER Predicate ToByteFunction ToShortFunction ToIntFunction ToLongFunction ToFloatFunction ToDoubleFunction
#argument APPLY test applyAsByte applyAsShort applyAsInt applyAsLong applyAsFloat applyAsDouble
#argument DATA_TYPE boolean byte short int long float double
#argument FILTER_TYPE BOOLEAN_COLLECTION_MODULE BYTE_COLLECTION_MODULE SHORT_COLLECTION_MODULE INT_COLLECTION_MODULE LONG_COLLECTION_MODULE FLOAT_COLLECTION_MODULE DOUBLE_COLLECTION_MODULE
#if FILTER_TYPE
private static class MAPPED_TYPEIterator<E> implements OUTPUT_ITERATOR
{
ITERATOR<E> iterator;
MAPPER<E> mapper;
MAPPED_TYPEIterator(ITERATOR KEY_SPECIAL_GENERIC_TYPE<E> iterator, MAPPER<E> mapper) {
this.iterator = iterator;
this.mapper = mapper;
}
@Override
public boolean hasNext() {
return iterator.hasNext();
}
@Override
public DATA_TYPE NEXT_TYPE() {
return mapper.APPLY(iterator.NEXT());
}
@Override
public int skip(int amount) {
return iterator.skip(amount);
}
}
#endif
#enditerate
#endif
private static class FlatMappedIterator KSS_GENERIC_TYPE<E, T,[SPACE]V extends Iterable<T>> implements ObjectIterator<T>
{
ITERATOR KEY_SPECIAL_GENERIC_TYPE<E> iterator;
Iterator<T> last = null;
TO_OBJECT_FUNCTION KSS_GENERIC_TYPE<E, V> mapper;
boolean foundNext = false;
FlatMappedIterator(ITERATOR KEY_SPECIAL_GENERIC_TYPE<E> iterator, TO_OBJECT_FUNCTION KSS_GENERIC_TYPE<E, V> mapper) {
this.iterator = iterator;
this.mapper = mapper;
}
void compute() {
if(foundNext) return;
foundNext = true;
while(iterator.hasNext()) {
if(last != null && last.hasNext()) return;
last = mapper.apply(iterator.NEXT()).iterator();
}
}
@Override
public boolean hasNext() {
compute();
return last != null && last.hasNext();
}
@Override
public T next() {
if(!hasNext()) throw new NoSuchElementException();
T result = last.next();
foundNext = false;
return result;
}
}
private static class FlatMappedArrayIterator KSS_GENERIC_TYPE<E, T> implements ObjectIterator<T>
{
ITERATOR KEY_SPECIAL_GENERIC_TYPE<E> iterator;
Iterator<T> last = null;
TO_OBJECT_FUNCTION KSS_GENERIC_TYPE<E, T[]> mapper;
boolean foundNext = false;
FlatMappedArrayIterator(ITERATOR KEY_SPECIAL_GENERIC_TYPE<E> iterator, TO_OBJECT_FUNCTION KSS_GENERIC_TYPE<E, T[]> mapper) {
this.iterator = iterator;
this.mapper = mapper;
}
void compute() {
if(foundNext) return;
foundNext = true;
while(iterator.hasNext()) {
if(last != null && last.hasNext()) return;
last = ObjectIterators.wrap(mapper.apply(iterator.NEXT()));
}
}
@Override
public boolean hasNext() {
compute();
return last != null && last.hasNext();
}
@Override
public T next() {
if(!hasNext()) throw new NoSuchElementException();
T result = last.next();
foundNext = false;
return result;
}
}
private static class InfiniteIterator KEY_GENERIC_TYPE implements ITERATOR KEY_GENERIC_TYPE
{
ITERATOR KEY_GENERIC_TYPE iter;
CollectionWrapper KEY_GENERIC_TYPE looper = COLLECTIONS.wrapper();
int index = 0;
public InfiniteIterator(ITERATOR KEY_GENERIC_TYPE iter) {
this.iter = iter;
}
@Override
public boolean hasNext() {
return true;
}
@Override
public KEY_TYPE NEXT() {
if(iter != null) {
if(iter.hasNext()) {
KEY_TYPE value = iter.NEXT();
looper.add(value);
return value;
}
else iter = null;
}
return looper.GET_KEY((index++) % looper.size());
}
#if !TYPE_OBJECT
@Override
public void forEachRemaining(CONSUMER action) { throw new UnsupportedOperationException("This is a instant deadlock, so unsupported"); }
#endif
public void forEachRemaining(Consumer<? super CLASS_TYPE> action) { throw new UnsupportedOperationException("This is a instant deadlock, so unsupported"); }
public <E> void forEachRemaining(E input, BI_FROM_OBJECT_CONSUMER KSK_GENERIC_TYPE<E> action) { throw new UnsupportedOperationException("This is a instant deadlock, so unsupported"); }
}
private static class RepeatingIterator KEY_GENERIC_TYPE implements ITERATOR KEY_GENERIC_TYPE
{
final int repeats;
int index = 0;
ITERATOR KEY_GENERIC_TYPE iter;
COLLECTION KEY_GENERIC_TYPE repeater = COLLECTIONS.wrapper();
public RepeatingIterator(ITERATOR KEY_GENERIC_TYPE iter, int repeat) {
this.iter = iter;
this.repeats = repeat;
}
@Override
public boolean hasNext() {
if(iter.hasNext()) return true;
if(index < repeats) {
index++;
iter = repeater.iterator();
return iter.hasNext();
}
return false;
}
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
KEY_TYPE value = iter.NEXT();
if(index == 0) repeater.add(value);
return value;
}
}
private static class SortedIterator KEY_GENERIC_TYPE implements ITERATOR KEY_GENERIC_TYPE
{
ITERATOR KEY_GENERIC_TYPE iterator;
COMPARATOR KEY_GENERIC_TYPE sorter;
COLLECTIONS.CollectionWrapper KEY_GENERIC_TYPE sortedElements = null;
int index = 0;
public SortedIterator(ITERATOR KEY_GENERIC_TYPE iterator, COMPARATOR KEY_GENERIC_TYPE sorter) {
this.iterator = iterator;
this.sorter = sorter;
}
@Override
public boolean hasNext() {
if(sortedElements == null) {
boolean hasNext = iterator.hasNext();
if(hasNext) {
sortedElements = COLLECTIONS.wrapper();
pour(iterator, sortedElements);
}
else sortedElements = COLLECTIONS.wrapper();
if(hasNext) sortedElements.unstableSort(sorter);
}
return index < sortedElements.size();
}
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
return sortedElements.GET_KEY(index++);
}
}
private static class DistinctIterator KEY_GENERIC_TYPE implements ITERATOR KEY_GENERIC_TYPE
{
ITERATOR KEY_GENERIC_TYPE iterator;
#if TYPE_BOOLEAN
int filtered;
#else
COLLECTION KEY_GENERIC_TYPE filtered = COLLECTIONS.distinctWrapper();
#endif
KEY_TYPE lastFound;
boolean foundNext = false;
public DistinctIterator(ITERATOR KEY_GENERIC_TYPE iterator) {
this.iterator = iterator;
}
void compute() {
if(foundNext) return;
#if TYPE_BOOLEAN
if(filtered == 3) return;
#endif
while(iterator.hasNext()) {
lastFound = iterator.NEXT();
#if TYPE_BOOLEAN
if((filtered & (lastFound ? 1 : 2)) == 0) {
filtered |= (lastFound ? 1 : 2);
foundNext = true;
break;
}
#else
if(filtered.add(lastFound)) {
foundNext = true;
break;
}
#endif
}
}
@Override
public boolean hasNext() {
compute();
return foundNext;
}
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
foundNext = false;
return lastFound;
}
}
private static class FilteredIterator KEY_GENERIC_TYPE implements ITERATOR KEY_GENERIC_TYPE
{
ITERATOR KEY_GENERIC_TYPE iterator;
PREDICATE KEY_GENERIC_TYPE filter;
KEY_TYPE lastFound;
boolean foundNext = false;
public FilteredIterator(ITERATOR KEY_GENERIC_TYPE iterator, PREDICATE KEY_GENERIC_TYPE filter) {
this.iterator = iterator;
this.filter = filter;
}
void compute() {
if(foundNext) return;
while(iterator.hasNext()) {
lastFound = iterator.NEXT();
if(filter.test(lastFound)) {
foundNext = true;
break;
}
}
}
@Override
public boolean hasNext() {
compute();
return foundNext;
}
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
foundNext = false;
return lastFound;
}
}
private static class LimitedIterator KEY_GENERIC_TYPE implements ITERATOR KEY_GENERIC_TYPE
{
ITERATOR KEY_GENERIC_TYPE iterator;
long limit;
public LimitedIterator(ITERATOR KEY_GENERIC_TYPE iterator, long limit) {
this.iterator = iterator;
this.limit = limit;
}
@Override
public boolean hasNext() {
return limit > 0 && iterator.hasNext();
}
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
limit--;
return iterator.NEXT();
}
}
private static class PeekIterator KEY_GENERIC_TYPE implements ITERATOR KEY_GENERIC_TYPE
{
ITERATOR KEY_GENERIC_TYPE iterator;
CONSUMER KEY_GENERIC_TYPE action;
public PeekIterator(ITERATOR KEY_GENERIC_TYPE iterator, CONSUMER KEY_GENERIC_TYPE action) {
this.iterator = iterator;
this.action = action;
}
@Override
public boolean hasNext() {
return iterator.hasNext();
}
@Override
public KEY_TYPE NEXT() {
if(!hasNext()) throw new NoSuchElementException();
KEY_TYPE result = iterator.NEXT();
action.accept(result);
return result;
}
}
}