source: doc/generic_types/evaluation/cfa-stack.c @ 122aecd

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

Expand benchmarks

  • 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) {
10        (&s->head){ 0 };
11}
12
13forall(otype T) void ?{}(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) stack(T) ?=?(stack(T)* s, stack(T) t) {
26        if ( s->head == t.head ) return *s;
27        clear(s);
28        s{ t };
29        return *s;
30}
31
32forall(otype T) void ^?{}(stack(T)* s) {
33        clear(s);
34}
35
36forall(otype T) _Bool empty(const stack(T)* s) {
37        return s->head == 0;
38}
39
40forall(otype T) void push(stack(T)* s, T value) {
41        s->head = ((stack_node(T)*)malloc()){ value, s->head };
42}
43
44forall(otype T) T pop(stack(T)* s) {
45        stack_node(T)* n = s->head;
46        s->head = n->next;
47        T x = n->value;
48        delete(n);
49        return x;
50}
51
52forall(otype T) void clear(stack(T)* s) {
53        stack_node(T)* next = s->head;
54        while ( next ) {
55                stack_node(T)* crnt = next;
56                next = crnt->next;
57                delete(crnt);
58        }
59}
Note: See TracBrowser for help on using the repository browser.