summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/yap/example/autodiff_library/Tape.h
blob: 9ca62c4df0c463c69b1c5c7a18572dc16467e611 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 * Tape.h
 *
 *  Created on: 15 Apr 2013
 *      Author: s0965328
 */

#ifndef TAPE_H_
#define TAPE_H_

#include <vector>
#include <string>
#include <cassert>
#include <sstream>
#include <iostream>


namespace AutoDiff {

using namespace std;
#define TT 	(Tape<double>::valueTape)
#define II 	(Tape<unsigned int>::indexTape)

template<typename T> class Tape {
public:
	Tape<T> () : index(0){};
	T& at(const unsigned int index);
	const T& get(const unsigned int index);
	void set(T& v);
	unsigned int size();
	void clear();
	bool empty();
	string toString();
	virtual ~Tape();

	vector<T> vals;
	unsigned int index;

	static Tape<double>* valueTape;
	static Tape<unsigned int>* indexTape;
};


template<typename T> Tape<T>::~Tape<T>()
{
	index = 0;
	vals.clear();
}

template<typename T> T& Tape<T>::at(const unsigned int i)
{
	assert(this->vals.size()>i);
	return vals[i];
}
template<typename T> const T& Tape<T>::get(const unsigned int i)
{
	assert(this->vals.size()>i);
	return vals[i];
}
template <typename T> void Tape<T>::set(T& v)
{
	vals.push_back(v);
	index++;
}

template<typename T> unsigned int Tape<T>::size()
{
	return this->vals.size();
}

template<typename T> bool Tape<T>::empty()
{
	return vals.empty();
}

template<typename T> void Tape<T>::clear()
{
	this->vals.clear();
	this->index = 0;
	assert(this->vals.size()==0);
	assert(this->vals.empty());
}

template<typename T>  string Tape<T>::toString()
{
	assert(vals.size()>=index);
	ostringstream oss;
	oss<<"Tape size["<<vals.size()<<"]";
	for(unsigned int i=0;i<vals.size();i++){
		if(i%10==0) oss<<endl;
		oss<<vals[i]<<",";
	}
	oss<<endl;
	return oss.str();
}
}
#endif /* TAPE_H_ */