source: doc/generic_types/evaluation/cpp-stack.h@ c51b5a3

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 with_gc
Last change on this file since c51b5a3 was d919f47, checked in by Aaron Moss <a3moss@…>, 9 years ago

Update generics paper benchmarks

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