From 7a4a0f05d19367350ccc608f763102f71b5ff1c7 Mon Sep 17 00:00:00 2001 From: Speiger Date: Sat, 25 Dec 2021 20:07:37 +0100 Subject: [PATCH] Added subFrom (which is the inverse of addTo) --- Changelog.md | 5 +++ build.gradle | 2 +- .../templates/lists/ArrayList.template | 2 +- .../customHash/OpenCustomHashMap.template | 9 +++++ .../maps/impl/hash/OpenHashMap.template | 9 +++++ .../immutable/ImmutableOpenHashMap.template | 9 +++-- .../maps/impl/misc/ArrayMap.template | 10 +++++ .../templates/maps/impl/misc/EnumMap.template | 12 ++++++ .../maps/impl/tree/AVLTreeMap.template | 38 +++++++++++++++++-- .../maps/impl/tree/RBTreeMap.template | 36 ++++++++++++++++++ .../templates/maps/interfaces/Map.template | 11 ++++++ .../templates/utils/maps/Maps.template | 8 ++++ 12 files changed, 142 insertions(+), 9 deletions(-) diff --git a/Changelog.md b/Changelog.md index 41bc646..c54906c 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,5 +1,10 @@ # Changelog of versions +### Version 0.5.3 +- Added: OrderedMap/Set +- Added: Deprecation to Functions that are specific to Ordered interfaces in the SortedMap/Set +- Added: subFrom to Maps which is the counterpart of the addTo method + ### Version 0.5.2 - Fixed: Bugs with Queues starting with the wrong size - Fixed: ArrayGrowth for Queues was +1 instead of +50% diff --git a/build.gradle b/build.gradle index fcd8464..eb8a90e 100644 --- a/build.gradle +++ b/build.gradle @@ -18,7 +18,7 @@ repositories { } archivesBaseName = 'Primitive Collections' -version = '0.5.2'; +version = '0.5.3'; sourceCompatibility = targetCompatibility = compileJava.sourceCompatibility = compileJava.targetCompatibility = '1.8' 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 b8195d3..366bed1 100644 --- a/src/builder/resources/speiger/assets/collections/templates/lists/ArrayList.template +++ b/src/builder/resources/speiger/assets/collections/templates/lists/ArrayList.template @@ -1130,7 +1130,7 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE } protected void grow(int capacity) { - if(capacity < data.length) return; + if(capacity <= data.length) return; data = Arrays.copyOf(data, data == ARRAYS.EMPTY_ARRAY ? Math.max(DEFAULT_ARRAY_SIZE, capacity) : (int)Math.max(Math.min((long)data.length + (data.length >> 1), SanityChecks.MAX_ARRAY_SIZE), capacity)); } 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 fb51db7..426eb11 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 @@ -286,6 +286,15 @@ public class CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VAL return oldValue; } + @Override + public VALUE_TYPE subFrom(KEY_TYPE key, VALUE_TYPE value) { + int slot = findIndex(key); + if(slot < 0) return getDefaultReturnValue(); + VALUE_TYPE oldValue = values[slot]; + values[slot] -= value; + if(value < 0 ? (values[slot] >= getDefaultReturnValue()) : (values[slot] <= getDefaultReturnValue())) removeIndex(slot); + return oldValue; + } #endif #if !TYPE_OBJECT @Override 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 d7fc958..3429412 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 @@ -261,6 +261,15 @@ public class HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENE return oldValue; } + @Override + public VALUE_TYPE subFrom(KEY_TYPE key, VALUE_TYPE value) { + int slot = findIndex(key); + if(slot < 0) return getDefaultReturnValue(); + VALUE_TYPE oldValue = values[slot]; + values[slot] -= value; + if(value < 0 ? (values[slot] >= getDefaultReturnValue()) : (values[slot] <= getDefaultReturnValue())) removeIndex(slot); + return oldValue; + } #endif #if !TYPE_OBJECT @Override 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 6b7f0b6..921185b 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 @@ -25,6 +25,7 @@ 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.interfaces.SORTED_MAP; +import speiger.src.collections.PACKAGE.maps.interfaces.ORDERED_MAP; import speiger.src.collections.PACKAGE.maps.abstracts.ABSTRACT_MAP; import speiger.src.collections.PACKAGE.maps.interfaces.MAP; import speiger.src.collections.PACKAGE.sets.SORTED_SET; @@ -84,7 +85,7 @@ import speiger.src.collections.utils.SanityChecks; * @Type(T) * @ValueType(V) */ -public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE implements SORTED_MAP KEY_VALUE_GENERIC_TYPE +public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENERIC_TYPE implements SORTED_MAP KEY_VALUE_GENERIC_TYPE, ORDERED_MAP KEY_VALUE_GENERIC_TYPE { /** The Backing keys array */ protected transient KEY_TYPE[] keys; @@ -296,12 +297,12 @@ public class IMMUTABLE_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_ @Override public VALUE_TYPE put(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); } @Override - public VALUE_TYPE putIfAbsent(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); } - + public VALUE_TYPE putIfAbsent(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); } #if VALUE_PRIMITIVES @Override public VALUE_TYPE addTo(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); } - + @Override + public VALUE_TYPE subFrom(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); } #endif @Override public VALUE_TYPE putAndMoveToFirst(KEY_TYPE key, VALUE_TYPE value) { 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 2514dac..c015c6f 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 @@ -222,6 +222,16 @@ public class ARRAY_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GEN return oldValue; } + @Override + public VALUE_TYPE subFrom(KEY_TYPE key, VALUE_TYPE value) { + int slot = findIndex(key); + if(slot < 0) return getDefaultReturnValue(); + VALUE_TYPE oldValue = values[slot]; + values[slot] -= value; + if(value < 0 ? (values[slot] >= getDefaultReturnValue()) : (values[slot] <= getDefaultReturnValue())) removeIndex(slot); + return oldValue; + } + #endif @Override public VALUE_TYPE putAndMoveToFirst(KEY_TYPE key, VALUE_TYPE value) { 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 04a69dc..4879f17 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 @@ -180,6 +180,18 @@ public class ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE return getDefaultReturnValue(); } + @Override + public VALUE_TYPE subFrom(T key, VALUE_TYPE value) { + int index = key.ordinal(); + if(!isSet(index)) return getDefaultReturnValue(); + VALUE_TYPE oldValue = values[index]; + values[index] -= value; + if(value < 0 ? (values[index] >= getDefaultReturnValue()) : (values[index] <= getDefaultReturnValue())) { + clear(index); + values[index] = EMPTY_VALUE; + } + return oldValue; + } #endif @Override public boolean containsKey(Object key) { 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 fd3af9a..92205f5 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 @@ -306,9 +306,6 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ #if VALUE_PRIMITIVES @Override public VALUE_TYPE addTo(KEY_TYPE key, VALUE_TYPE value) { -#if TYPE_OBJECT - validate(key); -#endif if(tree == null) { tree = first = last = new NodeKV_BRACES(key, value, null); size++; @@ -341,6 +338,30 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ return getDefaultReturnValue(); } + @Override + public VALUE_TYPE subFrom(KEY_TYPE key, VALUE_TYPE value) { + if(tree == null) return getDefaultReturnValue(); + int compare = 0; + Node KEY_VALUE_GENERIC_TYPE parent = tree; + while(true) { + if((compare = compare(key, parent.key)) == 0) + { + VALUE_TYPE oldValue = parent.subFrom(value); + if(value < 0 ? (parent.value >= getDefaultReturnValue()) : (parent.value <= getDefaultReturnValue())) removeNode(parent); + return oldValue; + } + if(compare < 0) { + if(parent.left == null) break; + parent = parent.left; + } + else if(compare > 0) { + if(parent.right == null) break; + parent = parent.right; + } + } + return getDefaultReturnValue(); + } + #endif @Override public VALUE_TYPE putAndMoveToFirst(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); } @@ -1604,6 +1625,12 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ return map.addTo(key, value); } + @Override + public VALUE_TYPE subFrom(KEY_TYPE key, VALUE_TYPE value) { + if(!inRange(key)) throw new IllegalArgumentException("key out of range"); + return map.subFrom(key, value); + } + #endif #if TYPE_OBJECT @Override @@ -2674,6 +2701,11 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ return oldValue; } + VALUE_TYPE subFrom(VALUE_TYPE value) { + VALUE_TYPE oldValue = this.value; + this.value -= value; + return oldValue; + } #endif @Override public boolean equals(Object obj) { 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 5abcb41..33b1ac8 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 @@ -341,6 +341,30 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G return getDefaultReturnValue(); } + @Override + public VALUE_TYPE subFrom(KEY_TYPE key, VALUE_TYPE value) { + if(tree == null) return getDefaultReturnValue(); + int compare = 0; + Node KEY_VALUE_GENERIC_TYPE parent = tree; + while(true) { + if((compare = compare(key, parent.key)) == 0) + { + VALUE_TYPE oldValue = parent.subFrom(value); + if(value < 0 ? (parent.value >= getDefaultReturnValue()) : (parent.value <= getDefaultReturnValue())) removeNode(parent); + return oldValue; + } + if(compare < 0) { + if(parent.left == null) break; + parent = parent.left; + } + else if(compare > 0) { + if(parent.right == null) break; + parent = parent.right; + } + } + return getDefaultReturnValue(); + } + #endif @Override public VALUE_TYPE putAndMoveToFirst(KEY_TYPE key, VALUE_TYPE value) { throw new UnsupportedOperationException(); } @@ -1659,6 +1683,12 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G return map.addTo(key, value); } + @Override + public VALUE_TYPE subFrom(KEY_TYPE key, VALUE_TYPE value) { + if(!inRange(key)) throw new IllegalArgumentException("key out of range"); + return map.subFrom(key, value); + } + #endif #if TYPE_OBJECT @Override @@ -2730,6 +2760,12 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G this.value += value; return oldValue; } + + VALUE_TYPE subFrom(VALUE_TYPE value) { + VALUE_TYPE oldValue = this.value; + this.value -= value; + return oldValue; + } #endif @Override diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/interfaces/Map.template b/src/builder/resources/speiger/assets/collections/templates/maps/interfaces/Map.template index 891f0f3..3af9b47 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/interfaces/Map.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/interfaces/Map.template @@ -179,6 +179,17 @@ public interface MAP KEY_VALUE_GENERIC_TYPE extends Map