ADTarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change
on this file since 2b7bf59 was
6a8ac0b,
checked in by Peter A. Buhr <pabuhr@…>, 7 years ago
|
more cleanup
|
-
Property mode set to
100644
|
File size:
1.1 KB
|
Rev | Line | |
---|
[b276be5] | 1 | #include "cpp-vstack.hpp" |
---|
| 2 | #include <utility> |
---|
| 3 | |
---|
[c87cd93] | 4 | stack::node::node( const object& v, node* n ) : value( v.new_copy() ), next( n ) {} |
---|
[b276be5] | 5 | |
---|
| 6 | void stack::copy(const stack& o) { |
---|
| 7 | node** crnt = &head; |
---|
[6a8ac0b] | 8 | for ( node* next = o.head; next; next = next->next ) { |
---|
[b276be5] | 9 | *crnt = new node{ *next->value }; |
---|
| 10 | crnt = &(*crnt)->next; |
---|
| 11 | } |
---|
| 12 | *crnt = nullptr; |
---|
| 13 | } |
---|
| 14 | |
---|
| 15 | stack::stack() : head(nullptr) {} |
---|
| 16 | stack::stack(const stack& o) { copy(o); } |
---|
| 17 | stack::stack(stack&& o) : head(o.head) { o.head = nullptr; } |
---|
| 18 | stack::~stack() { clear(); } |
---|
| 19 | |
---|
| 20 | stack& stack::operator= (const stack& o) { |
---|
| 21 | if ( this == &o ) return *this; |
---|
| 22 | clear(); |
---|
| 23 | copy(o); |
---|
| 24 | return *this; |
---|
| 25 | } |
---|
| 26 | |
---|
| 27 | stack& stack::operator= (stack&& o) { |
---|
| 28 | if ( this == &o ) return *this; |
---|
| 29 | head = o.head; |
---|
| 30 | o.head = nullptr; |
---|
| 31 | return *this; |
---|
| 32 | } |
---|
| 33 | |
---|
[a381b46] | 34 | void stack::clear() { |
---|
[6a8ac0b] | 35 | for ( node* next = head; next; ) { |
---|
[a381b46] | 36 | node* crnt = next; |
---|
| 37 | next = crnt->next; |
---|
| 38 | delete crnt; |
---|
| 39 | } |
---|
| 40 | head = nullptr; |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | |
---|
[79b8dc3] | 44 | bool stack::empty() const { return head == nullptr; } |
---|
[b276be5] | 45 | |
---|
[c87cd93] | 46 | void stack::push(const object& value) { head = new node{ value, head }; /***/ } |
---|
[b276be5] | 47 | |
---|
[79b8dc3] | 48 | ptr<object> stack::pop() { |
---|
[b276be5] | 49 | node* n = head; |
---|
| 50 | head = n->next; |
---|
[79b8dc3] | 51 | ptr<object> x = std::move(n->value); |
---|
[b276be5] | 52 | delete n; |
---|
| 53 | return x; |
---|
| 54 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.