From e401fb037c555eb18db54708037a796523258c32 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Wed, 10 Jul 2019 17:10:51 -0400 Subject: General improvements to stack --- src/main/java/bjc/esodata/QueueStack.java | 6 ++--- src/main/java/bjc/esodata/SimpleStack.java | 6 ++--- src/main/java/bjc/esodata/SpaghettiStack.java | 8 +++---- src/main/java/bjc/esodata/Stack.java | 33 +++++++++++++++++++-------- src/main/java/bjc/esodata/ThresholdSet.java | 28 ++++++++++++++++++++++- 5 files changed, 61 insertions(+), 20 deletions(-) (limited to 'src/main/java') 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 extends Stack { @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 extends Stack { } @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 extends Stack { @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 extends Stack { } @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 extends Stack { @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 extends Stack { } @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; *

Stack underflow

*

* 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. *

*

@@ -32,7 +33,7 @@ public abstract class Stack { * * @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; } @@ -45,6 +46,18 @@ public abstract class Stack { */ 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. * @@ -72,7 +85,9 @@ public abstract class Stack { * * @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 { final List 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 { * 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 { // 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 { return new SetView(); } + /** + * Static threshold set constructor. + * + * @param keys + * The initial keys to add to the threshold set. + */ + public static ThresholdSet TS(KType... keys) { + ThresholdSet 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() { -- cgit v1.2.3