summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/funcdata
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2016-04-18 19:56:32 -0400
committerbculkin2442 <bjculkin@mix.wvu.edu>2016-04-18 19:56:32 -0400
commitb4f5f98c0aa7fc892e96771ff2df729e61c21f74 (patch)
tree09820cc267577c295be7bf33dc5deabf662cb37c /BJC-Utils2/src/main/java/bjc/utils/funcdata
parent7c12fd8fe169944152ca73f0da4e8fe8e280f648 (diff)
Minor code changes
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/funcdata')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java40
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/funcdata/IFunctionalList.java24
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/funcdata/ITree.java148
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/funcdata/Tree.java265
4 files changed, 251 insertions, 226 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java
index 70d04cc..735c664 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/FunctionalList.java
@@ -182,8 +182,8 @@ public class FunctionalList<E> implements Cloneable, IFunctionalList<E> {
// Get the iterator for the other list
Iterator<T> rightIterator = rightList.toIterable().iterator();
- for (Iterator<E> leftIterator = wrappedList
- .iterator(); leftIterator.hasNext()
+ for (Iterator<E> leftIterator =
+ wrappedList.iterator(); leftIterator.hasNext()
&& rightIterator.hasNext();) {
// Add the transformed items to the result list
E leftVal = leftIterator.next();
@@ -228,18 +228,18 @@ public class FunctionalList<E> implements Cloneable, IFunctionalList<E> {
* Function)
*/
@Override
- public <T> IFunctionalList<T> flatMap(
- Function<E, IFunctionalList<T>> elementExpander) {
+ public <T> IFunctionalList<T>
+ flatMap(Function<E, IFunctionalList<T>> elementExpander) {
if (elementExpander == null) {
throw new NullPointerException("Expander must not be null");
}
- IFunctionalList<T> returnedList = new FunctionalList<>(
- this.wrappedList.size());
+ IFunctionalList<T> returnedList =
+ new FunctionalList<>(this.wrappedList.size());
forEach(element -> {
- IFunctionalList<T> expandedElement = elementExpander
- .apply(element);
+ IFunctionalList<T> expandedElement =
+ elementExpander.apply(element);
if (expandedElement == null) {
throw new NullPointerException(
@@ -370,8 +370,8 @@ public class FunctionalList<E> implements Cloneable, IFunctionalList<E> {
throw new NullPointerException("Transformer must be not null");
}
- IFunctionalList<T> returnedList = new FunctionalList<>(
- this.wrappedList.size());
+ IFunctionalList<T> returnedList =
+ new FunctionalList<>(this.wrappedList.size());
forEach(element -> {
// Add the transformed item to the result
@@ -388,8 +388,8 @@ public class FunctionalList<E> implements Cloneable, IFunctionalList<E> {
* IFunctionalList)
*/
@Override
- public <T> IFunctionalList<IPair<E, T>> pairWith(
- IFunctionalList<T> rightList) {
+ public <T> IFunctionalList<IPair<E, T>>
+ pairWith(IFunctionalList<T> rightList) {
return combineWith(rightList, Pair<E, T>::new);
}
@@ -399,8 +399,8 @@ public class FunctionalList<E> implements Cloneable, IFunctionalList<E> {
* @see bjc.utils.funcdata.IFunctionalList#partition(int)
*/
@Override
- public IFunctionalList<IFunctionalList<E>> partition(
- int numberPerPartition) {
+ public IFunctionalList<IFunctionalList<E>>
+ partition(int numberPerPartition) {
if (numberPerPartition < 1
|| numberPerPartition > wrappedList.size()) {
throw new IllegalArgumentException("" + numberPerPartition
@@ -408,11 +408,12 @@ public class FunctionalList<E> implements Cloneable, IFunctionalList<E> {
+ wrappedList.size());
}
- IFunctionalList<IFunctionalList<E>> returnedList = new FunctionalList<>();
+ IFunctionalList<IFunctionalList<E>> returnedList =
+ new FunctionalList<>();
// The current partition being filled
- IHolder<IFunctionalList<E>> currentPartition = new Identity<>(
- new FunctionalList<>());
+ IHolder<IFunctionalList<E>> currentPartition =
+ new Identity<>(new FunctionalList<>());
this.forEach((element) -> {
if (isPartitionFull(numberPerPartition, currentPartition)) {
@@ -595,4 +596,9 @@ public class FunctionalList<E> implements Cloneable, IFunctionalList<E> {
public IFunctionalList<E> tail() {
return new FunctionalList<>(wrappedList.subList(1, getSize()));
}
+
+ @Override
+ public void reverse() {
+ Collections.reverse(wrappedList);
+ }
} \ No newline at end of file
diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/IFunctionalList.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/IFunctionalList.java
index f22df57..2c5d2ae 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/IFunctionalList.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/IFunctionalList.java
@@ -172,8 +172,8 @@ public interface IFunctionalList<ContainedType> {
* The predicate to match by
* @return A list containing all elements that match the predicate
*/
- IFunctionalList<ContainedType> getMatching(
- Predicate<ContainedType> matchPredicate);
+ IFunctionalList<ContainedType>
+ getMatching(Predicate<ContainedType> matchPredicate);
/**
* Retrieve the size of the wrapped list
@@ -200,8 +200,8 @@ public interface IFunctionalList<ContainedType> {
* The function to apply to each element in the list
* @return A new list containing the mapped elements of this list.
*/
- <MappedType> IFunctionalList<MappedType> map(
- Function<ContainedType, MappedType> elementTransformer);
+ <MappedType> IFunctionalList<MappedType>
+ map(Function<ContainedType, MappedType> elementTransformer);
/**
* Zip two lists into a list of pairs
@@ -214,8 +214,8 @@ public interface IFunctionalList<ContainedType> {
* @return A list containing pairs of this element and the specified
* list
*/
- <OtherType> IFunctionalList<IPair<ContainedType, OtherType>> pairWith(
- IFunctionalList<OtherType> rightList);
+ <OtherType> IFunctionalList<IPair<ContainedType, OtherType>>
+ pairWith(IFunctionalList<OtherType> rightList);
/**
* Partition this list into a list of sublists
@@ -224,8 +224,8 @@ public interface IFunctionalList<ContainedType> {
* The size of elements to put into each one of the sublists
* @return A list partitioned into partitions of size nPerPart
*/
- IFunctionalList<IFunctionalList<ContainedType>> partition(
- int numberPerPartition);
+ IFunctionalList<IFunctionalList<ContainedType>>
+ partition(int numberPerPartition);
/**
* Prepend an item to the list
@@ -325,10 +325,16 @@ public interface IFunctionalList<ContainedType> {
* @return An iterable view onto the list
*/
Iterable<ContainedType> toIterable();
-
+
/**
* Get the tail of this list (the list without the first element
+ *
* @return The list without the first element
*/
public IFunctionalList<ContainedType> tail();
+
+ /**
+ * Reverse the contents of this list in place
+ */
+ void reverse();
} \ No newline at end of file
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 bbcefd3..866471c 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/ITree.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/ITree.java
@@ -25,43 +25,6 @@ public interface ITree<ContainedType> {
public void addChild(ITree<ContainedType> child);
/**
- * Transform the value that is the head of this node
- *
- * @param <TransformedType>
- * The type of the transformed value
- * @param transformer
- * The function to use to transform the value
- * @return The transformed value
- */
- public <TransformedType> TransformedType transformHead(
- Function<ContainedType, TransformedType> transformer);
-
- /**
- * Get a count of the number of direct children this node has
- *
- * @return The number of direct children this node has
- */
- public int getChildrenCount();
-
- /**
- * Transform one of this nodes children
- *
- * @param <TransformedType>
- * The type of the transformed value
- * @param childNo
- * The number of the child to transform
- * @param transformer
- * The function to use to transform the value
- * @return The transformed value
- *
- * @throws IllegalArgumentException
- * if the childNo is out of bounds (0 <= childNo <=
- * childCount())
- */
- public <TransformedType> TransformedType transformChild(int childNo,
- Function<ITree<ContainedType>, TransformedType> transformer);
-
- /**
* Collapse a tree into a single version
*
* @param <NewType>
@@ -84,6 +47,14 @@ public interface ITree<ContainedType> {
Function<NewType, ReturnedType> resultTransformer);
/**
+ * Execute a given action for each of this tree's children
+ *
+ * @param action
+ * The action to execute for each child
+ */
+ void doForChildren(Consumer<ITree<ContainedType>> action);
+
+ /**
* Expand the nodes of a tree into trees, and then merge the contents
* of those trees into a single tree
*
@@ -95,38 +66,31 @@ public interface ITree<ContainedType> {
Function<ContainedType, ITree<ContainedType>> mapper);
/**
- * Transform some of the nodes in this tree
+ * Get the specified child of this tree
*
- * @param nodePicker
- * The predicate to use to pick nodes to transform
- * @param transformer
- * The function to use to transform picked nodes
+ * @param childNo
+ * The number of the child to get
+ * @return The specified child of this tree
*/
- public void selectiveTransform(Predicate<ContainedType> nodePicker,
- UnaryOperator<ContainedType> transformer);
+ default ITree<ContainedType> getChild(int childNo) {
+ return transformChild(childNo, (child) -> child);
+ }
/**
- * Transform the tree into a tree with a different type of token
+ * Get a count of the number of direct children this node has
*
- * @param <MappedType>
- * The type of the new tree
- * @param transformer
- * The function to use to transform tokens
- * @return A tree with the token types transformed
+ * @return The number of direct children this node has
*/
- public <MappedType> ITree<MappedType>
- transformTree(Function<ContainedType, MappedType> transformer);
+ public int getChildrenCount();
/**
- * Perform an action on each part of the tree
+ * Get the data stored in this node
*
- * @param linearizationMethod
- * The way to traverse the tree
- * @param action
- * The action to perform on each tree node
+ * @return The data stored in this node
*/
- public void traverse(TreeLinearizationMethod linearizationMethod,
- Consumer<ContainedType> action);
+ default ContainedType getHead() {
+ return transformHead((head) -> head);
+ }
/**
* Rebuild the tree with the same structure, but different nodes
@@ -144,6 +108,17 @@ public interface ITree<ContainedType> {
Function<ContainedType, MappedType> operatorTransformer);
/**
+ * Transform some of the nodes in this tree
+ *
+ * @param nodePicker
+ * The predicate to use to pick nodes to transform
+ * @param transformer
+ * The function to use to transform picked nodes
+ */
+ public void selectiveTransform(Predicate<ContainedType> nodePicker,
+ UnaryOperator<ContainedType> transformer);
+
+ /**
* Do a top-down transform of the tree
*
* @param transformPicker
@@ -157,22 +132,55 @@ public interface ITree<ContainedType> {
UnaryOperator<ITree<ContainedType>> transformer);
/**
- * Get the specified child of this tree
+ * Transform one of this nodes children
*
+ * @param <TransformedType>
+ * The type of the transformed value
* @param childNo
- * The number of the child to get
- * @return The specified child of this tree
+ * The number of the child to transform
+ * @param transformer
+ * The function to use to transform the value
+ * @return The transformed value
+ *
+ * @throws IllegalArgumentException
+ * if the childNo is out of bounds (0 <= childNo <=
+ * childCount())
*/
- default ITree<ContainedType> getChild(int childNo) {
- return transformChild(childNo, (child) -> child);
- }
+ public <TransformedType> TransformedType transformChild(int childNo,
+ Function<ITree<ContainedType>, TransformedType> transformer);
/**
- * Get the data stored in this node
+ * Transform the value that is the head of this node
*
- * @return The data stored in this node
+ * @param <TransformedType>
+ * The type of the transformed value
+ * @param transformer
+ * The function to use to transform the value
+ * @return The transformed value
*/
- default ContainedType getHead() {
- return transformHead((head) -> head);
- }
+ public <TransformedType> TransformedType transformHead(
+ Function<ContainedType, TransformedType> transformer);
+
+ /**
+ * Transform the tree into a tree with a different type of token
+ *
+ * @param <MappedType>
+ * The type of the new tree
+ * @param transformer
+ * The function to use to transform tokens
+ * @return A tree with the token types transformed
+ */
+ public <MappedType> ITree<MappedType>
+ transformTree(Function<ContainedType, MappedType> transformer);
+
+ /**
+ * Perform an action on each part of the tree
+ *
+ * @param linearizationMethod
+ * The way to traverse the tree
+ * @param action
+ * The action to perform on each tree node
+ */
+ public void traverse(TreeLinearizationMethod linearizationMethod,
+ Consumer<ContainedType> action);
}
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 cd43df7..b56ff48 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/Tree.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/Tree.java
@@ -21,7 +21,7 @@ public class Tree<ContainedType> implements ITree<ContainedType> {
private boolean hasChildren;
- private int childCount;
+ private int childCount = 0;
/**
* Create a new leaf node in a tree
@@ -43,21 +43,15 @@ public class Tree<ContainedType> implements ITree<ContainedType> {
* @param childrn
* A list of children for this node
*/
- @SafeVarargs
- public Tree(ContainedType leafToken, ITree<ContainedType>... childrn) {
+ public Tree(ContainedType leafToken,
+ IFunctionalList<ITree<ContainedType>> childrn) {
data = leafToken;
hasChildren = true;
- childCount = 0;
-
- children = new FunctionalList<>();
-
- for (ITree<ContainedType> child : childrn) {
- children.add(child);
+ childCount = childrn.getSize();
- childCount++;
- }
+ children = childrn;
}
/**
@@ -68,15 +62,21 @@ public class Tree<ContainedType> implements ITree<ContainedType> {
* @param childrn
* A list of children for this node
*/
- public Tree(ContainedType leafToken,
- IFunctionalList<ITree<ContainedType>> childrn) {
+ @SafeVarargs
+ public Tree(ContainedType leafToken, ITree<ContainedType>... childrn) {
data = leafToken;
hasChildren = true;
- childCount = childrn.getSize();
+ childCount = 0;
- children = childrn;
+ children = new FunctionalList<>();
+
+ for (ITree<ContainedType> child : childrn) {
+ children.add(child);
+
+ childCount++;
+ }
}
@Override
@@ -93,28 +93,6 @@ public class Tree<ContainedType> implements ITree<ContainedType> {
}
@Override
- public <TransformedType> TransformedType transformHead(
- Function<ContainedType, TransformedType> transformer) {
- return transformer.apply(data);
- }
-
- @Override
- public int getChildrenCount() {
- return childCount;
- }
-
- @Override
- public <TransformedType> TransformedType transformChild(int childNo,
- Function<ITree<ContainedType>, TransformedType> transformer) {
- if (childNo < 0 || childNo > (childCount - 1)) {
- throw new IllegalArgumentException(
- "Child index #" + childNo + " is invalid");
- }
-
- return transformer.apply(children.getByIndex(childNo));
- }
-
- @Override
public <NewType, ReturnedType> ReturnedType collapse(
Function<ContainedType, NewType> leafTransform,
Function<ContainedType, Function<IFunctionalList<NewType>, NewType>> nodeCollapser,
@@ -124,23 +102,9 @@ public class Tree<ContainedType> implements ITree<ContainedType> {
.apply(internalCollapse(leafTransform, nodeCollapser));
}
- protected <NewType> NewType internalCollapse(
- Function<ContainedType, NewType> leafTransform,
- Function<ContainedType, Function<IFunctionalList<NewType>, NewType>> nodeCollapser) {
- if (hasChildren) {
- Function<IFunctionalList<NewType>, NewType> nodeTransformer =
- nodeCollapser.apply(data);
-
- IFunctionalList<NewType> collapsedChildren =
- children.map((child) -> {
- return child.collapse(leafTransform, nodeCollapser,
- (subTreeVal) -> subTreeVal);
- });
-
- return nodeTransformer.apply(collapsedChildren);
- }
-
- return leafTransform.apply(data);
+ @Override
+ public void doForChildren(Consumer<ITree<ContainedType>> action) {
+ children.forEach(action);
}
@Override
@@ -159,68 +123,44 @@ public class Tree<ContainedType> implements ITree<ContainedType> {
}
@Override
- public void selectiveTransform(Predicate<ContainedType> nodePicker,
- UnaryOperator<ContainedType> transformer) {
- if (hasChildren) {
- children.forEach((child) -> child
- .selectiveTransform(nodePicker, transformer));
- } else {
- data = transformer.apply(data);
- }
+ public int getChildrenCount() {
+ return childCount;
}
- @Override
- public <MappedType> ITree<MappedType> transformTree(
- Function<ContainedType, MappedType> transformer) {
+ protected <NewType> NewType internalCollapse(
+ Function<ContainedType, NewType> leafTransform,
+ Function<ContainedType, Function<IFunctionalList<NewType>, NewType>> nodeCollapser) {
if (hasChildren) {
- IFunctionalList<ITree<MappedType>> transformedChildren =
- children.map(
- (child) -> child.transformTree(transformer));
+ Function<IFunctionalList<NewType>, NewType> nodeTransformer =
+ nodeCollapser.apply(data);
- return new Tree<>(transformer.apply(data),
- transformedChildren);
+ IFunctionalList<NewType> collapsedChildren =
+ children.map((child) -> {
+ return child.collapse(leafTransform, nodeCollapser,
+ (subTreeVal) -> subTreeVal);
+ });
+
+ return nodeTransformer.apply(collapsedChildren);
}
- return new Tree<>(transformer.apply(data));
+ return leafTransform.apply(data);
}
- @Override
- public void traverse(TreeLinearizationMethod linearizationMethod,
- Consumer<ContainedType> action) {
- if (hasChildren) {
- switch (linearizationMethod) {
- case INORDER:
- if (childCount != 2) {
- throw new IllegalArgumentException(
- "Can only do in-order traversal for binary trees.");
- }
-
- children.getByIndex(0).traverse(linearizationMethod,
- action);
-
- action.accept(data);
-
- children.getByIndex(1).traverse(linearizationMethod,
- action);
- break;
- case POSTORDER:
- children.forEach((child) -> child
- .traverse(linearizationMethod, action));
-
- action.accept(data);
- break;
- case PREORDER:
- action.accept(data);
+ protected void internalToString(StringBuilder builder, int indentLevel,
+ boolean initial) {
+ if (!initial) {
+ StringUtils.indentNLevels(builder, indentLevel);
+ }
- children.forEach((child) -> child
- .traverse(linearizationMethod, action));
- break;
- default:
- break;
+ builder.append("Node: ");
+ builder.append(data == null ? "(null)" : data.toString());
+ builder.append("\n");
- }
- } else {
- action.accept(data);
+ if (hasChildren) {
+ children.forEach((child) -> {
+ ((Tree<ContainedType>) child).internalToString(builder,
+ indentLevel + 2, false);
+ });
}
}
@@ -243,31 +183,13 @@ public class Tree<ContainedType> implements ITree<ContainedType> {
}
@Override
- public String toString() {
- StringBuilder builder = new StringBuilder();
-
- internalToString(builder, 1, true);
-
- builder.deleteCharAt(builder.length() - 1);
-
- return builder.toString();
- }
-
- protected void internalToString(StringBuilder builder, int indentLevel,
- boolean initial) {
- if (!initial) {
- StringUtils.indentNLevels(builder, indentLevel);
- }
-
- builder.append("Node: ");
- builder.append(data == null ? "(null)" : data.toString());
- builder.append("\n");
-
+ public void selectiveTransform(Predicate<ContainedType> nodePicker,
+ UnaryOperator<ContainedType> transformer) {
if (hasChildren) {
- children.forEach((child) -> {
- ((Tree<ContainedType>) child).internalToString(builder,
- indentLevel + 2, false);
- });
+ children.forEach((child) -> child
+ .selectiveTransform(nodePicker, transformer));
+ } else {
+ data = transformer.apply(data);
}
}
@@ -312,4 +234,87 @@ public class Tree<ContainedType> implements ITree<ContainedType> {
}
}
+
+ @Override
+ public String toString() {
+ StringBuilder builder = new StringBuilder();
+
+ internalToString(builder, 1, true);
+
+ builder.deleteCharAt(builder.length() - 1);
+
+ return builder.toString();
+ }
+
+ @Override
+ public <TransformedType> TransformedType transformChild(int childNo,
+ Function<ITree<ContainedType>, TransformedType> transformer) {
+ if (childNo < 0 || childNo > (childCount - 1)) {
+ throw new IllegalArgumentException(
+ "Child index #" + childNo + " is invalid");
+ }
+
+ return transformer.apply(children.getByIndex(childNo));
+ }
+
+ @Override
+ public <TransformedType> TransformedType transformHead(
+ Function<ContainedType, TransformedType> transformer) {
+ return transformer.apply(data);
+ }
+
+ @Override
+ public <MappedType> ITree<MappedType> transformTree(
+ Function<ContainedType, MappedType> transformer) {
+ if (hasChildren) {
+ IFunctionalList<ITree<MappedType>> transformedChildren =
+ children.map(
+ (child) -> child.transformTree(transformer));
+
+ return new Tree<>(transformer.apply(data),
+ transformedChildren);
+ }
+
+ return new Tree<>(transformer.apply(data));
+ }
+
+ @Override
+ public void traverse(TreeLinearizationMethod linearizationMethod,
+ Consumer<ContainedType> action) {
+ if (hasChildren) {
+ switch (linearizationMethod) {
+ case INORDER:
+ if (childCount != 2) {
+ throw new IllegalArgumentException(
+ "Can only do in-order traversal for binary trees.");
+ }
+
+ children.getByIndex(0).traverse(linearizationMethod,
+ action);
+
+ action.accept(data);
+
+ children.getByIndex(1).traverse(linearizationMethod,
+ action);
+ break;
+ case POSTORDER:
+ children.forEach((child) -> child
+ .traverse(linearizationMethod, action));
+
+ action.accept(data);
+ break;
+ case PREORDER:
+ action.accept(data);
+
+ children.forEach((child) -> child
+ .traverse(linearizationMethod, action));
+ break;
+ default:
+ break;
+
+ }
+ } else {
+ action.accept(data);
+ }
+ }
}