Ignore:
Timestamp:
Apr 17, 2018, 12:01:09 PM (7 years ago)
Author:
Thierry Delisle <tdelisle@…>
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, with_gc
Children:
3265399
Parents:
b2fe1c9 (diff), 81bb114 (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

    rb2fe1c9 r32cab5b  
    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 ); }
    1214
    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:
    2215        void clear() {
    23             for ( node* next = head; next; ) {
    24                         node* crnt = next;
     16                for ( node * next = head; next; ) {
     17                        node * crnt = next;
    2518                        next = crnt->next;
    2619                        delete crnt;
     
    2922        }
    3023
    31         stack() : head(nullptr) {}
    32         stack(const stack<T>& o) { copy(o); }
    33         stack(stack<T>&& o) : head(o.head) { o.head = nullptr; }
     24        void copy( const stack<T> & o ) {
     25                node ** crnt = &head;
     26                for ( node * next = o.head; next; next = next->next ) {
     27                        *crnt = new node{ next->value }; /***/
     28                        crnt = &(*crnt)->next;
     29                }
     30                *crnt = nullptr;
     31        }
     32
    3433        ~stack() { clear(); }
    3534
    36         stack& operator= (const stack<T>& o) {
     35        stack & operator= ( const stack<T> & o ) {
    3736                if ( this == &o ) return *this;
    3837                clear();
    39                 copy(o);
    40                 return *this;
    41         }
    42 
    43         stack& operator= (stack<T>&& o) {
    44                 if ( this == &o ) return *this;
    45                 head = o.head;
    46                 o.head = nullptr;
     38                copy( o );
    4739                return *this;
    4840        }
     
    5042        bool empty() const { return head == nullptr; }
    5143
    52         void push(const T& value) { head = new node{ value, head };  /***/ }
     44        void push( const T & value ) { head = new node{ value, head };  /***/ }
    5345
    5446        T pop() {
    55                 node* n = head;
     47                node * n = head;
    5648                head = n->next;
    57                 T x = std::move(n->value);
     49                T v = std::move( n->value );
    5850                delete n;
    59                 return x;
     51                return v;
    6052        }
    6153};
Note: See TracChangeset for help on using the changeset viewer.