source:
doc/papers/general/evaluation/cfa-stack.c@
87555b7
| Last change on this file since 87555b7 was 79d4186, checked in by , 8 years ago | |
|---|---|
|
|
| File size: 1.2 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 | }; |
| 8 | ||
| [c9b3a41] | 9 | forall(otype T) void ?{}( stack(T) & s ) { (s.head){ 0 }; } |
| [604e76d] | 10 | |
| [c9b3a41] | 11 | forall(otype T) void ?{}( stack(T) & s, stack(T) t ) { |
| 12 | stack_node(T) ** crnt = &s.head; | |
| 13 | for ( stack_node(T) * next = t.head; next; next = next->next ) { | |
| [79d4186] | 14 | stack_node(T)* new_node = (stack_node(T)*)malloc(); /***/ |
| 15 | (*new_node){ next->value }; | |
| [986dd36] | 16 | *crnt = new_node; |
| [79d4186] | 17 | crnt = &(*crnt)->next; |
| [604e76d] | 18 | } |
| 19 | *crnt = 0; | |
| 20 | } | |
| 21 | ||
| [c9b3a41] | 22 | forall(otype T) stack(T) ?=?( stack(T) & s, stack(T) t ) { |
| [986dd36] | 23 | if ( s.head == t.head ) return s; |
| [c9b3a41] | 24 | clear( s ); |
| [604e76d] | 25 | s{ t }; |
| [986dd36] | 26 | return s; |
| [604e76d] | 27 | } |
| 28 | ||
| [c9b3a41] | 29 | forall(otype T) void ^?{}( stack(T) & s) { clear( s ); } |
| [604e76d] | 30 | |
| [c9b3a41] | 31 | forall(otype T) _Bool empty( const stack(T) & s ) { return s.head == 0; } |
| [604e76d] | 32 | |
| [c9b3a41] | 33 | forall(otype T) void push( stack(T) & s, T value ) { |
| [79d4186] | 34 | stack_node(T)* new_node = (stack_node(T)*)malloc(); /***/ |
| 35 | (*new_node){ value, s.head }; | |
| [986dd36] | 36 | s.head = new_node; |
| [604e76d] | 37 | } |
| 38 | ||
| [c9b3a41] | 39 | forall(otype T) T pop( stack(T) & s ) { |
| 40 | stack_node(T) * n = s.head; | |
| [986dd36] | 41 | s.head = n->next; |
| [c9b3a41] | 42 | T v = n->value; |
| [79d4186] | 43 | ^(*n){}; |
| 44 | free( n ); | |
| [c9b3a41] | 45 | return v; |
| [604e76d] | 46 | } |
| 47 | ||
| [c9b3a41] | 48 | forall(otype T) void clear( stack(T) & s ) { |
| 49 | for ( stack_node(T) * next = s.head; next; ) { | |
| 50 | stack_node(T) * crnt = next; | |
| [604e76d] | 51 | next = crnt->next; |
| [79d4186] | 52 | ^(*crnt){}; |
| 53 | free(crnt); | |
| [604e76d] | 54 | } |
| [986dd36] | 55 | s.head = 0; |
| [604e76d] | 56 | } |
Note:
See TracBrowser
for help on using the repository browser.