From f9283a20abd9eaed0b0436bc54c60576233121f4 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Mon, 11 Apr 2016 09:32:59 -0400 Subject: Added new method to pairs and holders --- .../src/main/java/bjc/utils/data/GenHolder.java | 5 ++ .../src/main/java/bjc/utils/data/IHolder.java | 11 +++ BJC-Utils2/src/main/java/bjc/utils/data/IPair.java | 18 ++++- BJC-Utils2/src/main/java/bjc/utils/data/Pair.java | 6 ++ .../main/java/bjc/utils/data/lazy/LazyHolder.java | 93 +++++++++++++++++++--- .../main/java/bjc/utils/data/lazy/LazyPair.java | 19 ++++- .../main/java/bjc/utils/funcutils/StringUtils.java | 20 +++++ .../main/java/bjc/utils/gen/WeightedGrammar.java | 19 +++-- .../src/main/java/bjc/utils/parserutils/AST.java | 22 +++++ .../bjc/utils/parserutils/TreeConstructor.java | 62 +++++++-------- 10 files changed, 221 insertions(+), 54 deletions(-) (limited to 'BJC-Utils2/src/main') diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/GenHolder.java b/BJC-Utils2/src/main/java/bjc/utils/data/GenHolder.java index e042554..bd87f07 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/GenHolder.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/GenHolder.java @@ -105,4 +105,9 @@ public class GenHolder implements IHolder { return heldValue.toString(); } + + @Override + public IHolder bind(Function> binder) { + return binder.apply(heldValue); + } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java b/BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java index 6290d5f..a4f4013 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/IHolder.java @@ -56,4 +56,15 @@ public interface IHolder { * @return The mapped value outside of a GenHolder */ public E unwrap(Function unwrapper); + + /** + * Bind the value in this holder to a new value + * + * @param + * The new type of the held value + * @param binder + * The function to do the binding with + * @return The bound value + */ + public IHolder bind(Function> binder); } \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/IPair.java b/BJC-Utils2/src/main/java/bjc/utils/data/IPair.java index e2ee6a4..a20ff36 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/IPair.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/IPair.java @@ -14,7 +14,7 @@ import java.util.function.Function; * @param * The type stored in the right side of the pair */ -public interface IPair { +public interface IPair { /** * Create a new pair by applying the given functions to the left/right. @@ -34,6 +34,22 @@ public interface IPair { public IPair apply(Function leftTransformer, Function rightTransformer); + /** + * Apply a function to the two internal values that returns a new pair. + * + * Is a monadic bind. + * + * @param + * The new left pair type + * @param + * The new right pair type + * @param binder + * The function to use as a bind + * @return The new pair + */ + public IPair + bind(BiFunction> binder); + /** * Execute an action with the values of this pair. Has no effect on the * internal contents diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java b/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java index f721a9a..d0397f7 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/Pair.java @@ -97,4 +97,10 @@ public class Pair implements IPair { public String toString() { return "pair[l=" + leftValue.toString() + ", r=" + rightValue.toString() + "]"; } + + @Override + public IPair + bind(BiFunction> binder) { + return binder.apply(leftValue, rightValue); + } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/lazy/LazyHolder.java b/BJC-Utils2/src/main/java/bjc/utils/data/lazy/LazyHolder.java index 2ad8dbf..9f9dc4a 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/lazy/LazyHolder.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/lazy/LazyHolder.java @@ -21,34 +21,100 @@ import bjc.utils.funcdata.IFunctionalList; * The type of the data being held */ public class LazyHolder implements IHolder, ILazy { - private final class LazyHolderSupplier + private static final class LazyHolderHolder + implements IHolder { + private Supplier> holderSource; + + private IHolder holder; + + private IFunctionalList> actions = new FunctionalList<>(); + + public LazyHolderHolder(Supplier> source) { + + holderSource = source; + } + + @Override + public void doWith(Consumer action) { + actions.add((val) -> { + action.accept(val); + + return val; + }); + } + + @Override + public IHolder map(Function transformer) { + // TODO implement me + throw new UnsupportedOperationException( + "Mapping is not yet supported on bound holders"); + } + + @Override + public IHolder transform(Function transformer) { + actions.add(transformer); + + return this; + } + + @Override + public E unwrap(Function unwrapper) { + if (holder == null) { + holder = holderSource.get(); + } + + if (!actions.isEmpty()) { + actions.forEach((transform) -> { + holder.transform(transform); + }); + } + + return holder.unwrap(unwrapper); + } + + @Override + public IHolder bind(Function> binder) { + return new LazyHolderHolder<>(() -> { + return binder.apply(unwrap((val) -> val)); + }); + } + + } + + private static final class LazyHolderSupplier implements Supplier { - private IFunctionalList> pendingActions; - private Function pendingTransform; + private IFunctionalList> pendingActions; + private Function pendingTransform; + + private T2 heldValue; + private Supplier heldSource; - public LazyHolderSupplier(IFunctionalList> actons, - Function transform) { + public LazyHolderSupplier(IFunctionalList> actons, + Function transform, T2 heldValue, + Supplier heldSource) { // Resolve latent bug I just realized. After a map, adding new // actions to the original holder could've resulted in changes // to all unactualized mapped values from that holder pendingActions = new FunctionalList<>(); - for (Function action : actons.toIterable()) { + for (Function action : actons.toIterable()) { pendingActions.add(action); } this.pendingTransform = transform; + this.heldValue = heldValue; + this.heldSource = heldSource; } @Override public NewT get() { if (heldValue == null) { return pendingActions.reduceAux(heldSource.get(), - Function::apply, pendingTransform::apply); + Function::apply, pendingTransform::apply); } return pendingActions.reduceAux(heldValue, - Function::apply, pendingTransform::apply); + Function::apply, pendingTransform::apply); } } @@ -114,8 +180,8 @@ public class LazyHolder implements IHolder, ILazy { } // Don't actually map until we need to - return new LazyHolder<>( - new LazyHolderSupplier<>(actions, transform)); + return new LazyHolder<>(new LazyHolderSupplier<>(actions, + transform, heldValue, heldSource)); } @Override @@ -179,4 +245,11 @@ public class LazyHolder implements IHolder, ILazy { heldValue = action.apply(heldValue); }); } + + @Override + public IHolder bind(Function> binder) { + return new LazyHolderHolder<>(() -> { + return binder.apply(unwrap((val) -> val)); + }); + } } \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/lazy/LazyPair.java b/BJC-Utils2/src/main/java/bjc/utils/data/lazy/LazyPair.java index e08c8fb..7ba7ee7 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/lazy/LazyPair.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/lazy/LazyPair.java @@ -98,9 +98,9 @@ public class LazyPair implements IPair, ILazy { throw new NullPointerException("Transforms must be non-null"); } - IHolder> newPair = - delegatePair.map((currentPair) -> currentPair - .apply(leftTransform, rightTransform)); + IHolder> newPair = delegatePair + .map((currentPair) -> currentPair.apply(leftTransform, + rightTransform)); return new LazyPair<>(newPair, materialized, true); } @@ -169,4 +169,17 @@ public class LazyPair implements IPair, ILazy { materialized = true; pendingActions = false; } + + @Override + public IPair bind( + BiFunction> binder) { + // TODO Auto-generated method stub + IHolder> newDelegate = delegatePair + .map((pairVal) -> { + return pairVal.bind(binder); + }); + + return new LazyPair<>(newDelegate, isMaterialized(), + hasPendingActions()); + } } \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/StringUtils.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/StringUtils.java index a73292c..ac36d15 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/StringUtils.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/StringUtils.java @@ -37,4 +37,24 @@ public class StringUtils { return input.matches("\\A(?:" + regex + ")+\\Z"); } + /** + * Checks if the given expression contains the specified operator in a + * situation that indicates its use as an infix operator. + * + * @param expression + * The expression to check + * @param operator + * The operator to see if it is contained + * @return Whether or not the given expression contains the specified + * operator as a infix operator + */ + public static boolean containsInfixOperator(String expression, + String operator) { + // Bit annoying to have to use a full class name, but what are you + // going to do? + return org.apache.commons.lang3.StringUtils + .countMatches(expression, operator) == 1 + && !expression.equalsIgnoreCase(operator) + && !expression.startsWith(operator); + } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java b/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java index 37f31b0..8d31576 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java @@ -368,16 +368,19 @@ public class WeightedGrammar { newRule.add(newCase); } - newRule.forEach( - (list) -> newResults - .add(new Pair<>( - pair.merge((left, right) -> left) - + additionalProbability, - list))); + newRule.forEach((list) -> { + Integer currentProb = pair.merge((left, right) -> left); + + newResults.add(new Pair<>( + currentProb + additionalProbability, list)); + }); }); - newResults.forEach((pair) -> pair - .doWith((left, right) -> addCase(ruleName, left, right))); + newResults.forEach((pair) -> { + pair.doWith((left, right) -> { + addCase(ruleName, left, right); + }); + }); } /** diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/AST.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/AST.java index 88b4862..b6c4d8c 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/AST.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/AST.java @@ -361,4 +361,26 @@ public class AST { action.accept(token); } } + + /** + * Change the type of nodes in the tree without changing the structure + * + * @param + * The new node type + * @param nodeTransform + * The transform to apply to leaf nodes + * @param operatorTransform + * The transform to apply to operator nodes + * @return An AST with the node types transformed + */ + public AST rebuildTree(Function nodeTransform, + Function operatorTransform) { + return collapse((leafNode) -> { + E transformedNode = nodeTransform.apply(leafNode); + return new AST<>(transformedNode); + }, (operator) -> (AST newLeft, AST newRight) -> { + return new AST<>(operatorTransform.apply(operator), newLeft, + newRight); + }, (resultValue) -> resultValue); + } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java b/BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java index 46e4da4..68ec70e 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java +++ b/BJC-Utils2/src/main/java/bjc/utils/parserutils/TreeConstructor.java @@ -28,40 +28,38 @@ public class TreeConstructor { } @Override - public IPair>, AST> - apply(IPair>, AST> pair) { - Deque> queuedASTs = - pair.merge((queue, currentAST) -> queue); - - AST mergedAST = pair.merge((queue, currentAST) -> { - AST newAST; - - if (isSpecialOperator.test(element)) { - newAST = handleSpecialOperator.apply(queue); - } else { - if (queue.size() < 2) { - throw new IllegalStateException( - "Attempted to parse binary operator without enough operands.\n" - + "Problem operator is " - + element - + "\nPossible operand is: \n\t" - + queue.peek()); - } - - AST rightAST = queue.pop(); - AST leftAST = queue.pop(); - - newAST = new AST<>(element, leftAST, rightAST); + public IPair>, AST> apply( + IPair>, AST> pair) { + return pair.bind((queuedASTs, currentAST) -> { + return handleOperator(queuedASTs); + }); + } + + private IPair>, AST> handleOperator( + Deque> queuedASTs) { + AST newAST; + + if (isSpecialOperator.test(element)) { + newAST = handleSpecialOperator.apply(queuedASTs); + } else { + if (queuedASTs.size() < 2) { + throw new IllegalStateException( + "Attempted to parse binary operator without enough operands.\n" + + "Problem operator is " + + element + + "\nPossible operand is: \n\t" + + queuedASTs.peek()); } - queue.push(newAST); - return newAST; - }); + AST rightAST = queuedASTs.pop(); + AST leftAST = queuedASTs.pop(); + + newAST = new AST<>(element, leftAST, rightAST); + } - Pair>, AST> newPair = - new Pair<>(queuedASTs, mergedAST); + queuedASTs.push(newAST); - return newPair; + return new Pair<>(queuedASTs, newAST); } } @@ -162,8 +160,8 @@ public class TreeConstructor { "Special operator determiner must not be null"); } - GenHolder>, AST>> initialState = - new GenHolder<>(new Pair<>(new LinkedList<>(), null)); + GenHolder>, AST>> initialState = new GenHolder<>( + new Pair<>(new LinkedList<>(), null)); tokens.forEach( new TokenTransformer<>(initialState, operatorPredicate, -- cgit v1.2.3