source: doc/generic_types/evaluation/cpp-vstack.cpp@ 3fb7f5e

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new stuck-waitfor-destruct with_gc
Last change on this file since 3fb7f5e was 3fb7f5e, checked in by Aaron Moss <a3moss@…>, 9 years ago

Update benchmarks, cleanup edits to the evaluation section

  • Property mode set to 100644
File size: 1.3 KB
Line 
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 head = nullptr;
28}
29
30stack::stack() : head(nullptr) {}
31
32stack::stack(const stack& o) { copy(o); }
33
34stack::stack(stack&& o) : head(o.head) { o.head = nullptr; }
35
36stack::~stack() { clear(); }
37
38stack& stack::operator= (const stack& o) {
39 if ( this == &o ) return *this;
40 clear();
41 copy(o);
42 return *this;
43}
44
45stack& stack::operator= (stack&& o) {
46 if ( this == &o ) return *this;
47 head = o.head;
48 o.head = nullptr;
49 return *this;
50}
51
52bool stack::empty() const {
53 return head == nullptr;
54}
55
56void stack::push(std::unique_ptr<object>&& value) {
57 head = new node{ std::move(value), head }; /***/
58}
59
60std::unique_ptr<object> stack::pop() {
61 node* n = head;
62 head = n->next;
63 std::unique_ptr<object> x = std::move(n->value);
64 delete n;
65 return x;
66}
Note: See TracBrowser for help on using the repository browser.