From 3b27604258238867745e181e883350cbe27d3821 Mon Sep 17 00:00:00 2001 From: Speiger Date: Thu, 24 Jun 2021 17:16:36 +0200 Subject: [PATCH] More fixes. - Fixed: containsKey & containsValue in HashMaps were deprecated for Object Variants. - Fixed: HashMap wasn't deleting Keys & Values references when removing a Object - Fixed: AVLTreeMap didn't balance properly. - Changed: EnumMap no longer tries to access SharedSecrets since its gone in java11 - Added: HashMaps now implement ITrimmable - Added: AVLTreeSet didn't balance properly - Fixed: HashMaps & LinkedMaps weren't clearing references properly. --- Changelog.md | 5 ++++- .../maps/impl/customHash/LinkedOpenCustomHashMap.template | 2 +- .../maps/impl/customHash/OpenCustomHashMap.template | 1 + .../templates/maps/impl/hash/LinkedOpenHashMap.template | 2 +- .../templates/maps/impl/hash/OpenHashMap.template | 1 + .../templates/maps/impl/misc/LinkedEnumMap.template | 2 +- .../templates/maps/impl/tree/AVLTreeMap.template | 6 +++++- .../assets/collections/templates/sets/AVLTreeSet.template | 8 ++++++-- .../templates/sets/LinkedOpenCustomHashSet.template | 2 +- .../collections/templates/sets/LinkedOpenHashSet.template | 2 +- .../collections/templates/sets/OpenCustomHashSet.template | 2 ++ .../collections/templates/sets/OpenHashSet.template | 4 +++- 12 files changed, 27 insertions(+), 10 deletions(-) diff --git a/Changelog.md b/Changelog.md index 892b43a8..a0cad17f 100644 --- a/Changelog.md +++ b/Changelog.md @@ -4,8 +4,11 @@ ### Version 0.3.1 - Fixed: containsKey & containsValue in HashMaps were deprecated for Object Variants. - Fixed: HashMap wasn't deleting Keys & Values references when removing a Object -- Fixed: AVLTreeSet didn't balance properly. +- Fixed: AVLTreeMap didn't balance properly. - Changed: EnumMap no longer tries to access SharedSecrets since its gone in java11 +- Added: HashMaps now implement ITrimmable +- Added: AVLTreeSet didn't balance properly +- Fixed: HashMaps & LinkedMaps weren't clearing references properly. ### Version 0.3.0 (Breaking 0.2.0) - Added: Stack.isEmpty was missing diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/impl/customHash/LinkedOpenCustomHashMap.template b/src/builder/resources/speiger/assets/collections/templates/maps/impl/customHash/LinkedOpenCustomHashMap.template index 6f42058f..1d58eecc 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/impl/customHash/LinkedOpenCustomHashMap.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/impl/customHash/LinkedOpenCustomHashMap.template @@ -481,7 +481,7 @@ public class LINKED_CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends CUSTOM_HASH_M } else if(lastIndex == pos) { lastIndex = (int)(links[pos] >>> 32); - if(0 <= lastIndex) links[pos] |= 0xFFFFFFFFL; + if(0 <= lastIndex) links[lastIndex] |= 0xFFFFFFFFL; } else { long link = links[pos]; 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 fc9f90fc..e73e0dbf 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 @@ -566,6 +566,7 @@ public class CUSTOM_HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VAL } protected VALUE_TYPE removeIndex(int pos) { + if(pos == nullIndex) return containsNull ? removeNullIndex() : getDefaultReturnValue(); VALUE_TYPE value = values[pos]; keys[pos] = EMPTY_KEY_VALUE; values[pos] = EMPTY_VALUE; diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/impl/hash/LinkedOpenHashMap.template b/src/builder/resources/speiger/assets/collections/templates/maps/impl/hash/LinkedOpenHashMap.template index 54705664..baa92f37 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/impl/hash/LinkedOpenHashMap.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/impl/hash/LinkedOpenHashMap.template @@ -461,7 +461,7 @@ public class LINKED_HASH_MAP KEY_VALUE_GENERIC_TYPE extends HASH_MAP KEY_VALUE_G } else if(lastIndex == pos) { lastIndex = (int)(links[pos] >>> 32); - if(0 <= lastIndex) links[pos] |= 0xFFFFFFFFL; + if(0 <= lastIndex) links[lastIndex] |= 0xFFFFFFFFL; } else { long link = links[pos]; 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 832f6879..8ba1fcfc 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 @@ -526,6 +526,7 @@ public class HASH_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_GENE } protected VALUE_TYPE removeIndex(int pos) { + if(pos == nullIndex) return containsNull ? removeNullIndex() : getDefaultReturnValue(); VALUE_TYPE value = values[pos]; keys[pos] = EMPTY_KEY_VALUE; values[pos] = EMPTY_VALUE; diff --git a/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/LinkedEnumMap.template b/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/LinkedEnumMap.template index 9f08bf03..4580e4dc 100644 --- a/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/LinkedEnumMap.template +++ b/src/builder/resources/speiger/assets/collections/templates/maps/impl/misc/LinkedEnumMap.template @@ -280,7 +280,7 @@ public class LINKED_ENUM_MAP KEY_ENUM_VALUE_GENERIC_TYPE extends ENUM_MAP KEY_VA } else if(lastIndex == pos) { lastIndex = (int)(links[pos] >>> 32); - if(0 <= lastIndex) links[pos] |= 0xFFFFFFFFL; + if(0 <= lastIndex) links[lastIndex] |= 0xFFFFFFFFL; } else { long link = links[pos]; 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 7d6e443e..75a57327 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 @@ -801,6 +801,8 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ else entry.parent.right = right; right.left = entry; entry.parent = right; + entry.updateHeight(); + right.updateHeight(); } } @@ -815,6 +817,8 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ else entry.parent.left = left; left.right = entry; entry.parent = left; + entry.updateHeight(); + left.updateHeight(); } } @@ -1933,7 +1937,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ void updateHeight() { state = (1 + Math.max(left == null ? -1 : left.getHeight(), right == null ? -1 : right.getHeight())); } - int getBalance() { return (left == null ? -1 : left.getHeight()) - (right == null ? -1 : right.getBalance()); } + int getBalance() { return (left == null ? -1 : left.getHeight()) - (right == null ? -1 : right.getHeight()); } boolean needsSuccessor() { return left != null && right != null; } diff --git a/src/builder/resources/speiger/assets/collections/templates/sets/AVLTreeSet.template b/src/builder/resources/speiger/assets/collections/templates/sets/AVLTreeSet.template index 9f50a18c..3c253509 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/AVLTreeSet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/AVLTreeSet.template @@ -549,6 +549,8 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE else entry.parent.right = right; right.left = entry; entry.parent = right; + entry.updateHeight(); + right.updateHeight(); } } @@ -564,6 +566,8 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE else entry.parent.left = left; left.right = entry; entry.parent = left; + entry.updateHeight(); + left.updateHeight(); } } @@ -1120,9 +1124,9 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE int getHeight() { return state; } - void updateHeight() { state = (1 + Math.max(left == null ? 0 : left.getHeight(), right == null ? 0 : right.getHeight())); } + void updateHeight() { state = (1 + Math.max(left == null ? -1 : left.getHeight(), right == null ? -1 : right.getHeight())); } - int getBalance() { return (left == null ? 0 : left.getHeight()) - (right == null ? 0 : right.getBalance()); } + int getBalance() { return (left == null ? -1 : left.getHeight()) - (right == null ? -1 : right.getHeight()); } boolean needsSuccessor() { return left != null && right != null; } diff --git a/src/builder/resources/speiger/assets/collections/templates/sets/LinkedOpenCustomHashSet.template b/src/builder/resources/speiger/assets/collections/templates/sets/LinkedOpenCustomHashSet.template index 2a67b26c..df9b3caa 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/LinkedOpenCustomHashSet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/LinkedOpenCustomHashSet.template @@ -424,7 +424,7 @@ public class LINKED_CUSTOM_HASH_SET KEY_GENERIC_TYPE extends CUSTOM_HASH_SET KEY } else if(lastIndex == pos) { lastIndex = (int)(links[pos] >>> 32); - if(0 <= lastIndex) links[pos] |= 0xFFFFFFFFL; + if(0 <= lastIndex) links[lastIndex] |= 0xFFFFFFFFL; } else { long link = links[pos]; diff --git a/src/builder/resources/speiger/assets/collections/templates/sets/LinkedOpenHashSet.template b/src/builder/resources/speiger/assets/collections/templates/sets/LinkedOpenHashSet.template index 89bd7a58..61ae67e0 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/LinkedOpenHashSet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/LinkedOpenHashSet.template @@ -396,7 +396,7 @@ public class LINKED_HASH_SET KEY_GENERIC_TYPE extends HASH_SET KEY_GENERIC_TYPE } else if(lastIndex == pos) { lastIndex = (int)(links[pos] >>> 32); - if(0 <= lastIndex) links[pos] |= 0xFFFFFFFFL; + if(0 <= lastIndex) links[lastIndex] |= 0xFFFFFFFFL; } else { long link = links[pos]; diff --git a/src/builder/resources/speiger/assets/collections/templates/sets/OpenCustomHashSet.template b/src/builder/resources/speiger/assets/collections/templates/sets/OpenCustomHashSet.template index 09429702..b1eedc13 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/OpenCustomHashSet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/OpenCustomHashSet.template @@ -366,6 +366,8 @@ public class CUSTOM_HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_T } protected boolean removeIndex(int pos) { + if(pos == nullIndex) return containsNull ? removeNullIndex() : false; + keys[pos] = EMPTY_KEY_VALUE; size--; onNodeRemoved(pos); shiftKeys(pos); diff --git a/src/builder/resources/speiger/assets/collections/templates/sets/OpenHashSet.template b/src/builder/resources/speiger/assets/collections/templates/sets/OpenHashSet.template index 63cce66d..e05be612 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/OpenHashSet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/OpenHashSet.template @@ -283,7 +283,7 @@ public class HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE imp @Override public boolean remove(KEY_TYPE o) { - if(KEY_EQUALS_NULL(o)) return (containsNull ? removeNullIndex() : false); + if(KEY_EQUALS_NULL(o)) return containsNull ? removeNullIndex() : false; int pos = HashUtil.mix(KEY_TO_HASH(o)) & mask; KEY_TYPE current = keys[pos]; if(KEY_EQUALS_NULL(current)) return false; @@ -324,6 +324,8 @@ public class HASH_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE imp } protected boolean removeIndex(int pos) { + if(pos == nullIndex) return containsNull ? removeNullIndex() : false; + keys[pos] = EMPTY_KEY_VALUE; size--; onNodeRemoved(pos); shiftKeys(pos);