Blame view

src/Parameters/ParamData.hh 8.2 KB
fbe3c2bb   Benjamin Renard   First commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/**
 * ParamData.hh
 *
 *  Created on: 17 oct. 2012
 *      Author: AKKA IS
 */

#ifndef PARAMDATA_HH_
#define PARAMDATA_HH_

#include <map>
#include <string>
#include <vector>
#include <cmath>
#include <math.h>
29f304b1   Benjamin Renard   Define Not A Numb...
16
#include <limits>
fbe3c2bb   Benjamin Renard   First commit
17
18
19
20
21
22
23
24
25
26
27
28

#include <boost/shared_ptr.hpp>

#include <log4cxx/logger.h>

#include "AMDA_exception.hh"

#include "Demangle.hh"
#include "Parameter.hh"
#include "Container.hh"
#include "VisitorOfParamData.hh"

263de286   Benjamin Renard   Apply time restri...
29
30
31
32
33
class NotANumber {
};

extern const NotANumber notANumber;

fbe3c2bb   Benjamin Renard   First commit
34
35
36
namespace AMDA {
namespace Parameters {

fbe3c2bb   Benjamin Renard   First commit
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
/**
 * @brief storage data of a Parameter
 *
 *	contain Time data and this implementation contain data value
 *	Respect design pattern Visitor is an visited element
 */
class ParamData {
public:
	typedef Container<double> BlockTimeTab;

	ParamData();
	virtual ~ParamData();

	/**
	 * get string accessor to an element
	 */
	static std::string getAccessor() {return "->get(index)";}

	BlockTimeTab& getTimeList() { return _blockTimeTab; }

	/**
	 * design pattern visitor
	 * accept a visitor
	 */
	virtual void accept(VisitorOfParamData &visitor) = 0;

	/**
	 * addTime in TimeSet
	 * @param dataNumber number data must be added in data set
	 * @param data data must be added in data set, the real type is known by the child of ParamData
	 */
	virtual void pushTime( double time) { _blockTimeTab.push_back(time); }

	/**
	 * addTime in TimeSet
	 * @param dataNumber number data must be added in data set
	 * @param data data must be added in data set, the real type is known by the child of ParamData
	 */
	virtual double getTime(unsigned int index) const { return _blockTimeTab[index]; }

	virtual void freeBefore(unsigned int pIndexMin) {
		_blockTimeTab.freeBefore(pIndexMin);
		_paramDataIndexInfo._startIndex=pIndexMin;
	}

	double getMinSampling() {
		return _minSampling;
	}
	void setMinSampling(double pMinSampling){  _minSampling = pMinSampling;}

	ParamDataIndexInfo &getIndexInfo() {
		_paramDataIndexInfo._nbDataToProcess = _blockTimeTab.size();
		return _paramDataIndexInfo;
	}


	virtual const char *getParamDataType() = 0;
	virtual const char *getElementType() = 0;

ad920fd6   Benjamin Renard   First implementat...
96
97
	virtual unsigned int getDim1()= 0;
	virtual unsigned int getDim2()= 0;
2f05d991   RENARD Benjamin   Generate param in...
98
99
        
        virtual void updateDims() = 0;
ad920fd6   Benjamin Renard   First implementat...
100

263de286   Benjamin Renard   Apply time restri...
101
102
	virtual void setNAN(unsigned int index) = 0;

fbe3c2bb   Benjamin Renard   First commit
103
104
	unsigned int getDataNumber() { return _blockTimeTab.size(); }

9f0d52de   Benjamin Renard   First test
105
106
	void setFillValue(double fillValue) { _fillValue = fillValue; }

fbe3c2bb   Benjamin Renard   First commit
107
108
109
110
111
112
113
114
115
protected:

	/** logger of paramData */
	static log4cxx::LoggerPtr _logger;
	/**
	 * must be updated by ApramGet or process which sampling modify
	 */
	double _minSampling;

9f0d52de   Benjamin Renard   First test
116
117
118

	double _fillValue;

fbe3c2bb   Benjamin Renard   First commit
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
private:


	/**
	 * vector of block of NB_DATA_BY_BLOCK data
	 */
	BlockTimeTab _blockTimeTab;

	ParamDataIndexInfo _paramDataIndexInfo;

};


template<class ElType, class ClassName>
class ParamDataSpec_CRTP : public ParamData{
public:
	/**
	 * alias of element type
	 */
	typedef ElType ElementType ;
	typedef ClassName ParamType ;

9f0d52de   Benjamin Renard   First test
141
	ParamDataSpec_CRTP(int dim1=1, int dim2=2) : _dim1(dim1), _dim2(dim2) {}
fbe3c2bb   Benjamin Renard   First commit
142
143
144
145
146
147
148
149
150
	~ParamDataSpec_CRTP() {}

	typedef Container<ElementType> DataList;

	const ElementType& get(unsigned int index) const { return _dataList[index]; }
	DataList& getDataList() { return _dataList; }
	const DataList& getDataList() const { return _dataList; }
	double getMinSampling(){ return _minSampling;}

9f0d52de   Benjamin Renard   First test
151
152
	void push(ElementType el) ;
	
fbe3c2bb   Benjamin Renard   First commit
153

ad920fd6   Benjamin Renard   First implementat...
154
155
156
	virtual unsigned int getDim1() { return _dim1; }
	virtual unsigned int getDim2() { return _dim2; }

2f05d991   RENARD Benjamin   Generate param in...
157
158
159
160
        void setDim1(unsigned int dim1) { _dim1 = dim1; }
	void setDim2(unsigned int dim2) { _dim2 = dim2; }
        

fbe3c2bb   Benjamin Renard   First commit
161
162
163
164
165
166
167
168
169
170
171
172
173
	/**
	 * design pattern visitor
	 * accept a visitor
	 */
	virtual void accept(VisitorOfParamData &visitor) {
		visitor.visit(dynamic_cast<ParamType*>(this));
	}

	virtual void freeBefore(unsigned int indexMin) { ParamData::freeBefore(indexMin); _dataList.freeBefore(indexMin); }

	virtual const char *getParamDataType() { return Helpers::Demangle(typeid(ClassName).name());}
	virtual const char *getElementType() {return Helpers::Demangle(typeid(ElType).name());}

2f05d991   RENARD Benjamin   Generate param in...
174
175
176
177
        virtual void updateDims() {
            if (_dataList.size() > 0)
                updateParamDataDims(get(0));
        }
263de286   Benjamin Renard   Apply time restri...
178
179
180
181
182
183

	virtual void setNAN(unsigned int index) {
		ElType val;
		val << NotANumber();
		_dataList.replace(val,index);
	}
9f0d52de   Benjamin Renard   First test
184
185

	
2f05d991   RENARD Benjamin   Generate param in...
186
        
fbe3c2bb   Benjamin Renard   First commit
187
private:
2f05d991   RENARD Benjamin   Generate param in...
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
    template<typename Type>
    void updateParamDataDims(Type /*data*/) {
        _dim1 = 1;
        _dim2 = 1;
    }
    
    template<typename Type>
    void updateParamDataDims(std::vector<Type> data) {
        _dim1 = data.size();
        _dim2 = 1;
    }

    template<typename Type>
    void updateParamDataDims(Tab2DData<Type> data) {
        _dim1 = data.getDim1Size();
        _dim2 = data.getDim2Size();
    }
    
    /**
     * vector of block of NB_DATA_BY_BLOCK data
    */
    DataList _dataList;

    unsigned int _dim1;
    unsigned int _dim2;
9f0d52de   Benjamin Renard   First test
213

2f05d991   RENARD Benjamin   Generate param in...
214
        
fbe3c2bb   Benjamin Renard   First commit
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
};

template <typename Type>
class ParamDataSpec;

template <class Type>
class ParamDataSpec: public ParamDataSpec_CRTP<Type, ParamDataSpec<Type> > {
public:
ParamDataSpec(int dim1=1,int dim2=1) : ParamDataSpec_CRTP<Type, ParamDataSpec<Type> >(dim1,dim2) {}
virtual ~ParamDataSpec() {}

};


	template <typename ElementType>
	ParamData* generate() { return NULL; }

	//ParamDataSpec<float> generate(float) { return ParamDataSpec<float>(); }
#define GENERATED_generate(Type) inline ParamDataSpec<Type> generate(Type) { return ParamDataSpec<Type>(); }\
inline ParamDataSpec<std::vector<Type> > generate(std::vector<Type>) { return ParamDataSpec<std::vector<Type> >(); }\
inline ParamDataSpec<Tab2DData<Type> > generate(Tab2DData<Type>) { return ParamDataSpec<Tab2DData<Type> >(); }

	GENERATED_generate(short)
	GENERATED_generate(float)
	GENERATED_generate(double)
	GENERATED_generate(long double)
	GENERATED_generate(int)
	GENERATED_generate(LogicalData)

} /* namespace Parameters */
} /* namespace AMDA */

fbe3c2bb   Benjamin Renard   First commit
247
248
249
250
251
252
253
254
class ElemNull {

};
extern const ElemNull elemNull;


#define GENARATED_SCALAIRE(type, val) \
		void operator <<(type &a, NotANumber b) ; \
9f0d52de   Benjamin Renard   First test
255
256
		void operator <<(type &a, ElemNull b) ; \
		void applyFillValue(type &a, double fillVal) ;
fbe3c2bb   Benjamin Renard   First commit
257

29f304b1   Benjamin Renard   Define Not A Numb...
258
GENARATED_SCALAIRE(short, SHRT_MIN)
fbe3c2bb   Benjamin Renard   First commit
259
260
261
GENARATED_SCALAIRE(float, NAN)
GENARATED_SCALAIRE(double, NAN)
GENARATED_SCALAIRE(long double, NAN)
29f304b1   Benjamin Renard   Define Not A Numb...
262
GENARATED_SCALAIRE(int, INT_MIN)
fbe3c2bb   Benjamin Renard   First commit
263
264
265
266
GENARATED_SCALAIRE(AMDA::Parameters::LogicalData, AMDA::Parameters::LogicalData::NaN)

#define GENARATED_VECTOR(type, val) \
		void operator <<(std::vector<type> &a, NotANumber b); \
9f0d52de   Benjamin Renard   First test
267
268
		void operator <<(std::vector<type> &a, ElemNull b); \
		void applyFillValue(std::vector<type> &a, double fillVal); 
fbe3c2bb   Benjamin Renard   First commit
269

29f304b1   Benjamin Renard   Define Not A Numb...
270
GENARATED_VECTOR(short,SHRT_MIN)
fbe3c2bb   Benjamin Renard   First commit
271
272
273
GENARATED_VECTOR(float, NAN)
GENARATED_VECTOR(double, NAN)
GENARATED_VECTOR(long double, NAN)
29f304b1   Benjamin Renard   Define Not A Numb...
274
GENARATED_VECTOR(int, INT_MIN)
fbe3c2bb   Benjamin Renard   First commit
275
276
277
278
GENARATED_VECTOR(AMDA::Parameters::LogicalData, AMDA::Parameters::LogicalData::NaN)

#define GENARATED_TAB2D(type, val) \
		void operator <<(AMDA::Parameters::Tab2DData<type> &a, NotANumber b); \
9f0d52de   Benjamin Renard   First test
279
280
		void operator <<(AMDA::Parameters::Tab2DData<type> &a, ElemNull b); \
		void applyFillValue(AMDA::Parameters::Tab2DData<type> &a, double fillVal);
fbe3c2bb   Benjamin Renard   First commit
281

29f304b1   Benjamin Renard   Define Not A Numb...
282
GENARATED_TAB2D(short,SHRT_MIN)
fbe3c2bb   Benjamin Renard   First commit
283
284
285
GENARATED_TAB2D(float, NAN)
GENARATED_TAB2D(double, NAN)
GENARATED_TAB2D(long double, NAN)
29f304b1   Benjamin Renard   Define Not A Numb...
286
GENARATED_TAB2D(int, INT_MIN)
fbe3c2bb   Benjamin Renard   First commit
287
288
GENARATED_TAB2D(AMDA::Parameters::LogicalData, AMDA::Parameters::LogicalData::NaN)

9f0d52de   Benjamin Renard   First test
289
290
291
292
293
294
295
template<class ElType, class ClassName>
void AMDA::Parameters::ParamDataSpec_CRTP<ElType, ClassName>::push(ElementType el) {
		if (!std::isnan(_fillValue))
			applyFillValue(el,_fillValue);
		_dataList.push_back(el); 
}

fbe3c2bb   Benjamin Renard   First commit
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
bool isNAN(short pval);
bool isNAN(float pval);
bool isNAN(double pval);
bool isNAN(long double pval);
bool isNAN(int pval);
bool isNAN(AMDA::Parameters::LogicalData pval);

bool isNAN(std::vector<short> pval);
bool isNAN(std::vector<float> pval);
bool isNAN(std::vector<double> pval);
bool isNAN(std::vector<long double> pval);
bool isNAN(std::vector<int> pval);
bool isNAN(std::vector<AMDA::Parameters::LogicalData> pval);

bool isNAN(AMDA::Parameters::Tab2DData<short> pval);
bool isNAN(AMDA::Parameters::Tab2DData<float> pval);
bool isNAN(AMDA::Parameters::Tab2DData<double> pval);
bool isNAN(AMDA::Parameters::Tab2DData<long double> pval);
bool isNAN(AMDA::Parameters::Tab2DData<int> pval);
bool isNAN(AMDA::Parameters::Tab2DData<AMDA::Parameters::LogicalData> pval);

71249c7c   Benjamin Renard   Fix fillValue det...
317
318
bool isApproximatelyEqual(double a, double b);

fbe3c2bb   Benjamin Renard   First commit
319
#endif /* PARAMDATA_HH_ */