From ef5fdbd377d01e86223852d40005b3fc02ebf704 Mon Sep 17 00:00:00 2001 From: Speiger Date: Wed, 14 Jun 2023 17:40:27 +0200 Subject: [PATCH] Finished Code Cleanup with Compute functions and added ReversedWrappers --- Changelog.md | 2 + .../templates/lists/AbstractList.template | 142 +++++++++++++++++- .../collections/templates/lists/List.template | 6 + .../concurrent/ConcurrentOpenHashMap.template | 136 ++++++++++++++++- .../customHash/OpenCustomHashMap.template | 90 ++++++++++- .../maps/impl/hash/OpenHashMap.template | 88 +++++++++++ .../immutable/ImmutableOpenHashMap.template | 11 +- .../maps/impl/misc/ArrayMap.template | 88 +++++++++++ .../templates/maps/impl/misc/EnumMap.template | 89 +++++++++++ .../maps/impl/tree/AVLTreeMap.template | 98 ++++++++++++ .../maps/impl/tree/RBTreeMap.template | 98 ++++++++++++ .../templates/utils/Lists.template | 13 ++ 12 files changed, 853 insertions(+), 8 deletions(-) diff --git a/Changelog.md b/Changelog.md index ba74fc6..cd7085f 100644 --- a/Changelog.md +++ b/Changelog.md @@ -5,7 +5,9 @@ - Added: Dedicated Set toArray implementations. - Added: ToArray/pushTop functions to Stack.class. - Added: ComputeNonDefault functions which will contain the current behavior of the Compute function, while the Compute will be changed to be more java compliant! +- Added: List.reversed, which returns a SubList that has all elements in reversed order and also inserts reversed. - Fixed: SetValue wasn't working on forEach implementations. +- Fixed: Compute functions now perform with primitives more java compliant. Meaning that getDefaultReturnValue function no longer is seen as null. ### Version 0.8.0 - Added: getFirst/getLast/removeFirst/removeLast to Lists diff --git a/src/builder/resources/speiger/assets/collections/templates/lists/AbstractList.template b/src/builder/resources/speiger/assets/collections/templates/lists/AbstractList.template index f1803f6..6276055 100644 --- a/src/builder/resources/speiger/assets/collections/templates/lists/AbstractList.template +++ b/src/builder/resources/speiger/assets/collections/templates/lists/AbstractList.template @@ -1,10 +1,12 @@ package speiger.src.collections.PACKAGE.lists; import java.util.Collection; +import java.util.Iterator; import java.util.List; import java.util.ListIterator; import java.util.NoSuchElementException; import java.util.Objects; +import java.util.RandomAccess; import speiger.src.collections.PACKAGE.collections.ABSTRACT_COLLECTION; import speiger.src.collections.PACKAGE.collections.COLLECTION; @@ -66,8 +68,7 @@ public abstract class ABSTRACT_LIST KEY_GENERIC_TYPE extends ABSTRACT_COLLECTION */ @Override @Deprecated - public boolean addAll(Collection c) - { + public boolean addAll(Collection c) { return c instanceof COLLECTION ? addAll((COLLECTION KEY_GENERIC_TYPE)c) : addAll(size(), c); } @@ -221,6 +222,11 @@ public abstract class ABSTRACT_LIST KEY_GENERIC_TYPE extends ABSTRACT_COLLECTION return new SubList(this, 0, fromIndex, toIndex); } + @Override + public LIST KEY_GENERIC_TYPE reversed() { + return new ReversedList(this); + } + @Override public ITERATOR KEY_GENERIC_TYPE iterator() { return listIterator(0); @@ -245,6 +251,138 @@ public abstract class ABSTRACT_LIST KEY_GENERIC_TYPE extends ABSTRACT_COLLECTION public ABSTRACT_LIST KEY_GENERIC_TYPE copy() { throw new UnsupportedOperationException(); } + private class ReversedList extends ABSTRACT_LIST KEY_GENERIC_TYPE + { + final ABSTRACT_LIST KEY_GENERIC_TYPE list; + + public ReversedList(ABSTRACT_LIST KEY_GENERIC_TYPE list) { + this.list = list; + } + + @Override + public void add(int index, KEY_TYPE e) { + list.add(list.size() - index - 1, e); + } + + @Override + public boolean addAll(int index, COLLECTION KEY_GENERIC_TYPE c) { + return addCollection(index, c); + } + + @Override + public boolean addAll(int index, LIST KEY_GENERIC_TYPE c) { + if(c instanceof RandomAccess) { + for(int i = 0,m=c.size();i c) { + int i = 0; + for(Iterator iter = c.iterator();iter.hasNext();i++) { + list.add(list.size() - index - i - 1, iter.next()); + } + return true; + } + + @Override + public KEY_TYPE GET_KEY(int index) { + return list.GET_KEY(list.size() - index - 1); + } + + @Override + public KEY_TYPE set(int index, KEY_TYPE e) { + return list.set(list.size() - index - 1, e); + } + + @Override + public KEY_TYPE REMOVE(int index) { + return list.REMOVE(list.size() - index - 1); + } + + @Override + public void addElements(int from, KEY_TYPE[] a, int offset, int length) { + for(int i = 0,m=length;i K[] extractElements(int from, int to, Class type) { + return reverse(list.extractElements(list.size() - to - 1, list.size() - from - 1, type)); + } + +#else + @Override + public KEY_TYPE[] extractElements(int from, int to) { + return reverse(list.extractElements(list.size() - to - 1, list.size() - from - 1)); + } + +#endif + @Override + public int size() { + return list.size(); + } + + @Override + public void clear() { + list.clear(); + } + + @Override + public LIST KEY_GENERIC_TYPE reversed() { + return list; + } + +#if TYPE_OBJECT + private K[] reverse(K[] data) { + for (int i = 0, mid = data.length >> 1, j = data.length - 1; i < mid; i++, j--) { + K t = data[i]; + data[i] = data[j]; + data[j] = t; + } + return data; + } +#else + private KEY_TYPE[] reverse(KEY_TYPE[] data) { + for (int i = 0, mid = data.length >> 1, j = data.length - 1; i < mid; i++, j--) { + KEY_TYPE t = data[i]; + data[i] = data[j]; + data[j] = t; + } + return data; + } + +#endif + } + private class SubList extends ABSTRACT_LIST KEY_GENERIC_TYPE { final ABSTRACT_LIST KEY_GENERIC_TYPE list; diff --git a/src/builder/resources/speiger/assets/collections/templates/lists/List.template b/src/builder/resources/speiger/assets/collections/templates/lists/List.template index f39ed7d..8835013 100644 --- a/src/builder/resources/speiger/assets/collections/templates/lists/List.template +++ b/src/builder/resources/speiger/assets/collections/templates/lists/List.template @@ -432,6 +432,12 @@ public interface LIST KEY_GENERIC_TYPE extends COLLECTION KEY_GENERIC_TYPE, List @Override public LIST KEY_GENERIC_TYPE subList(int from, int to); + /** + * A Type-Specific List Helper that shows all elements in reverse. + * @return a list wrapper that has all elements reversed! + */ + public LIST KEY_GENERIC_TYPE reversed(); + #if LISTS_FEATURE /** * Creates a Wrapped List that is Synchronized diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/impl/concurrent/ConcurrentOpenHashMap.template b/src/builder/resources/speiger/assets/collections/templates/maps/impl/concurrent/ConcurrentOpenHashMap.template index b888a7d..dfb5a45 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/impl/concurrent/ConcurrentOpenHashMap.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/impl/concurrent/ConcurrentOpenHashMap.template @@ -517,6 +517,13 @@ public class CONCURRENT_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY int hash = getHashCode(key); return getSegment(hash).compute(hash, key, mappingFunction); } + + @Override + public VALUE_TYPE COMPUTENonDefault(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { + Objects.requireNonNull(mappingFunction); + int hash = getHashCode(key); + return getSegment(hash).computeNonDefault(hash, key, mappingFunction); + } @Override public VALUE_TYPE COMPUTE_IF_ABSENT(KEY_TYPE key, FUNCTION KEY_VALUE_GENERIC_TYPE mappingFunction) { @@ -524,6 +531,13 @@ public class CONCURRENT_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY int hash = getHashCode(key); return getSegment(hash).computeIfAbsent(hash, key, mappingFunction); } + + @Override + public VALUE_TYPE COMPUTE_IF_ABSENTNonDefault(KEY_TYPE key, FUNCTION KEY_VALUE_GENERIC_TYPE mappingFunction) { + Objects.requireNonNull(mappingFunction); + int hash = getHashCode(key); + return getSegment(hash).computeIfAbsentNonDefault(hash, key, mappingFunction); + } @Override public VALUE_TYPE SUPPLY_IF_ABSENT(KEY_TYPE key, VALUE_SUPPLIER VALUE_GENERIC_TYPE valueProvider) { @@ -531,14 +545,28 @@ public class CONCURRENT_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY int hash = getHashCode(key); return getSegment(hash).supplyIfAbsent(hash, key, valueProvider); } - + + @Override + public VALUE_TYPE SUPPLY_IF_ABSENTNonDefault(KEY_TYPE key, VALUE_SUPPLIER VALUE_GENERIC_TYPE valueProvider) { + Objects.requireNonNull(valueProvider); + int hash = getHashCode(key); + return getSegment(hash).supplyIfAbsentNonDefault(hash, key, valueProvider); + } + @Override public VALUE_TYPE COMPUTE_IF_PRESENT(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { Objects.requireNonNull(mappingFunction); int hash = getHashCode(key); return getSegment(hash).computeIfPresent(hash, key, mappingFunction); } - + + @Override + public VALUE_TYPE COMPUTE_IF_PRESENTNonDefault(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { + Objects.requireNonNull(mappingFunction); + int hash = getHashCode(key); + return getSegment(hash).computeIfPresentNonDefault(hash, key, mappingFunction); + } + @Override public VALUE_TYPE MERGE(KEY_TYPE key, VALUE_TYPE value, VALUE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE mappingFunction) { Objects.requireNonNull(mappingFunction); @@ -2193,6 +2221,33 @@ public class CONCURRENT_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY int index = findIndex(hash, key); if(index < 0) { VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, getDefaultReturnValue()); +#if VALUE_OBJECT + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; +#endif + insert(-index-1, key, newValue); + return newValue; + } + VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, values[index]); +#if VALUE_OBJECT + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) { + removeIndex(index); + return newValue; + } +#endif + values[index] = newValue; + return newValue; + } + finally { + unlockWrite(stamp); + } + } + + protected VALUE_TYPE computeNonDefault(int hash, KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { + long stamp = writeLock(); + try { + int index = findIndex(hash, key); + if(index < 0) { + VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, getDefaultReturnValue()); if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; insert(-index-1, key, newValue); return newValue; @@ -2216,6 +2271,33 @@ public class CONCURRENT_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY int index = findIndex(hash, key); if(index < 0) { VALUE_TYPE newValue = mappingFunction.APPLY(key); +#if VALUE_OBJECT + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; +#endif + insert(-index-1, key, newValue); + return newValue; + } + VALUE_TYPE newValue = values[index]; +#if VALUE_OBJECT + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) { + newValue = mappingFunction.APPLY(key); + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; + values[index] = newValue; + } +#endif + return newValue; + } + finally { + unlockWrite(stamp); + } + } + + protected VALUE_TYPE computeIfAbsentNonDefault(int hash, KEY_TYPE key, FUNCTION KEY_VALUE_GENERIC_TYPE mappingFunction) { + long stamp = writeLock(); + try { + int index = findIndex(hash, key); + if(index < 0) { + VALUE_TYPE newValue = mappingFunction.APPLY(key); if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; insert(-index-1, key, newValue); return newValue; @@ -2239,6 +2321,33 @@ public class CONCURRENT_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY int index = findIndex(hash, key); if(index < 0) { VALUE_TYPE newValue = valueProvider.VALUE_SUPPLY_GET(); +#if VALUE_OBJECT + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; +#endif + insert(-index-1, key, newValue); + return newValue; + } + VALUE_TYPE newValue = values[index]; +#if VALUE_OBJECT + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) { + newValue = valueProvider.VALUE_SUPPLY_GET(); + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; + values[index] = newValue; + } +#endif + return newValue; + } + finally { + unlockWrite(stamp); + } + } + + protected VALUE_TYPE supplyIfAbsentNonDefault(int hash, KEY_TYPE key, VALUE_SUPPLIER VALUE_GENERIC_TYPE valueProvider) { + long stamp = writeLock(); + try { + int index = findIndex(hash, key); + if(index < 0) { + VALUE_TYPE newValue = valueProvider.VALUE_SUPPLY_GET(); if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; insert(-index-1, key, newValue); return newValue; @@ -2260,6 +2369,29 @@ public class CONCURRENT_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY long stamp = writeLock(); try { int index = findIndex(hash, key); +#if !VALUE_OBJECT + if(index < 0) return getDefaultReturnValue(); + VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, values[index]); +#else + if(index < 0 || VALUE_EQUALS(values[index], getDefaultReturnValue())) return getDefaultReturnValue(); + VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, values[index]); + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) { + removeIndex(index); + return newValue; + } +#endif + values[index] = newValue; + return newValue; + } + finally { + unlockWrite(stamp); + } + } + + protected VALUE_TYPE computeIfPresentNonDefault(int hash, KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { + long stamp = writeLock(); + try { + int index = findIndex(hash, key); if(index < 0 || VALUE_EQUALS(values[index], getDefaultReturnValue())) return getDefaultReturnValue(); VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, values[index]); if(VALUE_EQUALS(newValue, getDefaultReturnValue())) { 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 d0402cc..2f5dc2d 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 @@ -537,6 +537,29 @@ 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 VALUE_OBJECT + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; +#endif + insert(-index-1, key, newValue); + return newValue; + } + VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, values[index]); +#if VALUE_OBJECT + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) { + removeIndex(index); + return newValue; + } +#endif + values[index] = newValue; + return newValue; + } + + @Override + public VALUE_TYPE COMPUTENonDefault(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { + Objects.requireNonNull(mappingFunction); + int index = findIndex(key); + if(index < 0) { + VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, getDefaultReturnValue()); if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; insert(-index-1, key, newValue); return newValue; @@ -556,6 +579,29 @@ 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(key); +#if VALUE_OBJECT + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; +#endif + insert(-index-1, key, newValue); + return newValue; + } + VALUE_TYPE newValue = values[index]; +#if VALUE_OBJECT + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) { + newValue = mappingFunction.APPLY(key); + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; + values[index] = newValue; + } +#endif + return newValue; + } + + @Override + public VALUE_TYPE COMPUTE_IF_ABSENTNonDefault(KEY_TYPE key, FUNCTION KEY_VALUE_GENERIC_TYPE mappingFunction) { + Objects.requireNonNull(mappingFunction); + int index = findIndex(key); + if(index < 0) { + VALUE_TYPE newValue = mappingFunction.APPLY(key); if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; insert(-index-1, key, newValue); return newValue; @@ -575,6 +621,29 @@ public class CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VAL int index = findIndex(key); if(index < 0) { VALUE_TYPE newValue = valueProvider.VALUE_SUPPLY_GET(); +#if VALUE_OBJECT + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; +#endif + insert(-index-1, key, newValue); + return newValue; + } + VALUE_TYPE newValue = values[index]; +#if VALUE_OBJECT + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) { + newValue = valueProvider.VALUE_SUPPLY_GET(); + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; + values[index] = newValue; + } +#endif + return newValue; + } + + @Override + public VALUE_TYPE SUPPLY_IF_ABSENTNonDefault(KEY_TYPE key, VALUE_SUPPLIER VALUE_GENERIC_TYPE valueProvider) { + Objects.requireNonNull(valueProvider); + int index = findIndex(key); + if(index < 0) { + VALUE_TYPE newValue = valueProvider.VALUE_SUPPLY_GET(); if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; insert(-index-1, key, newValue); return newValue; @@ -587,11 +656,30 @@ public class CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VAL } return newValue; } - + @Override public VALUE_TYPE COMPUTE_IF_PRESENT(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { Objects.requireNonNull(mappingFunction); int index = findIndex(key); +#if !VALUE_OBJECT + if(index < 0) return getDefaultReturnValue(); + VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, values[index]); +#else + if(index < 0 || VALUE_EQUALS(values[index], getDefaultReturnValue())) return getDefaultReturnValue(); + VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, values[index]); + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) { + removeIndex(index); + return newValue; + } +#endif + values[index] = newValue; + return newValue; + } + + @Override + public VALUE_TYPE COMPUTE_IF_PRESENTNonDefault(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { + Objects.requireNonNull(mappingFunction); + int index = findIndex(key); if(index < 0 || VALUE_EQUALS(values[index], getDefaultReturnValue())) return getDefaultReturnValue(); VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, values[index]); if(VALUE_EQUALS(newValue, getDefaultReturnValue())) { 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 7c53877..20c4feb 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 @@ -497,6 +497,29 @@ 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 VALUE_OBJECT + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; +#endif + insert(-index-1, key, newValue); + return newValue; + } + VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, values[index]); +#if VALUE_OBJECT + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) { + removeIndex(index); + return newValue; + } +#endif + values[index] = newValue; + return newValue; + } + + @Override + public VALUE_TYPE COMPUTENonDefault(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { + Objects.requireNonNull(mappingFunction); + int index = findIndex(key); + if(index < 0) { + VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, getDefaultReturnValue()); if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; insert(-index-1, key, newValue); return newValue; @@ -516,6 +539,29 @@ 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(key); +#if VALUE_OBJECT + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; +#endif + insert(-index-1, key, newValue); + return newValue; + } + VALUE_TYPE newValue = values[index]; +#if VALUE_OBJECT + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) { + newValue = mappingFunction.APPLY(key); + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; + values[index] = newValue; + } +#endif + return newValue; + } + + @Override + public VALUE_TYPE COMPUTE_IF_ABSENTNonDefault(KEY_TYPE key, FUNCTION KEY_VALUE_GENERIC_TYPE mappingFunction) { + Objects.requireNonNull(mappingFunction); + int index = findIndex(key); + if(index < 0) { + VALUE_TYPE newValue = mappingFunction.APPLY(key); if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; insert(-index-1, key, newValue); return newValue; @@ -535,6 +581,29 @@ public class HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENE int index = findIndex(key); if(index < 0) { VALUE_TYPE newValue = valueProvider.VALUE_SUPPLY_GET(); +#if VALUE_OBJECT + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; +#endif + insert(-index-1, key, newValue); + return newValue; + } + VALUE_TYPE newValue = values[index]; +#if VALUE_OBJECT + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) { + newValue = valueProvider.VALUE_SUPPLY_GET(); + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; + values[index] = newValue; + } +#endif + return newValue; + } + + @Override + public VALUE_TYPE SUPPLY_IF_ABSENTNonDefault(KEY_TYPE key, VALUE_SUPPLIER VALUE_GENERIC_TYPE valueProvider) { + Objects.requireNonNull(valueProvider); + int index = findIndex(key); + if(index < 0) { + VALUE_TYPE newValue = valueProvider.VALUE_SUPPLY_GET(); if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; insert(-index-1, key, newValue); return newValue; @@ -552,6 +621,25 @@ public class HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENE public VALUE_TYPE COMPUTE_IF_PRESENT(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { Objects.requireNonNull(mappingFunction); int index = findIndex(key); +#if !VALUE_OBJECT + if(index < 0) return getDefaultReturnValue(); + VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, values[index]); +#else + if(index < 0 || VALUE_EQUALS(values[index], getDefaultReturnValue())) return getDefaultReturnValue(); + VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, values[index]); + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) { + removeIndex(index); + return newValue; + } +#endif + values[index] = newValue; + return newValue; + } + + @Override + public VALUE_TYPE COMPUTE_IF_PRESENTNonDefault(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { + Objects.requireNonNull(mappingFunction); + int index = findIndex(key); if(index < 0 || VALUE_EQUALS(values[index], getDefaultReturnValue())) return getDefaultReturnValue(); VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, values[index]); if(VALUE_EQUALS(newValue, getDefaultReturnValue())) { diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/impl/immutable/ImmutableOpenHashMap.template b/src/builder/resources/speiger/assets/collections/templates/maps/impl/immutable/ImmutableOpenHashMap.template index 13bbb03..57abc36 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/impl/immutable/ImmutableOpenHashMap.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/impl/immutable/ImmutableOpenHashMap.template @@ -493,15 +493,20 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_ @Override public VALUE_TYPE COMPUTE(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { throw new UnsupportedOperationException(); } - + @Override + public VALUE_TYPE COMPUTENonDefault(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { throw new UnsupportedOperationException(); } @Override public VALUE_TYPE COMPUTE_IF_ABSENT(KEY_TYPE key, FUNCTION KEY_VALUE_GENERIC_TYPE mappingFunction) { throw new UnsupportedOperationException(); } - + @Override + public VALUE_TYPE COMPUTE_IF_ABSENTNonDefault(KEY_TYPE key, FUNCTION KEY_VALUE_GENERIC_TYPE mappingFunction) { throw new UnsupportedOperationException(); } @Override public VALUE_TYPE SUPPLY_IF_ABSENT(KEY_TYPE key, VALUE_SUPPLIER VALUE_GENERIC_TYPE valueProvider) { throw new UnsupportedOperationException(); } - + @Override + public VALUE_TYPE SUPPLY_IF_ABSENTNonDefault(KEY_TYPE key, VALUE_SUPPLIER VALUE_GENERIC_TYPE valueProvider) { throw new UnsupportedOperationException(); } @Override public VALUE_TYPE COMPUTE_IF_PRESENT(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { throw new UnsupportedOperationException(); } + @Override + public VALUE_TYPE COMPUTE_IF_PRESENTNonDefault(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { throw new UnsupportedOperationException(); } @Override public VALUE_TYPE MERGE(KEY_TYPE key, VALUE_TYPE value, VALUE_UNARY_OPERATOR VALUE_VALUE_GENERIC_TYPE mappingFunction) { throw new UnsupportedOperationException(); } 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 a631ce1..ff1ee6a 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 @@ -495,6 +495,29 @@ 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 VALUE_OBJECT + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; +#endif + insertIndex(size++, key, newValue); + return newValue; + } + VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, values[index]); +#if VALUE_OBJECT + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) { + removeIndex(index); + return newValue; + } +#endif + values[index] = newValue; + return newValue; + } + + @Override + public VALUE_TYPE COMPUTENonDefault(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { + Objects.requireNonNull(mappingFunction); + int index = findIndex(key); + if(index == -1) { + VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, getDefaultReturnValue()); if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; insertIndex(size++, key, newValue); return newValue; @@ -514,6 +537,29 @@ 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(key); +#if VALUE_OBJECT + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; +#endif + insertIndex(size++, key, newValue); + return newValue; + } + VALUE_TYPE newValue = values[index]; +#if VALUE_OBJECT + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) { + newValue = mappingFunction.APPLY(key); + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; + values[index] = newValue; + } +#endif + return newValue; + } + + @Override + public VALUE_TYPE COMPUTE_IF_ABSENTNonDefault(KEY_TYPE key, FUNCTION KEY_VALUE_GENERIC_TYPE mappingFunction) { + Objects.requireNonNull(mappingFunction); + int index = findIndex(key); + if(index == -1) { + VALUE_TYPE newValue = mappingFunction.APPLY(key); if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; insertIndex(size++, key, newValue); return newValue; @@ -533,6 +579,29 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN int index = findIndex(key); if(index == -1) { VALUE_TYPE newValue = valueProvider.VALUE_SUPPLY_GET(); +#if VALUE_OBJECT + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; +#endif + insertIndex(size++, key, newValue); + return newValue; + } + VALUE_TYPE newValue = values[index]; +#if VALUE_OBJECT + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) { + newValue = valueProvider.VALUE_SUPPLY_GET(); + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; + values[index] = newValue; + } +#endif + return newValue; + } + + @Override + public VALUE_TYPE SUPPLY_IF_ABSENTNonDefault(KEY_TYPE key, VALUE_SUPPLIER VALUE_GENERIC_TYPE valueProvider) { + Objects.requireNonNull(valueProvider); + int index = findIndex(key); + if(index == -1) { + VALUE_TYPE newValue = valueProvider.VALUE_SUPPLY_GET(); if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; insertIndex(size++, key, newValue); return newValue; @@ -550,6 +619,25 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN public VALUE_TYPE COMPUTE_IF_PRESENT(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { Objects.requireNonNull(mappingFunction); int index = findIndex(key); +#if !VALUE_OBJECT + if(index == -1) return getDefaultReturnValue(); + VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, values[index]); +#else + if(index == -1 || VALUE_EQUALS(values[index], getDefaultReturnValue())) return getDefaultReturnValue(); + VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, values[index]); + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) { + removeIndex(index); + return newValue; + } +#endif + values[index] = newValue; + return newValue; + } + + @Override + public VALUE_TYPE COMPUTE_IF_PRESENTNonDefault(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { + Objects.requireNonNull(mappingFunction); + int index = findIndex(key); if(index == -1 || VALUE_EQUALS(values[index], getDefaultReturnValue())) return getDefaultReturnValue(); VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, values[index]); if(VALUE_EQUALS(newValue, getDefaultReturnValue())) { diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/EnumMap.template b/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/EnumMap.template index e117ecb..edb2e88 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/EnumMap.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/EnumMap.template @@ -363,6 +363,30 @@ public class ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE int index = key.ordinal(); if(!isSet(index)) { VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, getDefaultReturnValue()); +#if VALUE_OBJECT + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; +#endif + set(index); + values[index] = newValue; + return newValue; + } + VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, values[index]); +#if VALUE_OBJECT + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) { + clear(index); + values[index] = EMPTY_VALUE; + return newValue; + } +#endif + values[index] = newValue; + return newValue; + } + + @Override + public VALUE_TYPE COMPUTENonDefault(T key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { + int index = key.ordinal(); + if(!isSet(index)) { + VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, getDefaultReturnValue()); if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; set(index); values[index] = newValue; @@ -383,6 +407,29 @@ public class ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE int index = key.ordinal(); if(!isSet(index)) { VALUE_TYPE newValue = mappingFunction.APPLY(key); +#if VALUE_OBJECT + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; +#endif + set(index); + values[index] = newValue; + return newValue; + } + VALUE_TYPE newValue = values[index]; +#if VALUE_OBJECT + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) { + newValue = mappingFunction.APPLY(key); + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; + values[index] = newValue; + } +#endif + return newValue; + } + + @Override + public VALUE_TYPE COMPUTE_IF_ABSENTNonDefault(T key, FUNCTION KEY_VALUE_GENERIC_TYPE mappingFunction) { + int index = key.ordinal(); + if(!isSet(index)) { + VALUE_TYPE newValue = mappingFunction.APPLY(key); if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; set(index); values[index] = newValue; @@ -402,6 +449,29 @@ public class ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE int index = key.ordinal(); if(!isSet(index)) { VALUE_TYPE newValue = valueProvider.VALUE_SUPPLY_GET(); +#if VALUE_OBJECT + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; +#endif + set(index); + values[index] = newValue; + return newValue; + } + VALUE_TYPE newValue = values[index]; +#if VALUE_OBJECT + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) { + newValue = valueProvider.VALUE_SUPPLY_GET(); + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; + values[index] = newValue; + } +#endif + return newValue; + } + + @Override + public VALUE_TYPE SUPPLY_IF_ABSENTNonDefault(T key, VALUE_SUPPLIER VALUE_GENERIC_TYPE valueProvider) { + int index = key.ordinal(); + if(!isSet(index)) { + VALUE_TYPE newValue = valueProvider.VALUE_SUPPLY_GET(); if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; set(index); values[index] = newValue; @@ -419,6 +489,25 @@ public class ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE @Override public VALUE_TYPE COMPUTE_IF_PRESENT(T key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { int index = key.ordinal(); +#if !VALUE_OBJECT + if(!isSet(index)) return getDefaultReturnValue(); + VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, values[index]); +#else + if(!isSet(index) || VALUE_EQUALS(values[index], getDefaultReturnValue())) return getDefaultReturnValue(); + VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, values[index]); + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) { + clear(index); + values[index] = EMPTY_VALUE; + return newValue; + } +#endif + values[index] = newValue; + return newValue; + } + + @Override + public VALUE_TYPE COMPUTE_IF_PRESENTNonDefault(T key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { + int index = key.ordinal(); if(!isSet(index) || VALUE_EQUALS(values[index], getDefaultReturnValue())) return getDefaultReturnValue(); VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, values[index]); if(VALUE_EQUALS(newValue, getDefaultReturnValue())) { 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 03a1300..a90d2c1 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 @@ -546,6 +546,32 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ Objects.requireNonNull(mappingFunction); #if TYPE_OBJECT validate(key); +#endif + Node KEY_VALUE_GENERIC_TYPE entry = findNode(key); + if(entry == null) { + VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, getDefaultReturnValue()); +#if VALUE_OBJECT + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; +#endif + put(key, newValue); + return newValue; + } + VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, entry.value); +#if VALUE_OBJECT + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) { + removeNode(entry); + return newValue; + } +#endif + entry.value = newValue; + return newValue; + } + + @Override + public VALUE_TYPE COMPUTENonDefault(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { + Objects.requireNonNull(mappingFunction); +#if TYPE_OBJECT + validate(key); #endif Node KEY_VALUE_GENERIC_TYPE entry = findNode(key); if(entry == null) { @@ -568,6 +594,31 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ Objects.requireNonNull(mappingFunction); #if TYPE_OBJECT validate(key); +#endif + Node KEY_VALUE_GENERIC_TYPE entry = findNode(key); + if(entry == null) { + VALUE_TYPE newValue = mappingFunction.APPLY(key); +#if VALUE_OBJECT + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; +#endif + put(key, newValue); + return newValue; + } +#if VALUE_OBJECT + if(Objects.equals(entry.value, getDefaultReturnValue())) { + VALUE_TYPE newValue = mappingFunction.APPLY(key); + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; + entry.value = newValue; + } +#endif + return entry.value; + } + + @Override + public VALUE_TYPE COMPUTE_IF_ABSENTNonDefault(KEY_TYPE key, FUNCTION KEY_VALUE_GENERIC_TYPE mappingFunction) { + Objects.requireNonNull(mappingFunction); +#if TYPE_OBJECT + validate(key); #endif Node KEY_VALUE_GENERIC_TYPE entry = findNode(key); if(entry == null) { @@ -589,6 +640,31 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ Objects.requireNonNull(valueProvider); #if TYPE_OBJECT validate(key); +#endif + Node KEY_VALUE_GENERIC_TYPE entry = findNode(key); + if(entry == null) { + VALUE_TYPE newValue = valueProvider.VALUE_SUPPLY_GET(); +#if VALUE_OBJECT + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; +#endif + put(key, newValue); + return newValue; + } +#if VALUE_OBJECT + if(VALUE_EQUALS(entry.value, getDefaultReturnValue())) { + VALUE_TYPE newValue = valueProvider.VALUE_SUPPLY_GET(); + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; + entry.value = newValue; + } +#endif + return entry.value; + } + + @Override + public VALUE_TYPE SUPPLY_IF_ABSENTNonDefault(KEY_TYPE key, VALUE_SUPPLIER VALUE_GENERIC_TYPE valueProvider) { + Objects.requireNonNull(valueProvider); +#if TYPE_OBJECT + validate(key); #endif Node KEY_VALUE_GENERIC_TYPE entry = findNode(key); if(entry == null) { @@ -610,6 +686,28 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ Objects.requireNonNull(mappingFunction); #if TYPE_OBJECT validate(key); +#endif + Node KEY_VALUE_GENERIC_TYPE entry = findNode(key); +#if !VALUE_OBJECT + if(entry == null) return getDefaultReturnValue(); + VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, entry.value); +#else + if(entry == null || VALUE_EQUALS(entry.value, getDefaultReturnValue())) return getDefaultReturnValue(); + VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, entry.value); + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) { + removeNode(entry); + return newValue; + } +#endif + entry.value = newValue; + return newValue; + } + + @Override + public VALUE_TYPE COMPUTE_IF_PRESENTNonDefault(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { + Objects.requireNonNull(mappingFunction); +#if TYPE_OBJECT + validate(key); #endif Node KEY_VALUE_GENERIC_TYPE entry = findNode(key); if(entry == null || VALUE_EQUALS(entry.value, getDefaultReturnValue())) return getDefaultReturnValue(); 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 6ef4347..b036f14 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 @@ -548,6 +548,32 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G Objects.requireNonNull(mappingFunction); #if TYPE_OBJECT validate(key); +#endif + Node KEY_VALUE_GENERIC_TYPE entry = findNode(key); + if(entry == null) { + VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, getDefaultReturnValue()); +#if VALUE_OBJECT + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; +#endif + put(key, newValue); + return newValue; + } + VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, entry.value); +#if VALUE_OBJECT + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) { + removeNode(entry); + return newValue; + } +#endif + entry.value = newValue; + return newValue; + } + + @Override + public VALUE_TYPE COMPUTENonDefault(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { + Objects.requireNonNull(mappingFunction); +#if TYPE_OBJECT + validate(key); #endif Node KEY_VALUE_GENERIC_TYPE entry = findNode(key); if(entry == null) { @@ -570,6 +596,31 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G Objects.requireNonNull(mappingFunction); #if TYPE_OBJECT validate(key); +#endif + Node KEY_VALUE_GENERIC_TYPE entry = findNode(key); + if(entry == null) { + VALUE_TYPE newValue = mappingFunction.APPLY(key); +#if VALUE_OBJECT + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; +#endif + put(key, newValue); + return newValue; + } +#if VALUE_OBJECT + if(Objects.equals(entry.value, getDefaultReturnValue())) { + VALUE_TYPE newValue = mappingFunction.APPLY(key); + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; + entry.value = newValue; + } +#endif + return entry.value; + } + + @Override + public VALUE_TYPE COMPUTE_IF_ABSENTNonDefault(KEY_TYPE key, FUNCTION KEY_VALUE_GENERIC_TYPE mappingFunction) { + Objects.requireNonNull(mappingFunction); +#if TYPE_OBJECT + validate(key); #endif Node KEY_VALUE_GENERIC_TYPE entry = findNode(key); if(entry == null) { @@ -591,6 +642,31 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G Objects.requireNonNull(valueProvider); #if TYPE_OBJECT validate(key); +#endif + Node KEY_VALUE_GENERIC_TYPE entry = findNode(key); + if(entry == null) { + VALUE_TYPE newValue = valueProvider.VALUE_SUPPLY_GET(); +#if VALUE_OBJECT + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; +#endif + put(key, newValue); + return newValue; + } +#if VALUE_OBJECT + if(VALUE_EQUALS(entry.value, getDefaultReturnValue())) { + VALUE_TYPE newValue = valueProvider.VALUE_SUPPLY_GET(); + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) return newValue; + entry.value = newValue; + } +#endif + return entry.value; + } + + @Override + public VALUE_TYPE SUPPLY_IF_ABSENTNonDefault(KEY_TYPE key, VALUE_SUPPLIER VALUE_GENERIC_TYPE valueProvider) { + Objects.requireNonNull(valueProvider); +#if TYPE_OBJECT + validate(key); #endif Node KEY_VALUE_GENERIC_TYPE entry = findNode(key); if(entry == null) { @@ -612,6 +688,28 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G Objects.requireNonNull(mappingFunction); #if TYPE_OBJECT validate(key); +#endif + Node KEY_VALUE_GENERIC_TYPE entry = findNode(key); +#if !VALUE_OBJECT + if(entry == null) return getDefaultReturnValue(); + VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, entry.value); +#else + if(entry == null || VALUE_EQUALS(entry.value, getDefaultReturnValue())) return getDefaultReturnValue(); + VALUE_TYPE newValue = mappingFunction.APPLY_VALUE(key, entry.value); + if(VALUE_EQUALS(newValue, getDefaultReturnValue())) { + removeNode(entry); + return newValue; + } +#endif + entry.value = newValue; + return newValue; + } + + @Override + public VALUE_TYPE COMPUTE_IF_PRESENTNonDefault(KEY_TYPE key, UNARY_OPERATOR KEY_VALUE_GENERIC_TYPE mappingFunction) { + Objects.requireNonNull(mappingFunction); +#if TYPE_OBJECT + validate(key); #endif Node KEY_VALUE_GENERIC_TYPE entry = findNode(key); if(entry == null || VALUE_EQUALS(entry.value, getDefaultReturnValue())) return getDefaultReturnValue(); diff --git a/src/builder/resources/speiger/assets/collections/templates/utils/Lists.template b/src/builder/resources/speiger/assets/collections/templates/utils/Lists.template index 5ba3fc6..b160d45 100644 --- a/src/builder/resources/speiger/assets/collections/templates/utils/Lists.template +++ b/src/builder/resources/speiger/assets/collections/templates/utils/Lists.template @@ -413,6 +413,11 @@ public class LISTS return LISTS.synchronize(l.subList(from, to)); } + @Override + public LIST KEY_GENERIC_TYPE reversed() { + return LISTS.synchronize(l.reversed()); + } + @Override public void size(int size) { synchronized(mutex) { l.size(size); } } @@ -535,6 +540,11 @@ public class LISTS return LISTS.unmodifiable(l.subList(from, to)); } + @Override + public LIST KEY_GENERIC_TYPE reversed() { + return LISTS.unmodifiable(l.reversed()); + } + @Override public void size(int size) { throw new UnsupportedOperationException(); } @@ -636,6 +646,9 @@ public class LISTS @Override public LIST KEY_GENERIC_TYPE subList(int from, int to) { throw new UnsupportedOperationException(); } + @Override + public LIST KEY_GENERIC_TYPE reversed() { return this; } + @Override public void size(int size) { throw new UnsupportedOperationException(); }