/vqdr/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/vqdr/trunk
54 by Gustav Hartvigsson
More work torwards inperementing the parser.
1
/*
2
 * The contects of this file is in the Public Domain.
3
 *
4
 * Created by Gustav Hartivgsson.
5
 */
6
[CCode (cname = "V", cprefix = "v_")]
46 by Gustav Hartvigsson
Added a simple stack type.
7
namespace Utils {
8
  [CCode (cname = "VStack", cprefix = "v_stack_")]
9
  public class Stack <T> {
52.1.3 by Gustav Hartvigsson
int -> int32
10
    private static int32 step_size = 11;
46 by Gustav Hartvigsson
Added a simple stack type.
11
    private T[] stack;
52.1.3 by Gustav Hartvigsson
int -> int32
12
    private int32 pntr;
46 by Gustav Hartvigsson
Added a simple stack type.
13
    public size_t elements {get {return pntr + 1;}}
14
    private size_t size;
15
    
49 by Gustav Hartvigsson
Added NamedVector
16
    [CCode (has_target = true)]
17
    public delegate bool ForEachFunc<V> (V item);
18
46 by Gustav Hartvigsson
Added a simple stack type.
19
    public Stack (size_t size = 23) {
20
      this.stack = new T[size];
21
      this.pntr = -1;
22
      this.size = size;
23
    }
24
25
    public void push (T val) {
26
      this.pntr++;
27
      this.stack[this.pntr] = val;
28
      if (this.pntr + 1 >= this.size) {
29
        this.stack.resize (this.stack.length + Stack.step_size);
30
      }
31
    }
32
33
    public T pop () {
34
      if (this.pntr < 0) {
35
        info ("Tryinng to pop value from empty Stack:\n" +
36
                 "\treturning NULL.");
37
        return null;
38
      }
39
      T ret_val = this.stack[this.pntr];
40
      this.stack[this.pntr] = null;
41
      this.pntr--;
42
      return ret_val;
43
    }
44
45
    public bool is_empty () {
46
      return (this.pntr == -1);
47
    }
48
49
    public T peek () {
50
      return peek_n (0);
51
    }
52
    
49 by Gustav Hartvigsson
Added NamedVector
53
    public T peek_n (size_t n) requires (n >= 0)  {
46 by Gustav Hartvigsson
Added a simple stack type.
54
      if (this.pntr < 0) {
55
        info ("Trying to peek a value from empty Stack:\n" +
56
                 "\tReturning NULL.");
57
        return null;
58
      }
59
      if ((this.pntr - n) <= 0) {
60
        return stack[0];
61
      }
62
      return this.stack[this.pntr - n];
63
    }
49 by Gustav Hartvigsson
Added NamedVector
64
65
    /**
66
     * Applies func to each item in the stack, popping the stack content. 
67
     * (Consumes the list)
68
     *
69
     * Adding items into the list is a violation.
70
     */
71
    public void foreach_pop (ForEachFunc<T> func) {
72
      while (this.elements < 0) {
73
         var i = elements;
74
         func (pop ());
75
         // Make sure the user does not put anpthing back into the stacstackk.
76
         assert (elements < i);
77
      }
78
    }
79
80
    /**
81
     * Applies func to each item in the stack, peeking the staks content.
82
     *
83
     * Changing the number of items in the list is a violation.
84
     */
85
    public void foreach_peek (ForEachFunc<T> func) {
86
      for (var i = this.pntr; i >= 0; i--) {
87
        var k = this.elements;
88
        func (this.stack[i]);
89
        assert (k == this.elements);
90
      }
91
    }
46 by Gustav Hartvigsson
Added a simple stack type.
92
  }
93
}