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