source:
doc/generic_types/evaluation/cfa-stack.c
@
298581c
Last change on this file since 298581c was 6a8ac0b, checked in by , 7 years ago | |
---|---|
|
|
File size: 1.1 KB |
Rev | Line | |
---|---|---|
[309be81] | 1 | #include <stdlib> |
2 | #include "cfa-stack.h" | |
3 | ||
4 | forall(otype T) struct stack_node { | |
5 | T value; | |
6 | stack_node(T)* next; | |
7 | }; | |
8 | ||
[a381b46] | 9 | forall(otype T) void ?{}(stack(T)* s) { (&s->head){ 0 }; } |
[d919f47] | 10 | |
[122aecd] | 11 | forall(otype T) void ?{}(stack(T)* s, stack(T) t) { |
12 | stack_node(T)** crnt = &s->head; | |
[6a8ac0b] | 13 | for ( stack_node(T)* next = t.head; next; next = next->next ) { |
[3fb7f5e] | 14 | *crnt = ((stack_node(T)*)malloc()){ next->value }; /***/ |
[122aecd] | 15 | stack_node(T)* acrnt = *crnt; |
16 | crnt = &acrnt->next; | |
[309be81] | 17 | } |
[122aecd] | 18 | *crnt = 0; |
19 | } | |
20 | ||
21 | forall(otype T) stack(T) ?=?(stack(T)* s, stack(T) t) { | |
22 | if ( s->head == t.head ) return *s; | |
23 | clear(s); | |
24 | s{ t }; | |
25 | return *s; | |
26 | } | |
27 | ||
[a381b46] | 28 | forall(otype T) void ^?{}(stack(T)* s) { clear(s); } |
[309be81] | 29 | |
[a381b46] | 30 | forall(otype T) _Bool empty(const stack(T)* s) { return s->head == 0; } |
[309be81] | 31 | |
32 | forall(otype T) void push(stack(T)* s, T value) { | |
[3fb7f5e] | 33 | s->head = ((stack_node(T)*)malloc()){ value, s->head }; /***/ |
[309be81] | 34 | } |
35 | ||
36 | forall(otype T) T pop(stack(T)* s) { | |
37 | stack_node(T)* n = s->head; | |
38 | s->head = n->next; | |
39 | T x = n->value; | |
[65cb413] | 40 | ^n{}; |
41 | free(n); | |
[309be81] | 42 | return x; |
43 | } | |
[122aecd] | 44 | |
45 | forall(otype T) void clear(stack(T)* s) { | |
[6a8ac0b] | 46 | for ( stack_node(T)* next = s->head; next; ) { |
[122aecd] | 47 | stack_node(T)* crnt = next; |
48 | next = crnt->next; | |
49 | delete(crnt); | |
50 | } | |
[d919f47] | 51 | s->head = 0; |
[122aecd] | 52 | } |
Note: See TracBrowser
for help on using the repository browser.