source: doc/papers/general/evaluation/cpp-stack.hpp@ 81e8ab0

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 81e8ab0 was 81e8ab0, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

more updates

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