From 3f6e7fbb8838f0525c2022ec80e926d2ccd43df6 Mon Sep 17 00:00:00 2001 From: Speiger Date: Thu, 15 Dec 2022 20:08:33 +0100 Subject: [PATCH] Added forEachIndexed --- Changelog.md | 1 + .../src/builder/modules/FunctionModule.java | 1 + .../templates/lists/LinkedList.template | 9 +++++++++ .../collections/templates/lists/List.template | 18 ++++++++++++++---- 4 files changed, 25 insertions(+), 4 deletions(-) diff --git a/Changelog.md b/Changelog.md index c727874..4a00c94 100644 --- a/Changelog.md +++ b/Changelog.md @@ -6,6 +6,7 @@ - Added: ToArray function into Iterable which uses ISizeProvider to reduce overhead of duplicating arrays. - Added: Functions that have the same type, Int2IntFunction as example, have now a identity function. - Added: Functions of a BooleanValue have now alwaysTrue/False function. +- Added: ForEachIndexed for Lists only for now. (Might get added to others too if necessary) - Fixed: putIfAbsent now replaces defaultValues - Fixed: OpenHashSet/Map and their Custom Variants no longer rely on List implementations. - Fixed: ObjectCopyOnWriteList.of did create a ObjectArrayList instead of the CopyOnWrite variant. diff --git a/src/builder/java/speiger/src/builder/modules/FunctionModule.java b/src/builder/java/speiger/src/builder/modules/FunctionModule.java index a20a8df..1ab81fc 100644 --- a/src/builder/java/speiger/src/builder/modules/FunctionModule.java +++ b/src/builder/java/speiger/src/builder/modules/FunctionModule.java @@ -65,6 +65,7 @@ public class FunctionModule extends BaseModule addBiClassMapper("BI_CONSUMER", "Consumer", ""); addClassMapper("BI_TO_OBJECT_CONSUMER", "ObjectConsumer"); addAbstractMapper("BI_FROM_OBJECT_CONSUMER", "Object%sConsumer"); + addAbstractMapper("BI_FROM_INT_CONSUMER", "Int%sConsumer"); if(keyType.isObject()) { addSimpleMapper("TO_OBJECT_FUNCTION", keyType.getNonFileType()+"UnaryOperator"); addSimpleMapper("VALUE_TO_OBJECT_FUNCTION", valueType.isObject() ? "UnaryOperator" : valueType.getFileType()+"Function"); diff --git a/src/builder/resources/speiger/assets/collections/templates/lists/LinkedList.template b/src/builder/resources/speiger/assets/collections/templates/lists/LinkedList.template index 273c27d..e6cb679 100644 --- a/src/builder/resources/speiger/assets/collections/templates/lists/LinkedList.template +++ b/src/builder/resources/speiger/assets/collections/templates/lists/LinkedList.template @@ -32,6 +32,7 @@ import java.util.function.JAVA_PREDICATE; #endif import java.util.function.JAVA_UNARY_OPERATOR; #endif +import speiger.src.collections.ints.functions.consumer.BI_FROM_INT_CONSUMER; import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER; #if !JDK_FUNCTION import speiger.src.collections.PACKAGE.functions.function.PREDICATE; @@ -420,6 +421,14 @@ public class LINKED_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE } } + @Override + public void forEachIndexed(BI_FROM_INT_CONSUMER KEY_GENERIC_TYPE action) { + Objects.requireNonNull(action); + int index = 0; + for(Entry KEY_GENERIC_TYPE entry = first;entry != null;entry = entry.next) + action.accept(index++, entry.value); + } + @Override public void forEach(E input, BI_FROM_OBJECT_CONSUMER KSK_GENERIC_TYPE action) { Objects.requireNonNull(action); diff --git a/src/builder/resources/speiger/assets/collections/templates/lists/List.template b/src/builder/resources/speiger/assets/collections/templates/lists/List.template index 4b82136..59faaf0 100644 --- a/src/builder/resources/speiger/assets/collections/templates/lists/List.template +++ b/src/builder/resources/speiger/assets/collections/templates/lists/List.template @@ -5,17 +5,18 @@ import java.nio.JAVA_BUFFER; #endif import java.util.List; #if !TYPE_OBJECT && !TYPE_BOOLEAN -import java.util.Objects; import java.util.function.JAVA_UNARY_OPERATOR; -import java.util.function.UnaryOperator; -#else if TYPE_OBJECT +#endif + import java.util.Objects; +import java.util.Comparator; +#if !TYPE_BOOLEAN import java.util.function.UnaryOperator; #endif -import java.util.Comparator; import speiger.src.collections.PACKAGE.collections.COLLECTION; import speiger.src.collections.PACKAGE.collections.SPLIT_ITERATOR; +import speiger.src.collections.ints.functions.consumer.BI_FROM_INT_CONSUMER; #if !TYPE_OBJECT import speiger.src.collections.PACKAGE.functions.COMPARATOR; #endif @@ -360,6 +361,15 @@ public interface LIST KEY_GENERIC_TYPE extends COLLECTION KEY_GENERIC_TYPE, List } #endif + /** + * A Indexed forEach implementation that allows you to keep track of how many elements were already iterated over. + * @param action The action to be performed for each element + * @throws java.lang.NullPointerException if the specified action is null + */ + public default void forEachIndexed(BI_FROM_INT_CONSUMER KEY_GENERIC_TYPE action) { + Objects.requireNonNull(action); + for(int i = 0,m=size();i