source: doc/generic_types/evaluation/cfa-stack.c @ 9a9a5c4

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 9a9a5c4 was a381b46, checked in by Aaron Moss <a3moss@…>, 7 years ago

Minor cleanup, also filled in benchmark source appendix

  • Property mode set to 100644
File size: 1.1 KB
Line 
1#include <stdlib>
2#include "cfa-stack.h"
3
4forall(otype T) struct stack_node {
5        T value;
6        stack_node(T)* next;
7};
8
9forall(otype T) void ?{}(stack(T)* s) { (&s->head){ 0 }; }
10
11forall(otype T) void ?{}(stack(T)* s, stack(T) t) {
12        stack_node(T)** crnt = &s->head;
13        stack_node(T)* next = t.head;
14        while ( next ) {
15                *crnt = ((stack_node(T)*)malloc()){ next->value }; /***/
16                stack_node(T)* acrnt = *crnt;
17                crnt = &acrnt->next;
18                next = next->next;
19        }
20        *crnt = 0;
21}
22
23forall(otype T) stack(T) ?=?(stack(T)* s, stack(T) t) {
24        if ( s->head == t.head ) return *s;
25        clear(s);
26        s{ t };
27        return *s;
28}
29
30forall(otype T) void ^?{}(stack(T)* s) { clear(s); }
31
32forall(otype T) _Bool empty(const stack(T)* s) { return s->head == 0; }
33
34forall(otype T) void push(stack(T)* s, T value) {
35        s->head = ((stack_node(T)*)malloc()){ value, s->head }; /***/
36}
37
38forall(otype T) T pop(stack(T)* s) {
39        stack_node(T)* n = s->head;
40        s->head = n->next;
41        T x = n->value;
42        ^n{};
43        free(n);
44        return x;
45}
46
47forall(otype T) void clear(stack(T)* s) {
48        stack_node(T)* next = s->head;
49        while ( next ) {
50                stack_node(T)* crnt = next;
51                next = crnt->next;
52                delete(crnt);
53        }
54        s->head = 0;
55}
Note: See TracBrowser for help on using the repository browser.