MCPcopy Create free account
hub / github.com/cuberite/cuberite / MapValue

Method MapValue

src/ProbabDistrib.cpp:111–138  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

109
110
111int cProbabDistrib::MapValue(int a_OrigValue) const
112{
113 ASSERT(a_OrigValue >= 0);
114 ASSERT(a_OrigValue < m_Sum);
115
116 // Binary search through m_Cumulative for placement:
117 size_t Lo = 0;
118 size_t Hi = m_Cumulative.size() - 1;
119 while (Hi - Lo > 1)
120 {
121 int Mid = (Lo + Hi) / 2;
122 int MidProbab = m_Cumulative[Mid].m_Probability;
123 if (MidProbab < a_OrigValue)
124 {
125 Lo = Mid;
126 }
127 else
128 {
129 Hi = Mid;
130 }
131 }
132 ASSERT(Hi - Lo == 1);
133
134 // Linearly interpolate between Lo and Hi:
135 int ProbDif = m_Cumulative[Hi].m_Probability - m_Cumulative[Lo].m_Probability;
136 int ValueDif = m_Cumulative[Hi].m_Value - m_Cumulative[Lo].m_Value;
137 return m_Cumulative[Lo].m_Value + (a_OrigValue - m_Cumulative[Lo].m_Probability) * ValueDif / ProbDif;
138}
139
140
141

Callers 1

GenFinishMethod · 0.80

Calls 1

sizeMethod · 0.80

Tested by

no test coverage detected