From 33918524d7faab0146a0a92c13eaaef46cdbea8a Mon Sep 17 00:00:00 2001 From: bjculkin Date: Fri, 24 Mar 2017 10:53:59 -0400 Subject: Update Pratt parser. --- BJC-Utils2/src/main/java/bjc/utils/data/Tree.java | 78 +++++++++++++---------- 1 file changed, 45 insertions(+), 33 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/data/Tree.java') diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/Tree.java b/BJC-Utils2/src/main/java/bjc/utils/data/Tree.java index cf4d1fb..cef9d51 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Tree.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Tree.java @@ -74,7 +74,7 @@ public class Tree implements ITree { children = new FunctionalList<>(); - for(ITree child : childrn) { + for (ITree child : childrn) { children.add(child); childCount++; @@ -83,7 +83,7 @@ public class Tree implements ITree { @Override public void addChild(ITree child) { - if(hasChildren == false) { + if (hasChildren == false) { hasChildren = true; children = new FunctionalList<>(); @@ -104,14 +104,14 @@ public class Tree implements ITree { @Override public void doForChildren(Consumer> action) { - if(childCount > 0) { + if (childCount > 0) { children.forEach(action); } } @Override public ITree flatMapTree(Function> mapper) { - if(hasChildren) { + if (hasChildren) { ITree flatMappedData = mapper.apply(data); children.map((child) -> child.flatMapTree(mapper)) @@ -130,11 +130,14 @@ public class Tree implements ITree { protected NewType internalCollapse(Function leafTransform, Function, NewType>> nodeCollapser) { - if(hasChildren) { + if (hasChildren) { Function, NewType> nodeTransformer = nodeCollapser.apply(data); IList collapsedChildren = children.map((child) -> { - return child.collapse(leafTransform, nodeCollapser, (subTreeVal) -> subTreeVal); + NewType collapsed = child.collapse(leafTransform, nodeCollapser, + (subTreeVal) -> subTreeVal); + + return collapsed; }); return nodeTransformer.apply(collapsedChildren); @@ -144,7 +147,7 @@ public class Tree implements ITree { } protected void internalToString(StringBuilder builder, int indentLevel, boolean initial) { - for(int i = 0; i < indentLevel; i++) { + for (int i = 0; i < indentLevel; i++) { builder.append(">\t"); } @@ -154,7 +157,7 @@ public class Tree implements ITree { builder.append(data == null ? "(null)" : data.toString()); builder.append("\n"); - if(hasChildren) { + if (hasChildren) { children.forEach((child) -> { ((Tree) child).internalToString(builder, indentLevel + 1, false); }); @@ -164,7 +167,7 @@ public class Tree implements ITree { @Override public ITree rebuildTree(Function leafTransformer, Function operatorTransformer) { - if(hasChildren) { + if (hasChildren) { IList> mappedChildren = children.map((child) -> { return child.rebuildTree(leafTransformer, operatorTransformer); }); @@ -177,7 +180,7 @@ public class Tree implements ITree { @Override public void selectiveTransform(Predicate nodePicker, UnaryOperator transformer) { - if(hasChildren) { + if (hasChildren) { children.forEach((child) -> child.selectiveTransform(nodePicker, transformer)); } else { data = transformer.apply(data); @@ -189,11 +192,11 @@ public class Tree implements ITree { UnaryOperator> transformer) { TopDownTransformResult transformResult = transformPicker.apply(data); - switch(transformResult) { + switch (transformResult) { case PASSTHROUGH: ITree result = new Tree<>(data); - if(hasChildren) { + if (hasChildren) { children.forEach((child) -> { result.addChild(child.topDownTransform(transformPicker, transformer)); }); @@ -209,7 +212,7 @@ public class Tree implements ITree { case PUSHDOWN: result = new Tree<>(data); - if(hasChildren) { + if (hasChildren) { children.forEach((child) -> { result.addChild(child.topDownTransform(transformPicker, transformer)); }); @@ -246,7 +249,7 @@ public class Tree implements ITree { @Override public TransformedType transformChild(int childNo, Function, TransformedType> transformer) { - if(childNo < 0 || childNo > childCount - 1) + if (childNo < 0 || childNo > childCount - 1) throw new IllegalArgumentException("Child index #" + childNo + " is invalid"); return transformer.apply(children.getByIndex(childNo)); @@ -259,7 +262,7 @@ public class Tree implements ITree { @Override public ITree transformTree(Function transformer) { - if(hasChildren) { + if (hasChildren) { IList> transformedChildren = children .map((child) -> child.transformTree(transformer)); @@ -271,11 +274,12 @@ public class Tree implements ITree { @Override public void traverse(TreeLinearizationMethod linearizationMethod, Consumer action) { - if(hasChildren) { - switch(linearizationMethod) { + if (hasChildren) { + switch (linearizationMethod) { case INORDER: - if(childCount != 2) throw new IllegalArgumentException( - "Can only do in-order traversal for binary trees."); + if (childCount != 2) + throw new IllegalArgumentException( + "Can only do in-order traversal for binary trees."); children.getByIndex(0).traverse(linearizationMethod, action); @@ -316,32 +320,40 @@ public class Tree implements ITree { @Override public boolean equals(Object obj) { - if(this == obj) return true; - if(obj == null) return false; - if(getClass() != obj.getClass()) return false; + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; Tree other = (Tree) obj; - if(data == null) { - if(other.data != null) return false; - } else if(!data.equals(other.data)) return false; + if (data == null) { + if (other.data != null) + return false; + } else if (!data.equals(other.data)) + return false; - if(childCount != other.childCount) return false; + if (childCount != other.childCount) + return false; - if(children == null) { - if(other.children != null) return false; - } else if(!children.equals(other.children)) return false; + if (children == null) { + if (other.children != null) + return false; + } else if (!children.equals(other.children)) + return false; return true; } @Override public int revFind(Predicate> childPred) { - if(childCount == 0) { + if (childCount == 0) { return -1; } else { - for(int i = childCount - 1; i >= 0; i--) { - if(childPred.test(getChild(i))) { + for (int i = childCount - 1; i >= 0; i--) { + if (childPred.test(getChild(i))) { return i; } } @@ -352,7 +364,7 @@ public class Tree implements ITree { @Override public void prependChild(ITree child) { - if(hasChildren == false) { + if (hasChildren == false) { hasChildren = true; children = new FunctionalList<>(); -- cgit v1.2.3