source: doc/generic_types/evaluation/cpp-vstack.cpp @ 2ccb93c

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 2ccb93c was b276be5, checked in by Aaron Moss <a3moss@…>, 7 years ago

Further updates to benchmarks (new C++ virtual benchmark still slightly broken)

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