| 24 | |
| 25 | |
| 26 | void cProbabDistrib::SetPoints(const cProbabDistrib::cPoints & a_Points) |
| 27 | { |
| 28 | ASSERT(!a_Points.empty()); |
| 29 | m_Sum = 0; |
| 30 | m_Cumulative.clear(); |
| 31 | m_Cumulative.reserve(a_Points.size() + 1); |
| 32 | int ProbSum = 0; |
| 33 | int LastProb = 0; |
| 34 | int LastValue = -1; |
| 35 | if (a_Points[0].m_Value != 0) |
| 36 | { |
| 37 | m_Cumulative.push_back(cPoint(0, 0)); // Always push in the [0, 0] point for easier search algorithm bounds |
| 38 | LastValue = 0; |
| 39 | } |
| 40 | for (cPoints::const_iterator itr = a_Points.begin(), end = a_Points.end(); itr != end; ++itr) |
| 41 | { |
| 42 | if (itr->m_Value == LastValue) |
| 43 | { |
| 44 | continue; |
| 45 | } |
| 46 | |
| 47 | // Add the current trapezoid to the sum: |
| 48 | ProbSum += (LastProb + itr->m_Probability) * (itr->m_Value - LastValue) / 2; |
| 49 | LastProb = itr->m_Probability; |
| 50 | LastValue = itr->m_Value; |
| 51 | m_Cumulative.push_back(cPoint(itr->m_Value, ProbSum)); |
| 52 | } // for itr - a_Points[] |
| 53 | if (LastValue != m_MaxValue) |
| 54 | { |
| 55 | m_Cumulative.push_back(cPoint(m_MaxValue, 0)); // Always push in the last point for easier search algorithm bounds |
| 56 | } |
| 57 | m_Sum = ProbSum; |
| 58 | } |
| 59 | |
| 60 | |
| 61 | |