StdAir Logo  1.00.3
C++ Standard Airline IT Object Library
ContinuousAttributeLite.hpp
Go to the documentation of this file.
1 #ifndef __STDAIR_BAS_CONTINUOUSATTRIBUTELITE_HPP
2 #define __STDAIR_BAS_CONTINUOUSATTRIBUTELITE_HPP
3 
4 // //////////////////////////////////////////////////////////////////////
5 // Import section
6 // //////////////////////////////////////////////////////////////////////
7 // STL
8 #include <cassert>
9 #include <iosfwd>
10 #include <string>
11 #include <vector>
12 #include <map>
13 // StdAir
15 // TraDemGen
18 
19 namespace stdair {
20 
25  template <typename T>
27  public:
28  // ///////////////////// Type definitions ///////////////////////
32  typedef std::map<T, stdair::Probability_T> ContinuousDistribution_T;
33 
34  public:
35  // ////////////////////// Business Methods ////////////////////
39  const T getValue(const stdair::Probability_T& iCumulativeProbability) const{
40  const DictionaryKey_T& lKey =
41  DictionaryManager::valueToKey (iCumulativeProbability);
42 
43  // Find the first cumulative probablity value greater or equal to lKey.
44  unsigned int idx = 0;
45  for (; idx < _size; ++idx) {
46  if (_cumulativeDistribution.at(idx) > lKey) {
47  break;
48  }
49  }
50 
51  if (idx == 0) {
52  return _valueArray.at(idx);
53  }
54  if (idx == _size) {
55  return _valueArray.at(idx-1);
56  }
57 
58  //
59  const stdair::Probability_T& lCumulativeCurrentPoint =
60  DictionaryManager::keyToValue (_cumulativeDistribution.at(idx));
61  const T& lValueCurrentPoint = _valueArray.at(idx);
62 
63  //
64  const stdair::Probability_T& lCumulativePreviousPoint =
65  DictionaryManager::keyToValue (_cumulativeDistribution.at(idx-1));
66  const T& lValuePreviousPoint = _valueArray.at(idx-1);
67 
68  if (lCumulativePreviousPoint == lCumulativeCurrentPoint) {
69  return lValuePreviousPoint;
70  }
71 
72  T oValue= lValuePreviousPoint + (lValueCurrentPoint - lValuePreviousPoint)
73  * (iCumulativeProbability - lCumulativePreviousPoint)
74  / (lCumulativeCurrentPoint - lCumulativePreviousPoint);
75 
76  return oValue;
77  }
78 
79  public:
80  // ////////////////////// Business Methods ////////////////////
84  const stdair::Probability_T getRemainingProportion(const T& iValue) const {
85 
86  // Find the first value greater than iValue.
87  unsigned int idx = 0;
88  for (; idx < _size; ++idx) {
89  if (_valueArray.at(idx) > iValue) {
90  break;
91  }
92  }
93  if (idx == 0) {
94  const stdair::Probability_T& oCumulativeProbability =
95  DictionaryManager::keyToValue (_cumulativeDistribution.at(idx));
96  return 1 - oCumulativeProbability;
97  }
98  if (idx == _size) {
99  const stdair::Probability_T& oCumulativeProbability =
100  DictionaryManager::keyToValue (_cumulativeDistribution.at(idx-1));
101  return 1 - oCumulativeProbability;
102  }
103 
104  //
105  const stdair::Probability_T& lCumulativeCurrentPoint =
106  DictionaryManager::keyToValue (_cumulativeDistribution.at(idx));
107  const T& lValueCurrentPoint = _valueArray.at(idx);
108 
109  //
110  const stdair::Probability_T& lCumulativePreviousPoint =
111  DictionaryManager::keyToValue (_cumulativeDistribution.at(idx-1));
112  const T& lValuePreviousPoint = _valueArray.at(idx-1);
113 
114  if (lValuePreviousPoint == lValueCurrentPoint) {
115  return 1 - lCumulativePreviousPoint;
116  }
117 
118  const stdair::Probability_T& oCumulativeProbability =
119  lCumulativePreviousPoint + (lCumulativeCurrentPoint - lCumulativePreviousPoint)
120  * (iValue - lValuePreviousPoint)
121  / (lValueCurrentPoint - lValuePreviousPoint);
122 
123  return 1 - oCumulativeProbability;
124  }
125 
126  public:
127  // ////////////////////// Business Methods ////////////////////
131  const double getDerivativeValue(const T iKey) const{
132 
133  // Find the first key value greater or equal to iKey.
134  unsigned int idx = 0;
135  for (; idx < _size; ++idx) {
136  if (_valueArray.at(idx) > iKey) {
137  break;
138  }
139  }
140  assert (idx != 0);
141  assert (idx != _size);
142 
143  //
144  const stdair::Probability_T& lCumulativeCurrentPoint =
145  DictionaryManager::keyToValue (_cumulativeDistribution.at(idx));
146  const T& lValueCurrentPoint = _valueArray.at(idx);
147 
148  //
149  const stdair::Probability_T& lCumulativePreviousPoint =
150  DictionaryManager::keyToValue (_cumulativeDistribution.at(idx-1));
151  const T& lValuePreviousPoint = _valueArray.at(idx-1);
152  assert (lValueCurrentPoint != lValuePreviousPoint);
153 
154  const double oValue= (lCumulativeCurrentPoint - lCumulativePreviousPoint)
155  / (lValueCurrentPoint - lValuePreviousPoint);
156 
157  return oValue;
158  }
159 
163  const T getUpperBound (const T iKey) const {
164  // Find the first key value greater or equal to iKey.
165  unsigned int idx = 0;
166  for (; idx < _size; ++idx) {
167  if (_valueArray.at(idx) > iKey) {
168  break;
169  }
170  }
171  assert (idx != 0);
172  assert (idx != _size);
173 
174  return _valueArray.at (idx);
175  }
176 
177  public:
178  // ////////////// Display Support Methods ////////////////
182  const std::string displayCumulativeDistribution() const {
183  std::ostringstream oStr;
184 
185  for (unsigned int idx = 0; idx < _size; ++idx) {
186  if (idx != 0) {
187  oStr << ", ";
188  }
189 
190  const stdair::Probability_T& lProbability =
191  DictionaryManager::keyToValue (_cumulativeDistribution.at(idx));
192 
193  oStr << _valueArray.at(idx) << ":" << lProbability;
194  }
195  return oStr.str();
196  }
197 
198 
199  public:
200  // ////////// Constructors and destructors //////////////
204  ContinuousAttributeLite (const ContinuousDistribution_T& iValueMap)
205  : _size (iValueMap.size()) {
206  init (iValueMap);
207  }
208 
213  : _size (iCAL._size),
214  _cumulativeDistribution (iCAL._cumulativeDistribution),
215  _valueArray (iCAL._valueArray) {
216  }
217 
222  _size = iCAL._size;
223  _cumulativeDistribution = iCAL._cumulativeDistribution;
224  _valueArray = iCAL._valueArray;
225  return *this;
226  }
227 
232  }
233 
234  private:
238  ContinuousAttributeLite() : _size(1) {
239  }
240 
245  void init (const ContinuousDistribution_T& iValueMap) {
246  //
247  const unsigned int lSize = iValueMap.size();
248  _cumulativeDistribution.reserve (lSize);
249  _valueArray.reserve (lSize);
250 
251  // Browse the map to retrieve the values and cumulative probabilities.
252  for (typename ContinuousDistribution_T::const_iterator it =
253  iValueMap.begin(); it != iValueMap.end(); ++it) {
254 
255  const T& attributeValue = it->first;
256  const DictionaryKey_T& lKey = DictionaryManager::valueToKey (it->second);
257 
258  // Build the two arrays.
259  _cumulativeDistribution.push_back (lKey);
260  _valueArray.push_back (attributeValue);
261  }
262  }
263 
264 
265  private:
266  // ////////// Attributes //////////
270  unsigned int _size;
271 
275  std::vector<DictionaryKey_T> _cumulativeDistribution;
276 
280  std::vector<T> _valueArray;
281  };
282 
283 }
284 #endif // __STDAIR_BAS_CONTINUOUSATTRIBUTELITE_HPP
const std::string displayCumulativeDistribution() const
const stdair::Probability_T getRemainingProportion(const T &iValue) const
std::map< T, stdair::Probability_T > ContinuousDistribution_T
Handle on the StdAir library context.
static const stdair::Probability_T keyToValue(const DictionaryKey_T)
const T getUpperBound(const T iKey) const
Class modeling the distribution of values that can be taken by a continuous attribute.
const double getDerivativeValue(const T iKey) const
ContinuousAttributeLite(const ContinuousDistribution_T &iValueMap)
ContinuousAttributeLite & operator=(const ContinuousAttributeLite &iCAL)
const T getValue(const stdair::Probability_T &iCumulativeProbability) const
ContinuousAttributeLite(const ContinuousAttributeLite &iCAL)
static const DictionaryKey_T valueToKey(const stdair::Probability_T)
float Probability_T
unsigned short DictionaryKey_T