Ignore:
Timestamp:
Mar 9, 2018, 9:30:47 AM (7 years ago)
Author:
Peter A. Buhr <pabuhr@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
8b001bd
Parents:
29db723
Message:

more updates

Location:
doc/papers/general/evaluation
Files:
5 edited

Legend:

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

    r29db723 r81e8ab0  
    33
    44struct stack_node {
    5         void* value;
    6         struct stack_node* next;
     5        void * value;
     6        struct stack_node * next;
    77};
    88
    99struct stack new_stack() { return (struct stack){ NULL }; /***/ }
    1010
    11 void copy_stack(struct stack* s, const struct stack* t, void* (*copy)(const void*)) {
    12         struct stack_node** crnt = &s->head;
    13         for ( struct stack_node* next = t->head; next; next = next->next ) {
    14                 *crnt = malloc(sizeof(struct stack_node)); /***/
    15                 (*crnt)->value = copy(next->value);
     11void clear_stack( struct stack * s, void (*free_el)( void * ) ) {
     12        for ( struct stack_node * next = s->head; next; ) {
     13                struct stack_node * crnt = next;
     14                next = crnt->next;
     15                free_el( crnt->value );
     16                free( crnt );
     17        }
     18        s->head = NULL;
     19}
     20
     21void copy_stack( struct stack * s, const struct stack * t, void * (*copy)( const void * ) ) {
     22        struct stack_node ** crnt = &s->head;
     23        for ( struct stack_node * next = t->head; next; next = next->next ) {
     24                *crnt = malloc( sizeof(struct stack_node) ); /***/
     25                (*crnt)->value = copy( next->value );
    1626                crnt = &(*crnt)->next;
    1727        }
     
    1929}
    2030
    21 void clear_stack(struct stack* s, void (*free_el)(void*)) {
    22     for ( struct stack_node* next = s->head; next; ) {
    23                 struct stack_node* crnt = next;
    24                 next = crnt->next;
    25                 free_el(crnt->value);
    26                 free(crnt);
    27         }
    28         s->head = NULL;
    29 }
     31_Bool stack_empty( const struct stack * s ) { return s->head == NULL; }
    3032
    31 _Bool stack_empty(const struct stack* s) { return s->head == NULL; }
    32 
    33 void push_stack(struct stack* s, void* value) {
    34         struct stack_node* n = malloc(sizeof(struct stack_node)); /***/
     33void push_stack( struct stack * s, void * value ) {
     34        struct stack_node * n = malloc( sizeof(struct stack_node) ); /***/
    3535        *n = (struct stack_node){ value, s->head }; /***/
    3636        s->head = n;
    3737}
    3838
    39 void* pop_stack(struct stack* s) {
    40         struct stack_node* n = s->head;
     39void * pop_stack( struct stack * s ) {
     40        struct stack_node * n = s->head;
    4141        s->head = n->next;
    42         void* x = n->value;
    43         free(n);
    44         return x;
     42        void * v = n->value;
     43        free( n );
     44        return v;
    4545}
  • doc/papers/general/evaluation/cfa-stack.c

    r29db723 r81e8ab0  
    1717        }
    1818        *crnt = 0;
     19}
     20
     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);
     27        }
     28        head = 0;
    1929}
    2030
     
    4454        return v;
    4555}
    46 
    47 forall( otype T ) void clear( stack(T) & s ) with( s ) {
    48         for ( stack_node(T) * next = head; next; ) {
    49                 stack_node(T) * crnt = next;
    50                 next = crnt->next;
    51                 ^(*crnt){};
    52                 free(crnt);
    53         }
    54         head = 0;
    55 }
  • doc/papers/general/evaluation/cpp-stack.hpp

    r29db723 r81e8ab0  
    22#include <utility>
    33
    4 template<typename T> class stack {
     4template<typename T> struct stack {
    55        struct node {
    66                T value;
    7                 node* next;
     7                node * next;
     8                node( const T & v, node * n = nullptr ) : value( v ), next( n ) {}
     9        };
     10        node * head;
    811
    9                 node( const T& v, node* n = nullptr ) : value(v), next(n) {}
    10         };
    11         node* head;
     12        stack() : head( nullptr ) {}
     13        stack( const stack<T> & o) { copy( o ); }
     14        stack( stack<T> && o ) : head( o.head ) { o.head = nullptr; }
    1215
    13         void copy(const stack<T>& o) {
    14                 node** crnt = &head;
    15                 for ( node* next = o.head; next; next = next->next ) {
     16        void copy( const stack<T> & o ) {
     17                node ** crnt = &head;
     18                for ( node * next = o.head; next; next = next->next ) {
    1619                        *crnt = new node{ next->value }; /***/
    1720                        crnt = &(*crnt)->next;
     
    1922                *crnt = nullptr;
    2023        }
    21 public:
     24
    2225        void clear() {
    23             for ( node* next = head; next; ) {
    24                         node* crnt = next;
     26                for ( node * next = head; next; ) {
     27                        node * crnt = next;
    2528                        next = crnt->next;
    2629                        delete crnt;
     
    2932        }
    3033
    31         stack() : head(nullptr) {}
    32         stack(const stack<T>& o) { copy(o); }
    33         stack(stack<T>&& o) : head(o.head) { o.head = nullptr; }
    3434        ~stack() { clear(); }
    3535
    36         stack& operator= (const stack<T>& o) {
     36        stack & operator= ( const stack<T> & o ) {
    3737                if ( this == &o ) return *this;
    3838                clear();
    39                 copy(o);
     39                copy( o );
    4040                return *this;
    4141        }
    4242
    43         stack& operator= (stack<T>&& o) {
     43        stack & operator= ( stack<T> && o ) {
    4444                if ( this == &o ) return *this;
    4545                head = o.head;
     
    5050        bool empty() const { return head == nullptr; }
    5151
    52         void push(const T& value) { head = new node{ value, head };  /***/ }
     52        void push( const T & value ) { head = new node{ value, head };  /***/ }
    5353
    5454        T pop() {
    55                 node* n = head;
     55                node * n = head;
    5656                head = n->next;
    57                 T x = std::move(n->value);
     57                T v = std::move( n->value );
    5858                delete n;
    59                 return x;
     59                return v;
    6060        }
    6161};
  • doc/papers/general/evaluation/cpp-vstack.cpp

    r29db723 r81e8ab0  
    22#include <utility>
    33
    4 stack::node::node( const object& v, node* n ) : value( v.new_copy() ), next( n ) {}
     4stack::node::node( const object & v, node * n ) : value( v.new_copy() ), next( n ) {}
    55
    6 void stack::copy(const stack& o) {
    7         node** crnt = &head;
    8         for ( node* next = o.head; next; next = next->next ) {
     6void stack::copy( const stack & o ) {
     7        node ** crnt = &head;
     8        for ( node * next = o.head; next; next = next->next ) {
    99                *crnt = new node{ *next->value }; /***/
    1010                crnt = &(*crnt)->next;
     
    1313}
    1414
    15 stack::stack() : head(nullptr) {}
    16 stack::stack(const stack& o) { copy(o); }
    17 stack::stack(stack&& o) : head(o.head) { o.head = nullptr; }
     15void stack::clear() {
     16        for ( node * next = head; next; ) {
     17                node * crnt = next;
     18                next = crnt->next;
     19                delete crnt;
     20        }
     21        head = nullptr;
     22}
     23
     24stack::stack() : head( nullptr ) {}
     25stack::stack( const stack & o ) { copy( o ); }
     26stack::stack( stack && o ) : head( o.head ) { o.head = nullptr; }
    1827stack::~stack() { clear(); }
    1928
    20 stack& stack::operator= (const stack& o) {
     29stack & stack::operator=( const stack & o ) {
    2130        if ( this == &o ) return *this;
    2231        clear();
    23         copy(o);
     32        copy( o );
    2433        return *this;
    2534}
    2635
    27 stack& stack::operator= (stack&& o) {
     36stack & stack::operator=( stack && o ) {
    2837        if ( this == &o ) return *this;
    2938        head = o.head;
     
    3241}
    3342
    34 void stack::clear() {
    35     for ( node* next = head; next; ) {
    36                 node* crnt = next;
    37                 next = crnt->next;
    38                 delete crnt;
    39         }
    40         head = nullptr;
    41 }
    42 
    4343
    4444bool stack::empty() const { return head == nullptr; }
    4545
    46 void stack::push(const object& value) { head = new node{ value, head }; /***/ }
     46void stack::push( const object & value ) { head = new node{ value, head }; /***/ }
    4747
    4848ptr<object> stack::pop() {
    49         node* n = head;
     49        node * n = head;
    5050        head = n->next;
    51         ptr<object> x = std::move(n->value);
     51        ptr<object> v = std::move( n->value );
    5252        delete n;
    53         return x;
     53        return v;
    5454}
  • doc/papers/general/evaluation/cpp-vstack.hpp

    r29db723 r81e8ab0  
    22#include "object.hpp"
    33
    4 class stack {
     4struct stack {
    55        struct node {
    66                ptr<object> value;
    7                 node* next;
     7                node * next;
     8                node( const object & v, node * n = nullptr );
     9        };
     10        node * head;
    811
    9                 node( const object& v, node* n = nullptr );
    10         };
    11         node* head;
     12        void copy( const stack & o );
    1213
    13         void copy(const stack& o);
    14 public:
    1514        stack();
    16         stack(const stack& o);
    17         stack(stack&& o);
     15        stack( const stack & o );
     16        stack( stack && o );
    1817        ~stack();
    19         stack& operator= (const stack& o);
    20         stack& operator= (stack&& o);
     18        stack& operator=( const stack& o );
     19        stack& operator=( stack && o );
    2120
    2221        void clear();
    2322        bool empty() const;
    24         void push(const object& value);
     23        void push( const object & value );
    2524        ptr<object> pop();
    2625};
Note: See TracChangeset for help on using the changeset viewer.