diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-04-08 13:28:09 -0400 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2016-04-08 13:28:09 -0400 |
| commit | 275a627719fc2231b16caea41130ff09f0f2b6a1 (patch) | |
| tree | 757e8ca2061ba6ed9b2063f7155edbe954b72bdb /BJC-Utils2/src/main/java/bjc/utils/gen | |
| parent | 79d3a4a47cbc1fcf17c77c6fc12ff826a3077bac (diff) | |
Switch functional data to use interfaces
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/gen')
3 files changed, 71 insertions, 47 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/gen/RandomGrammar.java b/BJC-Utils2/src/main/java/bjc/utils/gen/RandomGrammar.java index 4f194f3..349d9fa 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gen/RandomGrammar.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gen/RandomGrammar.java @@ -1,8 +1,7 @@ package bjc.utils.gen; -import java.util.HashMap; - -import bjc.utils.funcdata.FunctionalList; +import bjc.utils.funcdata.FunctionalMap; +import bjc.utils.funcdata.IFunctionalList; /** * A weighted grammar where all the rules have a equal chance of occuring. @@ -18,7 +17,7 @@ public class RandomGrammar<E> extends WeightedGrammar<E> { * Create a new random grammar. */ public RandomGrammar() { - rules = new HashMap<>(); + rules = new FunctionalMap<>(); } /** @@ -30,8 +29,8 @@ public class RandomGrammar<E> extends WeightedGrammar<E> { * The cases to add for this rule. */ @SafeVarargs - public final void addCases(E rule, FunctionalList<E>... cases) { - for (FunctionalList<E> currentCase : cases) { + public final void addCases(E rule, IFunctionalList<E>... cases) { + for (IFunctionalList<E> currentCase : cases) { super.addCase(rule, 1, currentCase); } } @@ -45,10 +44,10 @@ public class RandomGrammar<E> extends WeightedGrammar<E> { * The cases to add for this rule. */ @SafeVarargs - public final void makeRule(E rule, FunctionalList<E>... cases) { + public final void makeRule(E rule, IFunctionalList<E>... cases) { super.addRule(rule); - for (FunctionalList<E> currentCase : cases) { + for (IFunctionalList<E> currentCase : cases) { super.addCase(rule, 1, currentCase); } } @@ -61,7 +60,8 @@ public class RandomGrammar<E> extends WeightedGrammar<E> { * @param cases * The cases to add for this rule. */ - public void makeRule(E rule, FunctionalList<FunctionalList<E>> cases) { + public void makeRule(E rule, + IFunctionalList<IFunctionalList<E>> cases) { if (cases == null) { throw new NullPointerException("Cases must not be null"); } 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 5d61201..37f31b0 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedGrammar.java @@ -1,13 +1,14 @@ package bjc.utils.gen; -import java.util.Hashtable; -import java.util.Map; import java.util.Random; -import java.util.Set; import java.util.function.Function; +import bjc.utils.data.IPair; import bjc.utils.data.Pair; import bjc.utils.funcdata.FunctionalList; +import bjc.utils.funcdata.FunctionalMap; +import bjc.utils.funcdata.IFunctionalList; +import bjc.utils.funcdata.IFunctionalMap; /** * A random grammar, where certain rules will come up more often than @@ -22,29 +23,29 @@ public class WeightedGrammar<E> { /** * The initial rule of the grammar */ - protected String initialRule; + protected String initialRule; /** * The rules currently in this grammar */ - protected Map<E, WeightedRandom<FunctionalList<E>>> rules; + protected IFunctionalMap<E, WeightedRandom<IFunctionalList<E>>> rules; /** * The random number generator used for random numbers */ - private Random rng; + private Random rng; /** * All of the subgrammars of this grammar */ - protected Map<E, WeightedGrammar<E>> subgrammars; + protected IFunctionalMap<E, WeightedGrammar<E>> subgrammars; /** * Create a new weighted grammar. */ public WeightedGrammar() { - rules = new Hashtable<>(); - subgrammars = new Hashtable<>(); + rules = new FunctionalMap<>(); + subgrammars = new FunctionalMap<>(); } /** @@ -76,7 +77,7 @@ public class WeightedGrammar<E> { * The case being added. */ public void addCase(E ruleName, int probability, - FunctionalList<E> cse) { + IFunctionalList<E> cse) { if (ruleName == null) { throw new NullPointerException("Rule name must be not null"); } else if (cse == null) { @@ -145,7 +146,7 @@ public class WeightedGrammar<E> { * @return Whether or not the rule was succesfully added. */ public boolean addRule(E name, - WeightedRandom<FunctionalList<E>> cases) { + WeightedRandom<IFunctionalList<E>> cases) { if (name == null) { throw new NullPointerException("Name must not be null"); } else if (cases == null) { @@ -193,15 +194,15 @@ public class WeightedGrammar<E> { * The rule to test. * @return A set of sentances generated by the specified rule. */ - public FunctionalList<FunctionalList<E>> generateDebugValues( + public IFunctionalList<IFunctionalList<E>> generateDebugValues( E ruleName) { if (ruleName == null) { throw new NullPointerException("Rule name must not be null"); } - FunctionalList<FunctionalList<E>> returnedList = new FunctionalList<>(); + IFunctionalList<IFunctionalList<E>> returnedList = new FunctionalList<>(); - WeightedRandom<FunctionalList<E>> ruleGenerator = rules + WeightedRandom<IFunctionalList<E>> ruleGenerator = rules .get(ruleName); for (int i = 0; i < 10; i++) { @@ -226,7 +227,7 @@ public class WeightedGrammar<E> { * @return A randomly generated sentance from the specified initial * rule. */ - public <T> FunctionalList<T> generateGenericValues(E initRule, + public <T> IFunctionalList<T> generateGenericValues(E initRule, Function<E, T> tokenTransformer, T spacer) { if (initRule == null) { throw new NullPointerException( @@ -237,7 +238,7 @@ public class WeightedGrammar<E> { throw new NullPointerException("Spacer must not be null"); } - FunctionalList<T> returnedList = new FunctionalList<>(); + IFunctionalList<T> returnedList = new FunctionalList<>(); if (subgrammars.containsKey(initRule)) { subgrammars.get(initRule).generateGenericValues(initRule, @@ -280,7 +281,7 @@ public class WeightedGrammar<E> { * @return A list of random grammar elements generated by the specified * rule. */ - public FunctionalList<E> generateListValues(E initRule, E spacer) { + public IFunctionalList<E> generateListValues(E initRule, E spacer) { return generateGenericValues(initRule, strang -> strang, spacer); } @@ -342,16 +343,23 @@ public class WeightedGrammar<E> { "Number of times to prefix must be positive."); } - WeightedRandom<FunctionalList<E>> rule = rules.get(ruleName); + WeightedRandom<IFunctionalList<E>> rule = rules.get(ruleName); - FunctionalList<Pair<Integer, FunctionalList<E>>> newResults = new FunctionalList<>(); + IFunctionalList<IPair<Integer, IFunctionalList<E>>> newResults = new FunctionalList<>(); rule.getValues().forEach((pair) -> { - FunctionalList<FunctionalList<E>> newRule = new FunctionalList<>(); + IFunctionalList<IFunctionalList<E>> newRule = new FunctionalList<>(); for (int i = 1; i <= numberOfTimes; i++) { - FunctionalList<E> newCase = pair - .merge((left, right) -> right.clone()); + IFunctionalList<E> newCase = pair.merge((left, right) -> { + IFunctionalList<E> returnVal = new FunctionalList<>(); + + for (E val : right.toIterable()) { + returnVal.add(val); + } + + return returnVal; + }); for (int j = 1; j <= i; j++) { newCase.prepend(prefixToken); @@ -392,13 +400,21 @@ public class WeightedGrammar<E> { "Prefix token must not be null"); } - WeightedRandom<FunctionalList<E>> rule = rules.get(ruleName); + WeightedRandom<IFunctionalList<E>> rule = rules.get(ruleName); - FunctionalList<Pair<Integer, FunctionalList<E>>> newResults = new FunctionalList<>(); + IFunctionalList<IPair<Integer, IFunctionalList<E>>> newResults = new FunctionalList<>(); rule.getValues().forEach((pair) -> { - FunctionalList<E> newCase = pair - .merge((left, right) -> right.clone()); + IFunctionalList<E> newCase = pair.merge((left, right) -> { + IFunctionalList<E> returnVal = new FunctionalList<>(); + + for (E val : right.toIterable()) { + returnVal.add(val); + } + + return returnVal; + }); + newCase.prepend(prefixToken); newResults.add(new Pair<>(pair.merge((left, right) -> left) @@ -443,7 +459,7 @@ public class WeightedGrammar<E> { * @return The number of rules in this grammar */ public int getRuleCount() { - return rules.size(); + return rules.getSize(); } /** @@ -451,8 +467,8 @@ public class WeightedGrammar<E> { * * @return The set of all rule names in this grammar */ - public Set<E> getRuleNames() { - return rules.keySet(); + public IFunctionalList<E> getRuleNames() { + return rules.keyList(); } /** @@ -484,13 +500,20 @@ public class WeightedGrammar<E> { "Prefix token must not be null"); } - WeightedRandom<FunctionalList<E>> rule = rules.get(ruleName); + WeightedRandom<IFunctionalList<E>> rule = rules.get(ruleName); - FunctionalList<Pair<Integer, FunctionalList<E>>> newResults = new FunctionalList<>(); + IFunctionalList<IPair<Integer, IFunctionalList<E>>> newResults = new FunctionalList<>(); rule.getValues().forEach((par) -> { - FunctionalList<E> newCase = par - .merge((left, right) -> right.clone()); + IFunctionalList<E> newCase = par.merge((left, right) -> { + IFunctionalList<E> returnVal = new FunctionalList<>(); + + for (E val : right.toIterable()) { + returnVal.add(val); + } + + return returnVal; + }); newCase.add(suffixToken); newResults.add(new Pair<>(par.merge((left, right) -> left) diff --git a/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java b/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java index 5157ee2..10da34e 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java +++ b/BJC-Utils2/src/main/java/bjc/utils/gen/WeightedRandom.java @@ -2,9 +2,10 @@ package bjc.utils.gen; import java.util.Random; import bjc.utils.funcdata.FunctionalList; +import bjc.utils.funcdata.IFunctionalList; import bjc.utils.data.GenHolder; import bjc.utils.data.IHolder; -import bjc.utils.data.Pair; +import bjc.utils.data.IPair; /** * Represents a random number generator where certain results are weighted @@ -19,12 +20,12 @@ public class WeightedRandom<E> { /** * The list of probabilities for each result */ - private FunctionalList<Integer> probabilities; + private IFunctionalList<Integer> probabilities; /** * The list of possible results to pick from */ - private FunctionalList<E> results; + private IFunctionalList<E> results; /** * The source for any needed random numbers @@ -101,7 +102,7 @@ public class WeightedRandom<E> { * * @return A list of all the values that can be generated */ - public FunctionalList<E> getResults() { + public IFunctionalList<E> getResults() { return results; } @@ -111,7 +112,7 @@ public class WeightedRandom<E> { * * @return A list of pairs of values and value probabilities */ - public FunctionalList<Pair<Integer, E>> getValues() { + public IFunctionalList<IPair<Integer, E>> getValues() { return probabilities.pairWith(results); } }
\ No newline at end of file |
