TraDemGen Logo  1.00.0
C++ Simulated Travel Demand Generation Library
 All Classes Namespaces Files Functions Variables Typedefs Friends Pages
CategoricalAttributeLite.hpp
Go to the documentation of this file.
1 #ifndef __TRADEMGEN_BAS_CATEGORICALATTRIBUTELITE_HPP
2 #define __TRADEMGEN_BAS_CATEGORICALATTRIBUTELITE_HPP
3 
4 // //////////////////////////////////////////////////////////////////////
5 // Import section
6 // //////////////////////////////////////////////////////////////////////
7 // STL
8 #include <cassert>
9 #include <sstream>
10 #include <string>
11 #include <vector>
12 #include <map>
13 // StdAir
14 #include <stdair/stdair_basic_types.hpp>
15 #include <stdair/service/Logger.hpp>
16 // TraDemGen
19 
20 namespace TRADEMGEN {
21 
26  template <typename T>
28  public:
29  // ///////////// Type definitions //////////////
33  typedef std::map<T, stdair::Probability_T> ProbabilityMassFunction_T;
34 
35 
36  public:
37  // /////////////// Business Methods //////////
41  const T& getValue (const stdair::Probability_T& iCumulativeProbability) const {
42  const DictionaryKey_T& lKey =
43  DictionaryManager::valueToKey (iCumulativeProbability);
44 
45  for (unsigned int idx = 0; idx < _size; ++idx) {
46  if (_cumulativeDistribution.at(idx) >= lKey) {
47  const T& oValue = _valueArray.at(idx);
48  return oValue;
49  }
50  }
51 
52  std::ostringstream oStr;
53  oStr << "The following cumulative probability is out of range: "
54  << iCumulativeProbability << displayProbabilityMass();
55  throw IndexOutOfRangeException (oStr.str());
56  }
57 
61  bool checkValue (const T& iValue) const {
62  for (unsigned int idx = 0; idx < _size; ++idx) {
63  if (_valueArray.at(idx) == iValue) {
64  return true;
65  }
66  }
67  return false;
68  }
69 
70 
71  public:
72  // ////////////// Display Support Methods //////////
76  const std::string displayProbabilityMass() const {
77  std::ostringstream oStr;
78 
79  for (unsigned int idx = 0; idx < _size; ++idx) {
80  if (idx != 0) {
81  oStr << ", ";
82  }
83  oStr << _valueArray.at(idx) << ":"
84  << DictionaryManager::keyToValue (_cumulativeDistribution[idx]);
85  }
86  return oStr.str();
87  }
88 
89 
90  public:
91  // ////////// Constructors and destructors /////////
96  : _size (iValueMap.size()) {
97  init (iValueMap);
98  }
99 
104  }
105 
110  : _size (iCAL._size),
111  _cumulativeDistribution (iCAL._cumulativeDistribution),
112  _valueArray (iCAL._valueArray) {
113  }
114 
119  _size = iCAL._size;
120  _cumulativeDistribution = iCAL._cumulativeDistribution;
121  _valueArray = iCAL._valueArray;
122  return *this;
123  }
124 
129  }
130 
131 
132  private:
136  void init (const ProbabilityMassFunction_T& iValueMap) {
137 
138  const unsigned int lSize = iValueMap.size();
139  _cumulativeDistribution.reserve (lSize);
140  _valueArray.reserve (lSize);
141 
142  stdair::Probability_T cumulative_probability_so_far = 0.0;
143 
144  // Browse the map to retrieve the values and to build the
145  // cumulative probabilities.
146  for (typename ProbabilityMassFunction_T::const_iterator
147  itProbabilityMassFunction = iValueMap.begin();
148  itProbabilityMassFunction != iValueMap.end();
149  ++itProbabilityMassFunction) {
150 
151  stdair::Probability_T attribute_probability_mass =
152  itProbabilityMassFunction->second;
153 
154  if (attribute_probability_mass > 0) {
155  const T& attribute_value = itProbabilityMassFunction->first;
156  cumulative_probability_so_far += attribute_probability_mass;
157 
158  const DictionaryKey_T& lKey =
159  DictionaryManager::valueToKey (cumulative_probability_so_far);
160 
161  // Build the two arrays.
162  _cumulativeDistribution.push_back (lKey);
163  _valueArray.push_back (attribute_value);
164  }
165  }
166  // Remember the actual array size.
167  _size = _valueArray.size();
168  }
169 
170  private:
171  // ////////// Attributes //////////
175  unsigned int _size;
176 
180  std::vector<DictionaryKey_T> _cumulativeDistribution;
181 
185  std::vector<T> _valueArray;
186  };
187 }
188 #endif // __TRADEMGEN_BAS_CATEGORICALATTRIBUTELITE_HPP