diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2019-07-10 17:10:51 -0400 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2019-07-10 17:10:51 -0400 |
| commit | e401fb037c555eb18db54708037a796523258c32 (patch) | |
| tree | 78aa519089a71fe056524abdc28af4894e2640cd /src/main/java/bjc/esodata | |
| parent | 4d0a59a0023f2b4fca144a089a3f75acb4ebd62b (diff) | |
General improvements to stack
Diffstat (limited to 'src/main/java/bjc/esodata')
| -rw-r--r-- | src/main/java/bjc/esodata/QueueStack.java | 6 | ||||
| -rw-r--r-- | src/main/java/bjc/esodata/SimpleStack.java | 6 | ||||
| -rw-r--r-- | src/main/java/bjc/esodata/SpaghettiStack.java | 8 | ||||
| -rw-r--r-- | src/main/java/bjc/esodata/Stack.java | 33 | ||||
| -rw-r--r-- | src/main/java/bjc/esodata/ThresholdSet.java | 28 |
5 files changed, 61 insertions, 20 deletions
diff --git a/src/main/java/bjc/esodata/QueueStack.java b/src/main/java/bjc/esodata/QueueStack.java index 49476f0..55c6fc4 100644 --- a/src/main/java/bjc/esodata/QueueStack.java +++ b/src/main/java/bjc/esodata/QueueStack.java @@ -29,14 +29,14 @@ public class QueueStack<T> extends Stack<T> { @Override public T pop() { - if(backing.isEmpty()) throw new StackUnderflowException(); + if(backing.isEmpty()) throw new StackUnderflow(); return backing.remove(); } @Override public T top() { - if(backing.isEmpty()) throw new StackUnderflowException(); + if(backing.isEmpty()) throw new StackUnderflow(); return backing.peek(); } @@ -47,7 +47,7 @@ public class QueueStack<T> extends Stack<T> { } @Override - public boolean empty() { + public boolean isEmpty() { return backing.size() == 0; } diff --git a/src/main/java/bjc/esodata/SimpleStack.java b/src/main/java/bjc/esodata/SimpleStack.java index 41b217d..dc6566c 100644 --- a/src/main/java/bjc/esodata/SimpleStack.java +++ b/src/main/java/bjc/esodata/SimpleStack.java @@ -27,14 +27,14 @@ public class SimpleStack<T> extends Stack<T> { @Override public T pop() { - if(backing.isEmpty()) throw new StackUnderflowException(); + if(backing.isEmpty()) throw new StackUnderflow(); return backing.pop(); } @Override public T top() { - if(backing.isEmpty()) throw new StackUnderflowException(); + if(backing.isEmpty()) throw new StackUnderflow(); return backing.peek(); } @@ -45,7 +45,7 @@ public class SimpleStack<T> extends Stack<T> { } @Override - public boolean empty() { + public boolean isEmpty() { return backing.size() == 0; } diff --git a/src/main/java/bjc/esodata/SpaghettiStack.java b/src/main/java/bjc/esodata/SpaghettiStack.java index 9280c28..1b7af25 100644 --- a/src/main/java/bjc/esodata/SpaghettiStack.java +++ b/src/main/java/bjc/esodata/SpaghettiStack.java @@ -37,14 +37,14 @@ class SpaghettiStack<T> extends Stack<T> { @Override public T pop() { - if(backing.empty()) return parent.pop(); + if(backing.isEmpty()) return parent.pop(); return backing.pop(); } @Override public T top() { - if(backing.empty()) return parent.top(); + if(backing.isEmpty()) return parent.top(); return backing.top(); } @@ -55,8 +55,8 @@ class SpaghettiStack<T> extends Stack<T> { } @Override - public boolean empty() { - return backing.empty() && parent.empty(); + public boolean isEmpty() { + return backing.isEmpty() && parent.isEmpty(); } @SuppressWarnings("unchecked") diff --git a/src/main/java/bjc/esodata/Stack.java b/src/main/java/bjc/esodata/Stack.java index 13480a3..f28e882 100644 --- a/src/main/java/bjc/esodata/Stack.java +++ b/src/main/java/bjc/esodata/Stack.java @@ -1,13 +1,14 @@ package bjc.esodata; -import java.util.ArrayList; -import java.util.List; -import java.util.function.Consumer; +import java.util.*; + +import java.util.function.*; /* * @TODO 10/11/17 Ben Culkin :StackCombinators * Implement more combinators for the stack. */ + /** * A stack, with support for forth/factor style stack combinators. * @@ -15,7 +16,7 @@ import java.util.function.Consumer; * <h2>Stack underflow</h2> * <p> * NOTE: In general, using any operation that attempts to remove more data from - * the stack than exists will cause a {@link StackUnderflowException} to be + * the stack than exists will cause a {@link StackUnderflow} to be * thrown. Check the size of the stack if you want to avoid this. * <p> * </p> @@ -32,7 +33,7 @@ public abstract class Stack<T> { * * @author EVE */ - public static class StackUnderflowException extends RuntimeException { + public static class StackUnderflow extends RuntimeException { /* The ID of the exception */ private static final long serialVersionUID = 1423867176204571539L; } @@ -46,6 +47,18 @@ public abstract class Stack<T> { public abstract void push(T elm); /** + * Push multiple elements onto the stack. + * + * @param elms + * The elements to insert. + */ + public void pushAll(T... elms) { + for (T elm : elms) { + push(elm); + } + } + + /** * Pop an element off of the stack. * * @return The element on top of the stack. @@ -72,7 +85,9 @@ public abstract class Stack<T> { * * @return Whether or not the stack is empty. */ - public abstract boolean empty(); + public boolean isEmpty() { + return size() == 0; + } /** * Create a spaghetti stack branching off of this one. @@ -136,10 +151,10 @@ public abstract class Stack<T> { final List<T> lst = new ArrayList<>(n); for(int i = n; i > 0; i--) { - lst.set(i - 1, pop()); + lst.add(0, pop()); } - for(int i = 0; i < m; i++) { + for(int i = 0; i <= m; i++) { for(final T elm : lst) { push(elm); } @@ -153,7 +168,7 @@ public abstract class Stack<T> { * The number of items to duplicate. */ public void dup(final int n) { - multidup(n, 2); + multidup(n, 1); } /** Duplicate the top item on the stack. */ diff --git a/src/main/java/bjc/esodata/ThresholdSet.java b/src/main/java/bjc/esodata/ThresholdSet.java index bc1b517..245eb5f 100644 --- a/src/main/java/bjc/esodata/ThresholdSet.java +++ b/src/main/java/bjc/esodata/ThresholdSet.java @@ -57,7 +57,7 @@ public class ThresholdSet<KeyType> { // Will throw a ClassCastException if you give us something bad. KeyType k = (KeyType)o; - int ret = ThresholdSet.this.remove(k); + int ret = ThresholdSet.this.contains(k); // The object is set-visible if (ret == 1) return true; @@ -235,6 +235,32 @@ public class ThresholdSet<KeyType> { return new SetView(); } + /** + * Static threshold set constructor. + * + * @param keys + * The initial keys to add to the threshold set. + */ + public static <KType> ThresholdSet<KType> TS(KType... keys) { + ThresholdSet<KType> ts = new ThresholdSet<>(); + + ts.addKeys(keys); + + return ts; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + + sb.append("Set: "); + sb.append(keySet); + sb.append("\nMap: "); + sb.append(keyMap); + + return sb.toString(); + } + // Implementation methods for setView int setSize() { |
