00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00030
00031
00032 #ifndef _GAIN_STATE_H
00033 #define _GAIN_STATE_H
00034
00035 #include <fstream>
00036 #include <map>
00037 using namespace std;
00038
00039 #include "old-annotations.h"
00040
00041
00042
00043 typedef MyGainAnnotationType MyGainTriStateType;
00044
00045
00046
00047
00048 enum MyGainBiStateType { BI_HYPOTHETICAL_STATE = 0, BI_ANNOTATED_STATE};
00049
00050
00051
00052
00053
00054 inline MyGainTriStateType convertState(MyGainBiStateType in)
00055 {
00056 if (BI_HYPOTHETICAL_STATE == in)
00057 return(HYPOTHETICAL_STATE);
00058 if (BI_ANNOTATED_STATE == in)
00059 return(ANNOTATED_STATE);
00060 }
00061
00062
00070
00071 template < typename StateType >
00072 class MyGainState
00073 {
00074 private:
00075
00076 map< StateType, MyNT > _stateWeights;
00077 public:
00078 MyGainState()
00079 : _stateWeights()
00080 {}
00090 MyGainState(StateType state, MyNT w = 1)
00091 : _stateWeights()
00092 {
00093
00094 _stateWeights[state] = w;
00095 this->_sanityCheck();
00096 }
00097 virtual ~MyGainState()
00098 {}
00099
00102 virtual const MyGainState &operator+=(const MyGainState &rhs)
00103 {
00104 typename map< StateType, MyNT >::const_iterator itr;
00105 for (itr = rhs._stateWeights.begin(); itr != rhs._stateWeights.end(); itr++)
00106 _stateWeights[itr->first] += itr->second;
00107 this->_sanityCheck();
00108 return(*this);
00109 }
00110
00112 virtual const MyGainState &operator*(MyNT weight)
00113 {
00114 typename map< StateType, MyNT >::const_iterator itr;
00115 for (itr = _stateWeights.begin(); itr != _stateWeights.end(); itr++)
00116 _stateWeights[itr->first] *= weight;
00117 this->_sanityCheck();
00118 return(*this);
00119 }
00120
00121 virtual bool operator==(const MyGainState &rhs) const
00122 {
00123 return(isEqual(rhs));
00124 }
00125
00126 virtual bool isEqual(const MyGainState &rhs) const
00127 {
00128 typename map< StateType, MyNT >::const_iterator itr;
00129 for (itr = rhs._stateWeights.begin(); itr != rhs._stateWeights.end(); itr++)
00130 if (_stateWeights.find(itr->first)->second != itr->second)
00131 return(false);
00132 return(true);
00133 }
00134
00135 virtual bool isEqual(const MyGainState &rhs, MyNT epsilon) const
00136 {
00137 typename map< StateType, MyNT >::const_iterator itr;
00138 for (itr = rhs._stateWeights.begin(); itr != rhs._stateWeights.end(); itr++)
00139 if (fabs(_stateWeights.find(itr->first)->second - itr->second) > epsilon)
00140 return(false);
00141 return(true);
00142 }
00143
00148 MyNT getWeight(StateType state) const
00149 {
00150 typename map< StateType, MyNT >::const_iterator itr;
00151 itr = _stateWeights.find(state);
00152 if (_stateWeights.end() == itr)
00153 return(0);
00154 return(itr->second);
00155 }
00156
00164 StateType collapse(MyNT threshold = 0);
00165
00168 ostream &print(ostream &str) const
00169 {
00170 typename map< StateType, MyNT >::const_iterator itr;
00171 for (itr = _stateWeights.begin(); itr != _stateWeights.end(); itr++)
00172 str << itr->first << ": " << itr->second << "\t";
00173
00174 return(str);
00175 }
00176
00177 void _sanityCheck() const
00178 {
00179 typename map< StateType, MyNT >::const_iterator itr;
00180 for (itr = _stateWeights.begin(); itr != _stateWeights.end(); itr++)
00181 {
00182 if (itr->second < 0)
00183 cerr << "MyGainState::_sanityCheck: negative weight for state " << itr->first << endl;
00184 }
00185 }
00186 };
00187
00190 template< typename StateType >
00191 class MyGainStateInfo
00192 {
00193
00194 private:
00195
00196 string id;
00197
00198 StateType initialState;
00199
00200 StateType currentState;
00201
00202 StateType previousState;
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215 bool clamped;
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226 public:
00227
00228 string getId() const
00229 {
00230 return(id);
00231 }
00232
00233 void setId(string _id)
00234 {
00235 id = _id;
00236 }
00237
00238 StateType getInitialState() const
00239 {
00240 return(initialState);
00241 }
00242
00243 StateType getCurrentState() const
00244 {
00245 return(currentState);
00246 }
00247
00248 StateType getPreviousState() const
00249 {
00250 return(previousState);
00251 }
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292 StateType getState() const
00293 {
00294 return(currentState);
00295 }
00296
00297
00298
00299 void setState(StateType newState)
00300 {
00301 previousState = currentState;
00302 currentState = newState;
00303 }
00304
00305 void initialiseState(StateType state)
00306 {
00307
00308 initialState = state;
00309 currentState = previousState = initialState;
00310 #ifdef DEBUG
00311
00312 #endif// DEBUG
00313 }
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00368 bool hasConverged()
00369 {
00370 return(currentState.collapse() == previousState.collapse());
00371 }
00372
00373
00380 bool hasConvergedApproximately(MyNT epsilon = 1e-03) const
00381 {
00382 return(currentState.isEqual(previousState, epsilon));
00383 }
00384
00385
00386 bool isClamped() const
00387 {
00388 return(clamped);
00389 }
00390
00391 void clamp()
00392 {
00393 clamped = true;
00394 }
00395
00396 void unclamp()
00397 {
00398 clamped = false;
00399 }
00400
00401 };
00402
00403
00404
00405 typedef MyGainState< MyGainBiStateType > MyGainBiState;
00406
00407 typedef MyGainStateInfo< MyGainBiState > MyGainBiStateInfo;
00408
00409
00410
00411 typedef MyGainState< MyGainTriStateType > MyGainTriState;
00412
00413 typedef MyGainStateInfo< MyGainTriState > MyGainTriStateInfo;
00414
00415
00416
00417 #endif // _GAIN-STATE_H
00418