From 946cab444bc301d8a7c756a1bab039558288de89 Mon Sep 17 00:00:00 2001 From: "Benjamin J. Culkin" Date: Wed, 11 Oct 2017 13:41:07 -0300 Subject: Cleanup work --- .../src/main/java/bjc/utils/esodata/AbbrevMap.java | 80 +++----- .../src/main/java/bjc/utils/esodata/Directory.java | 54 ++--- .../main/java/bjc/utils/esodata/DoubleTape.java | 86 +------- .../main/java/bjc/utils/esodata/PushdownMap.java | 27 ++- .../main/java/bjc/utils/esodata/QueueStack.java | 11 +- .../java/bjc/utils/esodata/SimpleDirectory.java | 16 +- .../main/java/bjc/utils/esodata/SimpleStack.java | 9 +- .../main/java/bjc/utils/esodata/SingleTape.java | 93 ++------- .../java/bjc/utils/esodata/SpaghettiStack.java | 11 +- base/src/main/java/bjc/utils/esodata/Stack.java | 221 +++++++++++---------- base/src/main/java/bjc/utils/esodata/Tape.java | 47 +++-- .../main/java/bjc/utils/esodata/TapeChanger.java | 108 ++-------- .../main/java/bjc/utils/esodata/TapeLibrary.java | 112 ++--------- .../java/bjc/utils/esodata/UnifiedDirectory.java | 14 +- base/src/main/java/bjc/utils/esodata/todos.txt | 6 + 15 files changed, 318 insertions(+), 577 deletions(-) create mode 100644 base/src/main/java/bjc/utils/esodata/todos.txt (limited to 'base/src/main/java/bjc/utils/esodata') diff --git a/base/src/main/java/bjc/utils/esodata/AbbrevMap.java b/base/src/main/java/bjc/utils/esodata/AbbrevMap.java index 0d54471..5aa44fc 100644 --- a/base/src/main/java/bjc/utils/esodata/AbbrevMap.java +++ b/base/src/main/java/bjc/utils/esodata/AbbrevMap.java @@ -14,37 +14,28 @@ import bjc.utils.funcdata.IMap; * Represents a mapping from a set of strings to a mapping of all unambiguous * prefixes of their respective strings. * - * This works the same as Ruby's Abbrev. + * This works the same as Ruby's Abbrev module. * * @author EVE - * */ public class AbbrevMap { - /* - * All of the words we have abbreviations for. - */ + /* All of the words we have abbreviations for. */ private final Set wrds; - /* - * Maps abbreviations to their strings. - */ + /* Maps abbreviations to their strings. */ private IMap abbrevMap; - /* - * Counts how many times we've seen a substring. - */ + /* Counts how many times we've seen a substring. */ private Set seen; - /* - * Maps ambiguous abbreviations to the strings they could be. - */ + /* Maps ambiguous abbreviations to the strings they could be. */ private SetMultimap ambMap; /** * Create a new abbreviation map. * * @param words - * The initial set of words to put in the map. + * The initial set of words to put in the map. */ public AbbrevMap(final String... words) { wrds = new HashSet<>(Arrays.asList(words)); @@ -54,6 +45,9 @@ public class AbbrevMap { /** * Recalculate all the abbreviations in this map. + * + * This may be needed after certain operations to ensure that all of the + * results are correct. */ public void recalculate() { abbrevMap = new FunctionalMap<>(); @@ -63,11 +57,6 @@ public class AbbrevMap { seen = new HashSet<>(); for (final String word : wrds) { - /* - * A word always abbreviates to itself. - */ - abbrevMap.put(word, word); - intAddWord(word); } } @@ -76,33 +65,25 @@ public class AbbrevMap { * Adds words to the abbreviation map. * * @param words - * The words to add to the abbreviation map. + * The words to add to the abbreviation map. */ public void addWords(final String... words) { wrds.addAll(Arrays.asList(words)); for (final String word : words) { - /* - * A word always abbreviates to itself. - */ - abbrevMap.put(word, word); - intAddWord(word); } } - /* - * Actually add abbreviations of a word. - */ + /* Actually add abbreviations of a word. */ private void intAddWord(final String word) { - /* - * Skip blank words. - */ + /* A word always abbreviates to itself. */ + abbrevMap.put(word, word); + + /* Skip blank words. */ if (word.equals("")) return; - /* - * Handle each possible abbreviation. - */ + /* Handle each possible abbreviation. */ for (int i = word.length(); i > 0; i--) { final String subword = word.substring(0, i); @@ -134,7 +115,7 @@ public class AbbrevMap { * the map. Use {@link AbbrevMap#recalculate()} to fix it if it occurs. * * @param words - * The words to remove. + * The words to remove. */ public void removeWords(final String... words) { wrds.removeAll(Arrays.asList(words)); @@ -144,18 +125,12 @@ public class AbbrevMap { } } - /* - * Actually remove a word. - */ + /* Actually remove a word. */ private void intRemoveWord(final String word) { - /* - * Skip blank words. - */ + /* Skip blank words. */ if (word.equals("")) return; - /* - * Handle each possible abbreviation. - */ + /* Handle each possible abbreviation. */ for (int i = word.length(); i > 0; i--) { final String subword = word.substring(0, i); @@ -169,6 +144,10 @@ public class AbbrevMap { if (possWords.size() == 0) { seen.remove(subword); } else if (possWords.size() == 1) { + /* + * An abbreviation went from ambiguous + * to non-ambiguous. + */ final String newWord = possWords.iterator().next(); abbrevMap.put(subword, newWord); @@ -183,14 +162,17 @@ public class AbbrevMap { * into. * * @param abbrev - * The abbreviation to convert. + * The abbreviation to convert. * - * @return All the expansions for the provided abbreviation. + * @return + * All the expansions for the provided abbreviation. */ public String[] deabbrev(final String abbrev) { - if (abbrevMap.containsKey(abbrev)) + if (abbrevMap.containsKey(abbrev)) { return new String[] { abbrevMap.get(abbrev) }; - else return ambMap.get(abbrev).toArray(new String[0]); + } else { + return ambMap.get(abbrev).toArray(new String[0]); + } } @Override diff --git a/base/src/main/java/bjc/utils/esodata/Directory.java b/base/src/main/java/bjc/utils/esodata/Directory.java index 17b70f5..4b25cda 100644 --- a/base/src/main/java/bjc/utils/esodata/Directory.java +++ b/base/src/main/java/bjc/utils/esodata/Directory.java @@ -7,21 +7,22 @@ package bjc.utils.esodata; * be able to ensure that they can't write outside of it. * * @param - * The key type of the map. + * The key type of the map. * @param - * The value type of the map. + * The value type of the map. */ public interface Directory { /** * Retrieves a given sub-directory. * * @param key - * The key to retrieve the sub-directory for. + * The key to retrieve the sub-directory for. * - * @return The sub-directory under that name. + * @return + * The sub-directory under that name. * * @throws IllegalArgumentException - * If the given sub-directory doesn't exist. + * If the given sub-directory doesn't exist. */ Directory getSubdirectory(K key); @@ -29,9 +30,10 @@ public interface Directory { * Check if a given sub-directory exists. * * @param key - * The key to look for the sub-directory under. + * The key to look for the sub-directory under. * - * @return Whether or not a sub-directory of that name exists. + * @return + * Whether or not a sub-directory of that name exists. */ boolean hasSubdirectory(K key); @@ -39,12 +41,13 @@ public interface Directory { * Insert a sub-directory into the dictionary. * * @param key - * The name of the new sub-directory + * The name of the new sub-directory * @param value - * The sub-directory to insert + * The sub-directory to insert * - * @return The old sub-directory attached to this key, or null if such a - * sub-directory didn't exist + * @return + * The old sub-directory attached to this key, or null if such a + * sub-directory didn't exist */ Directory putSubdirectory(K key, Directory value); @@ -54,10 +57,11 @@ public interface Directory { * Will fail if a sub-directory of that name already exists. * * @param key - * The name of the new sub-directory. + * The name of the new sub-directory. * - * @return The new sub-directory, or null if one by that name already - * exists. + * @return + * The new sub-directory, or null if one by that name already + * exists. */ default Directory newSubdirectory(final K key) { if (hasSubdirectory(key)) return null; @@ -73,9 +77,10 @@ public interface Directory { * Check if the directory contains a data-item under the given key. * * @param key - * The key to check for. + * The key to check for. * - * @return Whether or not there is a data item for the given key. + * @return + * Whether or not there is a data item for the given key. */ boolean containsKey(K key); @@ -83,12 +88,13 @@ public interface Directory { * Retrieve a given data-item from the directory. * * @param key - * The key to retrieve data for. + * The key to retrieve data for. * - * @return The value for the given key. + * @return + * The value for the given key. * * @throws IllegalArgumentException - * If no value exists for the given key. + * If no value exists for the given key. */ V getKey(K key); @@ -96,11 +102,13 @@ public interface Directory { * Insert a data-item into the directory. * * @param key - * The key to insert into. + * The key to insert into. + * * @param val - * The value to insert. + * The value to insert. * - * @return The old value of key, or null if such a value didn't exist. + * @return + * The old value of key, or null if such a value didn't exist. */ V putKey(K key, V val); -} \ No newline at end of file +} diff --git a/base/src/main/java/bjc/utils/esodata/DoubleTape.java b/base/src/main/java/bjc/utils/esodata/DoubleTape.java index 5c463c6..a0031ec 100644 --- a/base/src/main/java/bjc/utils/esodata/DoubleTape.java +++ b/base/src/main/java/bjc/utils/esodata/DoubleTape.java @@ -20,16 +20,17 @@ package bjc.utils.esodata; * Flip refers to the entire tape for 'obvious' reasons. * * @param - * The element type of the tape. + * The element type of the tape. + * * @author bjculkin */ public class DoubleTape implements Tape { + /* The front-side of the tape. */ private Tape front; + /* The back-side of the tape. */ private Tape back; - /** - * Create a new empty double-sided tape that doesn't autoextend. - */ + /** Create a new empty double-sided tape that doesn't autoextend. */ public DoubleTape() { this(false); } @@ -39,40 +40,23 @@ public class DoubleTape implements Tape { * auto-extension policy. * * @param autoExtnd - * Whether or not to auto-extend the tape to the right w/ - * nulls. + * Whether or not to auto-extend the tape to the right w/ nulls. */ public DoubleTape(final boolean autoExtnd) { front = new SingleTape<>(autoExtnd); back = new SingleTape<>(autoExtnd); } - /** - * Get the item the tape is currently on. - * - * @return The item the tape is on. - */ @Override public T item() { return front.item(); } - /** - * Set the item the tape is currently on. - * - * @param itm - * The new value for the tape item. - */ @Override public void item(final T itm) { front.item(itm); } - /** - * Get the current number of elements in the tape. - * - * @return The current number of elements in the tape. - */ @Override public int size() { return front.size(); @@ -83,35 +67,18 @@ public class DoubleTape implements Tape { return front.position(); } - /** - * Insert an element before the current item. - * - * @param itm - * The item to add. - */ @Override public void insertBefore(final T itm) { front.insertBefore(itm); back.insertAfter(null); } - /** - * Insert an element after the current item. - */ @Override public void insertAfter(final T itm) { front.insertAfter(itm); back.insertBefore(itm); } - /** - * Remove the current element. - * - * Also moves the cursor back one step if possible to maintain relative - * position, and removes the corresponding item from the non-active side - * - * @return The removed item from the active side. - */ @Override public T remove() { back.remove(); @@ -119,47 +86,23 @@ public class DoubleTape implements Tape { return front.remove(); } - /** - * Move the cursor to the left-most position. - */ @Override public void first() { front.first(); back.last(); } - /** - * Move the cursor the right-most position. - */ @Override public void last() { front.last(); back.first(); } - /** - * Move the cursor one space left. - * - * The cursor can't go past zero. - * - * @return True if the cursor was moved left. - */ @Override public boolean left() { return left(1); } - /** - * Move the cursor the specified amount left. - * - * The cursor can't go past zero. Attempts to move the cursor by amounts - * that would exceed zero don't move the cursor at all. - * - * @param amt - * The amount to attempt to move the cursor left. - * - * @return True if the cursor was moved left. - */ @Override public boolean left(final int amt) { final boolean succ = front.left(amt); @@ -171,28 +114,11 @@ public class DoubleTape implements Tape { return succ; } - /** - * Move the cursor one space right. - * - * Moving the cursor right will auto-extend the tape if that is enabled. - * - * @return Whether the cursor was moved right. - */ @Override public boolean right() { return right(1); } - /** - * Move the cursor the specified amount right. - * - * Moving the cursor right will auto-extend the tape if that is enabled. - * - * @param amt - * The amount to move the cursor right by. - * - * @return Whether the cursor was moved right. - */ @Override public boolean right(final int amt) { final boolean succ = front.right(amt); diff --git a/base/src/main/java/bjc/utils/esodata/PushdownMap.java b/base/src/main/java/bjc/utils/esodata/PushdownMap.java index a631704..18a9b46 100644 --- a/base/src/main/java/bjc/utils/esodata/PushdownMap.java +++ b/base/src/main/java/bjc/utils/esodata/PushdownMap.java @@ -12,23 +12,26 @@ import bjc.utils.funcdata.IMap; * A variant of a map where inserting a duplicate key shadows the existing value * instead of replacing it. * + * This could be useful for things like variable scopes. + * * @author EVE * * @param - * The key of the map. + * The key of the map. + * * @param - * The values in the map. + * The values in the map. */ public class PushdownMap implements IMap { + /* Our backing storage. */ private final IMap> backing; - /** - * Create a new empty stack-based map. - */ + /** Create a new empty stack-based map. */ public PushdownMap() { backing = new FunctionalMap<>(); } + /** Create a new empty stack-based map using the specified backing. */ private PushdownMap(final IMap> back) { backing = back; } @@ -80,6 +83,12 @@ public class PushdownMap implements IMap @Override public IMap transform(final Function transformer) { + /* + * @NOTE + * Can and should we support this? + * More to the point, maybe this should be a map sub-type + * that does what it needs to? + */ throw new UnsupportedOperationException("Cannot transform pushdown maps."); } @@ -106,14 +115,16 @@ public class PushdownMap implements IMap public ValueType remove(final KeyType key) { final Stack stk = backing.get(key); - if (stk.size() > 1) + if (stk.size() > 1) { return stk.pop(); - else return backing.remove(key).top(); + } else { + return backing.remove(key).top(); + } } @Override public IList valueList() { - return backing.valueList().map(stk -> stk.top()); + return backing.valueList().map(Stack::top); } @Override diff --git a/base/src/main/java/bjc/utils/esodata/QueueStack.java b/base/src/main/java/bjc/utils/esodata/QueueStack.java index 850598a..be393a3 100644 --- a/base/src/main/java/bjc/utils/esodata/QueueStack.java +++ b/base/src/main/java/bjc/utils/esodata/QueueStack.java @@ -5,18 +5,19 @@ import java.util.LinkedList; /** * A FIFO implementation of a stack. + * + * Basically, a stack that actually acts like a queue. * * @param - * The datatype stored in the stack. + * The datatype stored in the stack. + * * @author Ben Culkin */ public class QueueStack extends Stack { + /* Our backing queue. */ private final Deque backing; - /** - * Create a new empty stack queue. - * - */ + /** Create a new empty stack queue. */ public QueueStack() { backing = new LinkedList<>(); } diff --git a/base/src/main/java/bjc/utils/esodata/SimpleDirectory.java b/base/src/main/java/bjc/utils/esodata/SimpleDirectory.java index 69fd019..5e7f480 100644 --- a/base/src/main/java/bjc/utils/esodata/SimpleDirectory.java +++ b/base/src/main/java/bjc/utils/esodata/SimpleDirectory.java @@ -11,21 +11,21 @@ import bjc.utils.funcdata.IMap; * @author EVE * * @param - * The key type of the directory. + * The key type of the directory. + * * @param - * The value type of the directory. + * The value type of the directory. */ public class SimpleDirectory implements Directory { + /* Our sub-directories. */ private final IMap> children; - + /* Our data. */ private final IMap data; - /** - * Create a new directory. - */ + /** Create a new directory. */ public SimpleDirectory() { children = new FunctionalMap<>(); - data = new FunctionalMap<>(); + data = new FunctionalMap<>(); } @Override @@ -92,4 +92,4 @@ public class SimpleDirectory implements Directory { public String toString() { return String.format("SimpleDirectory [children=%s, data=%s]", children, data); } -} \ No newline at end of file +} diff --git a/base/src/main/java/bjc/utils/esodata/SimpleStack.java b/base/src/main/java/bjc/utils/esodata/SimpleStack.java index fdb3300..72fb343 100644 --- a/base/src/main/java/bjc/utils/esodata/SimpleStack.java +++ b/base/src/main/java/bjc/utils/esodata/SimpleStack.java @@ -7,16 +7,15 @@ import java.util.LinkedList; * Simple implementation of a stack. * * @param - * The datatype stored in the stack. + * The datatype stored in the stack. + * * @author Ben Culkin */ public class SimpleStack extends Stack { + /* Our backing stack. */ private final Deque backing; - /** - * Create a new empty stack. - * - */ + /** Create a new empty stack. */ public SimpleStack() { backing = new LinkedList<>(); } diff --git a/base/src/main/java/bjc/utils/esodata/SingleTape.java b/base/src/main/java/bjc/utils/esodata/SingleTape.java index c50be92..c7bf6ee 100644 --- a/base/src/main/java/bjc/utils/esodata/SingleTape.java +++ b/base/src/main/java/bjc/utils/esodata/SingleTape.java @@ -15,14 +15,21 @@ import java.util.ArrayList; * policy. * * @param - * The element type of the tape. + * The element type of the tape. * * @author bjculkin */ public class SingleTape implements Tape { + /* @NOTE + * Does this stuff still need to be protected? We're not trying to + * use inheritance to implement tape types any more, so I don't see + * any reason to not have it be private. + */ + /* Our backing store. */ protected ArrayList backing; + /* Our position in the list. */ protected int pos; - + /* Whether to auto-extend the list on the left with nulls. */ protected boolean autoExtend; /** @@ -38,9 +45,8 @@ public class SingleTape implements Tape { backing.add(val); } } - /** - * Create a new empty tape that doesn't autoextend. - */ + + /** Create a new empty tape that doesn't autoextend. */ public SingleTape() { this(false); } @@ -50,8 +56,7 @@ public class SingleTape implements Tape { * policy. * * @param autoExtnd - * Whether or not to auto-extend the tape to the right w/ - * nulls. + * Whether or not to auto-extend the tape to the right w/ nulls. */ public SingleTape(final boolean autoExtnd) { autoExtend = autoExtnd; @@ -59,32 +64,16 @@ public class SingleTape implements Tape { backing = new ArrayList<>(); } - /** - * Get the item the tape is currently on. - * - * @return The item the tape is on. - */ @Override public T item() { return backing.get(pos); } - /** - * Set the item the tape is currently on. - * - * @param itm - * The new value for the tape item. - */ @Override public void item(final T itm) { backing.set(pos, itm); } - /** - * Get the current number of elements in the tape. - * - * @return The current number of elements in the tape. - */ @Override public int size() { return backing.size(); @@ -95,20 +84,11 @@ public class SingleTape implements Tape { return pos; } - /** - * Insert an element before the current item. - * - * @param itm - * The item to add. - */ @Override public void insertBefore(final T itm) { backing.add(pos, itm); } - /** - * Insert an element after the current item. - */ @Override public void insertAfter(final T itm) { if (pos == backing.size() - 1) { @@ -118,14 +98,6 @@ public class SingleTape implements Tape { } } - /** - * Remove the current element. - * - * Also moves the cursor back one step if possible to maintain relative - * position. - * - * @return The removed item. - */ @Override public T remove() { final T res = backing.remove(pos); @@ -135,45 +107,21 @@ public class SingleTape implements Tape { return res; } - /** - * Move the cursor to the left-most position. - */ @Override public void first() { pos = 0; } - /** - * Move the cursor the right-most position. - */ @Override public void last() { pos = backing.size() - 1; } - /** - * Move the cursor one space left. - * - * The cursor can't go past zero. - * - * @return True if the cursor was moved left. - */ @Override public boolean left() { return left(1); } - /** - * Move the cursor the specified amount left. - * - * The cursor can't go past zero. Attempts to move the cursor by amounts - * that would exceed zero don't move the cursor at all. - * - * @param amt - * The amount to attempt to move the cursor left. - * - * @return True if the cursor was moved left. - */ @Override public boolean left(final int amt) { if (pos - amt < 0) return false; @@ -182,28 +130,11 @@ public class SingleTape implements Tape { return true; } - /** - * Move the cursor one space right. - * - * Moving the cursor right will auto-extend the tape if that is enabled. - * - * @return Whether the cursor was moved right. - */ @Override public boolean right() { return right(1); } - /** - * Move the cursor the specified amount right. - * - * Moving the cursor right will auto-extend the tape if that is enabled. - * - * @param amt - * The amount to move the cursor right by. - * - * @return Whether the cursor was moved right. - */ @Override public boolean right(final int amt) { if (pos + amt >= backing.size() - 1) { diff --git a/base/src/main/java/bjc/utils/esodata/SpaghettiStack.java b/base/src/main/java/bjc/utils/esodata/SpaghettiStack.java index 7c8c757..f445f75 100644 --- a/base/src/main/java/bjc/utils/esodata/SpaghettiStack.java +++ b/base/src/main/java/bjc/utils/esodata/SpaghettiStack.java @@ -3,23 +3,26 @@ package bjc.utils.esodata; import java.util.Arrays; import java.util.stream.Stream; -/* +/** * Implements a spaghetti stack, which is a stack that is branched off of a * parent stack. * - * @param T The datatype stored in the stack. + * @param + * The datatype stored in the stack. + * * @author Ben Culkin */ class SpaghettiStack extends Stack { + /* Our backing stack. */ private final Stack backing; - + /* The stack we branched off of. */ private final Stack parent; /** * Create a new empty spaghetti stack, off of the specified parent. * * @param par - * The parent stack + * The parent stack */ public SpaghettiStack(final Stack par) { backing = new SimpleStack<>(); diff --git a/base/src/main/java/bjc/utils/esodata/Stack.java b/base/src/main/java/bjc/utils/esodata/Stack.java index 9d74e9a..9bb61dc 100644 --- a/base/src/main/java/bjc/utils/esodata/Stack.java +++ b/base/src/main/java/bjc/utils/esodata/Stack.java @@ -4,10 +4,12 @@ import java.util.ArrayList; import java.util.List; import java.util.function.Consumer; +/* + * @TODO 10/11/17 Ben Culkin :StackCombinators + * Implement more combinators for the stack. + */ /** - * A stack, with support for combinators. - * - * A FILO stack with support for forth/factor style combinators. + * A stack, with support for forth/factor style stack combinators. * *

*

Stack underflow

@@ -19,7 +21,7 @@ import java.util.function.Consumer; *

* * @param - * The datatype stored in the stack. + * The datatype stored in the stack. * * @author Ben Culkin */ @@ -29,13 +31,9 @@ public abstract class Stack { * stack that isn't there. * * @author EVE - * */ public static class StackUnderflowException extends RuntimeException { - - /** - * - */ + /* The ID of the exception */ private static final long serialVersionUID = 1423867176204571539L; } @@ -43,14 +41,15 @@ public abstract class Stack { * Push an element onto the stack. * * @param elm - * The element to insert. + * The element to insert. */ public abstract void push(T elm); /** * Pop an element off of the stack. * - * @return The element on top of the stack. + * @return + * The element on top of the stack. */ public abstract T pop(); @@ -58,28 +57,32 @@ public abstract class Stack { * Retrieve the top element of this stack without removing it from the * stack. * - * @return The top element of this stack. + * @return + * The top element of this stack. */ public abstract T top(); /** * Get the number of elements in the stack. * - * @return the number of elements in the stack. + * @return + * the number of elements in the stack. */ public abstract int size(); /** * Check if the stack is empty. * - * @return Whether or not the stack is empty. + * @return + * Whether or not the stack is empty. */ public abstract boolean empty(); /** * Create a spaghetti stack branching off of this one. * - * @return A spaghetti stack with this stack as a parent. + * @return + * A spaghetti stack with this stack as a parent. */ public Stack spaghettify() { return new SpaghettiStack<>(this); @@ -93,7 +96,7 @@ public abstract class Stack { * Drop n items from the stack. * * @param n - * The number of items to drop. + * The number of items to drop. */ public void drop(final int n) { for (int i = 0; i < n; i++) { @@ -101,9 +104,7 @@ public abstract class Stack { } } - /** - * Drop one item from the stack. - */ + /** Drop one item from the stack. */ public void drop() { drop(1); } @@ -112,7 +113,7 @@ public abstract class Stack { * Delete n items below the current one. * * @param n - * The number of items below the top to delete. + * The number of items below the top to delete. */ public void nip(final int n) { final T elm = pop(); @@ -122,9 +123,7 @@ public abstract class Stack { push(elm); } - /** - * Delete the second element in the stack. - */ + /** Delete the second element in the stack. */ public void nip() { nip(1); } @@ -133,9 +132,10 @@ public abstract class Stack { * Replicate the top n items of the stack m times. * * @param n - * The number of items to duplicate. + * The number of items to duplicate. + * * @param m - * The number of times to duplicate items. + * The number of times to duplicate items. */ public void multidup(final int n, final int m) { final List lst = new ArrayList<>(n); @@ -155,15 +155,13 @@ public abstract class Stack { * Duplicate the top n items of the stack. * * @param n - * The number of items to duplicate. + * The number of items to duplicate. */ public void dup(final int n) { multidup(n, 2); } - /** - * Duplicate the top item on the stack. - */ + /** Duplicate the top item on the stack. */ public void dup() { dup(1); } @@ -172,9 +170,10 @@ public abstract class Stack { * Replicate the n elements below the top one m times. * * @param n - * The number of items to duplicate. + * The number of items to duplicate. + * * @param m - * The number of times to duplicate items. + * The number of times to duplicate items. */ public void multiover(final int n, final int m) { final List lst = new ArrayList<>(n); @@ -201,22 +200,18 @@ public abstract class Stack { * Duplicate the n elements below the top one. * * @param n - * The number of items to duplicate. + * The number of items to duplicate. */ public void over(final int n) { multiover(n, 2); } - /** - * Duplicate the second item in the stack. - */ + /** Duplicate the second item in the stack. */ public void over() { over(1); } - /** - * Duplicate the third item in the stack. - */ + /** Duplicate the third item in the stack. */ public void pick() { final T z = pop(); final T y = pop(); @@ -228,9 +223,7 @@ public abstract class Stack { push(x); } - /** - * Swap the top two items on the stack. - */ + /** Swap the top two items on the stack. */ public void swap() { final T y = pop(); final T x = pop(); @@ -239,9 +232,7 @@ public abstract class Stack { push(x); } - /** - * Duplicate the second item below the first item. - */ + /** Duplicate the second item below the first item. */ public void deepdup() { final T y = pop(); final T x = pop(); @@ -251,9 +242,7 @@ public abstract class Stack { push(y); } - /** - * Swap the second and third items in the stack. - */ + /** Swap the second and third items in the stack. */ public void deepswap() { final T z = pop(); final T y = pop(); @@ -264,9 +253,7 @@ public abstract class Stack { push(z); } - /** - * Rotate the top three items on the stack - */ + /** Rotate the top three items on the stack */ public void rot() { final T z = pop(); final T y = pop(); @@ -277,9 +264,7 @@ public abstract class Stack { push(x); } - /** - * Inversely rotate the top three items on the stack - */ + /** Inversely rotate the top three items on the stack */ public void invrot() { final T z = pop(); final T y = pop(); @@ -290,25 +275,32 @@ public abstract class Stack { push(y); } + /* + * :StackCombinators + * Add a general rotate/roll operator. + */ + /* * Dataflow Combinators */ + /** - * Hides the top n elements on the stack from cons. + * Hides the top n elements on the stack from an action. * * @param n - * The number of elements to hide. - * @param cons - * The action to hide the elements from + * The number of elements to hide. + * + * @param action + * The action to hide the elements from */ - public void dip(final int n, final Consumer> cons) { + public void dip(final int n, final Consumer> action) { final List elms = new ArrayList<>(n); for (int i = n; i > 0; i--) { elms.set(i - 1, pop()); } - cons.accept(this); + action.accept(this); for (final T elm : elms) { push(elm); @@ -316,75 +308,82 @@ public abstract class Stack { } /** - * Hide the top element of the stack from cons. + * Hide the top element of the stack from an action. * - * @param cons - * The action to hide the top from + * @param action + * The action to hide the top from */ - public void dip(final Consumer> cons) { - dip(1, cons); + public void dip(final Consumer> action) { + dip(1, action); } /** - * Copy the top n elements on the stack, replacing them once cons is + * Copy the top n elements on the stack, replacing them once an action is * done. * * @param n - * The number of elements to copy. - * @param cons - * The action to execute. + * The number of elements to copy. + * + * @param action + * The action to execute. */ - public void keep(final int n, final Consumer> cons) { + public void keep(final int n, final Consumer> action) { + /* + * @NOTE + * Is this correct? + */ dup(n); - dip(n, cons); + dip(n, action); } /** - * Apply all the actions in conses to the top n elements of the stack. + * Apply all the actions in a list to the top n elements of the stack. * * @param n - * The number of elements to give to cons. - * @param conses - * The actions to execute. + * The number of elements to give to cons. + * + * @param actions + * The actions to execute. */ - public void multicleave(final int n, final List>> conses) { + public void multicleave(final int n, final List>> actions) { final List elms = new ArrayList<>(n); for (int i = n; i > 0; i--) { elms.set(i - 1, pop()); } - for (final Consumer> cons : conses) { + for (final Consumer> action : actions) { for (final T elm : elms) { push(elm); } - cons.accept(this); + action.accept(this); } } /** - * Apply all the actions in conses to the top element of the stack. + * Apply all the actions in a list to the top element of the stack. * - * @param conses - * The actions to execute. + * @param actions + * The actions to execute. */ - public void cleave(final List>> conses) { - multicleave(1, conses); + public void cleave(final List>> actions) { + multicleave(1, actions); } /** - * Apply every action in cons to n arguments. + * Apply every action in a list of actions to n arguments. * * @param n - * The number of parameters each action takes. - * @param conses - * The actions to execute. + * The number of parameters each action takes. + * + * @param actions + * The actions to execute. */ - public void multispread(final int n, final List>> conses) { - final List> nelms = new ArrayList<>(conses.size()); + public void multispread(final int n, final List>> actions) { + final List> nelms = new ArrayList<>(actions.size()); - for (int i = conses.size(); i > 0; i--) { + for (int i = actions.size(); i > 0; i--) { final List elms = new ArrayList<>(n); for (int j = n; j > 0; j--) { @@ -400,60 +399,66 @@ public abstract class Stack { push(elm); } - conses.get(i).accept(this); + actions.get(i).accept(this); i += 1; } } /** - * Apply the actions in cons to corresponding elements from the stack. + * Apply the actions in a list of actions to corresponding elements from + * the stack. * * @param conses - * The actions to execute. + * The actions to execute. */ public void spread(final List>> conses) { multispread(1, conses); } /** - * Apply the action in cons to the first m groups of n arguments. + * Apply an action to the first m groups of n arguments. * * @param n - * The number of arguments cons takes. + * The number of arguments cons takes. + * * @param m - * The number of time to call cons. - * @param cons - * The action to execute. + * The number of time to call cons. + * + * @param action + * The action to execute. */ - public void multiapply(final int n, final int m, final Consumer> cons) { - final List>> conses = new ArrayList<>(m); + public void multiapply(final int n, final int m, final Consumer> action) { + final List>> actions = new ArrayList<>(m); for (int i = 0; i < m; i++) { - conses.add(cons); + actions.add(action); } - multispread(n, conses); + multispread(n, actions); } /** - * Apply cons n times to the corresponding elements in the stack. + * Apply an action n times to the corresponding elements in the stack. * * @param n - * The number of times to execute cons. - * @param cons - * The action to execute. + * The number of times to execute cons. + * + * @param action + * The action to execute. */ - public void apply(final int n, final Consumer> cons) { - multiapply(1, n, cons); + public void apply(final int n, final Consumer> action) { + multiapply(1, n, action); } /* * Misc. functions */ + /** * Get an array representing this stack. * - * @return The stack as an array. + * @return + * The stack as an array. */ public abstract T[] toArray(); } diff --git a/base/src/main/java/bjc/utils/esodata/Tape.java b/base/src/main/java/bjc/utils/esodata/Tape.java index b6a2c01..dab027f 100644 --- a/base/src/main/java/bjc/utils/esodata/Tape.java +++ b/base/src/main/java/bjc/utils/esodata/Tape.java @@ -8,7 +8,7 @@ package bjc.utils.esodata; * unbounded to the right, but in practice bounded by available memory. * * @param - * The element type of the tape. + * The element type of the tape. * * @author bjculkin */ @@ -16,7 +16,8 @@ public interface Tape { /** * Get the item the tape is currently on. * - * @return The item the tape is on. + * @return + * The item the tape is on. */ T item(); @@ -24,21 +25,23 @@ public interface Tape { * Set the item the tape is currently on. * * @param itm - * The new value for the tape item. + * The new value for the tape item. */ void item(T itm); /** * Get the current number of elements in the tape. * - * @return The current number of elements in the tape. + * @return + * The current number of elements in the tape. */ int size(); /** * Get the position of the current item. * - * @return The position of the current item. + * @return + * The position of the current item. */ int position(); @@ -46,7 +49,7 @@ public interface Tape { * Insert an element before the current item. * * @param itm - * The item to add. + * The item to add. */ void insertBefore(T itm); @@ -54,7 +57,7 @@ public interface Tape { * Insert an element after the current item. * * @param itm - * The item to insert. + * The item to insert. */ void insertAfter(T itm); @@ -64,18 +67,15 @@ public interface Tape { * Also moves the cursor back one step if possible to maintain relative * position. * - * @return The removed item. + * @return + * The removed item. */ T remove(); - /** - * Move the cursor to the left-most position. - */ + /** Move the cursor to the left-most position. */ void first(); - /** - * Move the cursor the right-most position. - */ + /** Move the cursor to the right-most position. */ void last(); /** @@ -83,7 +83,8 @@ public interface Tape { * * The cursor can't go past zero. * - * @return True if the cursor was moved left. + * @return + * True if the cursor was moved left. */ boolean left(); @@ -94,16 +95,18 @@ public interface Tape { * that would exceed zero don't move the cursor at all. * * @param amt - * The amount to attempt to move the cursor left. + * The amount to attempt to move the cursor left. * - * @return True if the cursor was moved left. + * @return + * True if the cursor was moved left. */ boolean left(int amt); /** * Move the cursor one space right. * - * @return Whether the cursor was moved right. + * @return + * Whether the cursor was moved right. */ boolean right(); @@ -111,16 +114,18 @@ public interface Tape { * Move the cursor the specified amount right. * * @param amt - * The amount to move the cursor right by. + * The amount to move the cursor right by. * - * @return Whether the cursor was moved right. + * @return + * Whether the cursor was moved right. */ boolean right(int amt); /** * Is this tape double sided? * - * @return Whether or not this tape is double-sided. + * @return + * Whether or not this tape is double-sided. */ boolean isDoubleSided(); } diff --git a/base/src/main/java/bjc/utils/esodata/TapeChanger.java b/base/src/main/java/bjc/utils/esodata/TapeChanger.java index dc885bc..08f56c1 100644 --- a/base/src/main/java/bjc/utils/esodata/TapeChanger.java +++ b/base/src/main/java/bjc/utils/esodata/TapeChanger.java @@ -10,15 +10,15 @@ package bjc.utils.esodata; * either return null/false. * * @param - * The element type of the tapes. + * The element type of the tapes. */ public class TapeChanger implements Tape { - private Tape> tapes; - private Tape currentTape; + /* Our list of tapes. */ + private Tape> tapes; + /* The current tape. */ + private Tape currentTape; - /** - * Create a new empty tape changer. - */ + /** Create a new empty tape changer. */ public TapeChanger() { tapes = new SingleTape<>(); } @@ -27,9 +27,10 @@ public class TapeChanger implements Tape { * Create a new tape changer with the specified tapes. * * @param current - * The tape to mount first. + * The tape to mount first. + * * @param others - * The tapes to put in this tape changer. + * The tapes to put in this tape changer. */ @SafeVarargs public TapeChanger(final Tape current, final Tape... others) { @@ -46,11 +47,6 @@ public class TapeChanger implements Tape { currentTape = tapes.item(); } - /** - * Get the item the tape is currently on. - * - * @return The item the tape is on. - */ @Override public T item() { if (currentTape == null) return null; @@ -58,12 +54,6 @@ public class TapeChanger implements Tape { return currentTape.item(); } - /** - * Set the item the tape is currently on. - * - * @param itm - * The new value for the tape item. - */ @Override public void item(final T itm) { if (currentTape == null) return; @@ -71,11 +61,6 @@ public class TapeChanger implements Tape { currentTape.item(itm); } - /** - * Get the current number of elements in the tape. - * - * @return The current number of elements in the tape. - */ @Override public int size() { if (currentTape == null) return 0; @@ -90,12 +75,6 @@ public class TapeChanger implements Tape { return currentTape.position(); } - /** - * Insert an element before the current item. - * - * @param itm - * The item to add. - */ @Override public void insertBefore(final T itm) { if (currentTape == null) return; @@ -103,9 +82,6 @@ public class TapeChanger implements Tape { currentTape.insertBefore(itm); } - /** - * Insert an element after the current item. - */ @Override public void insertAfter(final T itm) { if (currentTape == null) return; @@ -113,14 +89,6 @@ public class TapeChanger implements Tape { currentTape.insertAfter(itm); } - /** - * Remove the current element. - * - * Also moves the cursor back one step if possible to maintain relative - * position, and removes the corresponding item from the non-active side - * - * @return The removed item from the active side. - */ @Override public T remove() { if (currentTape == null) return null; @@ -128,9 +96,6 @@ public class TapeChanger implements Tape { return currentTape.remove(); } - /** - * Move the cursor to the left-most position. - */ @Override public void first() { if (currentTape == null) return; @@ -138,9 +103,6 @@ public class TapeChanger implements Tape { currentTape.first(); } - /** - * Move the cursor the right-most position. - */ @Override public void last() { if (currentTape == null) return; @@ -148,29 +110,11 @@ public class TapeChanger implements Tape { currentTape.last(); } - /** - * Move the cursor one space left. - * - * The cursor can't go past zero. - * - * @return True if the cursor was moved left. - */ @Override public boolean left() { return left(1); } - /** - * Move the cursor the specified amount left. - * - * The cursor can't go past zero. Attempts to move the cursor by amounts - * that would exceed zero don't move the cursor at all. - * - * @param amt - * The amount to attempt to move the cursor left. - * - * @return True if the cursor was moved left. - */ @Override public boolean left(final int amt) { if (currentTape == null) return false; @@ -178,28 +122,11 @@ public class TapeChanger implements Tape { return currentTape.left(amt); } - /** - * Move the cursor one space right. - * - * Moving the cursor right will auto-extend the tape if that is enabled. - * - * @return Whether the cursor was moved right. - */ @Override public boolean right() { return right(1); } - /** - * Move the cursor the specified amount right. - * - * Moving the cursor right will auto-extend the tape if that is enabled. - * - * @param amt - * The amount to move the cursor right by. - * - * @return Whether the cursor was moved right. - */ @Override public boolean right(final int amt) { if (currentTape == null) return false; @@ -233,7 +160,8 @@ public class TapeChanger implements Tape { /** * Check if a tape is currently loaded. * - * @return Whether or not a tape is loaded. + * @return + * Whether or not a tape is loaded. */ public boolean isLoaded() { return currentTape != null; @@ -245,7 +173,8 @@ public class TapeChanger implements Tape { * Attempting to load a tape that isn't there won't eject the current * tape. * - * @return Whether or not the next tape was loaded. + * @return + * Whether or not the next tape was loaded. */ public boolean nextTape() { final boolean succ = tapes.right(); @@ -263,7 +192,8 @@ public class TapeChanger implements Tape { * Attempting to load a tape that isn't there won't eject the current * tape. * - * @return Whether or not the previous tape was loaded. + * @return + * Whether or not the previous tape was loaded. */ public boolean prevTape() { final boolean succ = tapes.left(); @@ -283,7 +213,7 @@ public class TapeChanger implements Tape { * The specified tape is loaded. * * @param tp - * The tape to insert and load. + * The tape to insert and load. */ public void insertTape(final Tape tp) { tapes.insertAfter(tp); @@ -299,7 +229,8 @@ public class TapeChanger implements Tape { * * Loads the previous tape, if there is one. * - * @return The removed tape. + * @return + * The removed tape. */ public Tape removeTape() { if (currentTape == null) return null; @@ -322,7 +253,8 @@ public class TapeChanger implements Tape { /** * Get how many tapes are currently in the changer. * - * @return How many tapes are currently in the changer. + * @return + * How many tapes are currently in the changer. */ public int tapeCount() { return tapes.size(); diff --git a/base/src/main/java/bjc/utils/esodata/TapeLibrary.java b/base/src/main/java/bjc/utils/esodata/TapeLibrary.java index 2dbc70b..00e2e99 100644 --- a/base/src/main/java/bjc/utils/esodata/TapeLibrary.java +++ b/base/src/main/java/bjc/utils/esodata/TapeLibrary.java @@ -13,24 +13,19 @@ import java.util.Map; * either return null/false. * * @param - * The element type of the tapes. + * The element type of the tapes. */ public class TapeLibrary implements Tape { - private final Map> tapes; - private Tape currentTape; + /* Our backing store of tapes. */ + private final Map> tapes; + /* The current tape. */ + private Tape currentTape; - /** - * Create a new empty tape library. - */ + /** Create a new empty tape library. */ public TapeLibrary() { tapes = new HashMap<>(); } - /** - * Get the item the tape is currently on. - * - * @return The item the tape is on. - */ @Override public T item() { if (currentTape == null) return null; @@ -38,12 +33,6 @@ public class TapeLibrary implements Tape { return currentTape.item(); } - /** - * Set the item the tape is currently on. - * - * @param itm - * The new value for the tape item. - */ @Override public void item(final T itm) { if (currentTape == null) return; @@ -51,11 +40,6 @@ public class TapeLibrary implements Tape { currentTape.item(itm); } - /** - * Get the current number of elements in the tape. - * - * @return The current number of elements in the tape. - */ @Override public int size() { if (currentTape == null) return 0; @@ -69,12 +53,7 @@ public class TapeLibrary implements Tape { return currentTape.position(); } - /** - * Insert an element before the current item. - * - * @param itm - * The item to add. - */ + @Override public void insertBefore(final T itm) { if (currentTape == null) return; @@ -82,9 +61,6 @@ public class TapeLibrary implements Tape { currentTape.insertBefore(itm); } - /** - * Insert an element after the current item. - */ @Override public void insertAfter(final T itm) { if (currentTape == null) return; @@ -92,14 +68,6 @@ public class TapeLibrary implements Tape { currentTape.insertAfter(itm); } - /** - * Remove the current element. - * - * Also moves the cursor back one step if possible to maintain relative - * position, and removes the corresponding item from the non-active side - * - * @return The removed item from the active side. - */ @Override public T remove() { if (currentTape == null) return null; @@ -107,9 +75,6 @@ public class TapeLibrary implements Tape { return currentTape.remove(); } - /** - * Move the cursor to the left-most position. - */ @Override public void first() { if (currentTape == null) return; @@ -117,9 +82,6 @@ public class TapeLibrary implements Tape { currentTape.first(); } - /** - * Move the cursor the right-most position. - */ @Override public void last() { if (currentTape == null) return; @@ -127,29 +89,11 @@ public class TapeLibrary implements Tape { currentTape.last(); } - /** - * Move the cursor one space left. - * - * The cursor can't go past zero. - * - * @return True if the cursor was moved left. - */ @Override public boolean left() { return left(1); } - /** - * Move the cursor the specified amount left. - * - * The cursor can't go past zero. Attempts to move the cursor by amounts - * that would exceed zero don't move the cursor at all. - * - * @param amt - * The amount to attempt to move the cursor left. - * - * @return True if the cursor was moved left. - */ @Override public boolean left(final int amt) { if (currentTape == null) return false; @@ -157,28 +101,11 @@ public class TapeLibrary implements Tape { return currentTape.left(amt); } - /** - * Move the cursor one space right. - * - * Moving the cursor right will auto-extend the tape if that is enabled. - * - * @return Whether the cursor was moved right. - */ @Override public boolean right() { return right(1); } - /** - * Move the cursor the specified amount right. - * - * Moving the cursor right will auto-extend the tape if that is enabled. - * - * @param amt - * The amount to move the cursor right by. - * - * @return Whether the cursor was moved right. - */ @Override public boolean right(final int amt) { if (currentTape == null) return false; @@ -212,7 +139,8 @@ public class TapeLibrary implements Tape { /** * Check if a tape is currently loaded. * - * @return Whether or not a tape is loaded. + * @return + * Whether or not a tape is loaded. */ public boolean isLoaded() { return currentTape != null; @@ -225,9 +153,10 @@ public class TapeLibrary implements Tape { * tape. * * @param label - * The label of the tape to load. + * The label of the tape to load. * - * @return Whether or not the next tape was loaded. + * @return + * Whether or not the next tape was loaded. */ public boolean switchTape(final String label) { if (tapes.containsKey(label)) { @@ -248,10 +177,10 @@ public class TapeLibrary implements Tape { * Adding a duplicate tape will overwrite any existing types. * * @param label - * The label of the tape to add. + * The label of the tape to add. * * @param tp - * The tape to insert and load. + * The tape to insert and load. */ public void insertTape(final String label, final Tape tp) { tapes.put(label, tp); @@ -265,9 +194,10 @@ public class TapeLibrary implements Tape { * Does nothing if there is not a tape of that name loaded. * * @param label - * The tape to remove. + * The tape to remove. * - * @return The removed tape. + * @return + * The removed tape. */ public Tape removeTape(final String label) { return tapes.remove(label); @@ -285,7 +215,8 @@ public class TapeLibrary implements Tape { /** * Get how many tapes are currently in the library. * - * @return How many tapes are currently in the library. + * @return + * How many tapes are currently in the library. */ public int tapeCount() { return tapes.size(); @@ -295,9 +226,10 @@ public class TapeLibrary implements Tape { * Check if a specific tape is loaded into the library. * * @param label - * The tape to check for. + * The tape to check for. * - * @return Whether or not a tape of that name exists + * @return + * Whether or not a tape of that name exists */ public boolean hasTape(final String label) { return tapes.containsKey(label); diff --git a/base/src/main/java/bjc/utils/esodata/UnifiedDirectory.java b/base/src/main/java/bjc/utils/esodata/UnifiedDirectory.java index ffb639f..ed71512 100644 --- a/base/src/main/java/bjc/utils/esodata/UnifiedDirectory.java +++ b/base/src/main/java/bjc/utils/esodata/UnifiedDirectory.java @@ -11,18 +11,18 @@ import bjc.utils.funcdata.IMap; * @author EVE * * @param - * The key type of the directory. + * The key type of the directory. + * * @param - * The value type of the directory. + * The value type of the directory. */ public class UnifiedDirectory implements Directory { + /* Our directory children. */ private final IMap> children; - + /* Our data children. */ private final IMap data; - /** - * Create a new directory. - */ + /** Create a new directory. */ public UnifiedDirectory() { children = new FunctionalMap<>(); data = new FunctionalMap<>(); @@ -102,4 +102,4 @@ public class UnifiedDirectory implements Directory { public String toString() { return String.format("UnifiedDirectory [children=%s, data=%s]", children, data); } -} \ No newline at end of file +} diff --git a/base/src/main/java/bjc/utils/esodata/todos.txt b/base/src/main/java/bjc/utils/esodata/todos.txt new file mode 100644 index 0000000..5382ade --- /dev/null +++ b/base/src/main/java/bjc/utils/esodata/todos.txt @@ -0,0 +1,6 @@ +@TODO 10/11/17 Ben Culkin :TapeRefactor + Refactor double-sidedness into its own interface. + +@TODO 10/11/17 Ben Culkin :CursorHands + Add cursored list/tree data structures with the ability to pick/put the + current node. -- cgit v1.2.3