From a25ec85ba261861b015357fdedf0ecc1a4998f4a Mon Sep 17 00:00:00 2001 From: Speiger Date: Fri, 29 Oct 2021 16:03:39 +0200 Subject: [PATCH] Added Reduce function to all implementations --- .../speiger/src/builder/GlobalVariables.java | 2 + .../templates/collections/Iterable.template | 25 +- .../templates/lists/ArrayList.template | 14 ++ .../templates/lists/ImmutableList.template | 14 ++ .../templates/lists/LinkedList.template | 14 ++ .../maps/abstracts/AbstractMap.template | 2 +- .../LinkedOpenCustomHashMap.template | 132 +++++++++++ .../customHash/OpenCustomHashMap.template | 140 ++++++++++++ .../maps/impl/hash/LinkedOpenHashMap.template | 138 ++++++++++- .../maps/impl/hash/OpenHashMap.template | 140 ++++++++++++ .../immutable/ImmutableOpenHashMap.template | 131 +++++++++++ .../maps/impl/misc/ArrayMap.template | 216 ++++++++++++++++++ .../maps/impl/tree/AVLTreeMap.template | 171 ++++++++++++++ .../maps/impl/tree/RBTreeMap.template | 171 ++++++++++++++ .../templates/queues/ArrayFIFOQueue.template | 14 ++ .../queues/ArrayPriorityQueue.template | 40 ++-- .../queues/HeapPriorityQueue.template | 40 ++-- .../templates/sets/AVLTreeSet.template | 53 +++++ .../templates/sets/ArraySet.template | 14 ++ .../sets/ImmutableOpenHashSet.template | 46 ++++ .../sets/LinkedOpenCustomHashSet.template | 46 ++++ .../templates/sets/LinkedOpenHashSet.template | 46 ++++ .../templates/sets/OpenCustomHashSet.template | 50 ++++ .../templates/sets/OpenHashSet.template | 50 ++++ .../templates/sets/RBTreeSet.template | 71 +++++- .../templates/utils/Collections.template | 19 +- .../templates/utils/PriorityQueues.template | 5 - 27 files changed, 1755 insertions(+), 49 deletions(-) diff --git a/src/builder/java/speiger/src/builder/GlobalVariables.java b/src/builder/java/speiger/src/builder/GlobalVariables.java index 8fbfa4b5..7ec17c6c 100644 --- a/src/builder/java/speiger/src/builder/GlobalVariables.java +++ b/src/builder/java/speiger/src/builder/GlobalVariables.java @@ -229,6 +229,7 @@ public class GlobalVariables addClassMapper("STRATEGY", "Strategy"); addClassMapper("STACK", "Stack"); addClassMapper("SUPPLIER", "Supplier"); + addAbstractMapper("SINGLE_UNARY_OPERATOR", "%1$s%1$sUnaryOperator"); addBiClassMapper("UNARY_OPERATOR", "UnaryOperator", ""); if(type.isObject()) { @@ -254,6 +255,7 @@ public class GlobalVariables public GlobalVariables createFunctions() { + addSimpleMapper("APPLY_KEY_VALUE", type.isObject() ? "apply" : "applyAs"+type.getNonFileType()); addSimpleMapper("APPLY_VALUE", valueType.isObject() ? "apply" : "applyAs"+valueType.getNonFileType()); addSimpleMapper("APPLY_CAST", "applyAs"+type.getCustomJDKType().getNonFileType()); addSimpleMapper("APPLY", type.isObject() ? "apply" : "applyAs"+type.getNonFileType()); diff --git a/src/builder/resources/speiger/assets/collections/templates/collections/Iterable.template b/src/builder/resources/speiger/assets/collections/templates/collections/Iterable.template index f3c94c05..b9d1d0a9 100644 --- a/src/builder/resources/speiger/assets/collections/templates/collections/Iterable.template +++ b/src/builder/resources/speiger/assets/collections/templates/collections/Iterable.template @@ -6,6 +6,9 @@ import java.util.function.Consumer; import speiger.src.collections.PACKAGE.functions.CONSUMER; import speiger.src.collections.objects.collections.ObjectIterable; +#else +import java.util.function.BiFunction; + #endif import speiger.src.collections.PACKAGE.functions.function.TO_OBJECT_FUNCTION; import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER; @@ -172,6 +175,7 @@ public interface ITERABLE KEY_GENERIC_TYPE extends Iterable return EMPTY_VALUE; } +#if !TYPE_OBJECT /** * Performs a reduction on the * elements of this Iterable @@ -188,9 +192,28 @@ public interface ITERABLE KEY_GENERIC_TYPE extends Iterable return state; } +#else /** * Performs a reduction on the * elements of this Iterable + * @param operator the operation that should be applied + * @param identity the start value + * @Type(E) + * @return the reduction result, returns identity if nothing was found + */ + default KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction operator) { + Objects.requireNonNull(operator); + KEY_SPECIAL_TYPE state = identity; + for(ITERATOR KEY_GENERIC_TYPE iter = iterator();iter.hasNext();) { + state = operator.APPLY_VALUE(state, iter.NEXT()); + } + return state; + } + +#endif + /** + * Performs a reduction on the + * elements of this Iterable * @param operator the operation that should be applied * @return the reduction result, returns null value if nothing was found */ @@ -207,7 +230,7 @@ public interface ITERABLE KEY_GENERIC_TYPE extends Iterable state = operator.APPLY_VALUE(state, iter.NEXT()); } return state; - } + } /** * Helper function to reduce stream usage that allows to count the valid elements. diff --git a/src/builder/resources/speiger/assets/collections/templates/lists/ArrayList.template b/src/builder/resources/speiger/assets/collections/templates/lists/ArrayList.template index 3c8865c7..c1c1eb49 100644 --- a/src/builder/resources/speiger/assets/collections/templates/lists/ArrayList.template +++ b/src/builder/resources/speiger/assets/collections/templates/lists/ArrayList.template @@ -9,6 +9,7 @@ import java.util.Iterator; import java.util.Objects; #if TYPE_OBJECT import java.util.function.Consumer; +import java.util.function.BiFunction; #endif import java.util.function.Predicate; import java.util.function.UnaryOperator; @@ -651,6 +652,7 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE return EMPTY_VALUE; } +#if !TYPE_OBJECT @Override public KEY_TYPE reduce(KEY_TYPE identity, UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { Objects.requireNonNull(operator); @@ -661,6 +663,18 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE return state; } +#else + @Override + public KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction operator) { + Objects.requireNonNull(operator); + KEY_SPECIAL_TYPE state = identity; + for(int i = 0;i KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction operator) { + Objects.requireNonNull(operator); + KEY_SPECIAL_TYPE state = identity; + for(int i = 0,m=data.length;i KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction operator) { + Objects.requireNonNull(operator); + KEY_SPECIAL_TYPE state = identity; + for(Entry KEY_GENERIC_TYPE entry = first;entry != null;entry = entry.next) { + state = operator.APPLY_VALUE(state, entry.value); + } + return state; + } + +#endif @Override public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { Objects.requireNonNull(operator); diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/abstracts/AbstractMap.template b/src/builder/resources/speiger/assets/collections/templates/maps/abstracts/AbstractMap.template index 9ed3f62a..90cc6c67 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/abstracts/AbstractMap.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/abstracts/AbstractMap.template @@ -49,7 +49,7 @@ public abstract class ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE extends AbstractMap E reduce(E identity, BiFunction operator) { + Objects.requireNonNull(operator); + E state = identity; + int index = firstIndex; + while(index != -1) { + state = operator.apply(state, new BasicEntryKV_BRACES(keys[index], values[index])); + index = (int)links[index]; + } + return state; + } + + @Override + public MAP.Entry KEY_VALUE_GENERIC_TYPE reduce(ObjectObjectUnaryOperator operator) { + Objects.requireNonNull(operator); + MAP.Entry KEY_VALUE_GENERIC_TYPE state = null; + boolean empty = true; + int index = firstIndex; + while(index != -1) { + if(empty) { + empty = false; + state = new BasicEntryKV_BRACES(keys[index], values[index]); + index = (int)links[index]; + continue; + } + state = operator.apply(state, new BasicEntryKV_BRACES(keys[index], values[index])); + index = (int)links[index]; + } + return state; + } + @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE findFirst(Object2BooleanFunction filter) { Objects.requireNonNull(filter); @@ -971,6 +1011,52 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M return true; } +#if !TYPE_OBJECT + @Override + public KEY_TYPE reduce(KEY_TYPE identity, SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + KEY_TYPE state = identity; + int index = firstIndex; + while(index != -1) { + state = operator.APPLY_KEY_VALUE(state, keys[index]); + index = (int)links[index]; + } + return state; + } + +#else + @Override + public KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction operator) { + Objects.requireNonNull(operator); + KEY_SPECIAL_TYPE state = identity; + int index = firstIndex; + while(index != -1) { + state = operator.apply(state, keys[index]); + index = (int)links[index]; + } + return state; + } + +#endif + @Override + public KEY_TYPE reduce(SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + KEY_TYPE state = EMPTY_KEY_VALUE; + boolean empty = true; + int index = firstIndex; + while(index != -1) { + if(empty) { + empty = false; + state = keys[index]; + index = (int)links[index]; + continue; + } + state = operator.APPLY_KEY_VALUE(state, keys[index]); + index = (int)links[index]; + } + return state; + } + @Override public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { Objects.requireNonNull(filter); @@ -1100,6 +1186,52 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M return true; } +#if !VALUE_OBJECT + @Override + public VALUE_TYPE reduce(VALUE_TYPE identity, VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + VALUE_TYPE state = identity; + int index = firstIndex; + while(index != -1) { + state = operator.APPLY_VALUE(state, values[index]); + index = (int)links[index]; + } + return state; + } + +#else + @Override + public VALUE_SPECIAL_TYPE reduce(VALUE_SPECIAL_TYPE identity, BiFunction operator) { + Objects.requireNonNull(operator); + VALUE_SPECIAL_TYPE state = identity; + int index = firstIndex; + while(index != -1) { + state = operator.apply(state, values[index]); + index = (int)links[index]; + } + return state; + } + +#endif + @Override + public VALUE_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + VALUE_TYPE state = EMPTY_VALUE; + boolean empty = true; + int index = firstIndex; + while(index != -1) { + if(empty) { + empty = false; + state = values[index]; + index = (int)links[index]; + continue; + } + state = operator.APPLY_VALUE(state, values[index]); + index = (int)links[index]; + } + return state; + } + @Override public VALUE_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { Objects.requireNonNull(filter); diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/impl/customHash/OpenCustomHashMap.template b/src/builder/resources/speiger/assets/collections/templates/maps/impl/customHash/OpenCustomHashMap.template index 03f43570..805019b4 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/impl/customHash/OpenCustomHashMap.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/impl/customHash/OpenCustomHashMap.template @@ -6,6 +6,7 @@ import java.util.Map; import java.util.NoSuchElementException; import java.util.Objects; import java.util.function.Consumer; +import java.util.function.BiFunction; #if !TYPE_OBJECT import speiger.src.collections.PACKAGE.collections.ITERATOR; @@ -15,6 +16,9 @@ import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUME import speiger.src.collections.PACKAGE.functions.consumer.BI_CONSUMER; import speiger.src.collections.PACKAGE.functions.function.FUNCTION; import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR; +#if !SAME_TYPE +import speiger.src.collections.PACKAGE.functions.function.SINGLE_UNARY_OPERATOR; +#endif #if !TYPE_OBJECT && !VALUE_BOOLEAN import speiger.src.collections.PACKAGE.functions.function.PREDICATE; #endif @@ -33,10 +37,17 @@ import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_COLLECTION; import speiger.src.collections.VALUE_PACKAGE.functions.VALUE_SUPPLIER; #if !SAME_TYPE import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_UNARY_OPERATOR; + #if !VALUE_OBJECT +#if !TYPE_OBJECT +import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; +#endif import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_ITERATOR; import speiger.src.collections.VALUE_PACKAGE.functions.VALUE_CONSUMER; #endif +#else if !VALUE_OBJECT +import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; + #endif #if !TYPE_OBJECT && !VALUE_OBJECT || !VALUE_OBJECT import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; @@ -874,6 +885,39 @@ public class CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VAL return true; } + @Override + public E reduce(E identity, BiFunction operator) { + Objects.requireNonNull(operator); + E state = identity; + if(containsNull) state = operator.apply(state, new BasicEntryKV_BRACES(keys[nullIndex], values[nullIndex])); + for(int i = nullIndex-1;i>=0;i--) { + if(strategy.equals(keys[i], EMPTY_KEY_VALUE)) continue; + state = operator.apply(state, new BasicEntryKV_BRACES(keys[i], values[i])); + } + return state; + } + + @Override + public MAP.Entry KEY_VALUE_GENERIC_TYPE reduce(ObjectObjectUnaryOperator operator) { + Objects.requireNonNull(operator); + MAP.Entry KEY_VALUE_GENERIC_TYPE state = null; + boolean empty = true; + if(containsNull) { + state = new BasicEntryKV_BRACES(keys[nullIndex], values[nullIndex]); + empty = false; + } + for(int i = 0;i filter) { Objects.requireNonNull(filter); @@ -1045,6 +1089,54 @@ public class CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VAL return true; } +#if !TYPE_OBJECT + @Override + public KEY_TYPE reduce(KEY_TYPE identity, SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + KEY_TYPE state = identity; + if(containsNull) state = operator.APPLY_KEY_VALUE(state, keys[nullIndex]); + for(int i = nullIndex-1;i>=0;i--) { + if(strategy.equals(keys[i], EMPTY_KEY_VALUE)) continue; + state = operator.APPLY_KEY_VALUE(state, keys[i]); + } + return state; + } + +#else + @Override + public KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction operator) { + Objects.requireNonNull(operator); + KEY_SPECIAL_TYPE state = identity; + if(containsNull) state = operator.apply(state, keys[nullIndex]); + for(int i = nullIndex-1;i>=0;i--) { + if(strategy.equals(keys[i], EMPTY_KEY_VALUE)) continue; + state = operator.apply(state, keys[i]); + } + return state; + } + +#endif + @Override + public KEY_TYPE reduce(SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + KEY_TYPE state = EMPTY_KEY_VALUE; + boolean empty = true; + if(containsNull) { + state = keys[nullIndex]; + empty = false; + } + for(int i = 0;i=0;i--) { + if(strategy.equals(keys[i], EMPTY_KEY_VALUE)) continue; + state = operator.APPLY_VALUE(state, values[i]); + } + return state; + } + +#else + @Override + public VALUE_SPECIAL_TYPE reduce(VALUE_SPECIAL_TYPE identity, BiFunction operator) { + Objects.requireNonNull(operator); + VALUE_SPECIAL_TYPE state = identity; + if(containsNull) state = operator.APPLY_VALUE(state, values[nullIndex]); + for(int i = nullIndex-1;i>=0;i--) { + if(strategy.equals(keys[i], EMPTY_KEY_VALUE)) continue; + state = operator.APPLY_VALUE(state, values[i]); + } + return state; + } + +#endif + @Override + public VALUE_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + VALUE_TYPE state = EMPTY_VALUE; + boolean empty = true; + if(containsNull) { + state = values[nullIndex]; + empty = false; + } + for(int i = 0;i E reduce(E identity, BiFunction operator) { + Objects.requireNonNull(operator); + E state = identity; + int index = firstIndex; + while(index != -1) { + state = operator.apply(state, new BasicEntryKV_BRACES(keys[index], values[index])); + index = (int)links[index]; + } + return state; + } + + @Override + public MAP.Entry KEY_VALUE_GENERIC_TYPE reduce(ObjectObjectUnaryOperator operator) { + Objects.requireNonNull(operator); + MAP.Entry KEY_VALUE_GENERIC_TYPE state = null; + boolean empty = true; + int index = firstIndex; + while(index != -1) { + if(empty) { + empty = false; + state = new BasicEntryKV_BRACES(keys[index], values[index]); + index = (int)links[index]; + continue; + } + state = operator.apply(state, new BasicEntryKV_BRACES(keys[index], values[index])); + index = (int)links[index]; + } + return state; + } + @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE findFirst(Object2BooleanFunction filter) { Objects.requireNonNull(filter); @@ -941,13 +981,59 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G Objects.requireNonNull(filter); if(size() <= 0) return true; int index = firstIndex; - while(index != -1){ + while(index != -1) { if(!filter.TEST_VALUE(keys[index])) return false; index = (int)links[index]; } return true; } +#if !TYPE_OBJECT + @Override + public KEY_TYPE reduce(KEY_TYPE identity, SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + KEY_TYPE state = identity; + int index = firstIndex; + while(index != -1) { + state = operator.APPLY_KEY_VALUE(state, keys[index]); + index = (int)links[index]; + } + return state; + } + +#else + @Override + public KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction operator) { + Objects.requireNonNull(operator); + KEY_SPECIAL_TYPE state = identity; + int index = firstIndex; + while(index != -1) { + state = operator.apply(state, keys[index]); + index = (int)links[index]; + } + return state; + } + +#endif + @Override + public KEY_TYPE reduce(SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + KEY_TYPE state = EMPTY_KEY_VALUE; + boolean empty = true; + int index = firstIndex; + while(index != -1) { + if(empty) { + empty = false; + state = keys[index]; + index = (int)links[index]; + continue; + } + state = operator.APPLY_KEY_VALUE(state, keys[index]); + index = (int)links[index]; + } + return state; + } + @Override public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { Objects.requireNonNull(filter); @@ -1059,7 +1145,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G Objects.requireNonNull(filter); if(size() <= 0) return true; int index = firstIndex; - while(index != -1){ + while(index != -1) { if(filter.VALUE_TEST_VALUE(values[index])) return false; index = (int)links[index]; } @@ -1071,13 +1157,59 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G Objects.requireNonNull(filter); if(size() <= 0) return true; int index = firstIndex; - while(index != -1){ + while(index != -1) { if(!filter.VALUE_TEST_VALUE(values[index])) return false; index = (int)links[index]; } return true; } +#if !VALUE_OBJECT + @Override + public VALUE_TYPE reduce(VALUE_TYPE identity, VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + VALUE_TYPE state = identity; + int index = firstIndex; + while(index != -1) { + state = operator.APPLY_VALUE(state, values[index]); + index = (int)links[index]; + } + return state; + } + +#else + @Override + public VALUE_SPECIAL_TYPE reduce(VALUE_SPECIAL_TYPE identity, BiFunction operator) { + Objects.requireNonNull(operator); + VALUE_SPECIAL_TYPE state = identity; + int index = firstIndex; + while(index != -1) { + state = operator.apply(state, values[index]); + index = (int)links[index]; + } + return state; + } + +#endif + @Override + public VALUE_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + VALUE_TYPE state = EMPTY_VALUE; + boolean empty = true; + int index = firstIndex; + while(index != -1) { + if(empty) { + empty = false; + state = values[index]; + index = (int)links[index]; + continue; + } + state = operator.APPLY_VALUE(state, values[index]); + index = (int)links[index]; + } + return state; + } + @Override public VALUE_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { Objects.requireNonNull(filter); diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/impl/hash/OpenHashMap.template b/src/builder/resources/speiger/assets/collections/templates/maps/impl/hash/OpenHashMap.template index 5cd34740..e0c26cec 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/impl/hash/OpenHashMap.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/impl/hash/OpenHashMap.template @@ -6,6 +6,7 @@ import java.util.Map; import java.util.NoSuchElementException; import java.util.Objects; import java.util.function.Consumer; +import java.util.function.BiFunction; #if !TYPE_OBJECT import speiger.src.collections.PACKAGE.collections.ITERATOR; @@ -15,6 +16,9 @@ import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUME import speiger.src.collections.PACKAGE.functions.consumer.BI_CONSUMER; import speiger.src.collections.PACKAGE.functions.function.FUNCTION; import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR; +#if !SAME_TYPE +import speiger.src.collections.PACKAGE.functions.function.SINGLE_UNARY_OPERATOR; +#endif #if !TYPE_OBJECT && !VALUE_BOOLEAN import speiger.src.collections.PACKAGE.functions.function.PREDICATE; #endif @@ -34,9 +38,15 @@ import speiger.src.collections.VALUE_PACKAGE.functions.VALUE_SUPPLIER; import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_UNARY_OPERATOR; #if !VALUE_OBJECT +#if !TYPE_OBJECT +import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; +#endif import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_ITERATOR; import speiger.src.collections.VALUE_PACKAGE.functions.VALUE_CONSUMER; #endif +#else if !VALUE_OBJECT +import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; + #endif #if !TYPE_OBJECT && !VALUE_OBJECT || !VALUE_OBJECT import speiger.src.collections.objects.functions.consumer.ObjectObjectConsumer; @@ -50,6 +60,7 @@ import speiger.src.collections.objects.functions.function.Object2BooleanFunction #if !TYPE_OBJECT import speiger.src.collections.objects.functions.consumer.VALUE_BI_FROM_OBJECT_CONSUMER; #endif + import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_PREDICATE; #endif import speiger.src.collections.objects.collections.ObjectIterator; @@ -834,6 +845,39 @@ public class HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENE return true; } + @Override + public E reduce(E identity, BiFunction operator) { + Objects.requireNonNull(operator); + E state = identity; + if(containsNull) state = operator.apply(state, new BasicEntryKV_BRACES(keys[nullIndex], values[nullIndex])); + for(int i = nullIndex-1;i>=0;i--) { + if(KEY_EQUALS_NULL(keys[i])) continue; + state = operator.apply(state, new BasicEntryKV_BRACES(keys[i], values[i])); + } + return state; + } + + @Override + public MAP.Entry KEY_VALUE_GENERIC_TYPE reduce(ObjectObjectUnaryOperator operator) { + Objects.requireNonNull(operator); + MAP.Entry KEY_VALUE_GENERIC_TYPE state = null; + boolean empty = true; + if(containsNull) { + state = new BasicEntryKV_BRACES(keys[nullIndex], values[nullIndex]); + empty = false; + } + for(int i = 0;i filter) { Objects.requireNonNull(filter); @@ -1005,6 +1049,54 @@ public class HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENE return true; } +#if !TYPE_OBJECT + @Override + public KEY_TYPE reduce(KEY_TYPE identity, SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + KEY_TYPE state = identity; + if(containsNull) state = operator.APPLY_KEY_VALUE(state, keys[nullIndex]); + for(int i = nullIndex-1;i>=0;i--) { + if(KEY_EQUALS_NULL(keys[i])) continue; + state = operator.APPLY_KEY_VALUE(state, keys[i]); + } + return state; + } + +#else + @Override + public KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction operator) { + Objects.requireNonNull(operator); + KEY_SPECIAL_TYPE state = identity; + if(containsNull) state = operator.apply(state, keys[nullIndex]); + for(int i = nullIndex-1;i>=0;i--) { + if(KEY_EQUALS_NULL(keys[i])) continue; + state = operator.apply(state, keys[i]); + } + return state; + } + +#endif + @Override + public KEY_TYPE reduce(SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + KEY_TYPE state = EMPTY_KEY_VALUE; + boolean empty = true; + if(containsNull) { + state = keys[nullIndex]; + empty = false; + } + for(int i = 0;i=0;i--) { + if(KEY_EQUALS_NULL(keys[i])) continue; + state = operator.APPLY_VALUE(state, values[i]); + } + return state; + } + +#else + @Override + public VALUE_SPECIAL_TYPE reduce(VALUE_SPECIAL_TYPE identity, BiFunction operator) { + Objects.requireNonNull(operator); + VALUE_SPECIAL_TYPE state = identity; + if(containsNull) state = operator.APPLY_VALUE(state, values[nullIndex]); + for(int i = nullIndex-1;i>=0;i--) { + if(KEY_EQUALS_NULL(keys[i])) continue; + state = operator.APPLY_VALUE(state, values[i]); + } + return state; + } + +#endif + @Override + public VALUE_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + VALUE_TYPE state = EMPTY_VALUE; + boolean empty = true; + if(containsNull) { + state = values[nullIndex]; + empty = false; + } + for(int i = 0;i E reduce(E identity, BiFunction operator) { + Objects.requireNonNull(operator); + E state = identity; + int index = firstIndex; + while(index != -1) { + state = operator.apply(state, new BasicEntryKV_BRACES(keys[index], values[index])); + index = (int)links[index]; + } + return state; + } + + @Override + public MAP.Entry KEY_VALUE_GENERIC_TYPE reduce(ObjectObjectUnaryOperator operator) { + Objects.requireNonNull(operator); + MAP.Entry KEY_VALUE_GENERIC_TYPE state = null; + boolean empty = true; + int index = firstIndex; + while(index != -1) { + if(empty) { + empty = false; + state = new BasicEntryKV_BRACES(keys[index], values[index]); + index = (int)links[index]; + continue; + } + state = operator.apply(state, new BasicEntryKV_BRACES(keys[index], values[index])); + index = (int)links[index]; + } + return state; + } + @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE findFirst(Object2BooleanFunction filter) { Objects.requireNonNull(filter); @@ -879,6 +918,52 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_ return true; } +#if !TYPE_OBJECT + @Override + public KEY_TYPE reduce(KEY_TYPE identity, SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + KEY_TYPE state = identity; + int index = firstIndex; + while(index != -1) { + state = operator.APPLY_KEY_VALUE(state, keys[index]); + index = (int)links[index]; + } + return state; + } + +#else + @Override + public KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction operator) { + Objects.requireNonNull(operator); + KEY_SPECIAL_TYPE state = identity; + int index = firstIndex; + while(index != -1) { + state = operator.apply(state, keys[index]); + index = (int)links[index]; + } + return state; + } + +#endif + @Override + public KEY_TYPE reduce(SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + KEY_TYPE state = EMPTY_KEY_VALUE; + boolean empty = true; + int index = firstIndex; + while(index != -1) { + if(empty) { + empty = false; + state = keys[index]; + index = (int)links[index]; + continue; + } + state = operator.APPLY_KEY_VALUE(state, keys[index]); + index = (int)links[index]; + } + return state; + } + @Override public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { Objects.requireNonNull(filter); @@ -1006,6 +1091,52 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_ return true; } +#if !VALUE_OBJECT + @Override + public VALUE_TYPE reduce(VALUE_TYPE identity, VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + VALUE_TYPE state = identity; + int index = firstIndex; + while(index != -1) { + state = operator.APPLY_VALUE(state, values[index]); + index = (int)links[index]; + } + return state; + } + +#else + @Override + public VALUE_SPECIAL_TYPE reduce(VALUE_SPECIAL_TYPE identity, BiFunction operator) { + Objects.requireNonNull(operator); + VALUE_SPECIAL_TYPE state = identity; + int index = firstIndex; + while(index != -1) { + state = operator.apply(state, values[index]); + index = (int)links[index]; + } + return state; + } + +#endif + @Override + public VALUE_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + VALUE_TYPE state = EMPTY_VALUE; + boolean empty = true; + int index = firstIndex; + while(index != -1) { + if(empty) { + empty = false; + state = values[index]; + index = (int)links[index]; + continue; + } + state = operator.APPLY_VALUE(state, values[index]); + index = (int)links[index]; + } + return state; + } + @Override public VALUE_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { Objects.requireNonNull(filter); diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/ArrayMap.template b/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/ArrayMap.template index 18894baf..a91c7c17 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/ArrayMap.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/ArrayMap.template @@ -6,6 +6,7 @@ import java.util.Map; import java.util.NoSuchElementException; import java.util.Objects; import java.util.function.Consumer; +import java.util.function.BiFunction; import speiger.src.collections.PACKAGE.collections.BI_ITERATOR; #if !TYPE_OBJECT @@ -19,6 +20,9 @@ import speiger.src.collections.PACKAGE.functions.function.PREDICATE; import speiger.src.collections.PACKAGE.functions.consumer.BI_CONSUMER; import speiger.src.collections.PACKAGE.functions.function.FUNCTION; import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR; +#if !SAME_TYPE +import speiger.src.collections.PACKAGE.functions.function.SINGLE_UNARY_OPERATOR; +#endif import speiger.src.collections.PACKAGE.lists.LIST_ITERATOR; import speiger.src.collections.PACKAGE.maps.abstracts.ABSTRACT_MAP; import speiger.src.collections.PACKAGE.maps.interfaces.MAP; @@ -57,6 +61,10 @@ import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_PREDICATE; import speiger.src.collections.objects.collections.ObjectIterator; #endif #if !TYPE_OBJECT +#if !VALUE_OBJECT +import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; + +#endif import speiger.src.collections.objects.collections.ObjectBidirectionalIterator; import speiger.src.collections.objects.lists.ObjectListIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; @@ -1210,6 +1218,32 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN return true; } + @Override + public E reduce(E identity, BiFunction operator) { + Objects.requireNonNull(operator); + E state = identity; + for(int i = 0;i operator) { + Objects.requireNonNull(operator); + MAP.Entry KEY_VALUE_GENERIC_TYPE state = null; + boolean empty = true; + for(int i = 0;i filter) { Objects.requireNonNull(filter); @@ -1379,6 +1413,45 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN return true; } +#if !TYPE_OBJECT + @Override + public KEY_TYPE reduce(KEY_TYPE identity, SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + KEY_TYPE state = identity; + for(int i = 0;i KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction operator) { + Objects.requireNonNull(operator); + KEY_SPECIAL_TYPE state = identity; + for(int i = 0;i VALUE_SPECIAL_TYPE reduce(VALUE_SPECIAL_TYPE identity, BiFunction operator) { + Objects.requireNonNull(operator); + VALUE_SPECIAL_TYPE state = identity; + for(int i = 0;i E reduce(E identity, BiFunction operator) { + Objects.requireNonNull(operator); + E state = identity; + for(int i = 0;i operator) { + Objects.requireNonNull(operator); + MAP.Entry KEY_VALUE_GENERIC_TYPE state = null; + boolean empty = true; + for(int i = 0;i filter) { Objects.requireNonNull(filter); @@ -1948,6 +2086,45 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN return true; } +#if !TYPE_OBJECT + @Override + public KEY_TYPE reduce(KEY_TYPE identity, SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + KEY_TYPE state = identity; + for(int i = 0;i KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction operator) { + Objects.requireNonNull(operator); + KEY_SPECIAL_TYPE state = identity; + for(int i = 0;i VALUE_SPECIAL_TYPE reduce(VALUE_SPECIAL_TYPE identity, BiFunction operator) { + Objects.requireNonNull(operator); + VALUE_SPECIAL_TYPE state = identity; + for(int i = 0;i E reduce(E identity, BiFunction operator) { + Objects.requireNonNull(operator); + E state = identity; + for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) { + state = operator.apply(state, new BasicEntryKV_BRACES(entry.key, entry.value)); + } + return state; + } + + @Override + public MAP.Entry KEY_VALUE_GENERIC_TYPE reduce(ObjectObjectUnaryOperator operator) { + Objects.requireNonNull(operator); + MAP.Entry KEY_VALUE_GENERIC_TYPE state = null; + boolean empty = true; + for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) { + if(empty) { + empty = false; + state = new BasicEntryKV_BRACES(entry.key, entry.value); + continue; + } + state = operator.apply(state, new BasicEntryKV_BRACES(entry.key, entry.value)); + } + return state; + } + @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE findFirst(Object2BooleanFunction filter) { Objects.requireNonNull(filter); @@ -1653,6 +1687,43 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ return true; } +#if !VALUE_OBJECT + @Override + public VALUE_TYPE reduce(VALUE_TYPE identity, VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + VALUE_TYPE state = identity; + for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) + state = operator.APPLY_VALUE(state, entry.value); + return state; + } + +#else + @Override + public VALUE_SPECIAL_TYPE reduce(VALUE_SPECIAL_TYPE identity, BiFunction operator) { + Objects.requireNonNull(operator); + VALUE_SPECIAL_TYPE state = identity; + for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) + state = operator.apply(state, entry.value); + return state; + } + +#endif + @Override + public VALUE_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + VALUE_TYPE state = EMPTY_VALUE; + boolean empty = true; + for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) { + if(empty) { + empty = false; + state = entry.value; + continue; + } + state = operator.APPLY_VALUE(state, entry.value); + } + return state; + } + @Override public VALUE_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { Objects.requireNonNull(filter); @@ -1893,6 +1964,32 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ return true; } + @Override + public E reduce(E identity, BiFunction operator) { + Objects.requireNonNull(operator); + E state = identity; + for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { + state = operator.apply(state, new BasicEntryKV_BRACES(entry.key, entry.value)); + } + return state; + } + + @Override + public MAP.Entry KEY_VALUE_GENERIC_TYPE reduce(ObjectObjectUnaryOperator operator) { + Objects.requireNonNull(operator); + MAP.Entry KEY_VALUE_GENERIC_TYPE state = null; + boolean empty = true; + for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { + if(empty) { + empty = false; + state = new BasicEntryKV_BRACES(entry.key, entry.value); + continue; + } + state = operator.apply(state, new BasicEntryKV_BRACES(entry.key, entry.value)); + } + return state; + } + @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE findFirst(Object2BooleanFunction filter) { Objects.requireNonNull(filter); @@ -2077,6 +2174,43 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ return true; } +#if !TYPE_OBJECT + @Override + public KEY_TYPE reduce(KEY_TYPE identity, SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + KEY_TYPE state = identity; + for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) + state = operator.APPLY_KEY_VALUE(state, entry.key); + return state; + } + +#else + @Override + public KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction operator) { + Objects.requireNonNull(operator); + KEY_SPECIAL_TYPE state = identity; + for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) + state = operator.apply(state, entry.key); + return state; + } + +#endif + @Override + public KEY_TYPE reduce(SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + KEY_TYPE state = EMPTY_KEY_VALUE; + boolean empty = true; + for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) { + if(empty) { + empty = false; + state = entry.key; + continue; + } + state = operator.APPLY_KEY_VALUE(state, entry.key); + } + return state; + } + @Override public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { Objects.requireNonNull(filter); @@ -2164,6 +2298,43 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ return true; } +#if !VALUE_OBJECT + @Override + public VALUE_TYPE reduce(VALUE_TYPE identity, VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + VALUE_TYPE state = identity; + for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) + state = operator.APPLY_VALUE(state, entry.value); + return state; + } + +#else + @Override + public VALUE_SPECIAL_TYPE reduce(VALUE_SPECIAL_TYPE identity, BiFunction operator) { + Objects.requireNonNull(operator); + VALUE_SPECIAL_TYPE state = identity; + for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) + state = operator.apply(state, entry.value); + return state; + } + +#endif + @Override + public VALUE_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + VALUE_TYPE state = EMPTY_VALUE; + boolean empty = true; + for(AVL_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { + if(empty) { + empty = false; + state = entry.value; + continue; + } + state = operator.APPLY_VALUE(state, entry.value); + } + return state; + } + @Override public VALUE_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { Objects.requireNonNull(filter); diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/impl/tree/RBTreeMap.template b/src/builder/resources/speiger/assets/collections/templates/maps/impl/tree/RBTreeMap.template index 63a8eefc..dc38a8d9 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/impl/tree/RBTreeMap.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/impl/tree/RBTreeMap.template @@ -7,6 +7,7 @@ import java.util.Comparator; import java.util.Objects; import java.util.NoSuchElementException; import java.util.function.Consumer; +import java.util.function.BiFunction; import speiger.src.collections.PACKAGE.collections.BI_ITERATOR; #if !TYPE_OBJECT @@ -20,6 +21,9 @@ import speiger.src.collections.PACKAGE.functions.function.PREDICATE; import speiger.src.collections.PACKAGE.functions.consumer.BI_CONSUMER; import speiger.src.collections.PACKAGE.functions.function.FUNCTION; import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR; +#if !SAME_TYPE +import speiger.src.collections.PACKAGE.functions.function.SINGLE_UNARY_OPERATOR; +#endif import speiger.src.collections.PACKAGE.lists.LIST_ITERATOR; import speiger.src.collections.PACKAGE.maps.abstracts.ABSTRACT_MAP; import speiger.src.collections.PACKAGE.maps.interfaces.MAP; @@ -65,6 +69,10 @@ import speiger.src.collections.objects.utils.ObjectIterators; import speiger.src.collections.objects.collections.ObjectIterator; #endif #if !TYPE_OBJECT +#if !VALUE_OBJECT +import speiger.src.collections.objects.functions.function.ObjectObjectUnaryOperator; + +#endif import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; #endif @@ -1613,6 +1621,32 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G return true; } + @Override + public E reduce(E identity, BiFunction operator) { + Objects.requireNonNull(operator); + E state = identity; + for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) { + state = operator.apply(state, new BasicEntryKV_BRACES(entry.key, entry.value)); + } + return state; + } + + @Override + public MAP.Entry KEY_VALUE_GENERIC_TYPE reduce(ObjectObjectUnaryOperator operator) { + Objects.requireNonNull(operator); + MAP.Entry KEY_VALUE_GENERIC_TYPE state = null; + boolean empty = true; + for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) { + if(empty) { + empty = false; + state = new BasicEntryKV_BRACES(entry.key, entry.value); + continue; + } + state = operator.apply(state, new BasicEntryKV_BRACES(entry.key, entry.value)); + } + return state; + } + @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE findFirst(Object2BooleanFunction filter) { Objects.requireNonNull(filter); @@ -1707,6 +1741,43 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G return true; } +#if !VALUE_OBJECT + @Override + public VALUE_TYPE reduce(VALUE_TYPE identity, VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + VALUE_TYPE state = identity; + for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) + state = operator.APPLY_VALUE(state, entry.value); + return state; + } + +#else + @Override + public VALUE_SPECIAL_TYPE reduce(VALUE_SPECIAL_TYPE identity, BiFunction operator) { + Objects.requireNonNull(operator); + VALUE_SPECIAL_TYPE state = identity; + for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) + state = operator.apply(state, entry.value); + return state; + } + +#endif + @Override + public VALUE_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + VALUE_TYPE state = EMPTY_VALUE; + boolean empty = true; + for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = findLowest(), last = findHighest();entry != null && (last == null || last != previous(entry));entry = next(entry)) { + if(empty) { + empty = false; + state = entry.value; + continue; + } + state = operator.APPLY_VALUE(state, entry.value); + } + return state; + } + @Override public VALUE_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { Objects.requireNonNull(filter); @@ -1947,6 +2018,32 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G return true; } + @Override + public E reduce(E identity, BiFunction operator) { + Objects.requireNonNull(operator); + E state = identity; + for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { + state = operator.apply(state, new BasicEntryKV_BRACES(entry.key, entry.value)); + } + return state; + } + + @Override + public MAP.Entry KEY_VALUE_GENERIC_TYPE reduce(ObjectObjectUnaryOperator operator) { + Objects.requireNonNull(operator); + MAP.Entry KEY_VALUE_GENERIC_TYPE state = null; + boolean empty = true; + for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { + if(empty) { + empty = false; + state = new BasicEntryKV_BRACES(entry.key, entry.value); + continue; + } + state = operator.apply(state, new BasicEntryKV_BRACES(entry.key, entry.value)); + } + return state; + } + @Override public MAP.Entry KEY_VALUE_GENERIC_TYPE findFirst(Object2BooleanFunction filter) { Objects.requireNonNull(filter); @@ -2144,6 +2241,43 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G return true; } +#if !TYPE_OBJECT + @Override + public KEY_TYPE reduce(KEY_TYPE identity, SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + KEY_TYPE state = identity; + for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) + state = operator.APPLY_KEY_VALUE(state, entry.key); + return state; + } + +#else + @Override + public KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction operator) { + Objects.requireNonNull(operator); + KEY_SPECIAL_TYPE state = identity; + for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) + state = operator.apply(state, entry.key); + return state; + } + +#endif + @Override + public KEY_TYPE reduce(SINGLE_UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + KEY_TYPE state = EMPTY_KEY_VALUE; + boolean empty = true; + for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = start(), end = end();entry != null && (end == null || (end != previous(entry)));entry = next(entry)) { + if(empty) { + empty = false; + state = entry.key; + continue; + } + state = operator.APPLY_KEY_VALUE(state, entry.key); + } + return state; + } + @Override public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { Objects.requireNonNull(filter); @@ -2231,6 +2365,43 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G return true; } +#if !VALUE_OBJECT + @Override + public VALUE_TYPE reduce(VALUE_TYPE identity, VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + VALUE_TYPE state = identity; + for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) + state = operator.APPLY_VALUE(state, entry.value); + return state; + } + +#else + @Override + public VALUE_SPECIAL_TYPE reduce(VALUE_SPECIAL_TYPE identity, BiFunction operator) { + Objects.requireNonNull(operator); + VALUE_SPECIAL_TYPE state = identity; + for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) + state = operator.apply(state, entry.value); + return state; + } + +#endif + @Override + public VALUE_TYPE reduce(VALUE_SINGLE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + VALUE_TYPE state = EMPTY_VALUE; + boolean empty = true; + for(RB_TREE_MAP.Entry KEY_VALUE_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { + if(empty) { + empty = false; + state = entry.value; + continue; + } + state = operator.APPLY_VALUE(state, entry.value); + } + return state; + } + @Override public VALUE_TYPE findFirst(VALUE_PREDICATE VALUE_GENERIC_TYPE filter) { Objects.requireNonNull(filter); diff --git a/src/builder/resources/speiger/assets/collections/templates/queues/ArrayFIFOQueue.template b/src/builder/resources/speiger/assets/collections/templates/queues/ArrayFIFOQueue.template index 7d0e5705..d9001a5a 100644 --- a/src/builder/resources/speiger/assets/collections/templates/queues/ArrayFIFOQueue.template +++ b/src/builder/resources/speiger/assets/collections/templates/queues/ArrayFIFOQueue.template @@ -4,6 +4,7 @@ import java.util.Arrays; #if TYPE_OBJECT import java.util.Comparator; import java.util.function.Consumer; +import java.util.function.BiFunction; #endif import java.util.Objects; import java.util.NoSuchElementException; @@ -300,6 +301,7 @@ public class ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE extends ABSTRACT_PRIORITY_QUEUE K return EMPTY_VALUE; } +#if !TYPE_OBJECT @Override public KEY_TYPE reduce(KEY_TYPE identity, UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { Objects.requireNonNull(operator); @@ -310,6 +312,18 @@ public class ARRAY_FIFO_QUEUE KEY_GENERIC_TYPE extends ABSTRACT_PRIORITY_QUEUE K return state; } +#else + @Override + public KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction operator) { + Objects.requireNonNull(operator); + KEY_SPECIAL_TYPE state = identity; + for(int i = 0,m=size();i KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction operator) { + Objects.requireNonNull(operator); + KEY_SPECIAL_TYPE state = identity; + for(int i = 0;i KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction operator) { + Objects.requireNonNull(operator); + KEY_SPECIAL_TYPE state = identity; + for(int i = 0;i KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction operator) { + Objects.requireNonNull(operator); + KEY_SPECIAL_TYPE state = identity; + for(Entry KEY_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { + state = operator.APPLY_VALUE(state, entry.key); + } + return state; + } + +#endif @Override public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { Objects.requireNonNull(operator); @@ -1096,6 +1110,45 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE return true; } +#if !TYPE_OBJECT + @Override + public KEY_TYPE reduce(KEY_TYPE identity, UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + KEY_TYPE state = identity; + for(Entry KEY_GENERIC_TYPE entry = start();entry != null && inRange(entry.key);entry = next(entry)) { + state = operator.APPLY_VALUE(state, entry.key); + } + return state; + } + +#else + @Override + public KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction operator) { + Objects.requireNonNull(operator); + KEY_SPECIAL_TYPE state = identity; + for(Entry KEY_GENERIC_TYPE entry = start();entry != null && inRange(entry.key);entry = next(entry)) { + state = operator.APPLY_VALUE(state, entry.key); + } + return state; + } + +#endif + @Override + public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + KEY_TYPE state = EMPTY_VALUE; + boolean empty = true; + for(Entry KEY_GENERIC_TYPE entry = start();entry != null && inRange(entry.key);entry = next(entry)) { + if(empty) { + empty = false; + state = entry.key; + continue; + } + state = operator.APPLY_VALUE(state, entry.key); + } + return state; + } + @Override public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { Objects.requireNonNull(filter); diff --git a/src/builder/resources/speiger/assets/collections/templates/sets/ArraySet.template b/src/builder/resources/speiger/assets/collections/templates/sets/ArraySet.template index c700ca7e..73b50b1c 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/ArraySet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/ArraySet.template @@ -5,6 +5,7 @@ import java.util.Collection; #if TYPE_OBJECT import java.util.Comparator; import java.util.function.Consumer; +import java.util.function.BiFunction; #endif import java.util.NoSuchElementException; import java.util.Objects; @@ -423,6 +424,7 @@ public class ARRAY_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE im return EMPTY_VALUE; } +#if !TYPE_OBJECT @Override public KEY_TYPE reduce(KEY_TYPE identity, UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { Objects.requireNonNull(operator); @@ -433,6 +435,18 @@ public class ARRAY_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE im return state; } +#else + @Override + public KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction operator) { + Objects.requireNonNull(operator); + KEY_SPECIAL_TYPE state = identity; + for(int i = 0;i KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction operator) { + Objects.requireNonNull(operator); + KEY_SPECIAL_TYPE state = identity; + int index = firstIndex; + while(index != -1) { + state = operator.APPLY_VALUE(state, keys[index]); + index = (int)links[index]; + } + return state; + } + +#endif + @Override + public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + KEY_TYPE state = EMPTY_VALUE; + boolean empty = true; + int index = firstIndex; + while(index != -1) { + if(empty) { + state = keys[index]; + empty = false; + } + else state = operator.APPLY_VALUE(state, keys[index]); + index = (int)links[index]; + } + return state; + } + @Override public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { Objects.requireNonNull(filter); diff --git a/src/builder/resources/speiger/assets/collections/templates/sets/LinkedOpenCustomHashSet.template b/src/builder/resources/speiger/assets/collections/templates/sets/LinkedOpenCustomHashSet.template index add03e96..5c78d3f3 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/LinkedOpenCustomHashSet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/LinkedOpenCustomHashSet.template @@ -3,6 +3,7 @@ package speiger.src.collections.PACKAGE.sets; #if TYPE_OBJECT import java.util.Comparator; import java.util.function.Consumer; +import java.util.function.BiFunction; #endif import java.util.Arrays; import java.util.Objects; @@ -25,6 +26,7 @@ import speiger.src.collections.PACKAGE.functions.CONSUMER; #endif import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER; import speiger.src.collections.PACKAGE.functions.function.PREDICATE; +import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR; import speiger.src.collections.PACKAGE.utils.STRATEGY; import speiger.src.collections.utils.HashUtil; import speiger.src.collections.utils.SanityChecks; @@ -577,6 +579,50 @@ public class LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE extends CUSTOM_HASH_SET KEY return true; } +#if !TYPE_OBJECT + @Override + public KEY_TYPE reduce(KEY_TYPE identity, UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + KEY_TYPE state = identity; + int index = firstIndex; + while(index != -1) { + state = operator.APPLY_VALUE(state, keys[index]); + index = (int)links[index]; + } + return state; + } + +#else + @Override + public KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction operator) { + Objects.requireNonNull(operator); + KEY_SPECIAL_TYPE state = identity; + int index = firstIndex; + while(index != -1) { + state = operator.APPLY_VALUE(state, keys[index]); + index = (int)links[index]; + } + return state; + } + +#endif + @Override + public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + KEY_TYPE state = EMPTY_VALUE; + boolean empty = true; + int index = firstIndex; + while(index != -1) { + if(empty) { + state = keys[index]; + empty = false; + } + else state = operator.APPLY_VALUE(state, keys[index]); + index = (int)links[index]; + } + return state; + } + @Override public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { Objects.requireNonNull(filter); diff --git a/src/builder/resources/speiger/assets/collections/templates/sets/LinkedOpenHashSet.template b/src/builder/resources/speiger/assets/collections/templates/sets/LinkedOpenHashSet.template index 4c4ccb7b..67adf2ab 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/LinkedOpenHashSet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/LinkedOpenHashSet.template @@ -10,6 +10,7 @@ import java.util.NoSuchElementException; import java.util.Objects; #if TYPE_OBJECT import java.util.function.Consumer; +import java.util.function.BiFunction; #endif import speiger.src.collections.PACKAGE.collections.COLLECTION; @@ -23,6 +24,7 @@ import speiger.src.collections.PACKAGE.functions.CONSUMER; #endif import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER; import speiger.src.collections.PACKAGE.functions.function.PREDICATE; +import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR; import speiger.src.collections.PACKAGE.lists.LIST_ITERATOR; #if !TYPE_OBJECT import speiger.src.collections.PACKAGE.utils.ITERATORS; @@ -432,6 +434,50 @@ public class LINKED_HASH_SET KEY_GENERIC_TYPE extends HASH_SET KEY_GENERIC_TYPE return true; } +#if !TYPE_OBJECT + @Override + public KEY_TYPE reduce(KEY_TYPE identity, UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + KEY_TYPE state = identity; + int index = firstIndex; + while(index != -1) { + state = operator.APPLY_VALUE(state, keys[index]); + index = (int)links[index]; + } + return state; + } + +#else + @Override + public KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction operator) { + Objects.requireNonNull(operator); + KEY_SPECIAL_TYPE state = identity; + int index = firstIndex; + while(index != -1) { + state = operator.APPLY_VALUE(state, keys[index]); + index = (int)links[index]; + } + return state; + } + +#endif + @Override + public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + KEY_TYPE state = EMPTY_VALUE; + boolean empty = true; + int index = firstIndex; + while(index != -1) { + if(empty) { + state = keys[index]; + empty = false; + } + else state = operator.APPLY_VALUE(state, keys[index]); + index = (int)links[index]; + } + return state; + } + @Override public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { Objects.requireNonNull(filter); diff --git a/src/builder/resources/speiger/assets/collections/templates/sets/OpenCustomHashSet.template b/src/builder/resources/speiger/assets/collections/templates/sets/OpenCustomHashSet.template index fc69e8c6..a711e8a4 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/OpenCustomHashSet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/OpenCustomHashSet.template @@ -8,6 +8,7 @@ import java.util.NoSuchElementException; import java.util.Objects; #if TYPE_OBJECT import java.util.function.Consumer; +import java.util.function.BiFunction; #endif import speiger.src.collections.PACKAGE.collections.COLLECTION; @@ -20,6 +21,7 @@ import speiger.src.collections.PACKAGE.functions.CONSUMER; #endif import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER; import speiger.src.collections.PACKAGE.functions.function.PREDICATE; +import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR; import speiger.src.collections.PACKAGE.utils.STRATEGY; import speiger.src.collections.utils.HashUtil; @@ -527,6 +529,54 @@ public class CUSTOM_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_T return true; } +#if !TYPE_OBJECT + @Override + public KEY_TYPE reduce(KEY_TYPE identity, UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + KEY_TYPE state = identity; + if(containsNull) state = operator.APPLY_VALUE(state, keys[nullIndex]); + for(int i = nullIndex-1;i>=0;i--) { + if(!strategy.equals(keys[i], EMPTY_KEY_VALUE)) continue; + state = operator.APPLY_VALUE(state, keys[i]); + } + return state; + } + +#else + @Override + public KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction operator) { + Objects.requireNonNull(operator); + KEY_SPECIAL_TYPE state = identity; + if(containsNull) state = operator.APPLY_VALUE(state, keys[nullIndex]); + for(int i = nullIndex-1;i>=0;i--) { + if(strategy.equals(keys[i], EMPTY_KEY_VALUE)) continue; + state = operator.APPLY_VALUE(state, keys[i]); + } + return state; + } + +#endif + @Override + public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + KEY_TYPE state = EMPTY_VALUE; + boolean empty = true; + if(containsNull) { + state = keys[nullIndex]; + empty = false; + } + for(int i = 0;i=0;i--) { + if(KEY_EQUALS_NULL(keys[i])) continue; + state = operator.APPLY_VALUE(state, keys[i]); + } + return state; + } + +#else + @Override + public KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction operator) { + Objects.requireNonNull(operator); + KEY_SPECIAL_TYPE state = identity; + if(containsNull) state = operator.APPLY_VALUE(state, keys[nullIndex]); + for(int i = nullIndex-1;i>=0;i--) { + if(KEY_EQUALS_NULL(keys[i])) continue; + state = operator.APPLY_VALUE(state, keys[i]); + } + return state; + } + +#endif + @Override + public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + KEY_TYPE state = EMPTY_VALUE; + boolean empty = true; + if(containsNull) { + state = keys[nullIndex]; + empty = false; + } + for(int i = 0;i KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction operator) { + Objects.requireNonNull(operator); + KEY_SPECIAL_TYPE state = identity; + for(Entry KEY_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { + state = operator.APPLY_VALUE(state, entry.key); + } + return state; + } + +#endif @Override public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { Objects.requireNonNull(operator); @@ -373,6 +378,15 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE return state; } + @Override + public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { + Objects.requireNonNull(filter); + for(Entry KEY_GENERIC_TYPE entry = first;entry != null;entry = entry.next()) { + if(filter.TEST_VALUE(entry.key)) return entry.key; + } + return EMPTY_VALUE; + } + @Override public int count(PREDICATE KEY_GENERIC_TYPE filter) { Objects.requireNonNull(filter); @@ -1157,6 +1171,45 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE return true; } +#if !TYPE_OBJECT + @Override + public KEY_TYPE reduce(KEY_TYPE identity, UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + KEY_TYPE state = identity; + for(Entry KEY_GENERIC_TYPE entry = start();entry != null && inRange(entry.key);entry = next(entry)) { + state = operator.APPLY_VALUE(state, entry.key); + } + return state; + } + +#else + @Override + public KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction operator) { + Objects.requireNonNull(operator); + KEY_SPECIAL_TYPE state = identity; + for(Entry KEY_GENERIC_TYPE entry = start();entry != null && inRange(entry.key);entry = next(entry)) { + state = operator.APPLY_VALUE(state, entry.key); + } + return state; + } + +#endif + @Override + public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { + Objects.requireNonNull(operator); + KEY_TYPE state = EMPTY_VALUE; + boolean empty = true; + for(Entry KEY_GENERIC_TYPE entry = start();entry != null && inRange(entry.key);entry = next(entry)) { + if(empty) { + empty = false; + state = entry.key; + continue; + } + state = operator.APPLY_VALUE(state, entry.key); + } + return state; + } + @Override public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { Objects.requireNonNull(filter); diff --git a/src/builder/resources/speiger/assets/collections/templates/utils/Collections.template b/src/builder/resources/speiger/assets/collections/templates/utils/Collections.template index fd997fd8..9b4173b1 100644 --- a/src/builder/resources/speiger/assets/collections/templates/utils/Collections.template +++ b/src/builder/resources/speiger/assets/collections/templates/utils/Collections.template @@ -1,6 +1,9 @@ package speiger.src.collections.PACKAGE.utils; import java.util.Collection; +#if TYPE_OBJECT +import java.util.function.BiFunction; +#endif import java.util.function.Predicate; #if PRIMITIVES import java.util.function.JAVA_PREDICATE; @@ -194,13 +197,17 @@ public class COLLECTIONS public boolean matchesNone(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return c.matchesNone(filter); } } @Override public boolean matchesAll(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return c.matchesAll(filter); } } - @Override - public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return c.findFirst(filter); } } +#if TYPE_OBJECT + public KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction operator) { synchronized(mutex) { return c.reduce(identity, operator); } } +#else @Override public KEY_TYPE reduce(KEY_TYPE identity, UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { synchronized(mutex) { return c.reduce(identity, operator); } } +#endif @Override public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { synchronized(mutex) { return c.reduce(operator); } } @Override + public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return c.findFirst(filter); } } + @Override public int count(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return c.count(filter); } } } @@ -312,13 +319,17 @@ public class COLLECTIONS public boolean matchesNone(PREDICATE KEY_GENERIC_TYPE filter) { return c.matchesNone(filter); } @Override public boolean matchesAll(PREDICATE KEY_GENERIC_TYPE filter) { return c.matchesAll(filter); } - @Override - public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { return c.findFirst(filter); } +#if TYPE_OBJECT + public KEY_SPECIAL_TYPE reduce(KEY_SPECIAL_TYPE identity, BiFunction operator) { return c.reduce(identity, operator); } +#else @Override public KEY_TYPE reduce(KEY_TYPE identity, UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { return c.reduce(identity, operator); } +#endif @Override public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { return c.reduce(operator); } @Override + public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { return c.findFirst(filter); } + @Override public int count(PREDICATE KEY_GENERIC_TYPE filter) { return c.count(filter); } } diff --git a/src/builder/resources/speiger/assets/collections/templates/utils/PriorityQueues.template b/src/builder/resources/speiger/assets/collections/templates/utils/PriorityQueues.template index 54932184..25b20cb7 100644 --- a/src/builder/resources/speiger/assets/collections/templates/utils/PriorityQueues.template +++ b/src/builder/resources/speiger/assets/collections/templates/utils/PriorityQueues.template @@ -17,7 +17,6 @@ import speiger.src.collections.PACKAGE.queues.PRIORITY_QUEUE; import speiger.src.collections.PACKAGE.functions.CONSUMER; #endif import speiger.src.collections.PACKAGE.functions.function.PREDICATE; -import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR; import speiger.src.collections.objects.functions.consumer.BI_FROM_OBJECT_CONSUMER; /** @@ -132,10 +131,6 @@ public class PRIORITY_QUEUES @Override public KEY_TYPE findFirst(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return queue.findFirst(filter); } } @Override - public KEY_TYPE reduce(KEY_TYPE identity, UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { synchronized(mutex) { return queue.reduce(identity, operator); } } - @Override - public KEY_TYPE reduce(UNARY_OPERATOR KEY_KEY_GENERIC_TYPE operator) { synchronized(mutex) { return queue.reduce(operator); } } - @Override public int count(PREDICATE KEY_GENERIC_TYPE filter) { synchronized(mutex) { return queue.count(filter); } } }