source:
doc/papers/general/evaluation/cfa-stack.c
@
82c367d
Last change on this file since 82c367d was 000ff2c, checked in by , 6 years ago | |
---|---|
|
|
File size: 1.4 KB |
Rev | Line | |
---|---|---|
[604e76d] | 1 | #include <stdlib> |
2 | #include "cfa-stack.h" | |
3 | ||
4 | forall(otype T) struct stack_node { | |
5 | T value; | |
[c9b3a41] | 6 | stack_node(T) * next; |
[604e76d] | 7 | }; |
[c9b3a41] | 8 | forall(otype T) void ?{}( stack_node(T) & node, T value, stack_node(T) * next ) { |
9 | node.value = value; | |
10 | node.next = next; | |
11 | } | |
[604e76d] | 12 | |
[c9b3a41] | 13 | forall(otype T) void ?{}( stack(T) & s ) { (s.head){ 0 }; } |
[604e76d] | 14 | |
[c9b3a41] | 15 | forall(otype T) void ?{}( stack(T) & s, stack(T) t ) { |
16 | stack_node(T) ** crnt = &s.head; | |
17 | for ( stack_node(T) * next = t.head; next; next = next->next ) { | |
[000ff2c] | 18 | // *crnt = new( next->value, 0 ); |
[986dd36] | 19 | stack_node(T)* new_node = ((stack_node(T)*)malloc()); |
20 | (*new_node){ next->value }; /***/ | |
21 | *crnt = new_node; | |
[c9b3a41] | 22 | stack_node(T) * acrnt = *crnt; |
[604e76d] | 23 | crnt = &acrnt->next; |
24 | } | |
25 | *crnt = 0; | |
26 | } | |
27 | ||
[c9b3a41] | 28 | forall(otype T) stack(T) ?=?( stack(T) & s, stack(T) t ) { |
[986dd36] | 29 | if ( s.head == t.head ) return s; |
[c9b3a41] | 30 | clear( s ); |
[604e76d] | 31 | s{ t }; |
[986dd36] | 32 | return s; |
[604e76d] | 33 | } |
34 | ||
[c9b3a41] | 35 | forall(otype T) void ^?{}( stack(T) & s) { clear( s ); } |
[604e76d] | 36 | |
[c9b3a41] | 37 | forall(otype T) _Bool empty( const stack(T) & s ) { return s.head == 0; } |
[604e76d] | 38 | |
[c9b3a41] | 39 | forall(otype T) void push( stack(T) & s, T value ) { |
[000ff2c] | 40 | // s.head = new( value, s.head ); |
[986dd36] | 41 | stack_node(T)* new_node = ((stack_node(T)*)malloc()); |
42 | (*new_node){ value, s.head }; /***/ | |
43 | s.head = new_node; | |
[604e76d] | 44 | } |
45 | ||
[c9b3a41] | 46 | forall(otype T) T pop( stack(T) & s ) { |
47 | stack_node(T) * n = s.head; | |
[986dd36] | 48 | s.head = n->next; |
[c9b3a41] | 49 | T v = n->value; |
[000ff2c] | 50 | delete( n ); |
[c9b3a41] | 51 | return v; |
[604e76d] | 52 | } |
53 | ||
[c9b3a41] | 54 | forall(otype T) void clear( stack(T) & s ) { |
55 | for ( stack_node(T) * next = s.head; next; ) { | |
56 | stack_node(T) * crnt = next; | |
[604e76d] | 57 | next = crnt->next; |
[c9b3a41] | 58 | delete( crnt ); |
[604e76d] | 59 | } |
[986dd36] | 60 | s.head = 0; |
[604e76d] | 61 | } |
Note: See TracBrowser
for help on using the repository browser.