From c9cd62f5d76275d5d8f7ce9dfd7480329b131c32 Mon Sep 17 00:00:00 2001 From: Speiger Date: Wed, 15 Sep 2021 05:44:19 +0200 Subject: [PATCH] New Fixes - Fixed: Compute/ComputeIfAbsent/ComputeIfPresent/Merge/BulkMerge in maps now behave like they should. --- Changelog.md | 1 + .../impl/customHash/OpenCustomHashMap.template | 14 ++++++++++++-- .../templates/maps/impl/hash/OpenHashMap.template | 14 ++++++++++++-- .../templates/maps/impl/misc/ArrayMap.template | 14 ++++++++++++-- .../templates/maps/impl/tree/AVLTreeMap.template | 14 ++++++++++++-- .../templates/maps/impl/tree/RBTreeMap.template | 14 ++++++++++++-- 6 files changed, 61 insertions(+), 10 deletions(-) diff --git a/Changelog.md b/Changelog.md index 6a66cf38..5d157006 100644 --- a/Changelog.md +++ b/Changelog.md @@ -4,6 +4,7 @@ ### Version 0.3.7/0.4.0 - Changed: Iterable specific helper functions were moved out of Iterators and moved into Iterables - Added: New Stream replacing functions: findFirst, matchesAny/All/None +- Fixed: Compute/ComputeIfAbsent/ComputeIfPresent/Merge/BulkMerge in maps now behave like they should. ### Version 0.3.6 - Fixed: addAll non Type Specific Lists was causing crashes. 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 bacf996e..daa649ef 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 @@ -452,10 +452,15 @@ public class CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VAL int index = findIndex(key); if(index < 0) { VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, getDefaultReturnValue()); + if(strategy.equals(newValue, getDefaultReturnValue())) return newValue; insert(-index-1, key, newValue); return newValue; } VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, values[index]); + if(strategy.equals(newValue, getDefaultReturnValue())) { + removeIndex(index); + return newValue; + } values[index] = newValue; return newValue; } @@ -465,6 +470,7 @@ public class CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VAL int index = findIndex(key); if(index < 0) { VALUE_TYPE newValue = mappingFunction.GET_VALUE(key); + if(strategy.equals(newValue, getDefaultReturnValue())) return newValue; insert(-index-1, key, newValue); return newValue; } @@ -476,6 +482,10 @@ public class CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VAL int index = findIndex(key); if(index < 0) return getDefaultReturnValue(); VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, values[index]); + if(strategy.equals(newValue, getDefaultReturnValue())) { + removeIndex(index); + return newValue; + } values[index] = newValue; return newValue; } @@ -484,7 +494,7 @@ public class CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VAL public VALUE_TYPE MERGE(KEY_TYPE key, VALUE_TYPE value, VALUE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE mappingFunction) { int index = findIndex(key); VALUE_TYPE newValue = index < 0 ? value : mappingFunction.APPLY_VALUE(values[index], value); - if(newValue == getDefaultReturnValue()) { + if(strategy.equals(newValue, getDefaultReturnValue())) { if(index >= 0) removeIndex(index); } @@ -500,7 +510,7 @@ public class CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VAL KEY_TYPE key = entry.ENTRY_KEY(); int index = findIndex(key); VALUE_TYPE newValue = index < 0 ? entry.ENTRY_VALUE() : mappingFunction.APPLY_VALUE(values[index], entry.ENTRY_VALUE()); - if(newValue == getDefaultReturnValue()) { + if(strategy.equals(newValue, getDefaultReturnValue())) { if(index >= 0) removeIndex(index); } 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 6e328f8b..b8a7d7c3 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 @@ -413,10 +413,15 @@ public class HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENE int index = findIndex(key); if(index < 0) { VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, getDefaultReturnValue()); + if(KEY_EQUALS(newValue, getDefaultReturnValue())) return newValue; insert(-index-1, key, newValue); return newValue; } VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, values[index]); + if(KEY_EQUALS(newValue, getDefaultReturnValue())) { + removeIndex(index); + return newValue; + } values[index] = newValue; return newValue; } @@ -426,6 +431,7 @@ public class HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENE int index = findIndex(key); if(index < 0) { VALUE_TYPE newValue = mappingFunction.GET_VALUE(key); + if(KEY_EQUALS(newValue, getDefaultReturnValue())) return newValue; insert(-index-1, key, newValue); return newValue; } @@ -437,6 +443,10 @@ public class HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENE int index = findIndex(key); if(index < 0) return getDefaultReturnValue(); VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, values[index]); + if(KEY_EQUALS(newValue, getDefaultReturnValue())) { + removeIndex(index); + return newValue; + } values[index] = newValue; return newValue; } @@ -445,7 +455,7 @@ public class HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENE public VALUE_TYPE MERGE(KEY_TYPE key, VALUE_TYPE value, VALUE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE mappingFunction) { int index = findIndex(key); VALUE_TYPE newValue = index < 0 ? value : mappingFunction.APPLY_VALUE(values[index], value); - if(newValue == getDefaultReturnValue()) { + if(KEY_EQUALS(newValue, getDefaultReturnValue())) { if(index >= 0) removeIndex(index); } @@ -461,7 +471,7 @@ public class HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENE KEY_TYPE key = entry.ENTRY_KEY(); int index = findIndex(key); VALUE_TYPE newValue = index < 0 ? entry.ENTRY_VALUE() : mappingFunction.APPLY_VALUE(values[index], entry.ENTRY_VALUE()); - if(newValue == getDefaultReturnValue()) { + if(KEY_EQUALS(newValue, getDefaultReturnValue())) { if(index >= 0) removeIndex(index); } 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 2594fe15..bd7f107d 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 @@ -443,10 +443,15 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN int index = findIndex(key); if(index == -1) { VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, getDefaultReturnValue()); + if(KEY_EQUALS(newValue, getDefaultReturnValue())) return newValue; insertIndex(size++, key, newValue); return newValue; } VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, values[index]); + if(KEY_EQUALS(newValue, getDefaultReturnValue())) { + removeIndex(index); + return newValue; + } values[index] = newValue; return newValue; } @@ -456,6 +461,7 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN int index = findIndex(key); if(index == -1) { VALUE_TYPE newValue = mappingFunction.GET_VALUE(key); + if(KEY_EQUALS(newValue, getDefaultReturnValue())) return newValue; insertIndex(size++, key, newValue); return newValue; } @@ -467,6 +473,10 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN int index = findIndex(key); if(index == -1) return getDefaultReturnValue(); VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, values[index]); + if(KEY_EQUALS(newValue, getDefaultReturnValue())) { + removeIndex(index); + return newValue; + } values[index] = newValue; return newValue; } @@ -475,7 +485,7 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN public VALUE_TYPE MERGE(KEY_TYPE key, VALUE_TYPE value, VALUE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE mappingFunction) { int index = findIndex(key); VALUE_TYPE newValue = index == -1 ? value : mappingFunction.APPLY_VALUE(values[index], value); - if(newValue == getDefaultReturnValue()) { + if(KEY_EQUALS(newValue, getDefaultReturnValue())) { if(index >= 0) removeIndex(index); } @@ -491,7 +501,7 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN KEY_TYPE key = entry.ENTRY_KEY(); int index = findIndex(key); VALUE_TYPE newValue = index == -1 ? entry.ENTRY_VALUE() : mappingFunction.APPLY_VALUE(values[index], entry.ENTRY_VALUE()); - if(newValue == getDefaultReturnValue()) { + if(KEY_EQUALS(newValue, getDefaultReturnValue())) { if(index >= 0) removeIndex(index); } diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/impl/tree/AVLTreeMap.template b/src/builder/resources/speiger/assets/collections/templates/maps/impl/tree/AVLTreeMap.template index bf1b3262..85568899 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/impl/tree/AVLTreeMap.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/impl/tree/AVLTreeMap.template @@ -479,10 +479,15 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ Entry KEY_VALUE_GENERIC_TYPE entry = findNode(key); if(entry == null) { VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, getDefaultReturnValue()); + if(KEY_EQUALS(newValue, getDefaultReturnValue())) return newValue; put(key, newValue); return newValue; } VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, entry.value); + if(KEY_EQUALS(newValue, getDefaultReturnValue())) { + removeNode(entry); + return newValue; + } entry.value = newValue; return newValue; } @@ -492,6 +497,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ Entry KEY_VALUE_GENERIC_TYPE entry = findNode(key); if(entry == null) { VALUE_TYPE newValue = mappingFunction.GET_VALUE(key); + if(KEY_EQUALS(newValue, getDefaultReturnValue())) return newValue; put(key, newValue); return newValue; } @@ -503,6 +509,10 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ Entry KEY_VALUE_GENERIC_TYPE entry = findNode(key); if(entry == null) return getDefaultReturnValue(); VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, entry.value); + if(KEY_EQUALS(newValue, getDefaultReturnValue())) { + removeNode(entry); + return newValue; + } entry.value = newValue; return newValue; } @@ -511,7 +521,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ public VALUE_TYPE MERGE(KEY_TYPE key, VALUE_TYPE value, VALUE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE mappingFunction) { Entry KEY_VALUE_GENERIC_TYPE entry = findNode(key); VALUE_TYPE newValue = entry == null ? value : mappingFunction.APPLY_VALUE(entry.value, value); - if(newValue == getDefaultReturnValue()) { + if(KEY_EQUALS(newValue, getDefaultReturnValue())) { if(entry != null) removeNode(entry); } @@ -527,7 +537,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ KEY_TYPE key = entry.ENTRY_KEY(); Entry KEY_VALUE_GENERIC_TYPE subEntry = findNode(key); VALUE_TYPE newValue = subEntry == null ? entry.ENTRY_VALUE() : mappingFunction.APPLY_VALUE(subEntry.value, entry.ENTRY_VALUE()); - if(newValue == getDefaultReturnValue()) { + if(KEY_EQUALS(newValue, getDefaultReturnValue())) { if(subEntry != null) removeNode(subEntry); } 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 18dbec1b..e5b982f8 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 @@ -478,10 +478,15 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G Entry KEY_VALUE_GENERIC_TYPE entry = findNode(key); if(entry == null) { VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, getDefaultReturnValue()); + if(KEY_EQUALS(newValue, getDefaultReturnValue())) return newValue; put(key, newValue); return newValue; } VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, entry.value); + if(KEY_EQUALS(newValue, getDefaultReturnValue())) { + removeNode(entry); + return newValue; + } entry.value = newValue; return newValue; } @@ -491,6 +496,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G Entry KEY_VALUE_GENERIC_TYPE entry = findNode(key); if(entry == null) { VALUE_TYPE newValue = mappingFunction.GET_VALUE(key); + if(KEY_EQUALS(newValue, getDefaultReturnValue())) return newValue; put(key, newValue); return newValue; } @@ -502,6 +508,10 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G Entry KEY_VALUE_GENERIC_TYPE entry = findNode(key); if(entry == null) return getDefaultReturnValue(); VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, entry.value); + if(KEY_EQUALS(newValue, getDefaultReturnValue())) { + removeNode(entry); + return newValue; + } entry.value = newValue; return newValue; } @@ -510,7 +520,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G public VALUE_TYPE MERGE(KEY_TYPE key, VALUE_TYPE value, VALUE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE mappingFunction) { Entry KEY_VALUE_GENERIC_TYPE entry = findNode(key); VALUE_TYPE newValue = entry == null ? value : mappingFunction.APPLY_VALUE(entry.value, value); - if(newValue == getDefaultReturnValue()) { + if(KEY_EQUALS(newValue, getDefaultReturnValue())) { if(entry != null) removeNode(entry); } @@ -526,7 +536,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G KEY_TYPE key = entry.ENTRY_KEY(); Entry KEY_VALUE_GENERIC_TYPE subEntry = findNode(key); VALUE_TYPE newValue = subEntry == null ? entry.ENTRY_VALUE() : mappingFunction.APPLY_VALUE(subEntry.value, entry.ENTRY_VALUE()); - if(newValue == getDefaultReturnValue()) { + if(KEY_EQUALS(newValue, getDefaultReturnValue())) { if(subEntry != null) removeNode(subEntry); }