diff options
| author | student <student@localhost> | 2018-03-02 11:54:19 -0500 |
|---|---|---|
| committer | student <student@localhost> | 2018-03-02 11:54:19 -0500 |
| commit | ed51142cd7abaaeedcb9ef1439f26690b57ce839 (patch) | |
| tree | 68e4bdce841783588c09bb2fd30af2e4e791498d /base | |
| parent | 1fc8f90b94702206caec4eb940a18c258544a28e (diff) | |
Update
Diffstat (limited to 'base')
| -rw-r--r-- | base/src/examples/java/bjc/utils/examples/AbbrevMapTest.java | 46 | ||||
| -rw-r--r-- | base/src/main/java/bjc/utils/data/TopDownTransformIterator.java | 127 | ||||
| -rw-r--r-- | base/todos.txt | 6 |
3 files changed, 120 insertions, 59 deletions
diff --git a/base/src/examples/java/bjc/utils/examples/AbbrevMapTest.java b/base/src/examples/java/bjc/utils/examples/AbbrevMapTest.java index ac4ea76..cd6e6f1 100644 --- a/base/src/examples/java/bjc/utils/examples/AbbrevMapTest.java +++ b/base/src/examples/java/bjc/utils/examples/AbbrevMapTest.java @@ -1,5 +1,7 @@ package bjc.utils.examples; +import java.io.PrintStream; +import java.util.List; import java.util.Scanner; import bjc.utils.esodata.AbbrevMap; @@ -24,28 +26,39 @@ public class AbbrevMapTest { final AbbrevMap map = new AbbrevMap(); System.out.print("Enter a command (blank line to quit): "); - String ln = scn.nextLine(); + String ln = scn.nextLine().trim(); while (!ln.equals("")) { - final String[] commParts = ln.split(" "); + final List<String> commParts = StringUtils.processArguments(ln); - switch (commParts[0]) { + switch (commParts.get(0)) { case "add": - map.addWords(commParts[1]); + map.addWords(commParts.get(1)); break; case "remove": - map.removeWords(commParts[1]); + map.removeWords(commParts.get(1)); break; case "recalc": map.recalculate(); break; - case "check": - final String list = StringUtils.toEnglishList(map.deabbrev(commParts[1]), false); + case "check": { + String[] strings = map.deabbrev(commParts.get(1)); + + final String list = StringUtils.toEnglishList(strings, false); + System.out.println(list); break; + } case "debug": System.out.println(map.toString()); break; + case "help": + if(commParts.size() > 1) { + help(commParts.get(1)); + } else { + help(); + } + break; default: System.out.println("Unknown command: " + ln); } @@ -56,4 +69,23 @@ public class AbbrevMapTest { scn.close(); } + + private static void help() { + PrintStream strm = System.out; + + strm.println("Abbreviation Map Testing Commands:"); + strm.println("\tadd <word>\tAdd a word to the abbreviation map"); + strm.println("\tremove <word>\tRemove a word from the abbreviation map"); + strm.println("\trecalc \tRecalculate the abbreviation map"); + strm.println("\tcheck <word>\tCheck all of the possible things a word could be an abbreviation for"); + strm.println("\tdebug \tPrint out the abbreviation map"); + strm.println("\thelp \tList commands, or get help on a command\n"); + } + + private static void help(String com) { + switch(com) { + default: + System.out.printf("\tNo help available for command: %s\n", com); + } + } } diff --git a/base/src/main/java/bjc/utils/data/TopDownTransformIterator.java b/base/src/main/java/bjc/utils/data/TopDownTransformIterator.java index 8914ad4..9a99a70 100644 --- a/base/src/main/java/bjc/utils/data/TopDownTransformIterator.java +++ b/base/src/main/java/bjc/utils/data/TopDownTransformIterator.java @@ -22,11 +22,30 @@ import java.util.function.Function; * @author EVE * * @param <ContainedType> - * The type of the nodes in the tree. + * The type of the nodes in the tree. */ public class TopDownTransformIterator<ContainedType> implements Iterator<ITree<ContainedType>> { + /** + * Alias type for a tree transformation. + * + * @author student + * + * @param <ContainedType> + * The type contained in the tree. + */ + public interface TreeTransform<ContainedType> + extends BiFunction<ITree<ContainedType>, Consumer<Iterator<ITree<ContainedType>>>, ITree<ContainedType>> { + + } + + /* + * The function that picks how to transform a given node + */ private final Function<ContainedType, TopDownTransformResult> picker; - private final BiFunction<ITree<ContainedType>, Consumer<Iterator<ITree<ContainedType>>>, ITree<ContainedType>> transform; + /* + * The transform to apply to a given node. + */ + private final TreeTransform<ContainedType> transform; private ITree<ContainedType> preParent; private ITree<ContainedType> postParent; @@ -40,21 +59,20 @@ public class TopDownTransformIterator<ContainedType> implements Iterator<ITree<C private boolean initial; private final Deque<Iterator<ITree<ContainedType>>> toYield; - private Iterator<ITree<ContainedType>> curYield; + private Iterator<ITree<ContainedType>> currYield; /** * Create a new tree iterator. * * @param pickr - * The function to use to pick how to process nodes. + * The function to use to pick how to process nodes. * @param transfrm - * The transform to apply to the nodes. + * The transform to apply to the nodes. * @param tree - * The tree to transform. + * The tree to transform. */ public TopDownTransformIterator(final Function<ContainedType, TopDownTransformResult> pickr, - final BiFunction<ITree<ContainedType>, Consumer<Iterator<ITree<ContainedType>>>, ITree<ContainedType>> transfrm, - final ITree<ContainedType> tree) { + final TreeTransform<ContainedType> transfrm, final ITree<ContainedType> tree) { preParent = tree; preChildren = new LinkedList<>(); @@ -72,14 +90,14 @@ public class TopDownTransformIterator<ContainedType> implements Iterator<ITree<C * Add a set of nodes to yield. * * @param src - * The nodes to yield. + * The nodes to yield. */ public void addYield(final Iterator<ITree<ContainedType>> src) { - if(curYield != null) { - toYield.push(curYield); + if (currYield != null) { + toYield.push(currYield); } - curYield = src; + currYield = src; } @Override @@ -91,27 +109,35 @@ public class TopDownTransformIterator<ContainedType> implements Iterator<ITree<C * Get the next yielded value. * * @param val - * The sentinel value to yield. + * The sentinel value to yield. + * * @return The next yielded value. */ public ITree<ContainedType> flushYields(final ITree<ContainedType> val) { - if(curYield != null) { + if (currYield != null) { + /* + * We have non-sentinel values to yield. + */ + + /* + * Add the sentinel to yield later. + */ toYield.add(new SingleIterator<>(val)); - if(curYield.hasNext()) { - return curYield.next(); + if (currYield.hasNext()) { + return currYield.next(); } - while(toYield.size() != 0 && !curYield.hasNext()) { - curYield = toYield.pop(); + while (toYield.size() != 0 && !currYield.hasNext()) { + currYield = toYield.pop(); } - if(toYield.size() == 0 && !curYield.hasNext()) { - curYield = null; + if (toYield.size() == 0 && !currYield.hasNext()) { + currYield = null; return val; } - return curYield.next(); + return currYield.next(); } return val; @@ -119,33 +145,30 @@ public class TopDownTransformIterator<ContainedType> implements Iterator<ITree<C @Override public ITree<ContainedType> next() { - if(done) throw new NoSuchElementException(); - - if(curYield != null) { - if(curYield.hasNext()) { - return curYield.next(); - } - - while(toYield.size() != 0 && !curYield.hasNext()) { - curYield = toYield.pop(); - } - - if(toYield.size() == 0 && !curYield.hasNext()) { - curYield = null; - } else { - return curYield.next(); - } + if (done) + throw new NoSuchElementException(); + + /* + * Flush any values that need to be yielded. + */ + if (currYield != null) { + ITree<ContainedType> yeld = flushYields(null); + if (yeld != null) + return yeld; } - - if(initial) { + + if (initial) { + /* + * Get the way we are transforming. + */ final TopDownTransformResult res = picker.apply(preParent.getHead()); - switch(res) { + switch (res) { case PASSTHROUGH: postParent = new Tree<>(preParent.getHead()); - if(preParent.getChildrenCount() != 0) { - for(int i = 0; i < preParent.getChildrenCount(); i++) { + if (preParent.getChildrenCount() != 0) { + for (int i = 0; i < preParent.getChildrenCount(); i++) { preChildren.add(preParent.getChild(i)); } @@ -165,8 +188,8 @@ public class TopDownTransformIterator<ContainedType> implements Iterator<ITree<C preParent = transform.apply(preParent, this::addYield); return flushYields(preParent); case PUSHDOWN: - if(preParent.getChildrenCount() != 0) { - for(int i = 0; i < preParent.getChildrenCount(); i++) { + if (preParent.getChildrenCount() != 0) { + for (int i = 0; i < preParent.getChildrenCount(); i++) { preChildren.add(preParent.getChild(i)); } @@ -181,8 +204,8 @@ public class TopDownTransformIterator<ContainedType> implements Iterator<ITree<C postParent = new Tree<>(intRes.getHead()); - if(intRes.getChildrenCount() != 0) { - for(int i = 0; i < intRes.getChildrenCount(); i++) { + if (intRes.getChildrenCount() != 0) { + for (int i = 0; i < intRes.getChildrenCount(); i++) { preChildren.add(intRes.getChild(i)); } @@ -196,13 +219,13 @@ public class TopDownTransformIterator<ContainedType> implements Iterator<ITree<C throw new IllegalArgumentException("Unknown result type " + res); } - if(res != RTRANSFORM) { + if (res != RTRANSFORM) { initial = false; } } - if(curChild == null || !curChild.hasNext()) { - if(preChildren.size() != 0) { + if (curChild == null || !curChild.hasNext()) { + if (preChildren.size() != 0) { curChild = new TopDownTransformIterator<>(picker, transform, preChildren.pop()); final ITree<ContainedType> res = curChild.next(); @@ -214,12 +237,12 @@ public class TopDownTransformIterator<ContainedType> implements Iterator<ITree<C ITree<ContainedType> res = null; - if(postParent == null) { + if (postParent == null) { res = new Tree<>(preParent.getHead()); System.out.println("\t\tTRACE: adding nodes " + postChildren + " to " + res); - for(final ITree<ContainedType> child : postChildren) { + for (final ITree<ContainedType> child : postChildren) { res.addChild(child); } @@ -229,7 +252,7 @@ public class TopDownTransformIterator<ContainedType> implements Iterator<ITree<C res = postParent; System.out.println("\t\tTRACE: adding nodes " + postChildren + " to " + res); - for(final ITree<ContainedType> child : postChildren) { + for (final ITree<ContainedType> child : postChildren) { res.addChild(child); } } diff --git a/base/todos.txt b/base/todos.txt index 30da34e..d154630 100644 --- a/base/todos.txt +++ b/base/todos.txt @@ -1,2 +1,8 @@ @TODO 10/09/17 Ben Culkin :CLIArgsParsing Add some sort of library for handling command-line arguments + +@TODO 3/2/18 Benjamin Culkin :Examples + Add more examples for some of the more complex functionality + +@TODO 3/2/18 Benjamin Culkin :Testing + Add tests for some of the various features
\ No newline at end of file |
