$treeview $search $mathjax
00001 #ifndef __TRADEMGEN_BAS_CATEGORICALATTRIBUTELITE_HPP 00002 #define __TRADEMGEN_BAS_CATEGORICALATTRIBUTELITE_HPP 00003 00004 // ////////////////////////////////////////////////////////////////////// 00005 // Import section 00006 // ////////////////////////////////////////////////////////////////////// 00007 // STL 00008 #include <cassert> 00009 #include <sstream> 00010 #include <string> 00011 #include <vector> 00012 #include <map> 00013 // StdAir 00014 #include <stdair/stdair_basic_types.hpp> 00015 #include <stdair/service/Logger.hpp> 00016 // TraDemGen 00017 #include <trademgen/TRADEMGEN_Exceptions.hpp> 00018 #include <trademgen/basic/DictionaryManager.hpp> 00019 00020 namespace TRADEMGEN { 00021 00026 template <typename T> 00027 struct CategoricalAttributeLite { 00028 public: 00029 // ///////////// Type definitions ////////////// 00033 typedef std::map<T, stdair::Probability_T> ProbabilityMassFunction_T; 00034 00035 00036 public: 00037 // /////////////// Business Methods ////////// 00041 const T& getValue (const stdair::Probability_T& iCumulativeProbability) const { 00042 const DictionaryKey_T& lKey = 00043 DictionaryManager::valueToKey (iCumulativeProbability); 00044 00045 for (unsigned int idx = 0; idx < _size; ++idx) { 00046 if (_cumulativeDistribution.at(idx) >= lKey) { 00047 const T& oValue = _valueArray.at(idx); 00048 return oValue; 00049 } 00050 } 00051 00052 std::ostringstream oStr; 00053 oStr << "The following cumulative probability is out of range: " 00054 << iCumulativeProbability << displayProbabilityMass(); 00055 throw IndexOutOfRangeException (oStr.str()); 00056 } 00057 00061 bool checkValue (const T& iValue) const { 00062 for (unsigned int idx = 0; idx < _size; ++idx) { 00063 if (_valueArray.at(idx) == iValue) { 00064 return true; 00065 } 00066 } 00067 return false; 00068 } 00069 00070 00071 public: 00072 // ////////////// Display Support Methods ////////// 00076 const std::string displayProbabilityMass() const { 00077 std::ostringstream oStr; 00078 00079 for (unsigned int idx = 0; idx < _size; ++idx) { 00080 if (idx != 0) { 00081 oStr << ", "; 00082 } 00083 oStr << _valueArray.at(idx) << ":" 00084 << DictionaryManager::keyToValue (_cumulativeDistribution[idx]); 00085 } 00086 return oStr.str(); 00087 } 00088 00089 00090 public: 00091 // ////////// Constructors and destructors ///////// 00095 CategoricalAttributeLite (const ProbabilityMassFunction_T& iValueMap) 00096 : _size (iValueMap.size()) { 00097 init (iValueMap); 00098 } 00099 00103 CategoricalAttributeLite() : _size(1) { 00104 } 00105 00109 CategoricalAttributeLite (const CategoricalAttributeLite& iCAL) 00110 : _size (iCAL._size), 00111 _cumulativeDistribution (iCAL._cumulativeDistribution), 00112 _valueArray (iCAL._valueArray) { 00113 } 00114 00118 CategoricalAttributeLite& operator= (const CategoricalAttributeLite& iCAL) { 00119 _size = iCAL._size; 00120 _cumulativeDistribution = iCAL._cumulativeDistribution; 00121 _valueArray = iCAL._valueArray; 00122 return *this; 00123 } 00124 00128 virtual ~CategoricalAttributeLite() { 00129 } 00130 00131 00132 private: 00136 void init (const ProbabilityMassFunction_T& iValueMap) { 00137 00138 const unsigned int lSize = iValueMap.size(); 00139 _cumulativeDistribution.reserve (lSize); 00140 _valueArray.reserve (lSize); 00141 00142 stdair::Probability_T cumulative_probability_so_far = 0.0; 00143 00144 // Browse the map to retrieve the values and to build the 00145 // cumulative probabilities. 00146 for (typename ProbabilityMassFunction_T::const_iterator 00147 itProbabilityMassFunction = iValueMap.begin(); 00148 itProbabilityMassFunction != iValueMap.end(); 00149 ++itProbabilityMassFunction) { 00150 00151 stdair::Probability_T attribute_probability_mass = 00152 itProbabilityMassFunction->second; 00153 00154 if (attribute_probability_mass > 0) { 00155 const T& attribute_value = itProbabilityMassFunction->first; 00156 cumulative_probability_so_far += attribute_probability_mass; 00157 00158 const DictionaryKey_T& lKey = 00159 DictionaryManager::valueToKey (cumulative_probability_so_far); 00160 00161 // Build the two arrays. 00162 _cumulativeDistribution.push_back (lKey); 00163 _valueArray.push_back (attribute_value); 00164 } 00165 } 00166 // Remember the actual array size. 00167 _size = _valueArray.size(); 00168 } 00169 00170 private: 00171 // ////////// Attributes ////////// 00175 unsigned int _size; 00176 00180 std::vector<DictionaryKey_T> _cumulativeDistribution; 00181 00185 std::vector<T> _valueArray; 00186 }; 00187 } 00188 #endif // __TRADEMGEN_BAS_CATEGORICALATTRIBUTELITE_HPP