Changeset ac4dad2 for doc/papers/general/evaluation/cfastack.c
 Timestamp:
 Apr 28, 2018, 9:20:28 AM (4 years ago)
 Branches:
 aaronthesis, armeh, cleanupdtors, deferred_resn, demangler, jacob/cs343translation, jenkinssandbox, master, newast, newastuniqueexpr, newenv, no_list, persistentindexer, with_gc
 Children:
 6926a6d
 Parents:
 aa5fdac
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

doc/papers/general/evaluation/cfastack.c
raa5fdac rac4dad2 2 2 #include "cfastack.h" 3 3 4 forall( otype T ) struct stack_node { 5 T value; 6 stack_node(T) * next; 7 }; 4 forall( otype T ) { 5 struct node { 6 T value; 7 node(T) * next; 8 }; 8 9 9 forall( otype T ) void clear( stack(T) & s ) with( s ) { 10 for ( stack_node(T) * next = head; next; ) { 11 stack_node(T) * crnt = next; 12 next = crnt>next; 13 ^(*crnt){}; 14 free(crnt); 10 void ?{}( stack(T) & s ) { (s.head){ 0 }; } 11 12 void ?{}( stack(T) & s, stack(T) t ) { 13 node(T) ** cr = &s.head; 14 for ( node(T) * nx = t.head; nx; nx = nx>next ) { 15 *cr = alloc(); 16 ((*cr)>value){ nx>value }; 17 cr = &(*cr)>next; 18 } 19 *cr = 0; 15 20 } 16 head = 0; 21 22 void ^?{}( stack(T) & s) { clear( s ); } 23 24 void clear( stack(T) & s ) with( s ) { 25 for ( node(T) * nx = head; nx; ) { 26 node(T) * cr = nx; 27 nx = cr>next; 28 ^(*cr){}; 29 free(cr); 30 } 31 head = 0; 32 } 33 34 stack(T) ?=?( stack(T) & s, stack(T) t ) { 35 if ( s.head == t.head ) return s; 36 clear( s ); 37 s{ t }; 38 return s; 39 } 40 41 _Bool empty( const stack(T) & s ) { return s.head == 0; } 42 43 void push( stack(T) & s, T value ) with( s ) { 44 node(T) * n = alloc(); 45 (*n){ value, head }; 46 head = n; 47 } 48 49 T pop( stack(T) & s ) with( s ) { 50 node(T) * n = head; 51 head = n>next; 52 T v = n>value; 53 ^(*n){}; 54 free( n ); 55 return v; 56 } 17 57 } 18 19 forall( otype T ) void ?{}( stack(T) & s ) { (s.head){ 0 }; }20 21 forall( otype T ) void ?{}( stack(T) & s, stack(T) t ) {22 stack_node(T) ** crnt = &s.head;23 for ( stack_node(T) * next = t.head; next; next = next>next ) {24 *crnt = alloc();25 ((*crnt)>value){ next>value };26 crnt = &(*crnt)>next;27 }28 *crnt = 0;29 }30 31 forall( otype T ) stack(T) ?=?( stack(T) & s, stack(T) t ) {32 if ( s.head == t.head ) return s;33 clear( s );34 s{ t };35 return s;36 }37 38 forall( otype T ) void ^?{}( stack(T) & s) { clear( s ); }39 40 forall( otype T ) _Bool empty( const stack(T) & s ) { return s.head == 0; }41 42 forall( otype T ) void push( stack(T) & s, T value ) with( s ) {43 stack_node(T) * n = alloc();44 (*n){ value, head };45 head = n;46 }47 48 forall( otype T ) T pop( stack(T) & s ) with( s ) {49 stack_node(T) * n = head;50 head = n>next;51 T v = n>value;52 ^(*n){};53 free( n );54 return v;55 }
Note: See TracChangeset
for help on using the changeset viewer.