From f51f6da7319787348c38b875652b5c0e9f88c8aa Mon Sep 17 00:00:00 2001 From: Ben Culkin Date: Mon, 13 Apr 2020 18:43:13 -0400 Subject: Cleanup pass Pass to do some cleanups --- src/main/java/bjc/data/ITree.java | 111 ++++++++++++++++++++------------------ 1 file changed, 60 insertions(+), 51 deletions(-) (limited to 'src/main/java/bjc/data/ITree.java') diff --git a/src/main/java/bjc/data/ITree.java b/src/main/java/bjc/data/ITree.java index 4088c10..e9c829e 100644 --- a/src/main/java/bjc/data/ITree.java +++ b/src/main/java/bjc/data/ITree.java @@ -15,7 +15,7 @@ import bjc.funcdata.bst.TreeLinearizationMethod; * @author ben * * @param - * The type of data contained in the tree nodes. + * The type of data contained in the tree nodes. * */ public interface ITree { @@ -23,7 +23,7 @@ public interface ITree { * Append a child to this node. * * @param child - * The child to append to this node. + * The child to append to this node. */ void addChild(ITree child); @@ -31,7 +31,7 @@ public interface ITree { * Append a child to this node. * * @param child - * The child to append to this node. + * The child to append to this node. */ void addChild(ContainedType child); @@ -39,7 +39,7 @@ public interface ITree { * Prepend a child to this node. * * @param child - * The child to prepend to this node. + * The child to prepend to this node. */ void prependChild(ITree child); @@ -47,25 +47,26 @@ public interface ITree { * Collapse a tree into a single version. * * @param - * The intermediate type being folded. + * The intermediate type being folded. * * @param - * The type that is the end result. + * The type that is the end result. * * @param leafTransform - * The function to use to convert leaf values. + * The function to use to convert leaf values. * * @param nodeCollapser - * The function to use to convert internal nodes and their - * children. + * The function to use to convert internal nodes and + * their children. * * @param resultTransformer - * The function to use to convert a state to the returned - * version. + * The function to use to convert a state to the + * returned version. * * @return The final transformed state. */ - ReturnedType collapse(Function leafTransform, + ReturnedType collapse( + Function leafTransform, BiFunction, NewType> nodeCollapser, Function resultTransformer); @@ -73,22 +74,23 @@ public interface ITree { * Execute a given action for each of this tree's children. * * @param action - * The action to execute for each child. + * The action to execute for each child. */ void doForChildren(Consumer> action); /** - * Expand the nodes of a tree into trees, and then merge the contents of - * those trees into a single tree. + * Expand the nodes of a tree into trees, and then merge the contents of those + * trees into a single tree. * * @param mapper - * The function to use to map values into trees. + * The function to use to map values into trees. * * @return A tree, with some nodes expanded into trees. */ - default ITree flatMapTree(final Function> mapper) { + default ITree + flatMapTree(final Function> mapper) { return topDownTransform(dat -> TopDownTransformResult.PUSHDOWN, node -> { - if(node.getChildrenCount() > 0) { + if (node.getChildrenCount() > 0) { final ITree parent = node.transformHead(mapper); node.doForChildren(parent::addChild); @@ -104,7 +106,7 @@ public interface ITree { * Get the specified child of this tree. * * @param childNo - * The number of the child to get. + * The number of the child to get. * * @return The specified child of this tree. */ @@ -141,61 +143,64 @@ public interface ITree { * Rebuild the tree with the same structure, but different nodes. * * @param - * The type of the new tree. + * The type of the new tree. * * @param leafTransformer - * The function to use to transform leaf tokens. + * The function to use to transform leaf tokens. * * @param internalTransformer - * The function to use to transform internal tokens. + * The function to use to transform internal tokens. * * @return The tree, with the nodes changed. */ - ITree rebuildTree(Function leafTransformer, + ITree rebuildTree( + Function leafTransformer, Function internalTransformer); /** * Transform some of the nodes in this tree. * * @param nodePicker - * The predicate to use to pick nodes to transform. + * The predicate to use to pick nodes to transform. * * @param transformer - * The function to use to transform picked nodes. + * The function to use to transform picked nodes. */ - void selectiveTransform(Predicate nodePicker, UnaryOperator transformer); + void selectiveTransform(Predicate nodePicker, + UnaryOperator transformer); /** * Do a top-down transform of the tree. * * @param transformPicker - * The function to use to pick how to progress. + * The function to use to pick how to progress. * * @param transformer - * The function used to transform picked subtrees. + * The function used to transform picked subtrees. * * @return The tree with the transform applied to picked subtrees. */ - ITree topDownTransform(Function transformPicker, + ITree topDownTransform( + Function transformPicker, UnaryOperator> transformer); /** * Transform one of this nodes children. * * @param - * The type of the transformed value. + * The type of the transformed value. * * @param childNo - * The number of the child to transform. + * The number of the child to transform. * * @param transformer - * The function to use to transform the value. + * The function to use to transform the value. * * @return The transformed value. * * @throws IllegalArgumentException - * if the childNo is out of bounds (0 <= childNo <= - * childCount()). + * if the childNo is out of bounds (0 <= + * childNo <= childCount()). */ TransformedType transformChild(int childNo, Function, TransformedType> transformer); @@ -204,27 +209,29 @@ public interface ITree { * Transform the value that is the head of this node. * * @param - * The type of the transformed value. + * The type of the transformed value. * * @param transformer - * The function to use to transform the value. + * The function to use to transform the value. * * @return The transformed value. */ - TransformedType transformHead(Function transformer); + TransformedType + transformHead(Function transformer); /** * Transform the tree into a tree with a different type of token. * * @param - * The type of the new tree. + * The type of the new tree. * * @param transformer - * The function to use to transform tokens. + * The function to use to transform tokens. * * @return A tree with the token types transformed. */ - default ITree transformTree(final Function transformer) { + default ITree + transformTree(final Function transformer) { return rebuildTree(transformer, transformer); } @@ -232,21 +239,22 @@ public interface ITree { * Perform an action on each part of the tree. * * @param linearizationMethod - * The way to traverse the tree. + * The way to traverse the tree. * * @param action - * The action to perform on each tree node. + * The action to perform on each tree node. */ - void traverse(TreeLinearizationMethod linearizationMethod, Consumer action); + void traverse(TreeLinearizationMethod linearizationMethod, + Consumer action); /** * Find the farthest to right child that satisfies the given predicate. * * @param childPred - * The predicate to satisfy. + * The predicate to satisfy. * - * @return The index of the right-most child that satisfies the - * predicate, or -1 if one doesn't exist. + * @return The index of the right-most child that satisfies the predicate, or -1 + * if one doesn't exist. */ int revFind(Predicate> childPred); @@ -254,15 +262,16 @@ public interface ITree { * Check if this tree contains any nodes that satisfy the predicate. * * @param pred - * The predicate to look for. - * + * The predicate to look for. + * * @return Whether or not any items satisfied the predicate. */ default boolean containsMatching(Predicate pred) { Toggle tog = new OneWayToggle<>(false, true); - traverse(TreeLinearizationMethod.POSTORDER, (val) -> { - if(pred.test(val)) tog.get(); + traverse(TreeLinearizationMethod.POSTORDER, val -> { + if (pred.test(val)) + tog.get(); }); return tog.get(); @@ -272,7 +281,7 @@ public interface ITree { * Set the head of the tree. * * @param dat - * The value to set as the head of the tree. + * The value to set as the head of the tree. */ void setHead(ContainedType dat); } -- cgit v1.2.3