Ignore:
Timestamp:
Mar 9, 2018, 9:30:47 AM (8 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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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};
Note: See TracChangeset for help on using the changeset viewer.