From 27ad01657d342ef58c3a1a9cd259abe3b0ba1684 Mon Sep 17 00:00:00 2001 From: Speiger Date: Fri, 27 May 2022 16:35:41 +0200 Subject: [PATCH] More fixes from Unit tests. -Fixed: ArrayList/LinkedList extractElements crashing when 0 or less elements are desired. -Fixed: TreeMap pollFirst/LastKey should return the defaultMin/max value instead of a Empty value. -Fixed: TreeMap keySet implementation was missing the class type implementations to pass keySet tests. -Fixed: TreeMap.SubMap Iterator (primitive Keys) was crashing because a Null was set on to a primitive. --- build.gradle | 2 +- .../templates/lists/ArrayList.template | 10 ++--- .../templates/lists/LinkedList.template | 2 +- .../maps/impl/tree/AVLTreeMap.template | 45 +++++++++++++++---- .../maps/impl/tree/RBTreeMap.template | 45 +++++++++++++++---- 5 files changed, 79 insertions(+), 25 deletions(-) diff --git a/build.gradle b/build.gradle index 88dde8f1..cf721022 100644 --- a/build.gradle +++ b/build.gradle @@ -18,7 +18,7 @@ repositories { } archivesBaseName = 'Primitive Collections' -version = '0.6.1.10'; +version = '0.6.1.17-SNAPSHOT'; sourceCompatibility = targetCompatibility = compileJava.sourceCompatibility = compileJava.targetCompatibility = JavaVersion.current(); 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 6a193d4f..7822c9bc 100644 --- a/src/builder/resources/speiger/assets/collections/templates/lists/ArrayList.template +++ b/src/builder/resources/speiger/assets/collections/templates/lists/ArrayList.template @@ -362,11 +362,11 @@ public class ARRAY_LIST KEY_GENERIC_TYPE extends ABSTRACT_LIST KEY_GENERIC_TYPE int length = to - from; if(length <= 0) return; if(to != size) System.arraycopy(data, to, data, from, size - to); + size -= length; #if TYPE_OBJECT for(int i = 0;i 0;i++, length--) { 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 a610ae2d..5055e819 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 @@ -408,7 +408,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ @Override public KEY_TYPE POLL_FIRST_ENTRY_KEY() { - if(tree == null) return EMPTY_KEY_VALUE; + if(tree == null) return getDefaultMinValue(); KEY_TYPE result = first.key; removeNode(first); return result; @@ -422,7 +422,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ @Override public KEY_TYPE POLL_LAST_ENTRY_KEY() { - if(tree == null) return EMPTY_KEY_VALUE; + if(tree == null) return getDefaultMaxValue(); KEY_TYPE result = last.key; removeNode(last); return result; @@ -1022,6 +1022,33 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ public KEY_TYPE ceiling(KEY_TYPE e) { return map.ceilingKey(e); } @Override public KEY_TYPE higher(KEY_TYPE e) { return map.higherKey(e); } + +#if !TYPE_OBJECT + @Override + public CLASS_TYPE lower(CLASS_TYPE e) { + MAP.Entry KEY_VALUE_GENERIC_TYPE node = map.lowerEntry(OBJ_TO_KEY(e)); + return node != null ? node.getKey() : null; + } + + @Override + public CLASS_TYPE floor(CLASS_TYPE e) { + MAP.Entry KEY_VALUE_GENERIC_TYPE node = map.floorEntry(OBJ_TO_KEY(e)); + return node != null ? node.getKey() : null; + } + + @Override + public CLASS_TYPE higher(CLASS_TYPE e) { + MAP.Entry KEY_VALUE_GENERIC_TYPE node = map.higherEntry(OBJ_TO_KEY(e)); + return node != null ? node.getKey() : null; + } + + @Override + public CLASS_TYPE ceiling(CLASS_TYPE e) { + MAP.Entry KEY_VALUE_GENERIC_TYPE node = map.ceilingEntry(OBJ_TO_KEY(e)); + return node != null ? node.getKey() : null; + } + +#endif @Override public KEY_TYPE POLL_FIRST_KEY() { return map.POLL_FIRST_ENTRY_KEY(); } @Override @@ -1461,8 +1488,8 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ protected abstract BI_ITERATOR KEY_GENERIC_TYPE keyIterator(KEY_TYPE element); protected abstract VALUE_BI_ITERATOR VALUE_GENERIC_TYPE valueIterator(); protected abstract BI_ITERATOR KEY_GENERIC_TYPE descendingKeyIterator(); - protected KEY_TYPE lowKeyOrNull(Node KEY_VALUE_GENERIC_TYPE entry) { return entry == null ? EMPTY_KEY_VALUE : entry.key; } - protected KEY_TYPE highKeyOrNull(Node KEY_VALUE_GENERIC_TYPE entry) { return entry == null ? EMPTY_KEY_VALUE : entry.key; } + protected KEY_TYPE lowKeyOrNull(Node KEY_VALUE_GENERIC_TYPE entry) { return entry == null ? getDefaultMinValue() : entry.key; } + protected KEY_TYPE highKeyOrNull(Node KEY_VALUE_GENERIC_TYPE entry) { return entry == null ? getDefaultMaxValue() : entry.key; } protected Node KEY_VALUE_GENERIC_TYPE next(Node KEY_VALUE_GENERIC_TYPE entry) { return entry.next(); } protected Node KEY_VALUE_GENERIC_TYPE previous(Node KEY_VALUE_GENERIC_TYPE entry) { return entry.previous(); } @@ -1533,7 +1560,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ map.removeNode(entry); return result; } - return EMPTY_KEY_VALUE; + return getDefaultMinValue(); } @Override @@ -1544,7 +1571,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ map.removeNode(entry); return result; } - return EMPTY_KEY_VALUE; + return getDefaultMaxValue(); } @Override @@ -1632,7 +1659,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ @Override public VALUE_TYPE REMOVE_VALUEOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue) { - return inRange(key) ? map.REMOVE_VALUE(key) : defaultValue; + return inRange(key) ? map.REMOVE_VALUEOrDefault(key, defaultValue) : defaultValue; } #if TYPE_OBJECT && VALUE_OBJECT @@ -2147,8 +2174,8 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ public SubMapEntryIterator(Node KEY_VALUE_GENERIC_TYPE first, Node KEY_VALUE_GENERIC_TYPE forwardFence, Node KEY_VALUE_GENERIC_TYPE backwardFence) { next = first; - this.forwardFence = forwardFence == null ? null : forwardFence.key; - this.backwardFence = backwardFence == null ? null : backwardFence.key; + this.forwardFence = forwardFence == null ? EMPTY_KEY_VALUE : forwardFence.key; + this.backwardFence = backwardFence == null ? EMPTY_KEY_VALUE : backwardFence.key; unboundForwardFence = forwardFence == null; unboundBackwardFence = backwardFence == null; } 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 479c7ceb..48d470e6 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 @@ -411,7 +411,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G @Override public KEY_TYPE POLL_FIRST_ENTRY_KEY() { - if(tree == null) return EMPTY_KEY_VALUE; + if(tree == null) return getDefaultMinValue(); KEY_TYPE result = first.key; removeNode(first); return result; @@ -425,7 +425,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G @Override public KEY_TYPE POLL_LAST_ENTRY_KEY() { - if(tree == null) return EMPTY_KEY_VALUE; + if(tree == null) return getDefaultMaxValue(); KEY_TYPE result = last.key; removeNode(last); return result; @@ -1080,6 +1080,33 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G public KEY_TYPE ceiling(KEY_TYPE e) { return map.ceilingKey(e); } @Override public KEY_TYPE higher(KEY_TYPE e) { return map.higherKey(e); } + +#if !TYPE_OBJECT + @Override + public CLASS_TYPE lower(CLASS_TYPE e) { + MAP.Entry KEY_VALUE_GENERIC_TYPE node = map.lowerEntry(OBJ_TO_KEY(e)); + return node != null ? node.getKey() : null; + } + + @Override + public CLASS_TYPE floor(CLASS_TYPE e) { + MAP.Entry KEY_VALUE_GENERIC_TYPE node = map.floorEntry(OBJ_TO_KEY(e)); + return node != null ? node.getKey() : null; + } + + @Override + public CLASS_TYPE higher(CLASS_TYPE e) { + MAP.Entry KEY_VALUE_GENERIC_TYPE node = map.higherEntry(OBJ_TO_KEY(e)); + return node != null ? node.getKey() : null; + } + + @Override + public CLASS_TYPE ceiling(CLASS_TYPE e) { + MAP.Entry KEY_VALUE_GENERIC_TYPE node = map.ceilingEntry(OBJ_TO_KEY(e)); + return node != null ? node.getKey() : null; + } + +#endif @Override public KEY_TYPE POLL_FIRST_KEY() { return map.POLL_FIRST_ENTRY_KEY(); } @Override @@ -1518,8 +1545,8 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G protected abstract BI_ITERATOR KEY_GENERIC_TYPE keyIterator(KEY_TYPE element); protected abstract VALUE_BI_ITERATOR VALUE_GENERIC_TYPE valueIterator(); protected abstract BI_ITERATOR KEY_GENERIC_TYPE descendingKeyIterator(); - protected KEY_TYPE lowKeyOrNull(Node KEY_VALUE_GENERIC_TYPE entry) { return entry == null ? EMPTY_KEY_VALUE : entry.key; } - protected KEY_TYPE highKeyOrNull(Node KEY_VALUE_GENERIC_TYPE entry) { return entry == null ? EMPTY_KEY_VALUE : entry.key; } + protected KEY_TYPE lowKeyOrNull(Node KEY_VALUE_GENERIC_TYPE entry) { return entry == null ? getDefaultMinValue() : entry.key; } + protected KEY_TYPE highKeyOrNull(Node KEY_VALUE_GENERIC_TYPE entry) { return entry == null ? getDefaultMaxValue() : entry.key; } protected Node KEY_VALUE_GENERIC_TYPE next(Node KEY_VALUE_GENERIC_TYPE entry) { return entry.next(); } protected Node KEY_VALUE_GENERIC_TYPE previous(Node KEY_VALUE_GENERIC_TYPE entry) { return entry.previous(); } @@ -1590,7 +1617,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G map.removeNode(entry); return result; } - return EMPTY_KEY_VALUE; + return getDefaultMinValue(); } @Override @@ -1601,7 +1628,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G map.removeNode(entry); return result; } - return EMPTY_KEY_VALUE; + return getDefaultMaxValue(); } @Override @@ -1689,7 +1716,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G @Override public VALUE_TYPE REMOVE_VALUEOrDefault(KEY_TYPE key, VALUE_TYPE defaultValue) { - return inRange(key) ? map.REMOVE_VALUE(key) : defaultValue; + return inRange(key) ? map.REMOVE_VALUEOrDefault(key, defaultValue) : defaultValue; } #if TYPE_OBJECT && VALUE_OBJECT @@ -2204,8 +2231,8 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G public SubMapEntryIterator(Node KEY_VALUE_GENERIC_TYPE first, Node KEY_VALUE_GENERIC_TYPE forwardFence, Node KEY_VALUE_GENERIC_TYPE backwardFence) { next = first; - this.forwardFence = forwardFence == null ? null : forwardFence.key; - this.backwardFence = backwardFence == null ? null : backwardFence.key; + this.forwardFence = forwardFence == null ? EMPTY_KEY_VALUE : forwardFence.key; + this.backwardFence = backwardFence == null ? EMPTY_KEY_VALUE : backwardFence.key; unboundForwardFence = forwardFence == null; unboundBackwardFence = backwardFence == null; }