summaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/bjc/esodata/QueueStack.java6
-rw-r--r--src/main/java/bjc/esodata/SimpleStack.java6
-rw-r--r--src/main/java/bjc/esodata/SpaghettiStack.java8
-rw-r--r--src/main/java/bjc/esodata/Stack.java33
-rw-r--r--src/main/java/bjc/esodata/ThresholdSet.java28
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() {