source: doc/papers/general/evaluation/cfa-stack.c @ 520145b

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 520145b was 000ff2c, checked in by Rob Schluntz <rschlunt@…>, 6 years ago

Merge branch 'master' of plg.uwaterloo.ca:/u/cforall/software/cfa/cfa-cc

  • 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};
8forall(otype T) void ?{}( stack_node(T) & node, T value, stack_node(T) * next ) {
9    node.value = value;
10    node.next = next;
11}
12
13forall(otype T) void ?{}( stack(T) & s ) { (s.head){ 0 }; }
14
15forall(otype T) void ?{}( stack(T) & s, stack(T) t ) {
16        stack_node(T) ** crnt = &s.head;
17        for ( stack_node(T) * next = t.head; next; next = next->next ) {
18                // *crnt = new( next->value, 0 );
19                stack_node(T)* new_node = ((stack_node(T)*)malloc());
20                (*new_node){ next->value }; /***/
21                *crnt = new_node;
22                stack_node(T) * acrnt = *crnt;
23                crnt = &acrnt->next;
24        }
25        *crnt = 0;
26}
27
28forall(otype T) stack(T) ?=?( stack(T) & s, stack(T) t ) {
29        if ( s.head == t.head ) return s;
30        clear( s );
31        s{ t };
32        return s;
33}
34
35forall(otype T) void ^?{}( stack(T) & s) { clear( s ); }
36
37forall(otype T) _Bool empty( const stack(T) & s ) { return s.head == 0; }
38
39forall(otype T) void push( stack(T) & s, T value ) {
40        // s.head = new( value, s.head );
41        stack_node(T)* new_node = ((stack_node(T)*)malloc());
42        (*new_node){ value, s.head }; /***/
43        s.head = new_node;
44}
45
46forall(otype T) T pop( stack(T) & s ) {
47        stack_node(T) * n = s.head;
48        s.head = n->next;
49        T v = n->value;
50        delete( n );
51        return v;
52}
53
54forall(otype T) void clear( stack(T) & s ) {
55        for ( stack_node(T) * next = s.head; next; ) {
56                stack_node(T) * crnt = next;
57                next = crnt->next;
58                delete( crnt );
59        }
60        s.head = 0;
61}
Note: See TracBrowser for help on using the repository browser.