From a89c812c0678887b9d64445afe267070226158c0 Mon Sep 17 00:00:00 2001 From: Speiger Date: Sat, 17 Jun 2023 01:22:56 +0200 Subject: [PATCH] Added Infinite Iterators --- Changelog.md | 1 + .../templates/utils/Iterators.template | 60 ++++++++++++++++++- 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/Changelog.md b/Changelog.md index cd7085f..1e32e89 100644 --- a/Changelog.md +++ b/Changelog.md @@ -6,6 +6,7 @@ - Added: ToArray/pushTop functions to Stack.class. - Added: ComputeNonDefault functions which will contain the current behavior of the Compute function, while the Compute will be changed to be more java compliant! - Added: List.reversed, which returns a SubList that has all elements in reversed order and also inserts reversed. +- Added: Iterators.infinite as an option that will create a Infinite Iterator based on the inputted one. - Fixed: SetValue wasn't working on forEach implementations. - Fixed: Compute functions now perform with primitives more java compliant. Meaning that getDefaultReturnValue function no longer is seen as null. diff --git a/src/builder/resources/speiger/assets/collections/templates/utils/Iterators.template b/src/builder/resources/speiger/assets/collections/templates/utils/Iterators.template index 9b7ffa7..4734daf 100644 --- a/src/builder/resources/speiger/assets/collections/templates/utils/Iterators.template +++ b/src/builder/resources/speiger/assets/collections/templates/utils/Iterators.template @@ -4,8 +4,8 @@ import java.util.Iterator; import java.util.NoSuchElementException; #if TYPE_OBJECT import java.util.Comparator; -import java.util.function.CONSUMER; #endif +import java.util.function.Consumer; #if JDK_FUNCTION import java.util.function.PREDICATE; #endif @@ -18,6 +18,7 @@ import speiger.src.collections.PACKAGE.functions.CONSUMER; import speiger.src.collections.PACKAGE.functions.COMPARATOR; #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 @@ -33,6 +34,7 @@ import speiger.src.collections.PACKAGE.lists.LINKED_LIST; 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 @@ -245,6 +247,26 @@ public class ITERATORS 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 iterator) { + return new InfiniteIteratorBRACES(wrap(iterator)); + } + /** * A Helper function that hard limits the Iterator to a specific size * @param iterator that should be limited @@ -918,6 +940,42 @@ public class ITERATORS } } + 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 action) { throw new UnsupportedOperationException("This is a instant deadlock, so unsupported"); } + public void forEachRemaining(E input, BI_FROM_OBJECT_CONSUMER KSK_GENERIC_TYPE 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;