From 57baeb261e044e1cd471c7c481ac116e0fc9ea4d Mon Sep 17 00:00:00 2001 From: Speiger Date: Fri, 28 May 2021 22:10:30 +0200 Subject: [PATCH] Added Implementation specific functions to speed up logic. --- .../customHash/OpenCustomHashMap.template | 87 ++++++++++++++++++- .../maps/impl/hash/OpenHashMap.template | 87 ++++++++++++++++++- .../maps/impl/misc/ArrayMap.template | 86 +++++++++++++++++- .../maps/impl/tree/AVLTreeMap.template | 85 +++++++++++++++++- .../maps/impl/tree/RBTreeMap.template | 85 +++++++++++++++++- .../templates/sets/ArraySet.template | 62 +++++++++++++ 6 files changed, 487 insertions(+), 5 deletions(-) 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 2ed0d89..e1090a1 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 @@ -11,10 +11,13 @@ import speiger.src.collections.PACKAGE.collections.ITERATOR; import speiger.src.collections.PACKAGE.functions.CONSUMER; #endif 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; import speiger.src.collections.PACKAGE.lists.ARRAY_LIST; import speiger.src.collections.PACKAGE.lists.LIST; import speiger.src.collections.PACKAGE.maps.abstracts.ABSTRACT_MAP; import speiger.src.collections.PACKAGE.maps.interfaces.MAP; +import speiger.src.collections.PACKAGE.utils.maps.MAPS; #if !TYPE_OBJECT import speiger.src.collections.PACKAGE.sets.ABSTRACT_SET; import speiger.src.collections.PACKAGE.sets.SET; @@ -22,10 +25,13 @@ import speiger.src.collections.PACKAGE.sets.SET; import speiger.src.collections.PACKAGE.utils.STRATEGY; import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_ABSTRACT_COLLECTION; import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_COLLECTION; -#if !SAME_TYPE && !VALUE_OBJECT +#if !SAME_TYPE +import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_UNARY_OPERATOR; +#if !VALUE_OBJECT import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_ITERATOR; import speiger.src.collections.VALUE_PACKAGE.functions.VALUE_CONSUMER; #endif +#endif import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -423,6 +429,85 @@ public class CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VAL } } + @Override + public boolean replace(KEY_TYPE key, VALUE_TYPE oldValue, VALUE_TYPE newValue) { + int index = findIndex(key); + if(index < 0 || values[index] != oldValue) return false; + values[index] = newValue; + return true; + } + + @Override + public VALUE_TYPE replace(KEY_TYPE key, VALUE_TYPE value) { + int index = findIndex(key); + if(index < 0) return getDefaultReturnValue(); + VALUE_TYPE oldValue = values[index]; + values[index] = value; + return oldValue; + } + + @Override + public VALUE_TYPE COMPUTE(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { + int index = findIndex(key); + if(index < 0) { + VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, getDefaultReturnValue()); + insert(-index-1, key, newValue); + return newValue; + } + VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, values[index]); + values[index] = newValue; + return newValue; + } + + @Override + public VALUE_TYPE COMPUTE_IF_ABSENT(KEY_TYPE key, FUNCTION KEY_VALUE_GENERIC_TYPE mappingFunction) { + int index = findIndex(key); + if(index < 0) { + VALUE_TYPE newValue = mappingFunction.GET_VALUE(key); + insert(-index-1, key, newValue); + return newValue; + } + return values[index]; + } + + @Override + public VALUE_TYPE COMPUTE_IF_PRESENT(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { + int index = findIndex(key); + if(index < 0) return getDefaultReturnValue(); + VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, values[index]); + values[index] = newValue; + return newValue; + } + + @Override + 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(index >= 0) + removeIndex(index); + } + else if(index < 0) insert(-index-1, key, newValue); + else values[index] = newValue; + return newValue; + } + + @Override + public void BULK_MERGE(MAP KEY_VALUE_GENERIC_TYPE m, VALUE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE mappingFunction) { + Objects.requireNonNull(mappingFunction); + for(MAP.Entry KEY_VALUE_GENERIC_TYPE entry : MAPS.fastIterable(m)) { + 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(index >= 0) + removeIndex(index); + } + else if(index < 0) insert(-index-1, key, newValue); + else values[index] = newValue; + } + } + @Override public int size() { return size; } 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 418e867..84626e5 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 @@ -11,20 +11,26 @@ import speiger.src.collections.PACKAGE.collections.ITERATOR; import speiger.src.collections.PACKAGE.functions.CONSUMER; #endif 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; import speiger.src.collections.PACKAGE.lists.ARRAY_LIST; import speiger.src.collections.PACKAGE.lists.LIST; import speiger.src.collections.PACKAGE.maps.abstracts.ABSTRACT_MAP; import speiger.src.collections.PACKAGE.maps.interfaces.MAP; +import speiger.src.collections.PACKAGE.utils.maps.MAPS; #if !TYPE_OBJECT import speiger.src.collections.PACKAGE.sets.ABSTRACT_SET; import speiger.src.collections.PACKAGE.sets.SET; #endif import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_ABSTRACT_COLLECTION; import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_COLLECTION; -#if !SAME_TYPE && !VALUE_OBJECT +#if !SAME_TYPE +import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_UNARY_OPERATOR; +#if !VALUE_OBJECT import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_ITERATOR; import speiger.src.collections.VALUE_PACKAGE.functions.VALUE_CONSUMER; #endif +#endif import speiger.src.collections.objects.collections.ObjectIterator; import speiger.src.collections.objects.sets.AbstractObjectSet; import speiger.src.collections.objects.sets.ObjectSet; @@ -384,6 +390,85 @@ public class HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENE } } + @Override + public boolean replace(KEY_TYPE key, VALUE_TYPE oldValue, VALUE_TYPE newValue) { + int index = findIndex(key); + if(index < 0 || values[index] != oldValue) return false; + values[index] = newValue; + return true; + } + + @Override + public VALUE_TYPE replace(KEY_TYPE key, VALUE_TYPE value) { + int index = findIndex(key); + if(index < 0) return getDefaultReturnValue(); + VALUE_TYPE oldValue = values[index]; + values[index] = value; + return oldValue; + } + + @Override + public VALUE_TYPE COMPUTE(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { + int index = findIndex(key); + if(index < 0) { + VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, getDefaultReturnValue()); + insert(-index-1, key, newValue); + return newValue; + } + VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, values[index]); + values[index] = newValue; + return newValue; + } + + @Override + public VALUE_TYPE COMPUTE_IF_ABSENT(KEY_TYPE key, FUNCTION KEY_VALUE_GENERIC_TYPE mappingFunction) { + int index = findIndex(key); + if(index < 0) { + VALUE_TYPE newValue = mappingFunction.GET_VALUE(key); + insert(-index-1, key, newValue); + return newValue; + } + return values[index]; + } + + @Override + public VALUE_TYPE COMPUTE_IF_PRESENT(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { + int index = findIndex(key); + if(index < 0) return getDefaultReturnValue(); + VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, values[index]); + values[index] = newValue; + return newValue; + } + + @Override + 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(index >= 0) + removeIndex(index); + } + else if(index < 0) insert(-index-1, key, newValue); + else values[index] = newValue; + return newValue; + } + + @Override + public void BULK_MERGE(MAP KEY_VALUE_GENERIC_TYPE m, VALUE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE mappingFunction) { + Objects.requireNonNull(mappingFunction); + for(MAP.Entry KEY_VALUE_GENERIC_TYPE entry : MAPS.fastIterable(m)) { + 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(index >= 0) + removeIndex(index); + } + else if(index < 0) insert(-index-1, key, newValue); + else values[index] = newValue; + } + } + @Override public int size() { return size; } 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 d896f3f..1c9c186 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 @@ -13,6 +13,8 @@ import speiger.src.collections.PACKAGE.functions.COMPARATOR; import speiger.src.collections.PACKAGE.functions.CONSUMER; #endif 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; 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; @@ -24,10 +26,13 @@ import speiger.src.collections.PACKAGE.utils.maps.MAPS; import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_ABSTRACT_COLLECTION; import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_COLLECTION; import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_ITERATOR; -#if !SAME_TYPE && !VALUE_OBJECT +#if !SAME_TYPE +import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_UNARY_OPERATOR; +#if !VALUE_OBJECT import speiger.src.collections.VALUE_PACKAGE.functions.VALUE_CONSUMER; import speiger.src.collections.VALUE_PACKAGE.lists.VALUE_LIST_ITERATOR; #endif +#endif #if !VALUE_OBJECT import speiger.src.collections.objects.collections.ObjectIterator; #endif @@ -416,6 +421,85 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN return entrySet; } + @Override + public boolean replace(KEY_TYPE key, VALUE_TYPE oldValue, VALUE_TYPE newValue) { + int index = findIndex(key); + if(index < 0 || values[index] != oldValue) return false; + values[index] = newValue; + return true; + } + + @Override + public VALUE_TYPE replace(KEY_TYPE key, VALUE_TYPE value) { + int index = findIndex(key); + if(index < 0) return getDefaultReturnValue(); + VALUE_TYPE oldValue = values[index]; + values[index] = value; + return oldValue; + } + + @Override + public VALUE_TYPE COMPUTE(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { + int index = findIndex(key); + if(index == -1) { + VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, getDefaultReturnValue()); + insertIndex(size++, key, newValue); + return newValue; + } + VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, values[index]); + values[index] = newValue; + return newValue; + } + + @Override + public VALUE_TYPE COMPUTE_IF_ABSENT(KEY_TYPE key, FUNCTION KEY_VALUE_GENERIC_TYPE mappingFunction) { + int index = findIndex(key); + if(index == -1) { + VALUE_TYPE newValue = mappingFunction.GET_VALUE(key); + insertIndex(size++, key, newValue); + return newValue; + } + return values[index]; + } + + @Override + public VALUE_TYPE COMPUTE_IF_PRESENT(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { + int index = findIndex(key); + if(index == -1) return getDefaultReturnValue(); + VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, values[index]); + values[index] = newValue; + return newValue; + } + + @Override + 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(index >= 0) + removeIndex(index); + } + else if(index == -1) insertIndex(size++, key, newValue); + else values[index] = newValue; + return newValue; + } + + @Override + public void BULK_MERGE(MAP KEY_VALUE_GENERIC_TYPE m, VALUE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE mappingFunction) { + Objects.requireNonNull(mappingFunction); + for(MAP.Entry KEY_VALUE_GENERIC_TYPE entry : MAPS.fastIterable(m)) { + 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(index >= 0) + removeIndex(index); + } + else if(index == -1) insertIndex(size++, key, newValue); + else values[index] = newValue; + } + } + @Override public int size() { return size; 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 995fa57..7f22153 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 @@ -3,14 +3,16 @@ package speiger.src.collections.PACKAGE.maps.impl.tree; import java.util.Map; #if TYPE_OBJECT import java.util.Comparator; -import java.util.Objects; #endif +import java.util.Objects; import java.util.NoSuchElementException; import speiger.src.collections.PACKAGE.collections.BI_ITERATOR; #if !TYPE_OBJECT import speiger.src.collections.PACKAGE.functions.COMPARATOR; #endif +import speiger.src.collections.PACKAGE.functions.function.FUNCTION; +import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR; 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; @@ -20,10 +22,12 @@ import speiger.src.collections.PACKAGE.sets.NAVIGABLE_SET; import speiger.src.collections.PACKAGE.sets.SET; import speiger.src.collections.PACKAGE.sets.SORTED_SET; import speiger.src.collections.PACKAGE.utils.ITERATORS; +import speiger.src.collections.PACKAGE.utils.maps.MAPS; import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_ABSTRACT_COLLECTION; import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_COLLECTION; import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_ITERATOR; #if !SAME_TYPE +import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_UNARY_OPERATOR; import speiger.src.collections.VALUE_PACKAGE.lists.VALUE_LIST_ITERATOR; import speiger.src.collections.VALUE_PACKAGE.utils.VALUE_ITERATORS; #endif @@ -453,6 +457,85 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ } #endif + @Override + public boolean replace(KEY_TYPE key, VALUE_TYPE oldValue, VALUE_TYPE newValue) { + Entry KEY_VALUE_GENERIC_TYPE entry = findNode(key); + if(entry == null || entry.value != oldValue) return false; + entry.value = newValue; + return true; + } + + @Override + public VALUE_TYPE replace(KEY_TYPE key, VALUE_TYPE value) { + Entry KEY_VALUE_GENERIC_TYPE entry = findNode(key); + if(entry == null) return getDefaultReturnValue(); + VALUE_TYPE oldValue = entry.value; + entry.value = value; + return oldValue; + } + + @Override + public VALUE_TYPE COMPUTE(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { + Entry KEY_VALUE_GENERIC_TYPE entry = findNode(key); + if(entry == null) { + VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, getDefaultReturnValue()); + put(key, newValue); + return newValue; + } + VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, entry.value); + entry.value = newValue; + return newValue; + } + + @Override + public VALUE_TYPE COMPUTE_IF_ABSENT(KEY_TYPE key, FUNCTION KEY_VALUE_GENERIC_TYPE mappingFunction) { + Entry KEY_VALUE_GENERIC_TYPE entry = findNode(key); + if(entry == null) { + VALUE_TYPE newValue = mappingFunction.GET_VALUE(key); + put(key, newValue); + return newValue; + } + return entry.value; + } + + @Override + public VALUE_TYPE COMPUTE_IF_PRESENT(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { + Entry KEY_VALUE_GENERIC_TYPE entry = findNode(key); + if(entry == null) return getDefaultReturnValue(); + VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, entry.value); + entry.value = newValue; + return newValue; + } + + @Override + 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(entry != null) + removeNode(entry); + } + else if(entry == null) put(key, newValue); + else entry.value = newValue; + return newValue; + } + + @Override + public void BULK_MERGE(MAP KEY_VALUE_GENERIC_TYPE m, VALUE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE mappingFunction) { + Objects.requireNonNull(mappingFunction); + for(MAP.Entry KEY_VALUE_GENERIC_TYPE entry : MAPS.fastIterable(m)) { + 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(subEntry != null) + removeNode(subEntry); + } + else if(subEntry == null) put(key, newValue); + else subEntry.value = newValue; + } + } + @Override public int size() { return size; } 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 c020918..97704da 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 @@ -3,14 +3,16 @@ package speiger.src.collections.PACKAGE.maps.impl.tree; import java.util.Map; #if TYPE_OBJECT import java.util.Comparator; -import java.util.Objects; #endif +import java.util.Objects; import java.util.NoSuchElementException; import speiger.src.collections.PACKAGE.collections.BI_ITERATOR; #if !TYPE_OBJECT import speiger.src.collections.PACKAGE.functions.COMPARATOR; #endif +import speiger.src.collections.PACKAGE.functions.function.FUNCTION; +import speiger.src.collections.PACKAGE.functions.function.UNARY_OPERATOR; 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; @@ -20,10 +22,12 @@ import speiger.src.collections.PACKAGE.sets.NAVIGABLE_SET; import speiger.src.collections.PACKAGE.sets.SET; import speiger.src.collections.PACKAGE.sets.SORTED_SET; import speiger.src.collections.PACKAGE.utils.ITERATORS; +import speiger.src.collections.PACKAGE.utils.maps.MAPS; import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_ABSTRACT_COLLECTION; import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_COLLECTION; import speiger.src.collections.VALUE_PACKAGE.collections.VALUE_ITERATOR; #if !SAME_TYPE +import speiger.src.collections.VALUE_PACKAGE.functions.function.VALUE_UNARY_OPERATOR; import speiger.src.collections.VALUE_PACKAGE.lists.VALUE_LIST_ITERATOR; import speiger.src.collections.VALUE_PACKAGE.utils.VALUE_ITERATORS; #endif @@ -452,6 +456,85 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G } #endif + @Override + public boolean replace(KEY_TYPE key, VALUE_TYPE oldValue, VALUE_TYPE newValue) { + Entry KEY_VALUE_GENERIC_TYPE entry = findNode(key); + if(entry == null || entry.value != oldValue) return false; + entry.value = newValue; + return true; + } + + @Override + public VALUE_TYPE replace(KEY_TYPE key, VALUE_TYPE value) { + Entry KEY_VALUE_GENERIC_TYPE entry = findNode(key); + if(entry == null) return getDefaultReturnValue(); + VALUE_TYPE oldValue = entry.value; + entry.value = value; + return oldValue; + } + + @Override + public VALUE_TYPE COMPUTE(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { + Entry KEY_VALUE_GENERIC_TYPE entry = findNode(key); + if(entry == null) { + VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, getDefaultReturnValue()); + put(key, newValue); + return newValue; + } + VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, entry.value); + entry.value = newValue; + return newValue; + } + + @Override + public VALUE_TYPE COMPUTE_IF_ABSENT(KEY_TYPE key, FUNCTION KEY_VALUE_GENERIC_TYPE mappingFunction) { + Entry KEY_VALUE_GENERIC_TYPE entry = findNode(key); + if(entry == null) { + VALUE_TYPE newValue = mappingFunction.GET_VALUE(key); + put(key, newValue); + return newValue; + } + return entry.value; + } + + @Override + public VALUE_TYPE COMPUTE_IF_PRESENT(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { + Entry KEY_VALUE_GENERIC_TYPE entry = findNode(key); + if(entry == null) return getDefaultReturnValue(); + VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, entry.value); + entry.value = newValue; + return newValue; + } + + @Override + 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(entry != null) + removeNode(entry); + } + else if(entry == null) put(key, newValue); + else entry.value = newValue; + return newValue; + } + + @Override + public void BULK_MERGE(MAP KEY_VALUE_GENERIC_TYPE m, VALUE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE mappingFunction) { + Objects.requireNonNull(mappingFunction); + for(MAP.Entry KEY_VALUE_GENERIC_TYPE entry : MAPS.fastIterable(m)) { + 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(subEntry != null) + removeNode(subEntry); + } + else if(subEntry == null) put(key, newValue); + else subEntry.value = newValue; + } + } + @Override public int size() { return size; } 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 7453099..69071ae 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/ArraySet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/ArraySet.template @@ -210,6 +210,68 @@ public class ARRAY_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE im return data[size - 1]; } + @Override + public boolean removeAll(COLLECTION KEY_GENERIC_TYPE c) { + int j = 0; + for(int i = 0;i c) { + int j = 0; + for(int i = 0;i c) { + int j = 0; + for(int i = 0;i