diff options
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/esodata/Stack.java')
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/esodata/Stack.java | 118 |
1 files changed, 72 insertions, 46 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/esodata/Stack.java b/BJC-Utils2/src/main/java/bjc/utils/esodata/Stack.java index 1fc644e..21a922d 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/esodata/Stack.java +++ b/BJC-Utils2/src/main/java/bjc/utils/esodata/Stack.java @@ -9,14 +9,16 @@ import java.util.function.Consumer; * * A FILO stack with support for forth/factor style combinators. * - * @param T The datatype stored in the stack. + * @param T + * The datatype stored in the stack. * @author Ben Culkin */ public abstract class Stack<T> { /** * Push an element onto the stack. * - * @param elm The element to insert. + * @param elm + * The element to insert. */ public abstract void push(T elm); @@ -36,7 +38,8 @@ public abstract class Stack<T> { */ public abstract int size(); - /** Check if the stack is empty. + /** + * Check if the stack is empty. * * @return Whether or not the stack is empty. */ @@ -54,14 +57,15 @@ public abstract class Stack<T> { /* * Basic combinators */ - + /** * Drop n items from the stack. * - * @param n The number of items to drop. + * @param n + * The number of items to drop. */ public void drop(int n) { - for(int i = 0; i < n; i++) { + for (int i = 0; i < n; i++) { pop(); } } @@ -76,7 +80,8 @@ public abstract class Stack<T> { /** * Delete n items below the current one. * - * @param n The number of items below the top to delete. + * @param n + * The number of items below the top to delete. */ public void nip(int n) { T elm = pop(); @@ -96,18 +101,20 @@ public abstract class Stack<T> { /** * Replicate the top n items of the stack m times. * - * @param n The number of items to duplicate. - * @param m The number of times to duplicate items. + * @param n + * The number of items to duplicate. + * @param m + * The number of times to duplicate items. */ public void multidup(int n, int m) { List<T> lst = new ArrayList<>(n); - for(int i = n; i > 0; i--) { + for (int i = n; i > 0; i--) { lst.set(i - 1, pop()); } - - for(int i = 0; i < m; i++) { - for(T elm : lst) { + + for (int i = 0; i < m; i++) { + for (T elm : lst) { push(elm); } } @@ -116,7 +123,8 @@ public abstract class Stack<T> { /** * Duplicate the top n items of the stack. * - * @param n The number of items to duplicate. + * @param n + * The number of items to duplicate. */ public void dup(int n) { multidup(n, 2); @@ -132,25 +140,27 @@ public abstract class Stack<T> { /** * Replicate the n elements below the top one m times. * - * @param n The number of items to duplicate. - * @param m The number of times to duplicate items. + * @param n + * The number of items to duplicate. + * @param m + * The number of times to duplicate items. */ public void multiover(int n, int m) { List<T> lst = new ArrayList<>(n); T elm = pop(); - for(int i = n; i > 0; i--) { + for (int i = n; i > 0; i--) { lst.set(i - 1, pop()); } - for(T nelm : lst) { + for (T nelm : lst) { push(nelm); } push(elm); - for(int i = 1; i < m; i++) { - for(T nelm : lst) { + for (int i = 1; i < m; i++) { + for (T nelm : lst) { push(nelm); } } @@ -159,7 +169,8 @@ public abstract class Stack<T> { /** * Duplicate the n elements below the top one. * - * @param n The number of items to duplicate. + * @param n + * The number of items to duplicate. */ public void over(int n) { multiover(n, 2); @@ -254,19 +265,21 @@ public abstract class Stack<T> { /** * Hides the top n elements on the stack from cons. * - * @param n The number of elements to hide. - * @param cons The action to hide the elements from + * @param n + * The number of elements to hide. + * @param cons + * The action to hide the elements from */ public void dip(int n, Consumer<Stack<T>> cons) { List<T> elms = new ArrayList<>(n); - for(int i = n; i > 0; i--) { + for (int i = n; i > 0; i--) { elms.set(i - 1, pop()); } cons.accept(this); - for(T elm : elms) { + for (T elm : elms) { push(elm); } } @@ -274,7 +287,8 @@ public abstract class Stack<T> { /** * Hide the top element of the stack from cons. * - * @param cons The action to hide the top from + * @param cons + * The action to hide the top from */ public void dip(Consumer<Stack<T>> cons) { dip(1, cons); @@ -284,8 +298,10 @@ public abstract class Stack<T> { * Copy the top n elements on the stack, replacing them once cons is * done. * - * @param n The number of elements to copy. - * @param cons The action to execute. + * @param n + * The number of elements to copy. + * @param cons + * The action to execute. */ public void keep(int n, Consumer<Stack<T>> cons) { dup(n); @@ -295,18 +311,20 @@ public abstract class Stack<T> { /** * Apply all the actions in conses to the top n elements of the stack. * - * @param n The number of elements to give to cons. - * @param conses The actions to execute. + * @param n + * The number of elements to give to cons. + * @param conses + * The actions to execute. */ public void multicleave(int n, List<Consumer<Stack<T>>> conses) { List<T> elms = new ArrayList<>(n); - for(int i = n; i > 0; i--) { + for (int i = n; i > 0; i--) { elms.set(i - 1, pop()); } - for(Consumer<Stack<T>> cons : conses) { - for(T elm : elms) { + for (Consumer<Stack<T>> cons : conses) { + for (T elm : elms) { push(elm); } @@ -317,7 +335,8 @@ public abstract class Stack<T> { /** * Apply all the actions in conses to the top element of the stack. * - * @param conses The actions to execute. + * @param conses + * The actions to execute. */ public void cleave(List<Consumer<Stack<T>>> conses) { multicleave(1, conses); @@ -326,16 +345,18 @@ public abstract class Stack<T> { /** * Apply every action in cons to n arguments. * - * @param n The number of parameters each action takes. - * @param conses The actions to execute. + * @param n + * The number of parameters each action takes. + * @param conses + * The actions to execute. */ public void multispread(int n, List<Consumer<Stack<T>>> conses) { List<List<T>> nelms = new ArrayList<>(conses.size()); - for(int i = conses.size(); i > 0; i--) { + for (int i = conses.size(); i > 0; i--) { List<T> elms = new ArrayList<>(n); - for(int j = n; j > 0; j--) { + for (int j = n; j > 0; j--) { elms.set(j, pop()); } @@ -343,8 +364,8 @@ public abstract class Stack<T> { } int i = 0; - for(List<T> elms : nelms) { - for(T elm : elms) { + for (List<T> elms : nelms) { + for (T elm : elms) { push(elm); } @@ -365,14 +386,17 @@ public abstract class Stack<T> { /** * Apply the action in cons to the first m groups of n arguments. * - * @param n The number of arguments cons takes. - * @param m The number of time to call cons. - * @param cons The action to execute. + * @param n + * The number of arguments cons takes. + * @param m + * The number of time to call cons. + * @param cons + * The action to execute. */ public void multiapply(int n, int m, Consumer<Stack<T>> cons) { List<Consumer<Stack<T>>> conses = new ArrayList<>(m); - for(int i = 0; i < m; i++) { + for (int i = 0; i < m; i++) { conses.add(cons); } @@ -382,8 +406,10 @@ public abstract class Stack<T> { /** * Apply cons n times to the corresponding elements in the stack. * - * @param n The number of times to execute cons. - * @param cons The action to execute. + * @param n + * The number of times to execute cons. + * @param cons + * The action to execute. */ public void apply(int n, Consumer<Stack<T>> cons) { multiapply(1, n, cons); |
