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

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumwith_gc
Last change on this file since ac4dad2 was ac4dad2, checked in by Peter A. Buhr <pabuhr@…>, 6 years ago

shorten experimental code

  • Property mode set to 100644
File size: 970 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 * nx = head; nx; ) {
17                        node * cr = nx;
18                        nx = cr->next;
19                        delete cr;
20                }
21                head = nullptr;
22        }
23
24        void copy( const stack<T> & o ) {
25                node ** cr = &head;
26                for ( node * nx = o.head; nx; nx = nx->next ) {
27                        *cr = new node{ nx->value }; /***/
28                        cr = &(*cr)->next;
29                }
30                *cr = 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.