/vqdr/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/vqdr/trunk

« back to all changes in this revision

Viewing changes to src/utils/stack.vala

  • Committer: Gustav Hartvigsson
  • Date: 2022-05-30 18:35:38 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20220530183538-xlixbth43j8k3s42
Added NamedVector

Added Pair

Removed debugs from FastNumber

Added foreach_pop and foreach_peek to Stak.

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
    public size_t elements {get {return pntr + 1;}}
8
8
    private size_t size;
9
9
    
 
10
    [CCode (has_target = true)]
 
11
    public delegate bool ForEachFunc<V> (V item);
 
12
 
10
13
    public Stack (size_t size = 23) {
11
14
      this.stack = new T[size];
12
15
      this.pntr = -1;
41
44
      return peek_n (0);
42
45
    }
43
46
    
44
 
    public T peek_n (size_t n) {
 
47
    public T peek_n (size_t n) requires (n >= 0)  {
45
48
      if (this.pntr < 0) {
46
49
        info ("Trying to peek a value from empty Stack:\n" +
47
50
                 "\tReturning NULL.");
52
55
      }
53
56
      return this.stack[this.pntr - n];
54
57
    }
 
58
 
 
59
    /**
 
60
     * Applies func to each item in the stack, popping the stack content. 
 
61
     * (Consumes the list)
 
62
     *
 
63
     * Adding items into the list is a violation.
 
64
     */
 
65
    public void foreach_pop (ForEachFunc<T> func) {
 
66
      while (this.elements < 0) {
 
67
         var i = elements;
 
68
         func (pop ());
 
69
         // Make sure the user does not put anpthing back into the stacstackk.
 
70
         assert (elements < i);
 
71
      }
 
72
    }
 
73
 
 
74
    /**
 
75
     * Applies func to each item in the stack, peeking the staks content.
 
76
     *
 
77
     * Changing the number of items in the list is a violation.
 
78
     */
 
79
    public void foreach_peek (ForEachFunc<T> func) {
 
80
      for (var i = this.pntr; i >= 0; i--) {
 
81
        var k = this.elements;
 
82
        func (this.stack[i]);
 
83
        assert (k == this.elements);
 
84
      }
 
85
    }
55
86
  }
56
87
}