From 0b373102f64fd7bfd25837a24ffb4ccb44b9d7e9 Mon Sep 17 00:00:00 2001 From: bjculkin Date: Thu, 1 Mar 2018 14:16:15 -0500 Subject: Finish most of CL formatting. There are a couple of unimplemented directives, but the only ones I'd consider anywhere near crucial would be the floating-point ones, which I'm not sure what I should do with them. --- .../bjc/utils/data/TopDownTransformIterator.java | 141 +++++++++++++-------- 1 file changed, 87 insertions(+), 54 deletions(-) (limited to 'base/src/main/java/bjc/utils/data') diff --git a/base/src/main/java/bjc/utils/data/TopDownTransformIterator.java b/base/src/main/java/bjc/utils/data/TopDownTransformIterator.java index d4a676c..1e69071 100644 --- a/base/src/main/java/bjc/utils/data/TopDownTransformIterator.java +++ b/base/src/main/java/bjc/utils/data/TopDownTransformIterator.java @@ -16,6 +16,14 @@ import java.util.function.Function; * Figure out what is broken with this, and fix it so that step-wise * iteration works correctly. */ +/** + * An iterative top-down transform of a tree. + * + * @author EVE + * + * @param + * The type of the nodes in the tree. + */ public class TopDownTransformIterator implements Iterator> { private final Function picker; private final BiFunction, Consumer>>, ITree> transform; @@ -34,6 +42,16 @@ public class TopDownTransformIterator implements Iterator>> toYield; private Iterator> curYield; + /** + * Create a new tree iterator. + * + * @param pickr + * The function to use to pick how to process nodes. + * @param transfrm + * The transform to apply to the nodes. + * @param tree + * The tree to transform. + */ public TopDownTransformIterator(final Function pickr, final BiFunction, Consumer>>, ITree> transfrm, final ITree tree) { @@ -50,6 +68,12 @@ public class TopDownTransformIterator implements Iterator> src) { if(curYield != null) { toYield.push(curYield); @@ -63,25 +87,34 @@ public class TopDownTransformIterator implements Iterator flushYields(final ITree val) { if(curYield != null) { toYield.add(new SingleIterator<>(val)); - if(curYield.hasNext()) + if(curYield.hasNext()) { return curYield.next(); - else { - while(toYield.size() != 0 && !curYield.hasNext()) { - curYield = toYield.pop(); - } + } - if(toYield.size() == 0 && !curYield.hasNext()) { - curYield = null; - return val; - } else - return curYield.next(); + while(toYield.size() != 0 && !curYield.hasNext()) { + curYield = toYield.pop(); } - } else - return val; + + if(toYield.size() == 0 && !curYield.hasNext()) { + curYield = null; + return val; + } + + return curYield.next(); + } + + return val; } @Override @@ -89,17 +122,18 @@ public class TopDownTransformIterator implements Iterator implements Iterator implements Iterator(preParent.getHead()), - this::addYield)); } + + done = true; + return flushYields(transform.apply(new Tree<>(preParent.getHead()), this::addYield)); case PULLUP: final ITree intRes = transform.apply(preParent, this::addYield); @@ -155,10 +188,10 @@ public class TopDownTransformIterator implements Iterator implements Iterator res = null; - - if(postParent == null) { - res = new Tree<>(preParent.getHead()); + } - System.out.println("\t\tTRACE: adding nodes " + postChildren + " to " + res); + ITree res = null; - for(final ITree child : postChildren) { - res.addChild(child); - } + if(postParent == null) { + res = new Tree<>(preParent.getHead()); - // res = transform.apply(res, - // this::addYield); - } else { - res = postParent; + System.out.println("\t\tTRACE: adding nodes " + postChildren + " to " + res); - System.out.println("\t\tTRACE: adding nodes " + postChildren + " to " + res); - for(final ITree child : postChildren) { - res.addChild(child); - } + for(final ITree child : postChildren) { + res.addChild(child); } - done = true; - return flushYields(res); + // res = transform.apply(res, + // this::addYield); + } else { + res = postParent; + + System.out.println("\t\tTRACE: adding nodes " + postChildren + " to " + res); + for(final ITree child : postChildren) { + res.addChild(child); + } } - } else { - final ITree res = curChild.next(); - System.out.println("\t\tTRACE: adding node " + res + " to children"); - postChildren.add(res); + done = true; return flushYields(res); } + + final ITree res = curChild.next(); + System.out.println("\t\tTRACE: adding node " + res + " to children"); + postChildren.add(res); + + return flushYields(res); } } -- cgit v1.2.3