VisitorOfParamData.hh
6.12 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
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
213
214
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/**
* VisitorOfParamData.hh
*
* Created on: 17 oct. 2012
* Author: AKKA IS
*/
#ifndef VISITOROFPARAMDATA_HH_
#define VISITOROFPARAMDATA_HH_
#include <vector>
#include <cstddef>
#include <iostream>
namespace AMDA {
namespace Parameters {
template <typename Type>
class TabRow : public std::vector<Type>
{
public:
TabRow() : _size(0)
{
}
~TabRow()
{
free();
}
void reallocate(int size)
{
free();
if (size > 0)
{
std::vector<Type>::resize(size);
_size = size;
}
}
void free()
{
std::vector<Type>::clear();
_size = 0;
}
Type& operator[](const int col)
{
if (col < 0 || col >= _size)
throw;
return std::vector<Type>::at(col);
}
const Type& operator[](const int col) const
{
if (col < 0 || col >= _size)
throw;
return std::vector<Type>::at(col);
}
inline TabRow& operator = (std::vector<Type>& vector)
{
if ((int)vector.size() != _size)
throw;
int crtIndex = 0;
for (typename std::vector<Type>::const_iterator it = vector.begin(); it != vector.end(); ++it) {
std::vector<Type>::at(crtIndex) = (*it);
++crtIndex;
}
return (*this);
}
private:
int _size;
};
template <typename Type>
class Tab2DData
{
public :
Tab2DData(void) : _dim1Size(0), _dim2Size(0), _rows(NULL)
{
}
Tab2DData(int dim1Size, int dim2Size) : _dim1Size(dim1Size), _dim2Size(dim2Size), _rows(NULL)
{
reallocate(dim1Size,dim2Size);
}
Tab2DData(const Tab2DData& data) : Tab2DData()
{
reallocate(data.getDim1Size(),data.getDim2Size());
for (int i = 0; i < data.getDim1Size(); ++i)
for (int j = 0; j < data.getDim2Size(); ++j)
{
_rows[i][j] = data[i][j];
}
}
~Tab2DData(void)
{
free();
}
void reallocate(int dim1Size, int dim2Size)
{
free();
_dim1Size = dim1Size;
_dim2Size = dim2Size;
if ((_dim1Size <= 0) || (_dim2Size <= 0))
return;
_rows = new TabRow<Type>[_dim1Size];
for (int i = 0; i < _dim1Size; ++i)
_rows[i].reallocate(dim2Size);
}
void free()
{
if (_rows != NULL)
delete[] _rows;
_rows = NULL;
_dim1Size = 0;
_dim2Size = 0;
}
TabRow<Type>& operator[](const int row)
{
if (row < 0 || row >= _dim1Size)
throw;
return _rows[row];
}
const TabRow<Type>& operator[](const int row) const
{
if (row < 0 || row >= _dim1Size)
throw;
return _rows[row];
}
int getDim1Size(void) const
{
return _dim1Size;
}
int getDim2Size(void) const
{
return _dim2Size;
}
int size(void) const
{
return _dim1Size*_dim2Size;
}
std::vector<Type> getColumn(const int col) const
{
if (col < 0 || col >= _dim2Size)
throw;
std::vector<Type> r;
for (int i = 0; i < _dim1Size; ++i) {
r.push_back(_rows[i][col]);
}
return r;
}
Type getTotal(void)
{
Type r; /* << ElemNull();
for (int i = 0; i < _dim1Size*_dim2Size; ++i)
r += _data[i];*/
return r;
}
std::vector<Type> getTotalLine(void)
{
std::vector<Type> r;
/*for (int i = 0; i < _dim1Size; ++i)
{
//for each line, compute sum of column
Type sum << ElemNull();
for (int j = 0; j < _dim2Size; ++j)
sum += _data[i*_dim2Size+j];
r.push_back(sum);
}*/
return r;
}
std::vector<Type> getTotalColumn(void)
{
std::vector<Type> r;
/*for (int i = 0; i < _dim2Size; ++i)
{
//for each column, compute sum of line
Type sum << ElemNull();
for (int j = 0; j < _dim1Size; ++j)
sum += _data[j*_dim2Size+i];
r.push_back(sum);
}*/
return r;
}
inline Tab2DData& operator = (const Tab2DData& data)
{
reallocate(data.getDim1Size(),data.getDim2Size());
for (int i = 0; i < data.getDim1Size(); ++i)
for (int j = 0; j < data.getDim2Size(); ++j)
{
_rows[i][j] = data[i][j];
}
return (*this);
}
private:
int _dim1Size;
int _dim2Size;
TabRow<Type>* _rows;
};
enum LogicalData{
False = 0,
True = 1,
NaN = 2
};
template <typename Type>
class ParamDataSpec;
//scalar data
typedef class ParamDataSpec<short> ParamDataScalaireShort;
typedef class ParamDataSpec<float> ParamDataScalaireFloat;
typedef class ParamDataSpec<double> ParamDataScalaireDouble;
typedef class ParamDataSpec<long double> ParamDataScalaireLongDouble;
typedef class ParamDataSpec<int> ParamDataScalaireInt;
typedef class ParamDataSpec<LogicalData> ParamDataLogicalData;
//tab 1D data
typedef class ParamDataSpec<std::vector<short> > ParamDataTab1DShort;
typedef class ParamDataSpec<std::vector<float> > ParamDataTab1DFloat;
typedef class ParamDataSpec<std::vector<double> > ParamDataTab1DDouble;
typedef class ParamDataSpec<std::vector<long double> > ParamDataTab1DLongDouble;
typedef class ParamDataSpec<std::vector<int> > ParamDataTab1DInt;
typedef class ParamDataSpec<std::vector<LogicalData> > ParamDataTab1DLogicalData;
//tab 2D data
typedef class ParamDataSpec<Tab2DData<short>> ParamDataTab2DShort;
typedef class ParamDataSpec<Tab2DData<float>> ParamDataTab2DFloat;
typedef class ParamDataSpec<Tab2DData<double>> ParamDataTab2DDouble;
typedef class ParamDataSpec<Tab2DData<long double>> ParamDataTab2DLongDouble;
typedef class ParamDataSpec<Tab2DData<int>> ParamDataTab2DInt;
typedef class ParamDataSpec<Tab2DData<LogicalData>> ParamDataTab2DLogicalData;
/**
* AMDA::Parameters::VisitorOfParamData
*/
class VisitorOfParamData {
public:
VisitorOfParamData() {}
virtual ~VisitorOfParamData() {}
virtual void visit(ParamDataScalaireShort *) = 0;
virtual void visit(ParamDataScalaireFloat *) = 0;
virtual void visit(ParamDataScalaireDouble *) = 0;
virtual void visit(ParamDataScalaireLongDouble *) = 0;
virtual void visit(ParamDataScalaireInt *) = 0;
virtual void visit(ParamDataLogicalData *) = 0;
virtual void visit(ParamDataTab1DShort *) = 0;
virtual void visit(ParamDataTab1DFloat *) = 0;
virtual void visit(ParamDataTab1DDouble *) = 0;
virtual void visit(ParamDataTab1DLongDouble *) = 0;
virtual void visit(ParamDataTab1DInt *) = 0;
virtual void visit(ParamDataTab1DLogicalData *) = 0;
virtual void visit(ParamDataTab2DShort *) = 0;
virtual void visit(ParamDataTab2DFloat *) = 0;
virtual void visit(ParamDataTab2DDouble *) = 0;
virtual void visit(ParamDataTab2DLongDouble *) = 0;
virtual void visit(ParamDataTab2DInt *) = 0;
virtual void visit(ParamDataTab2DLogicalData *) = 0;
};
} /* namespace Parameters */
} /* namespace AMDA */
#endif /* VISITOROFPARAMDATA_HH_ */