/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-29 11:02:34 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20220529110234-3oojizzdgmbrctv4
Woops again.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
namespace Utils {
 
2
  [CCode (cname = "VStack", cprefix = "v_stack_")]
 
3
  public class Stack <T> {
 
4
    private static int step_size = 11;
 
5
    private T[] stack;
 
6
    private int pntr;
 
7
    public size_t elements {get {return pntr + 1;}}
 
8
    private size_t size;
 
9
    
 
10
    public Stack (size_t size = 23) {
 
11
      this.stack = new T[size];
 
12
      this.pntr = -1;
 
13
      this.size = size;
 
14
    }
 
15
 
 
16
    public void push (T val) {
 
17
      this.pntr++;
 
18
      this.stack[this.pntr] = val;
 
19
      if (this.pntr + 1 >= this.size) {
 
20
        this.stack.resize (this.stack.length + Stack.step_size);
 
21
      }
 
22
    }
 
23
 
 
24
    public T pop () {
 
25
      if (this.pntr < 0) {
 
26
        info ("Tryinng to pop value from empty Stack:\n" +
 
27
                 "\treturning NULL.");
 
28
        return null;
 
29
      }
 
30
      T ret_val = this.stack[this.pntr];
 
31
      this.stack[this.pntr] = null;
 
32
      this.pntr--;
 
33
      return ret_val;
 
34
    }
 
35
 
 
36
    public bool is_empty () {
 
37
      return (this.pntr == -1);
 
38
    }
 
39
 
 
40
    public T peek () {
 
41
      return peek_n (0);
 
42
    }
 
43
    
 
44
    public T peek_n (size_t n) {
 
45
      if (this.pntr < 0) {
 
46
        info ("Trying to peek a value from empty Stack:\n" +
 
47
                 "\tReturning NULL.");
 
48
        return null;
 
49
      }
 
50
      if ((this.pntr - n) <= 0) {
 
51
        return stack[0];
 
52
      }
 
53
      return this.stack[this.pntr - n];
 
54
    }
 
55
  }
 
56
}