Go to the documentation of this file.00001 #ifndef __TRADEMGEN_BAS_CONTINUOUSATTRIBUTELITE_HPP
00002 #define __TRADEMGEN_BAS_CONTINUOUSATTRIBUTELITE_HPP
00003
00004
00005
00006
00007
00008 #include <cassert>
00009 #include <iosfwd>
00010 #include <string>
00011 #include <vector>
00012 #include <map>
00013
00014 #include <stdair/stdair_basic_types.hpp>
00015
00016 #include <trademgen/TRADEMGEN_Exceptions.hpp>
00017 #include <trademgen/basic/DictionaryManager.hpp>
00018
00019 namespace TRADEMGEN {
00020
00025 template <typename T>
00026 struct ContinuousAttributeLite {
00027 public:
00028
00032 typedef std::map<T, stdair::Probability_T> ContinuousDistribution_T;
00033
00034 public:
00035
00039 const T getValue(const stdair::Probability_T& iCumulativeProbability) const{
00040 const DictionaryKey_T& lKey =
00041 DictionaryManager::valueToKey (iCumulativeProbability);
00042
00043
00044 unsigned int idx = 0;
00045 for (; idx < _size; ++idx) {
00046 if (_cumulativeDistribution.at(idx) > lKey) {
00047 break;
00048 }
00049 }
00050
00051 if (idx == 0) {
00052 return _valueArray.at(idx);
00053 }
00054 if (idx == _size) {
00055 return _valueArray.at(idx-1);
00056 }
00057
00058
00059 const stdair::Probability_T& lCumulativeCurrentPoint =
00060 DictionaryManager::keyToValue (_cumulativeDistribution.at(idx));
00061 const T& lValueCurrentPoint = _valueArray.at(idx);
00062
00063
00064 const stdair::Probability_T& lCumulativePreviousPoint =
00065 DictionaryManager::keyToValue (_cumulativeDistribution.at(idx-1));
00066 const T& lValuePreviousPoint = _valueArray.at(idx-1);
00067
00068 if (lCumulativePreviousPoint == lCumulativeCurrentPoint) {
00069 return lValuePreviousPoint;
00070 }
00071
00072 T oValue= lValuePreviousPoint + (lValueCurrentPoint - lValuePreviousPoint)
00073 * (iCumulativeProbability - lCumulativePreviousPoint)
00074 / (lCumulativeCurrentPoint - lCumulativePreviousPoint);
00075
00076 return oValue;
00077 }
00078
00082 const double getDerivativeValue(const T iKey) const{
00083
00084
00085 unsigned int idx = 0;
00086 for (; idx < _size; ++idx) {
00087 if (_valueArray.at(idx) > iKey) {
00088 break;
00089 }
00090 }
00091
00092 assert (idx != 0);
00093 assert (idx != _size);
00094
00095
00096 const stdair::Probability_T& lCumulativeCurrentPoint =
00097 DictionaryManager::keyToValue (_cumulativeDistribution.at(idx));
00098 const T& lValueCurrentPoint = _valueArray.at(idx);
00099
00100
00101 const stdair::Probability_T& lCumulativePreviousPoint =
00102 DictionaryManager::keyToValue (_cumulativeDistribution.at(idx-1));
00103 const T& lValuePreviousPoint = _valueArray.at(idx-1);
00104
00105 assert (lValueCurrentPoint != lValuePreviousPoint);
00106
00107 const double oValue= (lCumulativeCurrentPoint - lCumulativePreviousPoint)
00108 / (lValueCurrentPoint - lValuePreviousPoint);
00109
00110 return oValue;
00111 }
00112
00116 const T getUpperBound (const T iKey) const {
00117
00118 unsigned int idx = 0;
00119 for (; idx < _size; ++idx) {
00120 if (_valueArray.at(idx) > iKey) {
00121 break;
00122 }
00123 }
00124 assert (idx != 0);
00125 assert (idx != _size);
00126
00127 return _valueArray.at (idx);
00128 }
00129
00130 public:
00131
00135 const std::string displayCumulativeDistribution() const {
00136 std::ostringstream oStr;
00137
00138 for (unsigned int idx = 0; idx < _size; ++idx) {
00139 if (idx != 0) {
00140 oStr << ", ";
00141 }
00142
00143 const stdair::Probability_T& lProbability =
00144 DictionaryManager::keyToValue (_cumulativeDistribution.at(idx));
00145
00146 oStr << _valueArray.at(idx) << ":" << lProbability;
00147 }
00148 return oStr.str();
00149 }
00150
00151
00152 public:
00153
00157 ContinuousAttributeLite (const ContinuousDistribution_T& iValueMap)
00158 : _size (iValueMap.size()) {
00159 init (iValueMap);
00160 }
00161
00165 ContinuousAttributeLite (const ContinuousAttributeLite& iCAL)
00166 : _size (iCAL._size),
00167 _cumulativeDistribution (iCAL._cumulativeDistribution),
00168 _valueArray (iCAL._valueArray) {
00169 }
00170
00174 ContinuousAttributeLite& operator= (const ContinuousAttributeLite& iCAL) {
00175 _size = iCAL._size;
00176 _cumulativeDistribution = iCAL._cumulativeDistribution;
00177 _valueArray = iCAL._valueArray;
00178 return *this;
00179 }
00180
00184 virtual ~ContinuousAttributeLite() {
00185 }
00186
00187 private:
00191 ContinuousAttributeLite() : _size(1) {
00192 }
00193
00198 void init (const ContinuousDistribution_T& iValueMap) {
00199
00200 const unsigned int lSize = iValueMap.size();
00201 _cumulativeDistribution.reserve (lSize);
00202 _valueArray.reserve (lSize);
00203
00204
00205 for (typename ContinuousDistribution_T::const_iterator it =
00206 iValueMap.begin(); it != iValueMap.end(); ++it) {
00207
00208 const T& attributeValue = it->first;
00209 const DictionaryKey_T& lKey = DictionaryManager::valueToKey (it->second);
00210
00211
00212 _cumulativeDistribution.push_back (lKey);
00213 _valueArray.push_back (attributeValue);
00214 }
00215 }
00216
00217
00218 private:
00219
00223 unsigned int _size;
00224
00228 std::vector<DictionaryKey_T> _cumulativeDistribution;
00229
00233 std::vector<T> _valueArray;
00234 };
00235
00236 }
00237 #endif // __TRADEMGEN_BAS_CONTINUOUSATTRIBUTELITE_HPP