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

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

shorten experimental code

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