source: doc/papers/general/evaluation/cpp-vstack.cpp @ 3d8f2f8

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 3d8f2f8 was 8b001bd, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

more changes

  • 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::clear() {
7        for ( node * next = head; next; ) {
8                node * crnt = next;
9                next = crnt->next;
10                delete crnt;
11        }
12        head = nullptr;
13}
14
15void stack::copy( const stack & o ) {
16        node ** crnt = &head;
17        for ( node * next = o.head; next; next = next->next ) {
18                *crnt = new node{ *next->value }; /***/
19                crnt = &(*crnt)->next;
20        }
21        *crnt = 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
43bool stack::empty() const { return head == nullptr; }
44
45void stack::push( const object & value ) { head = new node{ value, head }; /***/ }
46
47ptr<object> stack::pop() {
48        node * n = head;
49        head = n->next;
50        ptr<object> v = std::move( n->value );
51        delete n;
52        return v;
53}
Note: See TracBrowser for help on using the repository browser.