/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/libvee/stack.vala

  • Committer: Gustav Hartvigsson
  • Date: 2024-12-22 00:30:29 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20241222003029-k74ogrm32zobz325
[General] Split libvee into it's own library.

Show diffs side-by-side

added added

removed removed

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