source: doc/generic_types/evaluation/cfa-stack.c @ 3fb7f5e

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 3fb7f5e was 3fb7f5e, checked in by Aaron Moss <a3moss@…>, 7 years ago

Update benchmarks, cleanup edits to the evaluation section

  • Property mode set to 100644
File size: 1.4 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) {
10        (&s->head){ 0 };
11}
12
13forall(otype T) void copy(stack(T)* s, stack(T)* t) {
14        stack_node(T)** crnt = &s->head;
15        stack_node(T)* next = t->head;
16        while ( next ) {
17                *crnt = ((stack_node(T)*)malloc()){ next->value }; /***/
18                stack_node(T)* acrnt = *crnt;
19                crnt = &acrnt->next;
20                next = next->next;
21        }
22        *crnt = 0;
23}
24
25forall(otype T) void ?{}(stack(T)* s, stack(T) t) {
26        stack_node(T)** crnt = &s->head;
27        stack_node(T)* next = t.head;
28        while ( next ) {
29                *crnt = ((stack_node(T)*)malloc()){ next->value }; /***/
30                stack_node(T)* acrnt = *crnt;
31                crnt = &acrnt->next;
32                next = next->next;
33        }
34        *crnt = 0;
35}
36
37forall(otype T) stack(T) ?=?(stack(T)* s, stack(T) t) {
38        if ( s->head == t.head ) return *s;
39        clear(s);
40        s{ t };
41        return *s;
42}
43
44forall(otype T) void ^?{}(stack(T)* s) {
45        clear(s);
46}
47
48forall(otype T) _Bool empty(const stack(T)* s) {
49        return s->head == 0;
50}
51
52forall(otype T) void push(stack(T)* s, T value) {
53        s->head = ((stack_node(T)*)malloc()){ value, s->head }; /***/
54}
55
56forall(otype T) T pop(stack(T)* s) {
57        stack_node(T)* n = s->head;
58        s->head = n->next;
59        T x = n->value;
60        ^n{};
61        free(n);
62        return x;
63}
64
65forall(otype T) void clear(stack(T)* s) {
66        stack_node(T)* next = s->head;
67        while ( next ) {
68                stack_node(T)* crnt = next;
69                next = crnt->next;
70                delete(crnt);
71        }
72        s->head = 0;
73}
Note: See TracBrowser for help on using the repository browser.