source: doc/generic_types/evaluation/cpp-stack.hpp @ eb79750

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 eb79750 was c87cd93, checked in by Aaron Moss <a3moss@…>, 7 years ago

Final version of the benchmark code

  • Property mode set to 100644
File size: 1.1 KB
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                node* next = o.head;
16                while ( next ) {
17                        *crnt = new node{ next->value }; /***/
18                        crnt = &(*crnt)->next;
19                        next = next->next;
20                }
21                *crnt = nullptr;
22        }
23public:
24        void clear() {
25                node* next = head;
26                while ( next ) {
27                        node* crnt = next;
28                        next = crnt->next;
29                        delete crnt;
30                }
31                head = nullptr;
32        }
33
34        stack() : head(nullptr) {}
35        stack(const stack<T>& o) { copy(o); }
36        stack(stack<T>&& o) : head(o.head) { o.head = nullptr; }
37        ~stack() { clear(); }
38
39        stack& operator= (const stack<T>& o) {
40                if ( this == &o ) return *this;
41                clear();
42                copy(o);
43                return *this;
44        }
45
46        stack& operator= (stack<T>&& o) {
47                if ( this == &o ) return *this;
48                head = o.head;
49                o.head = nullptr;
50                return *this;
51        }
52
53        bool empty() const { return head == nullptr; }
54
55        void push(const T& value) { head = new node{ value, head };  /***/ }
56
57        T pop() {
58                node* n = head;
59                head = n->next;
60                T x = std::move(n->value);
61                delete n;
62                return x;
63        }
64};
Note: See TracBrowser for help on using the repository browser.