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

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

more changes

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