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