1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
#ifndef _SIMULATOR_CDF_MAP_HPP_
#define _SIMULATOR_CDF_MAP_HPP_
#include <algorithm>
#include <cassert>
#include <vector>
#include <alignment/statistics/StatUtils.hpp>
template <typename T_Data>
class CDFMap
{
public:
std::vector<int> cdf;
std::vector<T_Data> data;
/* Tests:
* indices 0 1 2 3 4 5 6
* lengths: 1 3 5 9 10 10 11
* lengthHistogram.data: 1 3 5 9 10 11
* lengthHistogram.cdf : 1 2 3 4 6 7
*
* lengths: 1 3 5 9 10 11
* lengthHistogram.data: 1 3 5 9 10 11
* lengthHistogram.cdf : 1 2 3 4 5 6
*
* lengths: 10
* lengthHistogram.data: 10
* lengthHistogram.cdf : 1
*/
int SelectRandomValue(T_Data &value);
};
template <typename T_Data>
int CDFMap<T_Data>::SelectRandomValue(T_Data &value)
{
std::vector<int>::iterator search_it;
assert(cdf.size() >= 0);
int randomIndex = RandomInt(cdf[cdf.size() - 1]);
search_it = lower_bound(cdf.begin(), cdf.end(), randomIndex);
assert(search_it != cdf.end());
int valueIndex = search_it - cdf.begin();
value = data[valueIndex];
return valueIndex;
}
#endif
|