1 /* 2 * Tape.h 3 * 4 * Created on: 15 Apr 2013 5 * Author: s0965328 6 */ 7 8 #ifndef TAPE_H_ 9 #define TAPE_H_ 10 11 #include <vector> 12 #include <string> 13 #include <cassert> 14 #include <sstream> 15 #include <iostream> 16 17 18 namespace AutoDiff { 19 20 using namespace std; 21 #define TT (Tape<double>::valueTape) 22 #define II (Tape<unsigned int>::indexTape) 23 24 template<typename T> class Tape { 25 public: 26 Tape<T> () : index(0){}; 27 T& at(const unsigned int index); 28 const T& get(const unsigned int index); 29 void set(T& v); 30 unsigned int size(); 31 void clear(); 32 bool empty(); 33 string toString(); 34 virtual ~Tape(); 35 36 vector<T> vals; 37 unsigned int index; 38 39 static Tape<double>* valueTape; 40 static Tape<unsigned int>* indexTape; 41 }; 42 43 44 template<typename T> Tape<T>::~Tape<T>() 45 { 46 index = 0; 47 vals.clear(); 48 } 49 at(const unsigned int i)50template<typename T> T& Tape<T>::at(const unsigned int i) 51 { 52 assert(this->vals.size()>i); 53 return vals[i]; 54 } get(const unsigned int i)55template<typename T> const T& Tape<T>::get(const unsigned int i) 56 { 57 assert(this->vals.size()>i); 58 return vals[i]; 59 } set(T & v)60template <typename T> void Tape<T>::set(T& v) 61 { 62 vals.push_back(v); 63 index++; 64 } 65 size()66template<typename T> unsigned int Tape<T>::size() 67 { 68 return this->vals.size(); 69 } 70 empty()71template<typename T> bool Tape<T>::empty() 72 { 73 return vals.empty(); 74 } 75 clear()76template<typename T> void Tape<T>::clear() 77 { 78 this->vals.clear(); 79 this->index = 0; 80 assert(this->vals.size()==0); 81 assert(this->vals.empty()); 82 } 83 toString()84template<typename T> string Tape<T>::toString() 85 { 86 assert(vals.size()>=index); 87 ostringstream oss; 88 oss<<"Tape size["<<vals.size()<<"]"; 89 for(unsigned int i=0;i<vals.size();i++){ 90 if(i%10==0) oss<<endl; 91 oss<<vals[i]<<","; 92 } 93 oss<<endl; 94 return oss.str(); 95 } 96 } 97 #endif /* TAPE_H_ */ 98