#include <map>
struct WheelComp
{
bool operator()(const std::pair<int, int>& a, const std::pair<int, int>& b)
{
if(b.first == -1)
{
if(b.second >= a.first && b.second < a.second)
return false;
return (b.second >= a.second);
}
if(a.first == -1)
{
if(a.second >= b.first && a.second < b.second)
return false;
return (a.second < b.second);
}
return a.first < b.first;
}
};
//pair<start, end> ¹üÀ§¸¦ °¡Áø´Ù. ¹üÀ§ endÀÇ °ªÀº Æ÷ÇÔµÇÁö ¾Ê´Â´Ù.
class Wheel
{
protected:
int m_sum;
int m_index;
std::map<std::pair<int, int>, int, WheelComp> m_ranges;
public:
Wheel(): m_sum(0)
{
}
void Add(int fitness, int value)
{
m_ranges[std::pair<int, int>(m_sum, fitness + m_sum)] = value;
m_sum += fitness;
}
//-1À» ¸®ÅÏÇÏ¸é ¹üÀ§¸¦ ¹þ¾î³ °ªÀÌ´Ù.
int Spin(int random)
{
if(random >= m_sum)
return -1;
return m_ranges.find(std::pair<int, int>(-1, random))->second;
}
};
void main()
{
Wheel wheel;
wheel.Add(10, 10); //0~10 ¹üÀ§
wheel.Add(90, 100); //10~100 ¹üÀ§
wheel.Add(900, 1000); //100~1000 ¹üÀ§
wheel.Add(9000, 10000); //1000~10000 ¹üÀ§
int n9 = wheel.Spin(10);
int n90 = wheel.Spin(90);
int n900 = wheel.Spin(900);
int n9000 = wheel.Spin(9000);
int nx = wheel.Spin(10000);
}