From 59a417056c26e18a17cd73140b8583d12dbf4ebe Mon Sep 17 00:00:00 2001 From: Speiger Date: Mon, 13 Dec 2021 13:56:28 +0100 Subject: [PATCH] Added Missing method & fixed potential issue with TreeSetBiIterators --- .../maps/impl/tree/AVLTreeMap.template | 2 +- .../maps/impl/tree/RBTreeMap.template | 2 +- .../templates/sets/AVLTreeSet.template | 20 ++++++++++++++++--- .../templates/sets/RBTreeSet.template | 20 ++++++++++++++++--- 4 files changed, 36 insertions(+), 8 deletions(-) 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 804ea485..fd3af9af 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 @@ -650,7 +650,7 @@ public class AVL_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_ } protected BI_ITERATOR KEY_GENERIC_TYPE keyIterator(KEY_TYPE element) { - return null; + return new AscendingKeyIterator(findNode(element)); } protected BI_ITERATOR KEY_GENERIC_TYPE descendingKeyIterator() { 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 4dd32168..5abcb417 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 @@ -650,7 +650,7 @@ public class RB_TREE_MAP KEY_VALUE_GENERIC_TYPE extends ABSTRACT_MAP KEY_VALUE_G } protected BI_ITERATOR KEY_GENERIC_TYPE keyIterator(KEY_TYPE element) { - return null; + return new AscendingKeyIterator(findNode(element)); } protected BI_ITERATOR KEY_GENERIC_TYPE descendingKeyIterator() { 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 4f3c2afe..a375c455 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/AVLTreeSet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/AVLTreeSet.template @@ -1280,6 +1280,7 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE { Entry KEY_GENERIC_TYPE lastReturned; Entry KEY_GENERIC_TYPE next; + boolean forwards = false; boolean unboundForwardFence; boolean unboundBackwardFence; KEY_TYPE forwardFence; @@ -1305,6 +1306,7 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE lastReturned = next; KEY_TYPE result = next.key; next = next.next(); + forwards = true; return result; } @@ -1319,13 +1321,14 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE lastReturned = next; KEY_TYPE result = next.key; next = next.previous(); + forwards = false; return result; } @Override public void remove() { if(lastReturned == null) throw new IllegalStateException(); - if(lastReturned.needsSuccessor()) next = lastReturned; + if(forwards && lastReturned.needsSuccessor()) next = lastReturned; set.removeNode(lastReturned); lastReturned = null; } @@ -1335,6 +1338,7 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE { Entry KEY_GENERIC_TYPE lastReturned; Entry KEY_GENERIC_TYPE next; + boolean forwards = false; boolean unboundForwardFence; boolean unboundBackwardFence; KEY_TYPE forwardFence; @@ -1360,6 +1364,7 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE lastReturned = next; KEY_TYPE result = next.key; next = next.previous(); + forwards = false; return result; } @@ -1374,12 +1379,14 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE lastReturned = next; KEY_TYPE result = next.key; next = next.next(); + forwards = true; return result; } @Override public void remove() { if(lastReturned == null) throw new IllegalStateException(); + if(forwards && lastReturned.needsSuccessor()) next = lastReturned; set.removeNode(lastReturned); lastReturned = null; } @@ -1390,6 +1397,7 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE { Entry KEY_GENERIC_TYPE lastReturned; Entry KEY_GENERIC_TYPE next; + boolean forwards = false; public AscendingSetIterator(Entry KEY_GENERIC_TYPE first) { @@ -1407,6 +1415,7 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE lastReturned = next; KEY_TYPE result = next.key; next = next.next(); + forwards = true; return result; } @@ -1421,13 +1430,14 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE lastReturned = next; KEY_TYPE result = next.key; next = next.previous(); + forwards = false; return result; } @Override public void remove() { if(lastReturned == null) throw new IllegalStateException(); - if(lastReturned.needsSuccessor()) next = lastReturned; + if(forwards && lastReturned.needsSuccessor()) next = lastReturned; removeNode(lastReturned); lastReturned = null; } @@ -1437,7 +1447,8 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE { Entry KEY_GENERIC_TYPE lastReturned; Entry KEY_GENERIC_TYPE next; - + boolean forwards = false; + public DescendingSetIterator(Entry KEY_GENERIC_TYPE first) { next = first; @@ -1454,6 +1465,7 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE lastReturned = next; KEY_TYPE result = next.key; next = next.previous(); + forwards = false; return result; } @@ -1468,12 +1480,14 @@ public class AVL_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE lastReturned = next; KEY_TYPE result = next.key; next = next.next(); + forwards = true; return result; } @Override public void remove() { if(lastReturned == null) throw new IllegalStateException(); + if(forwards && lastReturned.needsSuccessor()) next = lastReturned; removeNode(lastReturned); lastReturned = null; } diff --git a/src/builder/resources/speiger/assets/collections/templates/sets/RBTreeSet.template b/src/builder/resources/speiger/assets/collections/templates/sets/RBTreeSet.template index b44817cd..b98e9300 100644 --- a/src/builder/resources/speiger/assets/collections/templates/sets/RBTreeSet.template +++ b/src/builder/resources/speiger/assets/collections/templates/sets/RBTreeSet.template @@ -1341,6 +1341,7 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE { Entry KEY_GENERIC_TYPE lastReturned; Entry KEY_GENERIC_TYPE next; + boolean forwards = false; boolean unboundForwardFence; boolean unboundBackwardFence; KEY_TYPE forwardFence; @@ -1366,6 +1367,7 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE lastReturned = next; KEY_TYPE result = next.key; next = next.next(); + forwards = true; return result; } @@ -1380,13 +1382,14 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE lastReturned = next; KEY_TYPE result = next.key; next = next.previous(); + forwards = false; return result; } @Override public void remove() { if(lastReturned == null) throw new IllegalStateException(); - if(lastReturned.needsSuccessor()) next = lastReturned; + if(forwards && lastReturned.needsSuccessor()) next = lastReturned; set.removeNode(lastReturned); lastReturned = null; } @@ -1396,6 +1399,7 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE { Entry KEY_GENERIC_TYPE lastReturned; Entry KEY_GENERIC_TYPE next; + boolean forwards = false; boolean unboundForwardFence; boolean unboundBackwardFence; KEY_TYPE forwardFence; @@ -1421,6 +1425,7 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE lastReturned = next; KEY_TYPE result = next.key; next = next.previous(); + forwards = false; return result; } @@ -1435,12 +1440,14 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE lastReturned = next; KEY_TYPE result = next.key; next = next.next(); + forwards = true; return result; } @Override public void remove() { if(lastReturned == null) throw new IllegalStateException(); + if(forwards && lastReturned.needsSuccessor()) next = lastReturned; set.removeNode(lastReturned); lastReturned = null; } @@ -1451,6 +1458,7 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE { Entry KEY_GENERIC_TYPE lastReturned; Entry KEY_GENERIC_TYPE next; + boolean forwards = false; public AscendingSetIterator(Entry KEY_GENERIC_TYPE first) { @@ -1468,6 +1476,7 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE lastReturned = next; KEY_TYPE result = next.key; next = next.next(); + forwards = true; return result; } @@ -1482,13 +1491,14 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE lastReturned = next; KEY_TYPE result = next.key; next = next.previous(); + forwards = false; return result; } @Override public void remove() { if(lastReturned == null) throw new IllegalStateException(); - if(lastReturned.needsSuccessor()) next = lastReturned; + if(forwards && lastReturned.needsSuccessor()) next = lastReturned; removeNode(lastReturned); lastReturned = null; } @@ -1498,7 +1508,8 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE { Entry KEY_GENERIC_TYPE lastReturned; Entry KEY_GENERIC_TYPE next; - + boolean forwards = false; + public DescendingSetIterator(Entry KEY_GENERIC_TYPE first) { next = first; @@ -1515,6 +1526,7 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE lastReturned = next; KEY_TYPE result = next.key; next = next.previous(); + forwards = false; return result; } @@ -1529,12 +1541,14 @@ public class RB_TREE_SET KEY_GENERIC_TYPE extends ABSTRACT_SET KEY_GENERIC_TYPE lastReturned = next; KEY_TYPE result = next.key; next = next.next(); + forwards = true; return result; } @Override public void remove() { if(lastReturned == null) throw new IllegalStateException(); + if(forwards && lastReturned.needsSuccessor()) next = lastReturned; removeNode(lastReturned); lastReturned = null; }