2
* The contects of this file is in the Public Domain.
4
* Created by Gustav Hartivgsson.
6
[CCode (cname = "V", cprefix = "v_")]
8
[CCode (cname = "VStack", cprefix = "v_stack_")]
9
public class Stack <T> {
10
private static int32 step_size = 11;
13
public size_t elements {get {return pntr + 1;}}
16
[CCode (has_target = true)]
17
public delegate bool ForEachFunc<V> (V item);
19
public Stack (size_t size = 23) {
20
this.stack = new T[size];
25
public void push (T val) {
27
this.stack[this.pntr] = val;
28
if (this.pntr + 1 >= this.size) {
29
this.stack.resize (this.stack.length + Stack.step_size);
35
info ("Tryinng to pop value from empty Stack:\n" +
39
T ret_val = this.stack[this.pntr];
40
this.stack[this.pntr] = null;
45
public bool is_empty () {
46
return (this.pntr == -1);
53
public T peek_n (size_t n) requires (n >= 0) {
55
info ("Trying to peek a value from empty Stack:\n" +
59
if ((this.pntr - n) <= 0) {
62
return this.stack[this.pntr - n];
66
* Applies func to each item in the stack, popping the stack content.
69
* Adding items into the list is a violation.
71
public void foreach_pop (ForEachFunc<T> func) {
72
while (this.elements < 0) {
75
// Make sure the user does not put anpthing back into the stacstackk.
76
assert (elements < i);
81
* Applies func to each item in the stack, peeking the staks content.
83
* Changing the number of items in the list is a violation.
85
public void foreach_peek (ForEachFunc<T> func) {
86
for (var i = this.pntr; i >= 0; i--) {
87
var k = this.elements;
89
assert (k == this.elements);