diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-04-18 08:38:24 -0400 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-04-18 08:38:24 -0400 |
| commit | 7c12fd8fe169944152ca73f0da4e8fe8e280f648 (patch) | |
| tree | 87a0cc6a430b0647b2daebcbec62101cfd67bbae /BJC-Utils2/src/main/java/bjc/utils | |
| parent | bada13a2ccedd860dfd7a45683e8e8f4ba8a038d (diff) | |
Added new option to top-down travers
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils')
3 files changed, 45 insertions, 2 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/ITree.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/ITree.java index 7f28682..bbcefd3 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/ITree.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/ITree.java @@ -155,4 +155,24 @@ public interface ITree<ContainedType> { public ITree<ContainedType> topDownTransform( Function<ContainedType, TopDownTransformResult> transformPicker, UnaryOperator<ITree<ContainedType>> transformer); + + /** + * Get the specified child of this tree + * + * @param childNo + * The number of the child to get + * @return The specified child of this tree + */ + default ITree<ContainedType> getChild(int childNo) { + return transformChild(childNo, (child) -> child); + } + + /** + * Get the data stored in this node + * + * @return The data stored in this node + */ + default ContainedType getHead() { + return transformHead((head) -> head); + } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/TopDownTransformResult.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/TopDownTransformResult.java index 9aec2b9..f48a90a 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/TopDownTransformResult.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/TopDownTransformResult.java @@ -18,5 +18,9 @@ public enum TopDownTransformResult { /** * Ignore this node, and traverse its children */ - PASSTHROUGH; + PASSTHROUGH, + /** + * Traverse this nodes children, then transform it + */ + PUSHDOWN; } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/Tree.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/Tree.java index 03a1f93..cd43df7 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/Tree.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/Tree.java @@ -60,7 +60,15 @@ public class Tree<ContainedType> implements ITree<ContainedType> { } } - private Tree(ContainedType leafToken, + /** + * Create a new tree node with the specified children + * + * @param leafToken + * The data to hold in this node + * @param childrn + * A list of children for this node + */ + public Tree(ContainedType leafToken, IFunctionalList<ITree<ContainedType>> childrn) { data = leafToken; @@ -286,6 +294,17 @@ public class Tree<ContainedType> implements ITree<ContainedType> { return this; case TRANSFORM: return transformer.apply(this); + case PUSHDOWN: + result = new Tree<>(data); + + if (hasChildren) { + children.forEach((child) -> { + result.addChild(child.topDownTransform( + transformPicker, transformer)); + }); + } + + return transformer.apply(result); default: throw new IllegalArgumentException( "Recieved unknown transform result " |
