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 |
*/
|
|
64
by Gustav Hartvigsson
[General] Major refactoring. Utils -> Vee nenaming. |
6 |
|
7 |
namespace Vee { |
|
46
by Gustav Hartvigsson
Added a simple stack type. |
8 |
public class Stack <T> { |
52.1.3
by Gustav Hartvigsson
int -> int32 |
9 |
private static int32 step_size = 11; |
46
by Gustav Hartvigsson
Added a simple stack type. |
10 |
private T[] stack; |
52.1.3
by Gustav Hartvigsson
int -> int32 |
11 |
private int32 pntr; |
46
by Gustav Hartvigsson
Added a simple stack type. |
12 |
public size_t elements {get {return pntr + 1;}} |
13 |
private size_t size; |
|
14 |
|
|
64
by Gustav Hartvigsson
[General] Major refactoring. Utils -> Vee nenaming. |
15 |
|
49
by Gustav Hartvigsson
Added NamedVector |
16 |
public delegate bool ForEachFunc<V> (V item); |
17 |
||
46
by Gustav Hartvigsson
Added a simple stack type. |
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) { |
|
62
by Gustav Hartvigsson
various changes |
34 |
info ("Trying to pop value from empty Stack:\n" + |
46
by Gustav Hartvigsson
Added a simple stack type. |
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 |
|
|
49
by Gustav Hartvigsson
Added NamedVector |
52 |
public T peek_n (size_t n) requires (n >= 0) { |
46
by Gustav Hartvigsson
Added a simple stack type. |
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 |
} |
|
49
by Gustav Hartvigsson
Added NamedVector |
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 |
} |
|
46
by Gustav Hartvigsson
Added a simple stack type. |
91 |
} |
92 |
}
|