2
* The contects of this file is in the Public Domain.
4
* Created by Gustav Hartivgsson.
8
public class Stack <T> {
9
private static int32 step_size = 11;
12
public size_t elements {get {return pntr + 1;}}
16
public delegate bool ForEachFunc<V> (V item);
18
public Stack (size_t size = 23) {
19
this.stack = new T[size];
24
public void push (T val) {
26
this.stack[this.pntr] = val;
27
if (this.pntr + 1 >= this.size) {
28
this.stack.resize (this.stack.length + Stack.step_size);
34
info ("Trying to pop value from empty Stack:\n" +
38
T ret_val = this.stack[this.pntr];
39
this.stack[this.pntr] = null;
44
public bool is_empty () {
45
return (this.pntr == -1);
52
public T peek_n (size_t n) requires (n >= 0) {
54
info ("Trying to peek a value from empty Stack:\n" +
58
if ((this.pntr - n) <= 0) {
61
return this.stack[this.pntr - n];
65
* Applies func to each item in the stack, popping the stack content.
68
* Adding items into the list is a violation.
70
public void foreach_pop (ForEachFunc<T> func) {
71
while (this.elements < 0) {
74
// Make sure the user does not put anpthing back into the stacstackk.
75
assert (elements < i);
80
* Applies func to each item in the stack, peeking the staks content.
82
* Changing the number of items in the list is a violation.
84
public void foreach_peek (ForEachFunc<T> func) {
85
for (var i = this.pntr; i >= 0; i--) {
86
var k = this.elements;
88
assert (k == this.elements);