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

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 with_gc
Last change on this file since a9b1b0c was 860f19f, checked in by Aaron Moss <a3moss@…>, 8 years ago

Clean up code edits

  • Property mode set to 100644
File size: 1004 bytes
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
15 void clear() {
16 for ( node * next = head; next; ) {
17 node * crnt = next;
18 next = crnt->next;
19 delete crnt;
20 }
21 head = nullptr;
22 }
23
24 void copy( const stack<T> & o ) {
25 node ** crnt = &head;
26 for ( node * next = o.head; next; next = next->next ) {
27 *crnt = new node{ next->value }; /***/
28 crnt = &(*crnt)->next;
29 }
30 *crnt = nullptr;
31 }
32
33 ~stack() { clear(); }
34
35 stack & operator= ( const stack<T> & o ) {
36 if ( this == &o ) return *this;
37 clear();
38 copy( o );
39 return *this;
40 }
41
42 bool empty() const { return head == nullptr; }
43
44 void push( const T & value ) { head = new node{ value, head }; /***/ }
45
46 T pop() {
47 node * n = head;
48 head = n->next;
49 T v = std::move( n->value );
50 delete n;
51 return v;
52 }
53};
Note: See TracBrowser for help on using the repository browser.