From 8a8b457c98e207d809a7616e73eb59bfe197a7a5 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Thu, 31 Mar 2016 11:43:21 -0400 Subject: More code maintenance --- .../main/java/bjc/utils/funcutils/ListUtils.java | 247 ++++++++++++--------- .../java/bjc/utils/funcutils/StatementUtils.java | 11 - 2 files changed, 145 insertions(+), 113 deletions(-) delete mode 100644 BJC-Utils2/src/main/java/bjc/utils/funcutils/StatementUtils.java (limited to 'BJC-Utils2/src/main/java/bjc/utils/funcutils') diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java index 6e8c63a..3963e69 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcutils/ListUtils.java @@ -1,6 +1,7 @@ package bjc.utils.funcutils; import java.util.Deque; +import java.util.function.BiFunction; import java.util.function.Consumer; import java.util.function.Function; @@ -18,6 +19,74 @@ import bjc.utils.funcdata.FunctionalList; public class ListUtils { private static final int MAX_NTRIESPART = 50; + private static final class TokenDeaffixer + implements BiFunction> { + private String token; + + public TokenDeaffixer(String tok) { + token = tok; + } + + @Override + public FunctionalList apply(String opName, + String opRegex) { + if (StringUtils.containsOnly(token, opRegex)) { + return new FunctionalList<>(token); + } else if (token.startsWith(opName)) { + return new FunctionalList<>(opName, + token.split(opRegex)[1]); + } else if (token.endsWith(opName)) { + return new FunctionalList<>(token.split(opRegex)[0], + opName); + } else { + return new FunctionalList<>(token); + } + } + } + + private static final class TokenSplitter + implements BiFunction> { + private String tokenToSplit; + + public TokenSplitter(String tok) { + this.tokenToSplit = tok; + } + + @Override + public FunctionalList apply(String operatorName, + String operatorRegex) { + if (tokenToSplit.contains(operatorName)) { + if (StringUtils.containsOnly(tokenToSplit, + operatorRegex)) { + return new FunctionalList<>(tokenToSplit); + } else { + FunctionalList splitTokens = + new FunctionalList<>( + tokenToSplit.split(operatorRegex)); + + FunctionalList result = new FunctionalList<>(); + + int tokenExpansionSize = splitTokens.getSize(); + + splitTokens.forEachIndexed((tokenIndex, token) -> { + + if (tokenIndex != tokenExpansionSize + && tokenIndex != 0) { + result.add(operatorName); + result.add(token); + } else { + result.add(token); + } + }); + + return result; + } + } else { + return new FunctionalList<>(tokenToSplit); + } + } + } + /** * Implements a single group partitioning pass on a list * @@ -28,42 +97,48 @@ public class ListUtils { */ private static final class GroupPartIteration implements Consumer { - private FunctionalList> ret; - private GenHolder> currPart; - private FunctionalList rejects; - private GenHolder numInCurrPart; - private int nPerPart; - private Function eleCount; - - public GroupPartIteration(FunctionalList> ret, + private FunctionalList> returnedList; + private GenHolder> currentPartition; + private FunctionalList rejectedItems; + private GenHolder numberInCurrentPartition; + private int numberPerPartition; + private Function elementCounter; + + public GroupPartIteration( + FunctionalList> returned, GenHolder> currPart, FunctionalList rejects, GenHolder numInCurrPart, int nPerPart, Function eleCount) { - this.ret = ret; - this.currPart = currPart; - this.rejects = rejects; - this.numInCurrPart = numInCurrPart; - this.nPerPart = nPerPart; - this.eleCount = eleCount; + this.returnedList = returned; + this.currentPartition = currPart; + this.rejectedItems = rejects; + this.numberInCurrentPartition = numInCurrPart; + this.numberPerPartition = nPerPart; + this.elementCounter = eleCount; } @Override - public void accept(E val) { - if (numInCurrPart.unwrap((vl) -> vl >= nPerPart)) { - ret.add(currPart.unwrap((vl) -> vl)); + public void accept(E value) { + if (numberInCurrentPartition + .unwrap((number) -> number >= numberPerPartition)) { + returnedList.add( + currentPartition.unwrap((partition) -> partition)); - currPart.transform((vl) -> new FunctionalList<>()); - numInCurrPart.transform((vl) -> 0); + currentPartition + .transform((partition) -> new FunctionalList<>()); + numberInCurrentPartition.transform((number) -> 0); } else { - int vCount = eleCount.apply(val); + int currentElementCount = elementCounter.apply(value); - if (numInCurrPart - .unwrap((vl) -> (vl + vCount) >= nPerPart)) { - rejects.add(val); + if (numberInCurrentPartition.unwrap((number) -> (number + + currentElementCount) >= numberPerPartition)) { + rejectedItems.add(value); } else { - currPart.unwrap((vl) -> vl.add(val)); - numInCurrPart.transform((vl) -> vl + vCount); + currentPartition + .unwrap((partition) -> partition.add(value)); + numberInCurrentPartition.transform( + (number) -> number + currentElementCount); } } } @@ -76,59 +151,66 @@ public class ListUtils { * @param * The type of elements in the list to partition * - * @param list + * @param input * The list to partition - * @param eleCount + * @param elementCounter * The function to determine the count for each element for - * @param nPerPart + * @param numberPerPartition * The number of elements to put in each partition * @return A list partitioned according to the above rules */ public static FunctionalList> groupPartition( - FunctionalList list, Function eleCount, - int nPerPart) { + FunctionalList input, Function elementCounter, + int numberPerPartition) { /* * List that holds our results */ - FunctionalList> ret = new FunctionalList<>(); + FunctionalList> returnedList = + new FunctionalList<>(); /* * List that holds current partition */ - GenHolder> currPart = + GenHolder> currentPartition = new GenHolder<>(new FunctionalList<>()); /* * List that holds elements rejected during current pass */ - FunctionalList rejects = new FunctionalList<>(); + FunctionalList rejectedElements = new FunctionalList<>(); /* * The effective number of elements in the current partitition */ - GenHolder numInCurrPart = new GenHolder<>(0); + GenHolder numberInCurrentPartition = new GenHolder<>(0); /* * Run up to a certain number of passes */ - for (int nIterations = 0; nIterations < MAX_NTRIESPART - && !rejects.isEmpty(); nIterations++) { - list.forEach(new GroupPartIteration<>(ret, currPart, rejects, - numInCurrPart, nPerPart, eleCount)); + for (int numberOfIterations = + 0; numberOfIterations < MAX_NTRIESPART + && !rejectedElements + .isEmpty(); numberOfIterations++) { + input.forEach(new GroupPartIteration<>(returnedList, + currentPartition, rejectedElements, + numberInCurrentPartition, numberPerPartition, + elementCounter)); - if (rejects.isEmpty()) { + if (rejectedElements.isEmpty()) { // Nothing was rejected, so we're done - return ret; + return returnedList; } } - throw new IllegalArgumentException("Heuristic (more than " - + MAX_NTRIESPART - + " iterations of partitioning) detected unpartitionable list " - + list.toString() - + "\nThe following elements were not partitioned: " - + rejects.toString() + "\nCurrent group in formation: " - + currPart.unwrap((vl) -> vl.toString()) - + "\nPreviously formed groups: " + ret.toString()); + throw new IllegalArgumentException( + "Heuristic (more than " + MAX_NTRIESPART + + " iterations of partitioning) detected unpartitionable list " + + input.toString() + + "\nThe following elements were not partitioned: " + + rejectedElements.toString() + + "\nCurrent group in formation: " + + currentPartition.unwrap((vl) -> vl.toString()) + + "\nPreviously formed groups: " + + returnedList.toString()); } /** @@ -139,49 +221,23 @@ public class ListUtils { * * @param input * The tokens to split - * @param ops - * The operators to split on. + * @param operators + * Pairs of operators to split on and regexes that match + * those operators * @return A list of tokens split on all the operators * */ public static FunctionalList splitTokens( FunctionalList input, - Deque> ops) { + Deque> operators) { GenHolder> ret = new GenHolder<>(input); - ops.forEach( + operators.forEach( (op) -> ret.transform((oldRet) -> oldRet.flatMap((tok) -> { - return op.merge((opName, opRegex) -> { - if (tok.contains(opName)) { - if (StringUtils.containsOnly(tok, opRegex)) { - return new FunctionalList<>(tok); - } else { - FunctionalList splitTokens = - new FunctionalList<>( - tok.split(opRegex)); - FunctionalList rt = - new FunctionalList<>(); - int tkSize = splitTokens.getSize(); - splitTokens.forEachIndexed((idx, tk) -> { - - if (idx != tkSize && idx != 0) { - rt.add(opName); - rt.add(tk); - - } else { - rt.add(tk); - - } - }); - return rt; - } - } else { - return new FunctionalList<>(tok); - } - }); + return op.merge(new TokenSplitter(tok)); }))); - return ret.unwrap((l) -> l); + return ret.unwrap((list) -> list); } /** @@ -194,32 +250,18 @@ public class ListUtils { * @return The tokens that have been deaffixed * */ - @SuppressWarnings("unchecked") public static FunctionalList deAffixTokens( FunctionalList input, Deque> ops) { - GenHolder> ret = new GenHolder<>(input); + GenHolder> returnedList = + new GenHolder<>(input); - ops.forEach( - (op) -> ret.transform((oldRet) -> oldRet.flatMap((tok) -> { - return (FunctionalList) op - .merge((opName, opRegex) -> { - if (StringUtils.containsOnly(tok, - opRegex)) { - return new FunctionalList<>(tok); - } else if (tok.startsWith(opName)) { - return new FunctionalList<>(op, - tok.split(opRegex)[1]); - } else if (tok.endsWith(opName)) { - return new FunctionalList<>( - tok.split(opRegex)[0], op); - } else { - return new FunctionalList<>(tok); - } - }); + ops.forEach((op) -> returnedList + .transform((oldRet) -> oldRet.flatMap((tok) -> { + return op.merge(new TokenDeaffixer(tok)); }))); - return ret.unwrap((l) -> l); + return returnedList.unwrap((list) -> list); } /** @@ -232,6 +274,7 @@ public class ListUtils { */ public static String collapseTokens(FunctionalList input) { return input.reduceAux("", - (currString, state) -> state + currString, (s) -> s); + (currentString, state) -> state + currentString, + (strang) -> strang); } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcutils/StatementUtils.java b/BJC-Utils2/src/main/java/bjc/utils/funcutils/StatementUtils.java deleted file mode 100644 index 9b4b03f..0000000 --- a/BJC-Utils2/src/main/java/bjc/utils/funcutils/StatementUtils.java +++ /dev/null @@ -1,11 +0,0 @@ -package bjc.utils.funcutils; - -/** - * Contains methods that produce effects mimicking statements - * - * @author ben - * - */ -public class StatementUtils { - -} -- cgit v1.2.3