forked from Conedy/Conedy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparams.h
436 lines (287 loc) · 10.3 KB
/
params.h
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#ifndef params_h
#define params_h params_h
#include <map>
#include "networkConstants.h"
#include <iostream>
#include <vector>
//#include "node.h"
#include <boost/function.hpp>
// using namespace boost;
namespace conedy
{
using namespace std;
/*!
\Brief Class for storing parameters.
Via registerStandard parameters are registered with standard values. Parameters are stored in sheets which allows nodes to share parameters to conserve memory. Each node only remembers the sheet number of its parameters.
*/
template <typename T>
class params
{
protected:
//! Ablage für alle Parameter. Eine Node/Edge belegt eine Reihe (row) Speicherplatz
static vector<vector<T*> > param;
//! Anzahl der Standartparameter (Wird beim Start von Conedy hochgezählt)
static unsigned int numberOfStandardParameterSheets;
//! Counter for the number of nodes, which use a certain sheet.
static vector <unsigned int> usageCounter;
//! Anzahl der bisher belegten Reihen
static unsigned int Nparams;
//! Zuordnung von "nodetype" zu Reihennummer mit Standardparametern
static map<networkElementType, int> standardParam;
//! Zuordnung von string zu (standardadresse, parameternummer)
static map <string, pair<int, int> > adress;
static map <pair<int,int>, string > name;
public:
//! The parameter sheet number of this instance.
unsigned int row;
//! Prints all prameters to the console for debug reasons.
void printStatistics();
static T getStandardParameter(networkElementType n, unsigned int number) { return * param[standardParam[n]][number]; }
// ###### KONSTRUKTOREN ######
//! Copyconstructor: Copies the sheet-number for the new node.
params ( const params<T> &b )
{
row = b.row;
if (!isStandard())
usageCounter.at(row)++;
}
params(const unsigned int N) { vector <T> newVec(N); param.push_back(newVec); row = Nparams; Nparams++; }
//! Copy-Konstructor, der für das erstellte Objekt, Standard-Parameter verwendet (Ist das sinnvoll?) TODO
params(networkElementType theNodeType);
//! Destruktor: Verringert den usageCounter um 1. Wenn usageCounter==1 wird der Parameter gelöscht.
virtual ~params()
{
if (usageCounter.at(row) == 1)
{
if (!isStandard())
{
for (unsigned int i = 0; i < param[row].size(); i++)
delete param[row][i];
}
usageCounter.at(row)=0;
}
else if (usageCounter.at(row)>1)
usageCounter.at(row)--;
};
// virtual void registerStandardValues() { throw "ERROROROR";};
//! Funktion zum Test, ob ein Parameter Std oder individuell ist.
bool isStandard() { return row < numberOfStandardParameterSheets;}
vector<T*> getParameters() { return param[row]; }
/*!
Funktion zum Abfragen der NodeType
\param string s
\return networkElementType
*/
static networkElementType getNodeTypeFromString(string s)
{
if (adress.count(s) == 0)
throw "String does not correspond to node parameter!";
int standardAdress = adress[s].first;
map<networkElementType, int>::iterator ia,ie;
ia = standardParam.begin();
ie = standardParam.end();
for (; ia != ie; ia++)
{
if (ia->second == standardAdress)
return ia->first;
}
throw "String does not correspond to node parameter!";
return _undefinedNodeType_;
}
void randomizeParameter(string s, networkElementType n, boost::function<double ()> r) // require: Nodetype ist richtig !
{
unsigned int place = (unsigned int )adress[s].second;
if (isStandard())
{
vector <T*> newVec(param[row].size());
param.push_back(newVec);
// param[Nparams].resize( param[row].size());
for (unsigned int i = 0; i < param[row].size(); i++)
{
if (i != place)
param[Nparams][i] = new T(*(param[row][i]));
else
param[Nparams][i] = new T(r());
}
row = Nparams;
Nparams++;
usageCounter.push_back(1);
}
else
{
// delete param[row][place];
*(param[row][place]) = r();
}
}
static void randomizeParameter( string parameterString, boost::function<double ()> r)
{
randomizeParameter(parameterString, getNodeTypeFromString(parameterString), r);
}
//static void initialise( void (*callback) (string,double *))
static void initialise( void (*callback) (string,T *))
{
map <string, pair<int, int> >::iterator it;
for (it = adress.begin(); it != adress.end(); it++)
{
(*callback)(it->first, param[it->second.first][it->second.second]);
}
}
//! Funktion zum Erstellen individueller Parameter aus einer Blaupause heraus. Neuer Speicherplatz wird hier zur Verfügung gestellt.
//
void rerouteParams (vector <T> argList) // am Besten nur einmal aufrufen solange die Parameter noch an der Standardstelle stehen
{
if (argList.size() != param[row].size())
{
throw "Fehler. falsche Argumenten Anzahl für Node";
}
else
{
if (usageCounter.at(row)>1)
usageCounter.at(row)--;
vector <T*> newVec; param.push_back(newVec); row = Nparams;
usageCounter.push_back(1);
Nparams++;
param[row].resize( argList.size());
for (unsigned int i = 0; i < argList.size(); i++)
param[row][i] = new T(argList[i]);
}
}
//! Definiert die Parameter vom Momentanen sheet um.
void setSheet (unsigned int theRow, vector <T> argList)
{
if (argList.size() != param[theRow].size())
{
throw "Fehler. falsche Argumenten Anzahl für Node";
}
else
{
for (unsigned int i = 0; i < argList.size(); i++)
*param[theRow][i] = argList[i];
}
}
bool compareSheets (unsigned int sheet1, unsigned int sheet2)
{
if (param [sheet1].size() != param [sheet2].size())
return 0;
for (unsigned int i = 0 ; i < param[sheet1].size() ; i++)
{
if (param[sheet1][i] != param [sheet2][i])
return 0;
}
return 1;
}
//! Setzt alle Parameter, im lokalen sheet
void setSheet( vector <T> argList)
{
if (argList.size() != param[row].size())
{
throw "Fehler. falsche Argumenten Anzahl für Node";
}
else
{
for (unsigned int i = 0; i < argList.size(); i++)
*param[row][i] = argList[i];
}
}
//! einfacher Zugriff auf die Parameter
T operator[] (const unsigned int n) { return param[row][n]; };
void setParams(const unsigned short i, T value) { *param[row][i]=value; }
// void setParams(inStream& in) { for (unsigned short i = 0; i < param[row].size(); i++) in >> param[row][i]; }
//! obsolete ?
T getParams(const unsigned short which) const { return *param[row][which]; };
void setParam(string parameterName, T value)
{
if (isStandard())
{
vector <T> newParams;
unsigned int toChange = adress[parameterName].second;
for (unsigned int i = 0; i < param[row].size(); i++)
{
if (toChange == i)
newParams.push_back(value);
else
newParams.push_back(*param[row][i]);
}
rerouteParams(newParams);
}
else
*param[row][adress[parameterName].second] = value;
}
T& getParam(string name) const { return *param[row][adress[name].second]; }
T * getParamPointer (const unsigned short which) { return param[row][which];}
// virtual valarray<T>* getParams() { return ¶m; }
//! Setzt Standardparameter fest für den nodeType theNodeType. Mitübergeben wird der Name des Parameters (Im Parser), ein Standarwert und eine laufende Nummer
static void registerStandard(networkElementType theNodeType, string s, unsigned int paramNumber, T value)
{
if (standardParam.count(theNodeType) == 0)
{
vector <T*> newVec; param.push_back(newVec);
usageCounter.push_back(0);
standardParam[theNodeType] = Nparams;
//usageCounter[Nparams] = 1;
Nparams++;
numberOfStandardParameterSheets++;
}
if (paramNumber +1 > param[standardParam[theNodeType]].size())
param[standardParam[theNodeType]].resize(paramNumber+1,0);
adress[s] = make_pair(standardParam[theNodeType], paramNumber);
name[ make_pair(standardParam[theNodeType], paramNumber)] = s;
param[standardParam[theNodeType]][paramNumber] = new T (value);
}
static void setStandard(string name, T d)
{
if (adress.count (name) != 0 )
*param[adress[name].first][adress[name].second] = d;
else
throw "setting with unknown string!";
}
// static void copyStandardSheet(networkElementType
//! obsolete
static void printAll() {
map<string, pair<int,int> >::iterator it;
for (it = adress.begin(); it != adress.end();it++)
cout << it->first << " " << *param[it->second.first][it->second.second] << endl;
};
};
template <typename T>
vector<vector<T*> > params<T>::param;
template <typename T>
unsigned int params<T>::Nparams;
template <typename T>
unsigned int params<T>::numberOfStandardParameterSheets = 0;
template <typename T>
map<networkElementType, int> params<T>::standardParam;
template <typename T>
map <string, pair<int, int> > params<T>::adress;
template <typename T>
map <pair<int, int>,string > params<T>::name;
template <typename T>
vector <unsigned int> params<T>::usageCounter;
template <typename T>
void params<T>::printStatistics()
{
cout << "parameter:" << endl;
if (isStandard())
cout << " standard parameter" << endl;
// cout << " row: " << row << endl;
for (unsigned int i = 0; i < param[row].size(); i++)
cout << " " << name[make_pair(row, i)] << ": \t " << *param[row][i] << " " << endl;
}
template <typename T>
params<T>::params(networkElementType theNodeType)
{
if (standardParam.count(theNodeType) == 0)
{
vector <T*> newVec; param.push_back(newVec);
usageCounter.push_back(0);
standardParam[theNodeType] = Nparams;
//usageCounter[Nparams] = 1;
Nparams++;
numberOfStandardParameterSheets++;
}
row = standardParam[theNodeType];
// usageCounter.push_back(1);
//cout << theNodeType << " " << row << endl;
}
}
#endif