diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2017-02-16 14:25:07 -0500 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2017-02-16 14:25:07 -0500 |
| commit | f43f4f79e2b90a637b1eaef34cd8d10b69333db6 (patch) | |
| tree | e2ae543d92f0be24227a9d996544b7e53f90f49c /BJC-Utils2/src/main/java/bjc/utils/esodata/Tape.java | |
| parent | 56c8e9e2147fe375313172905e6a5261a8cfb601 (diff) | |
Double-sided tapes and tape changers
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/esodata/Tape.java')
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/esodata/Tape.java | 177 |
1 files changed, 108 insertions, 69 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/esodata/Tape.java b/BJC-Utils2/src/main/java/bjc/utils/esodata/Tape.java index 9d50b63..d08feb1 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/esodata/Tape.java +++ b/BJC-Utils2/src/main/java/bjc/utils/esodata/Tape.java @@ -2,73 +2,112 @@ package bjc.utils.esodata; import java.util.ArrayList; -public class Tape<T> { - private ArrayList<T> backing; - private int pos; - - public Tape() { - backing = new ArrayList<>(); - } - - public T item() { - return backing.get(pos); - } - - public int size() { - return backing.size(); - } - - public void item(T itm) { - backing.set(pos, itm); - } - - public void append(T itm) { - backing.add(itm); - } - - // Add an item before the current - public void insert(T itm) { - backing.add(pos, itm); - } - - public T remove() { - if(pos != 0) pos -= 1; - return backing.remove(pos); - } - - public void first() { - pos = 0; - } - - public void last() { - pos = backing.size() - 1; - } - - public boolean left() { - if(pos == 0) return false; - - pos -= 1; - return true; - } - - public boolean left(int amt) { - if((pos - amt) < 0) return false; - - pos -= amt; - return true; - } - - public boolean right() { - if(pos == (backing.size() - 1)) return false; - - pos += 1; - return true; - } - - public boolean right(int amt) { - if((pos + amt) >= (backing.size() - 1)) return false; - - pos += amt; - return true; - } +/** + * Interface for something that acts like a tape. + * + * A tape is essentially a 1D array with a cursor attached to it, and you can only + * affect elements at that cursor. The size of the array is theoretically unbounded + * to the right, but in practice bounded by available memory. + * + * @param T The element type of the tape. + * @author bjculkin + */ +public interface Tape<T> { + /** + * Get the item the tape is currently on. + * + * @return The item the tape is on. + */ + T item(); + + /** + * Set the item the tape is currently on. + * + * @param itm 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. + */ + int size(); + + /** + * Insert an element before the current item. + * + * @param itm The item to add. + */ + void insertBefore(T itm); + + /** + * Insert an element after the current item. + */ + void insertAfter(T itm); + + /** + * Remove the current element. + * + * Also moves the cursor back one step if possible to maintain + * relative position. + * + * @return The removed item. + */ + T remove(); + + /** + * Move the cursor to the left-most position. + */ + void first(); + + /** + * Move the cursor the right-most position. + */ + void last(); + + /** + * Move the cursor one space left. + * + * The cursor can't go past zero. + * + * @return True if the cursor was moved left. + */ + boolean left(); + + /** + * 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. + */ + boolean left(int amt); + + /** + * Move the cursor one space right. + * + * @return Whether the cursor was moved right. + */ + boolean right(); + + /** + * Move the cursor the specified amount right. + * + * @param amt The amount to move the cursor right by. + * + * @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. + */ + boolean isDoubleSided(); } |
