Ignore:
Timestamp:
Mar 9, 2018, 1:34:04 PM (7 years ago)
Author:
Aaron Moss <a3moss@…>
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:
860f19f
Parents:
b51e5fdb (diff), 3d8f2f8 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

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

File:
1 edited

Legend:

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

    rb51e5fdb re84382b  
    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                         *crnt = new node{ next->value }; /***/
    17                         crnt = &(*crnt)->next;
    18                 }
    19                 *crnt = nullptr;
    20         }
    21 public:
    2216        void clear() {
    23             for ( node* next = head; next; ) {
    24                         node* crnt = next;
     17                for ( node * next = head; next; ) {
     18                        node * crnt = next;
    2519                        next = crnt->next;
    2620                        delete crnt;
     
    2923        }
    3024
    31         stack() : head(nullptr) {}
    32         stack(const stack<T>& o) { copy(o); }
     25        void copy( const stack<T> & o ) {
     26                node ** crnt = &head;
     27                for ( node * next = o.head; next; next = next->next ) {
     28                        *crnt = new node{ next->value }; /***/
     29                        crnt = &(*crnt)->next;
     30                }
     31                *crnt = nullptr;
     32        }
     33
    3334        ~stack() { clear(); }
    3435
    35         stack& operator= (const stack<T>& o) {
     36        stack & operator= ( const stack<T> & o ) {
    3637                if ( this == &o ) return *this;
    3738                clear();
    38                 copy(o);
     39                copy( o );
    3940                return *this;
    4041        }
     
    4243        bool empty() const { return head == nullptr; }
    4344
    44         void push(const T& value) { head = new node{ value, head };  /***/ }
     45        void push( const T & value ) { head = new node{ value, head };  /***/ }
    4546
    4647        T pop() {
    47                 node* n = head;
     48                node * n = head;
    4849                head = n->next;
    49                 T x = std::move(n->value);
     50                T v = std::move( n->value );
    5051                delete n;
    51                 return x;
     52                return v;
    5253        }
    5354};
Note: See TracChangeset for help on using the changeset viewer.