source: doc/papers/general/evaluation/cpp-stack.hpp @ f86c8e5

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since f86c8e5 was f86c8e5, checked in by Aaron Moss <a3moss@…>, 6 years ago

Remove move-operators from C++, C++obj benchmarks for parity

  • Property mode set to 100644
File size: 979 bytes
Line 
1#pragma once
2#include <utility>
3
4template<typename T> class stack {
5        struct node {
6                T value;
7                node* next;
8
9                node( const T& v, node* n = nullptr ) : value(v), next(n) {}
10        };
11        node* head;
12
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        }
21public:
22        void clear() {
23            for ( node* next = head; next; ) {
24                        node* crnt = next;
25                        next = crnt->next;
26                        delete crnt;
27                }
28                head = nullptr;
29        }
30
31        stack() : head(nullptr) {}
32        stack(const stack<T>& o) { copy(o); }
33        ~stack() { clear(); }
34
35        stack& operator= (const stack<T>& o) {
36                if ( this == &o ) return *this;
37                clear();
38                copy(o);
39                return *this;
40        }
41
42        bool empty() const { return head == nullptr; }
43
44        void push(const T& value) { head = new node{ value, head };  /***/ }
45
46        T pop() {
47                node* n = head;
48                head = n->next;
49                T x = std::move(n->value);
50                delete n;
51                return x;
52        }
53};
Note: See TracBrowser for help on using the repository browser.