source:
doc/papers/general/evaluation/cpp-vstack.cpp
@
a659b31
Last change on this file since a659b31 was 4c80a75, checked in by , 7 years ago | |
---|---|
|
|
File size: 947 bytes |
Rev | Line | |
---|---|---|
[604e76d] | 1 | #include "cpp-vstack.hpp" |
2 | #include <utility> | |
3 | ||
[81e8ab0] | 4 | stack::node::node( const object & v, node * n ) : value( v.new_copy() ), next( n ) {} |
[604e76d] | 5 | |
[8b001bd] | 6 | void stack::copy( const stack & o ) { |
[ac4dad2] | 7 | node ** cr = &head; |
8 | for ( node * nx = o.head; nx; nx = nx->next ) { | |
9 | *cr = new node{ *nx->value }; /***/ | |
10 | cr = &(*cr)->next; | |
[604e76d] | 11 | } |
[ac4dad2] | 12 | *cr = nullptr; |
[604e76d] | 13 | } |
14 | ||
[4c80a75] | 15 | void stack::clear() { |
16 | for ( node * nx = head; nx; ) { | |
17 | node * cr = nx; | |
18 | nx = cr->next; | |
19 | delete cr; | |
20 | } | |
21 | head = nullptr; | |
22 | } | |
23 | ||
[81e8ab0] | 24 | stack::stack() : head( nullptr ) {} |
25 | stack::stack( const stack & o ) { copy( o ); } | |
[604e76d] | 26 | stack::~stack() { clear(); } |
27 | ||
[81e8ab0] | 28 | stack & stack::operator=( const stack & o ) { |
[604e76d] | 29 | if ( this == &o ) return *this; |
30 | clear(); | |
[81e8ab0] | 31 | copy( o ); |
[604e76d] | 32 | return *this; |
33 | } | |
34 | ||
[4c80a75] | 35 | bool stack::empty() const { |
36 | return head == nullptr; | |
37 | } | |
[604e76d] | 38 | |
[4c80a75] | 39 | void stack::push( const object & value ) { |
40 | head = new node{ value, head }; /***/ | |
41 | } | |
[604e76d] | 42 | |
43 | ptr<object> stack::pop() { | |
[81e8ab0] | 44 | node * n = head; |
[604e76d] | 45 | head = n->next; |
[81e8ab0] | 46 | ptr<object> v = std::move( n->value ); |
[604e76d] | 47 | delete n; |
[81e8ab0] | 48 | return v; |
[604e76d] | 49 | } |
Note: See TracBrowser
for help on using the repository browser.