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