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