source: doc/papers/general/evaluation/cpp-vstack.cpp @ 81e8ab0

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 81e8ab0 was 81e8ab0, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

more updates

  • Property mode set to 100644
File size: 1.1 KB
Line 
1#include "cpp-vstack.hpp"
2#include <utility>
3
4stack::node::node( const object & v, node * n ) : value( v.new_copy() ), next( n ) {}
5
6void stack::copy( const stack & o ) {
7        node ** crnt = &head;
8        for ( node * next = o.head; next; next = next->next ) {
9                *crnt = new node{ *next->value }; /***/
10                crnt = &(*crnt)->next;
11        }
12        *crnt = nullptr;
13}
14
15void stack::clear() {
16        for ( node * next = head; next; ) {
17                node * crnt = next;
18                next = crnt->next;
19                delete crnt;
20        }
21        head = nullptr;
22}
23
24stack::stack() : head( nullptr ) {}
25stack::stack( const stack & o ) { copy( o ); }
26stack::stack( stack && o ) : head( o.head ) { o.head = nullptr; }
27stack::~stack() { clear(); }
28
29stack & stack::operator=( const stack & o ) {
30        if ( this == &o ) return *this;
31        clear();
32        copy( o );
33        return *this;
34}
35
36stack & stack::operator=( stack && o ) {
37        if ( this == &o ) return *this;
38        head = o.head;
39        o.head = nullptr;
40        return *this;
41}
42
43
44bool stack::empty() const { return head == nullptr; }
45
46void stack::push( const object & value ) { head = new node{ value, head }; /***/ }
47
48ptr<object> stack::pop() {
49        node * n = head;
50        head = n->next;
51        ptr<object> v = std::move( n->value );
52        delete n;
53        return v;
54}
Note: See TracBrowser for help on using the repository browser.