aaron-thesisarm-ehcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change
on this file since 33e22da was
a381b46,
checked in by Aaron Moss <a3moss@…>, 6 years ago
|
Minor cleanup, also filled in benchmark source appendix
|
-
Property mode set to
100644
|
File size:
1.2 KB
|
Line | |
---|
1 | #pragma once |
---|
2 | #include <utility> |
---|
3 | |
---|
4 | template<typename T> class stack { |
---|
5 | struct node { |
---|
6 | T value; |
---|
7 | node* next; |
---|
8 | |
---|
9 | node( T& v ) : value(v), next(nullptr) {} |
---|
10 | node( T&& v, node* n ) : value(std::move(v)), next(n) {} |
---|
11 | }; |
---|
12 | node* head; |
---|
13 | |
---|
14 | void copy(const stack<T>& o) { |
---|
15 | node** crnt = &head; |
---|
16 | node* next = o.head; |
---|
17 | while ( next ) { |
---|
18 | *crnt = new node{ next->value }; /***/ |
---|
19 | crnt = &(*crnt)->next; |
---|
20 | next = next->next; |
---|
21 | } |
---|
22 | *crnt = nullptr; |
---|
23 | } |
---|
24 | public: |
---|
25 | void clear() { |
---|
26 | node* next = head; |
---|
27 | while ( next ) { |
---|
28 | node* crnt = next; |
---|
29 | next = crnt->next; |
---|
30 | delete crnt; |
---|
31 | } |
---|
32 | head = nullptr; |
---|
33 | } |
---|
34 | |
---|
35 | stack() : head(nullptr) {} |
---|
36 | stack(const stack<T>& o) { copy(o); } |
---|
37 | stack(stack<T>&& o) : head(o.head) { o.head = nullptr; } |
---|
38 | ~stack() { clear(); } |
---|
39 | |
---|
40 | stack& operator= (const stack<T>& o) { |
---|
41 | if ( this == &o ) return *this; |
---|
42 | clear(); |
---|
43 | copy(o); |
---|
44 | return *this; |
---|
45 | } |
---|
46 | |
---|
47 | stack& operator= (stack<T>&& o) { |
---|
48 | if ( this == &o ) return *this; |
---|
49 | head = o.head; |
---|
50 | o.head = nullptr; |
---|
51 | return *this; |
---|
52 | } |
---|
53 | |
---|
54 | bool empty() const { return head == nullptr; } |
---|
55 | |
---|
56 | void push(T&& value) { head = new node{ std::move(value), head }; /***/ } |
---|
57 | |
---|
58 | T pop() { |
---|
59 | node* n = head; |
---|
60 | head = n->next; |
---|
61 | T x = std::move(n->value); |
---|
62 | delete n; |
---|
63 | return x; |
---|
64 | } |
---|
65 | }; |
---|
Note: See
TracBrowser
for help on using the repository browser.