summaryrefslogtreecommitdiff
path: root/src/main/java/bjc/esodata/Stack.java
blob: 360e57d6ffef1b32f9f01214c70d76db42fb1e43 (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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
package bjc.esodata;

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.
 *
 * <p>
 * <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 StackUnderflow} to be thrown. Check
 * the size of the stack if you want to avoid this.
 * <p>
 * </p>
 *
 * @param <T>
 *            The datatype stored in the stack.
 *
 * @author Ben Culkin
 */
public abstract class Stack<T> {
	/**
	 * The exception thrown when attempting to access an element from the stack that
	 * isn't there.
	 *
	 * @author EVE
	 */
	public static class StackUnderflow extends RuntimeException {
		/* The ID of the exception */
		private static final long serialVersionUID = 1423867176204571539L;
	}

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

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

	/**
	 * Retrieve the top element of this stack without removing it from the stack.
	 *
	 * @return The top element of this stack.
	 */
	public abstract T top();

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

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

	/**
	 * Create a spaghetti stack branching off of this one.
	 *
	 * @return A spaghetti stack with this stack as a parent.
	 */
	public Stack<T> spaghettify() {
		return new SpaghettiStack<>(this);
	}

	/*
	 * Multi-item add/remove.
	 */

	/**
	 * Push multiple elements onto the stack.
	 *
	 * @param elms
	 *             The elements to insert.
	 */
	public void pushAll(@SuppressWarnings("unchecked") T... elms) {
		for (T elm : elms) push(elm);
	}

	/**
	 * Push multiple elements onto the stack.
	 *
	 * @param elms
	 *             The elements to insert.
	 */
	public void pushAll(List<T> elms) {
		for (T elm : elms) push(elm);
	}

	/**
	 * Pop n items off of the stack and return them.
	 *
	 * @param n
	 *          The number of items to pop off of the stack.
	 *
	 * @return A list of the popped items, in the order they were popped.
	 */
	public List<T> multipop(int n) {
		List<T> lst = new LinkedList<>();

		for (int i = 0; i < n; i++) lst.add(pop());

		return lst;
	}

	/**
	 * Pop n items off of the stack and return them.
	 *
	 * @param n
	 *          The number of items to pop off of the stack.
	 *
	 * @return A list of the popped items, in the reverse order they were popped.
	 */
	public List<T> multipoprev(int n) {
		LinkedList<T> lst = new LinkedList<>();

		for (int i = 0; i < n; i++) lst.addFirst(pop());

		return lst;
	}

	/*
	 * Basic combinators
	 */

	/**
	 * Drop n items from the stack.
	 *
	 * @param n
	 *          The number of items to drop.
	 */
	public void drop(final int n) {
		for (int i = 0; i < n; i++) 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(final int n) {
		final T elm = pop();

		drop(n);

		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(final int n, final int m) {
		List<T> lst = multipoprev(n);

		for (int i = 0; i <= m; i++) pushAll(lst);
	}

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

	/** 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(final int n, final int m) {
		T elm = pop();

		List<T> lst = multipoprev(n);

		for (final T nelm : lst) push(nelm);

		push(elm);

		for (int i = 0; i < m; i++) pushAll(lst);
	}

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

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

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

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

	/**
	 * Rotate the n items m deep on the stack i positions.
	 *
	 * @param n
	 *          The number of items to rotate.
	 * @param m
	 *          The number of positions the item is down in the stack.
	 * @param i
	 *          The number of steps to rotate. Pass a negative number to rotate
	 *          things in the opposite direction.
	 */
	public void deepmultirot(int n, int m, int i) {
		List<T> kep = multipoprev(m);

		List<T> lst = multipoprev(n);

		Collections.rotate(lst, i);

		pushAll(lst);
		pushAll(kep);
	}

	/**
	 * Rotate the n items on top of the stack i positions.
	 *
	 * @param n
	 *          The number of items to rotate.
	 * @param i
	 *          The number of steps to rotate. Pass a negative number to rotate
	 *          things in the opposite direction.
	 */
	public void multirot(int n, int i) {
		deepmultirot(n, 0, i);
	}

	/** Swap the top two items on the stack. */
	public void swap() {
		multirot(2, 1);
	}

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

		push(x);
		push(x);
		push(y);
	}

	/** Swap the second and third items in the stack. */
	public void deepswap() {
		deepmultirot(2, 1, 1);
	}

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

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

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

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

	/*
	 * Dataflow Combinators
	 */

	/**
	 * Hides the top n elements on the stack from an action.
	 *
	 * @param n
	 *               The number of elements to hide.
	 *
	 * @param action
	 *               The action to hide the elements from
	 */
	public void dip(final int n, final Consumer<Stack<T>> action) {
		List<T> elms = multipoprev(n);

		action.accept(this);

		pushAll(elms);
	}

	/**
	 * Hide the top element of the stack from an action.
	 *
	 * @param action
	 *               The action to hide the top from
	 */
	public void dip(final Consumer<Stack<T>> action) {
		dip(1, action);
	}

	/**
	 * Copy the top n elements on the stack, replacing them once an action is done.
	 *
	 * @param n
	 *               The number of elements to copy.
	 *
	 * @param action
	 *               The action to execute.
	 */
	public void keep(final int n, final Consumer<Stack<T>> action) {
		dup(n);

		dip(n, action);
	}

	/**
	 * Copy the first element on the stack, replacing them once an action is done.
	 *
	 * @param action
	 *               The action to execute.
	 */
	public void keep(final Consumer<Stack<T>> action) {
		keep(1, action);
	}

	/**
	 * Apply all the actions in a list to the top n elements of the stack.
	 *
	 * @param n
	 *                The number of elements to give to cons.
	 *
	 * @param actions
	 *                The actions to execute.
	 */
	public void multicleave(final int n, final List<Consumer<Stack<T>>> actions) {
		List<T> elms = multipoprev(n);

		for (final Consumer<Stack<T>> action : actions) {
			pushAll(elms);

			action.accept(this);
		}
	}

	/**
	 * Apply all the actions in a list to the top n elements of the stack.
	 *
	 * @param n
	 *                The number of elements to give to cons.
	 *
	 * @param actions
	 *                The actions to execute.
	 */
	public void multicleave(final int n, @SuppressWarnings("unchecked") final Consumer<Stack<T>>... actions) {
		List<T> elms = multipoprev(n);

		for (final Consumer<Stack<T>> action : actions) {
			pushAll(elms);

			action.accept(this);
		}
	}

	/**
	 * Apply all the actions in a list to the top element of the stack.
	 *
	 * @param actions
	 *                The actions to execute.
	 */
	public void cleave(final List<Consumer<Stack<T>>> actions) {
		multicleave(1, actions);
	}

	/**
	 * Apply all the actions in a list to the top element of the stack.
	 *
	 * @param actions
	 *                The actions to execute.
	 */
	public void cleave(@SuppressWarnings("unchecked") final Consumer<Stack<T>>... actions) {
		multicleave(1, actions);
	}

	/**
	 * Apply every action in a list of actions to n arguments.
	 *
	 * @param n
	 *                The number of parameters each action takes.
	 *
	 * @param actions
	 *                The actions to execute.
	 */
	public void multispread(final int n, final List<Consumer<Stack<T>>> actions) {
		List<List<T>> nelms = new LinkedList<>();

		for (int i = 0; i < actions.size(); i++) {
			List<T> elms = multipoprev(n);

			nelms.add(elms);
		}

		Iterator<Consumer<Stack<T>>> itr = actions.iterator();
		for (final List<T> elms : nelms) {
			pushAll(elms);

			itr.next().accept(this);
		}
	}

	/**
	 * Apply every action in a list of actions to n arguments.
	 *
	 * @param n
	 *                The number of parameters each action takes.
	 *
	 * @param actions
	 *                The actions to execute.
	 */
	public void multispread(final int n, @SuppressWarnings("unchecked") final Consumer<Stack<T>>... actions) {
		List<List<T>> nelms = new LinkedList<>();

		for (int i = 0; i < actions.length; i++) {
			List<T> elms = multipoprev(n);

			nelms.add(elms);
		}

		int i = 0;
		for (final List<T> elms : nelms) {
			pushAll(elms);

			actions[i++].accept(this);
		}
	}

	/**
	 * Apply the actions in a list of actions to corresponding elements from the
	 * stack.
	 *
	 * @param conses
	 *               The actions to execute.
	 */
	public void spread(final List<Consumer<Stack<T>>> conses) {
		multispread(1, conses);
	}

	/**
	 * Apply the actions in a list of actions to corresponding elements from the
	 * stack.
	 *
	 * @param conses
	 *               The actions to execute.
	 */
	public void spread(@SuppressWarnings("unchecked") final Consumer<Stack<T>>... conses) {
		multispread(1, conses);
	}

	/**
	 * Apply an action 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 action
	 *               The action to execute.
	 */
	public void multiapply(final int n, final int m, final Consumer<Stack<T>> action) {
		final List<Consumer<Stack<T>>> actions = new ArrayList<>(m);

		for (int i = 0; i < m; i++) actions.add(action);

		multispread(n, actions);
	}

	/**
	 * Apply an action n times to the corresponding elements in the stack.
	 *
	 * @param n
	 *               The number of times to execute cons.
	 *
	 * @param action
	 *               The action to execute.
	 */
	public void apply(final int n, final Consumer<Stack<T>> action) {
		multiapply(1, n, action);
	}

	/*
	 * Misc. functions
	 */

	/**
	 * Get an array representing this stack.
	 *
	 * @return The stack as an array.
	 */
	public abstract T[] toArray();
}