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 87c5f40 was
b276be5,
checked in by Aaron Moss <a3moss@…>, 7 years ago
|
Further updates to benchmarks (new C++ virtual benchmark still slightly broken)
|
-
Property mode set to
100644
|
File size:
1.2 KB
|
Rev | Line | |
---|
[d919f47] | 1 | #pragma once |
---|
| 2 | |
---|
[309be81] | 3 | #include <utility> |
---|
| 4 | |
---|
| 5 | template<typename T> class stack { |
---|
| 6 | struct node { |
---|
| 7 | T value; |
---|
| 8 | node* next; |
---|
| 9 | |
---|
[122aecd] | 10 | node( T& v ) : value(v), next(nullptr) {} |
---|
[309be81] | 11 | node( T&& v, node* n ) : value(std::move(v)), next(n) {} |
---|
| 12 | }; |
---|
| 13 | |
---|
| 14 | node* head; |
---|
| 15 | |
---|
[122aecd] | 16 | void copy(const stack<T>& o) { |
---|
| 17 | node** crnt = &head; |
---|
| 18 | node* next = o.head; |
---|
| 19 | while ( next ) { |
---|
| 20 | *crnt = new node{ next->value }; |
---|
| 21 | crnt = &(*crnt)->next; |
---|
| 22 | next = next->next; |
---|
| 23 | } |
---|
| 24 | *crnt = nullptr; |
---|
| 25 | } |
---|
[309be81] | 26 | |
---|
[122aecd] | 27 | public: |
---|
| 28 | void clear() { |
---|
[309be81] | 29 | node* next = head; |
---|
| 30 | while ( next ) { |
---|
| 31 | node* crnt = next; |
---|
| 32 | next = crnt->next; |
---|
| 33 | delete crnt; |
---|
| 34 | } |
---|
| 35 | } |
---|
| 36 | |
---|
[122aecd] | 37 | stack() : head(nullptr) {} |
---|
| 38 | |
---|
| 39 | stack(const stack<T>& o) { copy(o); } |
---|
| 40 | |
---|
| 41 | stack(stack<T>&& o) : head(o.head) { o.head = nullptr; } |
---|
| 42 | |
---|
| 43 | ~stack() { clear(); } |
---|
| 44 | |
---|
| 45 | stack& operator= (const stack<T>& o) { |
---|
| 46 | if ( this == &o ) return *this; |
---|
| 47 | clear(); |
---|
| 48 | copy(o); |
---|
[b276be5] | 49 | return *this; |
---|
[122aecd] | 50 | } |
---|
| 51 | |
---|
| 52 | stack& operator= (stack<T>&& o) { |
---|
| 53 | if ( this == &o ) return *this; |
---|
| 54 | head = o.head; |
---|
| 55 | o.head = nullptr; |
---|
| 56 | return *this; |
---|
| 57 | } |
---|
| 58 | |
---|
[309be81] | 59 | bool empty() const { |
---|
| 60 | return head == nullptr; |
---|
| 61 | } |
---|
| 62 | |
---|
| 63 | void push(T&& value) { |
---|
| 64 | head = new node{ std::move(value), head }; |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | T pop() { |
---|
| 68 | node* n = head; |
---|
| 69 | head = n->next; |
---|
| 70 | T x = std::move(n->value); |
---|
| 71 | delete n; |
---|
| 72 | return x; |
---|
| 73 | } |
---|
| 74 | }; |
---|
Note: See
TracBrowser
for help on using the repository browser.