From ed51142cd7abaaeedcb9ef1439f26690b57ce839 Mon Sep 17 00:00:00 2001 From: student Date: Fri, 2 Mar 2018 11:54:19 -0500 Subject: Update --- .../bjc/utils/data/TopDownTransformIterator.java | 127 ++++++++++++--------- 1 file changed, 75 insertions(+), 52 deletions(-) (limited to 'base/src/main') diff --git a/base/src/main/java/bjc/utils/data/TopDownTransformIterator.java b/base/src/main/java/bjc/utils/data/TopDownTransformIterator.java index 8914ad4..9a99a70 100644 --- a/base/src/main/java/bjc/utils/data/TopDownTransformIterator.java +++ b/base/src/main/java/bjc/utils/data/TopDownTransformIterator.java @@ -22,11 +22,30 @@ import java.util.function.Function; * @author EVE * * @param - * The type of the nodes in the tree. + * The type of the nodes in the tree. */ public class TopDownTransformIterator implements Iterator> { + /** + * Alias type for a tree transformation. + * + * @author student + * + * @param + * The type contained in the tree. + */ + public interface TreeTransform + extends BiFunction, Consumer>>, ITree> { + + } + + /* + * The function that picks how to transform a given node + */ private final Function picker; - private final BiFunction, Consumer>>, ITree> transform; + /* + * The transform to apply to a given node. + */ + private final TreeTransform transform; private ITree preParent; private ITree postParent; @@ -40,21 +59,20 @@ public class TopDownTransformIterator implements Iterator>> toYield; - private Iterator> curYield; + private Iterator> currYield; /** * Create a new tree iterator. * * @param pickr - * The function to use to pick how to process nodes. + * The function to use to pick how to process nodes. * @param transfrm - * The transform to apply to the nodes. + * The transform to apply to the nodes. * @param tree - * The tree to transform. + * The tree to transform. */ public TopDownTransformIterator(final Function pickr, - final BiFunction, Consumer>>, ITree> transfrm, - final ITree tree) { + final TreeTransform transfrm, final ITree tree) { preParent = tree; preChildren = new LinkedList<>(); @@ -72,14 +90,14 @@ public class TopDownTransformIterator implements Iterator> src) { - if(curYield != null) { - toYield.push(curYield); + if (currYield != null) { + toYield.push(currYield); } - curYield = src; + currYield = src; } @Override @@ -91,27 +109,35 @@ public class TopDownTransformIterator implements Iterator flushYields(final ITree val) { - if(curYield != null) { + if (currYield != null) { + /* + * We have non-sentinel values to yield. + */ + + /* + * Add the sentinel to yield later. + */ toYield.add(new SingleIterator<>(val)); - if(curYield.hasNext()) { - return curYield.next(); + if (currYield.hasNext()) { + return currYield.next(); } - while(toYield.size() != 0 && !curYield.hasNext()) { - curYield = toYield.pop(); + while (toYield.size() != 0 && !currYield.hasNext()) { + currYield = toYield.pop(); } - if(toYield.size() == 0 && !curYield.hasNext()) { - curYield = null; + if (toYield.size() == 0 && !currYield.hasNext()) { + currYield = null; return val; } - return curYield.next(); + return currYield.next(); } return val; @@ -119,33 +145,30 @@ public class TopDownTransformIterator implements Iterator next() { - if(done) throw new NoSuchElementException(); - - if(curYield != null) { - if(curYield.hasNext()) { - return curYield.next(); - } - - while(toYield.size() != 0 && !curYield.hasNext()) { - curYield = toYield.pop(); - } - - if(toYield.size() == 0 && !curYield.hasNext()) { - curYield = null; - } else { - return curYield.next(); - } + if (done) + throw new NoSuchElementException(); + + /* + * Flush any values that need to be yielded. + */ + if (currYield != null) { + ITree yeld = flushYields(null); + if (yeld != null) + return yeld; } - - if(initial) { + + if (initial) { + /* + * Get the way we are transforming. + */ final TopDownTransformResult res = picker.apply(preParent.getHead()); - switch(res) { + switch (res) { case PASSTHROUGH: postParent = new Tree<>(preParent.getHead()); - if(preParent.getChildrenCount() != 0) { - for(int i = 0; i < preParent.getChildrenCount(); i++) { + if (preParent.getChildrenCount() != 0) { + for (int i = 0; i < preParent.getChildrenCount(); i++) { preChildren.add(preParent.getChild(i)); } @@ -165,8 +188,8 @@ public class TopDownTransformIterator implements Iterator implements Iterator(intRes.getHead()); - if(intRes.getChildrenCount() != 0) { - for(int i = 0; i < intRes.getChildrenCount(); i++) { + if (intRes.getChildrenCount() != 0) { + for (int i = 0; i < intRes.getChildrenCount(); i++) { preChildren.add(intRes.getChild(i)); } @@ -196,13 +219,13 @@ public class TopDownTransformIterator implements Iterator(picker, transform, preChildren.pop()); final ITree res = curChild.next(); @@ -214,12 +237,12 @@ public class TopDownTransformIterator implements Iterator res = null; - if(postParent == null) { + if (postParent == null) { res = new Tree<>(preParent.getHead()); System.out.println("\t\tTRACE: adding nodes " + postChildren + " to " + res); - for(final ITree child : postChildren) { + for (final ITree child : postChildren) { res.addChild(child); } @@ -229,7 +252,7 @@ public class TopDownTransformIterator implements Iterator child : postChildren) { + for (final ITree child : postChildren) { res.addChild(child); } } -- cgit v1.2.3