StdAir Logo  0.45.1
C++ Standard Airline IT Object Library
ContinuousAttributeLite.hpp
Go to the documentation of this file.
00001 #ifndef __STDAIR_BAS_CONTINUOUSATTRIBUTELITE_HPP
00002 #define __STDAIR_BAS_CONTINUOUSATTRIBUTELITE_HPP
00003 
00004 // //////////////////////////////////////////////////////////////////////
00005 // Import section
00006 // //////////////////////////////////////////////////////////////////////
00007 // STL
00008 #include <cassert>
00009 #include <iosfwd>
00010 #include <string>
00011 #include <vector>
00012 #include <map>
00013 // StdAir
00014 #include <stdair/stdair_basic_types.hpp>
00015 // TraDemGen
00016 #include <stdair/stdair_exceptions.hpp>
00017 #include <stdair/basic/DictionaryManager.hpp>
00018 
00019 namespace stdair {
00020 
00025   template <typename T>
00026   struct ContinuousAttributeLite {
00027   public:
00028     // ///////////////////// Type definitions ///////////////////////
00032     typedef std::map<T, stdair::Probability_T> ContinuousDistribution_T;
00033     
00034   public:
00035     // ////////////////////// Business Methods ////////////////////
00039     const T getValue(const stdair::Probability_T& iCumulativeProbability) const{
00040       const DictionaryKey_T& lKey =
00041         DictionaryManager::valueToKey (iCumulativeProbability);
00042 
00043       // Find the first cumulative probablity value greater or equal to lKey.
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 
00079   public:
00080     // ////////////////////// Business Methods ////////////////////
00084     const stdair::Probability_T getRemainingProportion(const T& iValue) const {     
00085 
00086       // Find the first value greater than iValue.
00087       unsigned int idx = 0;
00088       for (; idx < _size; ++idx) {
00089         if (_valueArray.at(idx) > iValue) {
00090           break;
00091         }
00092       }
00093       if (idx == 0) {
00094         const stdair::Probability_T& oCumulativeProbability =
00095           DictionaryManager::keyToValue (_cumulativeDistribution.at(idx));
00096         return 1 - oCumulativeProbability;
00097       }
00098       if (idx == _size) {
00099         const stdair::Probability_T& oCumulativeProbability =
00100           DictionaryManager::keyToValue (_cumulativeDistribution.at(idx-1));
00101         return 1 - oCumulativeProbability;        
00102       }
00103 
00104       //
00105       const stdair::Probability_T& lCumulativeCurrentPoint =
00106         DictionaryManager::keyToValue (_cumulativeDistribution.at(idx));
00107       const T& lValueCurrentPoint = _valueArray.at(idx);
00108 
00109       //
00110       const stdair::Probability_T& lCumulativePreviousPoint =
00111         DictionaryManager::keyToValue (_cumulativeDistribution.at(idx-1));
00112       const T& lValuePreviousPoint = _valueArray.at(idx-1);
00113 
00114       if (lValuePreviousPoint == lValueCurrentPoint) {
00115         return 1 - lCumulativePreviousPoint;
00116       }
00117 
00118       const stdair::Probability_T& oCumulativeProbability =
00119         lCumulativePreviousPoint + (lCumulativeCurrentPoint - lCumulativePreviousPoint)
00120         * (iValue - lValuePreviousPoint)
00121         / (lValueCurrentPoint - lValuePreviousPoint);
00122 
00123       return 1 - oCumulativeProbability;
00124     }
00125 
00126   public:
00127     // ////////////////////// Business Methods ////////////////////
00131     const double getDerivativeValue(const T iKey) const{
00132 
00133       // Find the first key value greater or equal to iKey.
00134       unsigned int idx = 0;
00135       for (; idx < _size; ++idx) {
00136         if (_valueArray.at(idx) > iKey) {
00137           break;
00138         }
00139       }
00140       assert (idx != 0);
00141       assert (idx != _size);
00142 
00143       // 
00144       const stdair::Probability_T& lCumulativeCurrentPoint =
00145         DictionaryManager::keyToValue (_cumulativeDistribution.at(idx));
00146       const T& lValueCurrentPoint = _valueArray.at(idx);
00147 
00148       //
00149       const stdair::Probability_T& lCumulativePreviousPoint =
00150         DictionaryManager::keyToValue (_cumulativeDistribution.at(idx-1));
00151       const T& lValuePreviousPoint = _valueArray.at(idx-1);
00152       assert (lValueCurrentPoint != lValuePreviousPoint);
00153       
00154       const double oValue= (lCumulativeCurrentPoint - lCumulativePreviousPoint)
00155         / (lValueCurrentPoint - lValuePreviousPoint);
00156 
00157       return oValue;
00158     }
00159 
00163     const T getUpperBound (const T iKey) const {
00164       // Find the first key value greater or equal to iKey.
00165       unsigned int idx = 0;
00166       for (; idx < _size; ++idx) {
00167         if (_valueArray.at(idx) > iKey) {
00168           break;
00169         }
00170       }
00171       assert (idx != 0);
00172       assert (idx != _size);
00173 
00174       return _valueArray.at (idx);
00175     }
00176     
00177   public:
00178     // ////////////// Display Support Methods ////////////////
00182     const std::string displayCumulativeDistribution() const {
00183       std::ostringstream oStr;
00184       
00185       for (unsigned int idx = 0; idx < _size; ++idx) {
00186         if (idx != 0) {
00187           oStr << ", ";
00188         }
00189 
00190         const stdair::Probability_T& lProbability =
00191           DictionaryManager::keyToValue (_cumulativeDistribution.at(idx));
00192       
00193         oStr << _valueArray.at(idx) << ":" << lProbability;
00194       }
00195       return oStr.str();
00196     }
00197 
00198 
00199   public:
00200     // ////////// Constructors and destructors //////////////
00204     ContinuousAttributeLite (const ContinuousDistribution_T& iValueMap)
00205       : _size (iValueMap.size()) {
00206       init (iValueMap);
00207     }
00208     
00212     ContinuousAttributeLite (const ContinuousAttributeLite& iCAL)
00213       : _size (iCAL._size),
00214         _cumulativeDistribution (iCAL._cumulativeDistribution),
00215         _valueArray (iCAL._valueArray) {
00216     }
00217 
00221     ContinuousAttributeLite& operator= (const ContinuousAttributeLite& iCAL) {
00222       _size = iCAL._size;
00223       _cumulativeDistribution = iCAL._cumulativeDistribution;
00224       _valueArray = iCAL._valueArray;
00225       return *this;
00226     }
00227 
00231     virtual ~ContinuousAttributeLite() {
00232     }
00233 
00234   private:
00238     ContinuousAttributeLite() : _size(1) {
00239     }
00240 
00245     void init (const ContinuousDistribution_T& iValueMap) {
00246       //
00247       const unsigned int lSize = iValueMap.size();
00248       _cumulativeDistribution.reserve (lSize);
00249       _valueArray.reserve (lSize);
00250 
00251       // Browse the map to retrieve the values and cumulative probabilities.
00252       for (typename ContinuousDistribution_T::const_iterator it =
00253              iValueMap.begin(); it != iValueMap.end(); ++it) {
00254         
00255         const T& attributeValue = it->first;
00256         const DictionaryKey_T& lKey = DictionaryManager::valueToKey (it->second);
00257         
00258         // Build the two arrays.
00259         _cumulativeDistribution.push_back (lKey);
00260         _valueArray.push_back (attributeValue);
00261       }
00262     }
00263   
00264 
00265   private:
00266     // ////////// Attributes //////////
00270     unsigned int _size;
00271     
00275     std::vector<DictionaryKey_T> _cumulativeDistribution;
00276 
00280     std::vector<T> _valueArray;
00281   };
00282   
00283 }
00284 #endif // __STDAIR_BAS_CONTINUOUSATTRIBUTELITE_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines