source: doc/generic_types/evaluation/cpp-vstack.cpp @ 2183e12

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 2183e12 was a381b46, checked in by Aaron Moss <a3moss@…>, 7 years ago

Minor cleanup, also filled in benchmark source appendix

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