summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/esodata/Stack.java
blob: fc04ca4fa80aaa3cf477a26656b72365a1ae9ff1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package bjc.utils.esodata;
import java.util.Deque;
import java.util.LinkedList;

/**
 * A stack, with support for combinators.
 *
 * A FILO stack with support for forth/factor style combinators.
 *
 * @param T The datatype stored in the stack.
 * @author Ben Culkin
 */
public class Stack<T> {
	private Deque<T> backing;

	/**
	 * Create a new empty stack.
	 *
	 */
	public Stack() {
		backing = new LinkedList();
	}

	/**
	 * Push an element onto the stack.
	 *
	 * @param elm The element to insert.
	 */
	public void push(T elm) {
		backing.push(elm);
	}

	/**
	 * Pop an element off of the stack.
	 *
	 * @return The element on top of the stack.
	 */
	public T pop(T elm) {
		return backing.pop();
	}

	/**
	 * Get the top item of the stack without removing it.
	 *
	 * @return The element on top of the stack.
	 */
	public T top() {
		return backing.peek();
	}

	/**
	 * Get the number of elements in the stack.
	 *
	 * @return the number of elements in the stack.
	 */
	public int size() {
		return backing.size();
	}

	/** Check if the stack is empty.
	 *
	 * @return Whether or not the stack is empty
	 */
	public boolean empty() {
		return backing.size() == 0;
	}

	/*
	 * Basic combinators
	 */
	
	/**
	 * Drop n items from the stack.
	 *
	 * @param n The number of items to drop.
	 */
	public void drop(int n) {
		for(int i = 0; i < n; i++) {
			backing.pop();
		}
	}

	/**
	 * Drop one item from the stack.
	 */
	public void drop() {
		drop(1);
	}

	/**
	 * Delete n items below the current one.
	 *
	 * @param n The number of items below the top to delete.
	 */
	public void nip(int n) {
		T elm = backing.pop();

		drop(n);

		backing.push(elm);
	}

	/**
	 * Delete the second element in the stack.
	 */
	public void nip() {
		nip(1);
	}

	/**
	 * 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.
	 */
	public void multidup(int n, int m) {
		LinkedList<T> lst = new LinkedList<>();

		for(int i = 0; i < n; i++) {
			lst.add(0, backing.pop());
		}
		
		for(int i = 0; i < m; i++) {
			for(T elm : lst) {
				backing.push(elm);
			}
		}
	}

	/**
	 * Duplicate the top n items of the stack.
	 *
	 * @param n The number of items to duplicate.
	 */
	public void dup(int n) {
		multidup(n, 2);
	}

	/**
	 * Duplicate the top item on the stack.
	 */
	public void dup() {
		dup(1);
	}

	/**
	 * 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.
	 */
	public void multiover(int n, int m) {
		LinkedList<T> lst = new LinkedList<>();

		T elm = backing.pop();

		for(int i = 0; i < n; i++) {
			lst.add(0, backing.pop());
		}

		for(T nelm : lst) {
			backing.push(nelm);
		}
		backing.push(elm);

		for(int i = 1; i < m; i++) {
			for(T nelm : lst) {
				backing.push(nelm);
			}
		}
	}

	/**
	 * Duplicate the n elements below the top one.
	 *
	 * @param n The number of items to duplicate.
	 */
	public void over(int n) {
		multiover(n, 2);
	}

	/**
	 * Duplicate the second item in the stack.
	 */
	public void over() {
		over(1);
	}

	/**
	 * Duplicate the third item in the stack.
	 */
	public void pick() {
		T z = backing.pop();
		T y = backing.pop();
		T x = backing.pop();

		backing.push(x);
		backing.push(y);
		backing.push(z);
		backing.push(x);
	}

	/**
	 * Swap the top two items on the stack.
	 */
	public void swap() {
		T y = backing.pop();
		T x = backing.pop();

		backing.push(y);
		backing.push(x);
	}

	/**
	 * Duplicate the second item below the first item.
	 */
	public void deepdup() {
		T y = backing.pop();
		T x = backing.pop();

		backing.push(x);
		backing.push(x);
		backing.push(y);
	}

	/**
	 * Swap the second and third items in the stack.
	 */
	public void deepswap() {
		T z = backing.pop();
		T y = backing.pop();
		T x = backing.pop();

		backing.push(y);
		backing.push(x);
		backing.push(z);
	}

	/**
	 * Rotate the top three items on the stack
	 */
	public void rot() {
		T z = backing.pop();
		T y = backing.pop();
		T x = backing.pop();

		backing.push(y);
		backing.push(z);
		backing.push(x);
	}

	/**
	 * Inversely rotate the top three items on the stack
	 */
	public void invrot() {
		T z = backing.pop();
		T y = backing.pop();
		T x = backing.pop();

		backing.push(z);
		backing.push(x);
		backing.push(y);
	}
}