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