From 73916f4fd964ff29aceb6d83901d6b4caa0bcd21 Mon Sep 17 00:00:00 2001 From: Speiger Date: Fri, 23 Jul 2021 22:04:59 +0200 Subject: [PATCH] Fixed AVLTrees pollFirst/pollLast --- Changelog.md | 1 + .../templates/maps/impl/tree/AVLTreeMap.template | 9 ++------- .../collections/templates/sets/AVLTreeSet.template | 9 ++------- .../collections/ints/base/BaseIntSortedSetTest.java | 11 +++++++++-- 4 files changed, 14 insertions(+), 16 deletions(-) diff --git a/Changelog.md b/Changelog.md index 559fbb79..66646beb 100644 --- a/Changelog.md +++ b/Changelog.md @@ -3,6 +3,7 @@ ### Version 0.3.3 - Added: Flat/Mapping function for Iterables/Iterators to help avoid streams for cleaner looking code +- Fixed: AVLTrees pollFirst/Last is now keeping orders and is fixed ### Version 0.3.2 - Fixed: Map.put wasn't referring to primitive variants. 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 d6d13071..bf1b3262 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 @@ -767,11 +767,11 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ entry.value = successor.value; entry = successor; } + if(entry.previous() == null) first = entry.next(); + if(entry.next() == null) last = entry.previous(); Entry KEY_VALUE_GENERIC_TYPE replacement = entry.left != null ? entry.left : entry.right; if(replacement != null) { if(entry.replace(replacement)) tree = replacement; - if(entry == first) first = replacement; - if(entry == last) last = entry.right != null ? entry.right : replacement; entry.left = entry.right = entry.parent = null; fixAfterDeletion(replacement); } @@ -779,11 +779,6 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ else { fixAfterDeletion(entry); entry.replace(null); - if(entry.parent != null) { - Entry KEY_VALUE_GENERIC_TYPE parent = entry.parent; - if(entry == first) first = parent.left != null ? parent.left : parent; - if(entry == last) last = entry.right != null ? parent.right : parent; - } entry.parent = 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 dd28a90e..e98517a9 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/AVLTreeSet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/AVLTreeSet.template @@ -514,11 +514,11 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE entry.key = successor.key; entry = successor; } + if(entry.previous() == null) first = entry.next(); + if(entry.next() == null) last = entry.previous(); Entry KEY_GENERIC_TYPE replacement = entry.left != null ? entry.left : entry.right; if(replacement != null) { if(entry.replace(replacement)) tree = replacement; - if(entry == first) first = replacement; - if(entry == last) last = entry.right != null ? entry.right : replacement; entry.left = entry.right = entry.parent = null; fixAfterDeletion(replacement); } @@ -526,11 +526,6 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE else { fixAfterDeletion(entry); entry.replace(null); - if(entry.parent != null) { - Entry KEY_GENERIC_TYPE parent = entry.parent; - if(entry == first) first = parent.left != null ? parent.left : parent; - if(entry == last) last = entry.right != null ? parent.right : parent; - } entry.parent = null; } } diff --git a/src/test/java/speiger/src/collections/ints/base/BaseIntSortedSetTest.java b/src/test/java/speiger/src/collections/ints/base/BaseIntSortedSetTest.java index 9aa7a887..f2716a86 100644 --- a/src/test/java/speiger/src/collections/ints/base/BaseIntSortedSetTest.java +++ b/src/test/java/speiger/src/collections/ints/base/BaseIntSortedSetTest.java @@ -49,8 +49,15 @@ public abstract class BaseIntSortedSetTest extends BaseIntCollectionTest public void pollTest() { if(getValidSortedSetTests().contains(SortedSetTest.POLL)) { IntSortedSet set = create(TEST_ARRAY); - Assert.assertEquals(set.pollFirstInt(), 0); - Assert.assertEquals(set.pollLastInt(), 99); + for(int i = 0;i<100;i++) + { + Assert.assertEquals(i, set.pollFirstInt()); + } + set = create(TEST_ARRAY); + for(int i = 99;i>=0;i--) + { + Assert.assertEquals(i, set.pollLastInt()); + } } }