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/Stack.java | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) (limited to 'src/main/java/bjc/esodata/Stack.java') 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. */ -- cgit v1.2.3