Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/papers/general/evaluation/cfa-stack.c

    r4c80a75 r81e8ab0  
    22#include "cfa-stack.h"
    33
    4 forall( otype T ) {
    5         struct node {
    6                 T value;
    7                 node(T) * next;
    8         };
     4forall( otype T ) struct stack_node {
     5        T value;
     6        stack_node(T) * next;
     7};
    98
    10         void ?{}( stack(T) & s, stack(T) t ) {          // copy
    11                 node(T) ** cr = &s.head;
    12                 for ( node(T) * nx = t.head; nx; nx = nx->next ) {
    13                         *cr = alloc();
    14                         ((*cr)->value){ nx->value };
    15                         cr = &(*cr)->next;
    16                 }
    17                 *cr = 0;
     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        for ( stack_node(T) * next = t.head; next; next = next->next ) {
     14                *crnt = alloc();
     15                ((*crnt)->value){ next->value };
     16                crnt = &(*crnt)->next;
    1817        }
     18        *crnt = 0;
     19}
    1920
    20         void clear( stack(T) & s ) with( s ) {
    21                 for ( node(T) * nx = head; nx; ) {
    22                         node(T) * cr = nx;
    23                         nx = cr->next;
    24                         ^(*cr){};
    25                         free( cr );
    26                 }
    27                 head = 0;
     21forall( otype T ) void clear( stack(T) & s ) with( s ) {
     22        for ( stack_node(T) * next = head; next; ) {
     23                stack_node(T) * crnt = next;
     24                next = crnt->next;
     25                ^(*crnt){};
     26                free(crnt);
    2827        }
     28        head = 0;
     29}
    2930
    30         void ?{}( stack(T) & s ) { (s.head){ 0 }; }
    31         void ^?{}( stack(T) & s) { clear( s ); }
     31forall( 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}
    3237
    33         stack(T) ?=?( stack(T) & s, stack(T) t ) {
    34                 if ( s.head == t.head ) return s;
    35                 clear( s );
    36                 s{ t };
    37                 return s;
    38         }
     38forall( otype T ) void ^?{}( stack(T) & s) { clear( s ); }
    3939
    40         _Bool empty( const stack(T) & s ) {
    41                 return s.head == 0;
    42         }
     40forall( otype T ) _Bool empty( const stack(T) & s ) { return s.head == 0; }
    4341
    44         void push( stack(T) & s, T value ) with( s ) {
    45                 node(T) * n = alloc();
    46                 (*n){ value, head };
    47                 head = n;
    48         }
     42forall( 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}
    4947
    50         T pop( stack(T) & s ) with( s ) {
    51                 node(T) * n = head;
    52                 head = n->next;
    53                 T v = n->value;
    54                 ^(*n){};
    55                 free( n );
    56                 return v;
    57         }
     48forall( 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;
    5855}
Note: See TracChangeset for help on using the changeset viewer.