From ed51142cd7abaaeedcb9ef1439f26690b57ce839 Mon Sep 17 00:00:00 2001 From: student Date: Fri, 2 Mar 2018 11:54:19 -0500 Subject: Update --- .../java/bjc/utils/examples/AbbrevMapTest.java | 46 ++++++-- .../bjc/utils/data/TopDownTransformIterator.java | 127 ++++++++++++--------- base/todos.txt | 6 + 3 files changed, 120 insertions(+), 59 deletions(-) (limited to 'base') 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 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 \tAdd a word to the abbreviation map"); + strm.println("\tremove \tRemove a word from the abbreviation map"); + strm.println("\trecalc \tRecalculate the abbreviation map"); + strm.println("\tcheck \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 - * The type of the nodes in the tree. + * The type of the nodes in the tree. */ public class TopDownTransformIterator implements Iterator> { + /** + * Alias type for a tree transformation. + * + * @author student + * + * @param + * The type contained in the tree. + */ + public interface TreeTransform + extends BiFunction, Consumer>>, ITree> { + + } + + /* + * The function that picks how to transform a given node + */ private final Function picker; - private final BiFunction, Consumer>>, ITree> transform; + /* + * The transform to apply to a given node. + */ + private final TreeTransform transform; private ITree preParent; private ITree postParent; @@ -40,21 +59,20 @@ public class TopDownTransformIterator implements Iterator>> toYield; - private Iterator> curYield; + private Iterator> 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 pickr, - final BiFunction, Consumer>>, ITree> transfrm, - final ITree tree) { + final TreeTransform transfrm, final ITree tree) { preParent = tree; preChildren = new LinkedList<>(); @@ -72,14 +90,14 @@ public class TopDownTransformIterator implements Iterator> 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 implements Iterator flushYields(final ITree 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 implements Iterator 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 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 implements Iterator implements Iterator(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 implements Iterator(picker, transform, preChildren.pop()); final ITree res = curChild.next(); @@ -214,12 +237,12 @@ public class TopDownTransformIterator implements Iterator 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 child : postChildren) { + for (final ITree child : postChildren) { res.addChild(child); } @@ -229,7 +252,7 @@ public class TopDownTransformIterator implements Iterator child : postChildren) { + for (final ITree 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 -- cgit v1.2.3