forked from Conedy/Conedy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparams.h
231 lines (137 loc) · 5.81 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
#ifndef params_h
#define params_h params_h
#include <map>
#include "networkConstants.h"
#include <iostream>
#include <vector>
//#include "node.h"
#include <boost/function.hpp>
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);
void randomizeParameter(string s, networkElementType n, boost::function<T ()> r); // require: Nodetype ist richtig !
static void initialise( void (*callback) (string,T *));
//! 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
//! Definiert die Parameter vom Momentanen sheet um.
void setSheet (unsigned int theRow, vector <T> argList);
bool compareSheets (unsigned int sheet1, unsigned int sheet2);
//! Setzt alle Parameter, im lokalen sheet
void setSheet( vector <T> argList);
//! einfacher Zugriff auf die Parameter
void setParams(const unsigned short i, T value) { *param[row][i]=value; }
//! obsolete ?
T getParams(const unsigned short which) const { return *param[row][which]; };
void setParam(string parameterName, T value);
T& getParam(string name) const { return *param[row][adress[name].second]; }
T * getParamPointer (const unsigned short which) { return param[row][which];}
//! 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);
static void setStandard(string name, T d);
};
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; TODO find a solution that works with vector<double>
}
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