ratioselector.h

来自「奇迹世界公用文件源代码,研究网络游戏的朋友可以研究下」· C头文件 代码 · 共 194 行

H
194
字号
#pragma once

#include <hash_map>
#include <map>
#include <vector>
#include <algorithm>
#include <iostream>
#include <assert.h>

using namespace std;
using namespace stdext;


/*
// 犬伏(DWORD)俊 蝶扼辑 Action阑 窍唱 急琶茄促.

RatioSelector<int>	rs();
rs.Add( 1, 20 );
rs.Add( 2, 10 );
rs.Add( 3, 50 );
rs.Add( 4, 30 );

//rs.GenerateRatios();	// 救秦档 Select()唱 GetTotalRatio()俊辑 磊悼栏肺 凳.

// m_Ratios狼 蔼甸			// Action	Ratio		瘤沥等 裹困
// [19] = 1					// 1		20			0~19
// [29] = 2					// 2		10			20~29
// [79] = 3					// 3		50			30~79
// [109] = 4				// 4		30			80~109

int a;
assert( rs.Select( 0, a ) ); assert( a == 1 );
assert( rs.Select( 19, a ) ); assert( a == 1 );
assert( rs.Select( 20, a ) ); assert( a == 2 );
assert( rs.Select( 21, a ) ); assert( a == 2 );
assert( rs.Select( 81, a ) ); assert( a == 4 );
assert( rs.Select( 109, a ) ); assert( a == 4 );
assert( !rs.Select( 110, a ) );  // 110篮 裹困甫 哈绢唱骨肺 角菩. a蔼篮 狼固绝促.
*/

template <class Action>
class RatioSelector
{
	typedef stdext::hash_map<Action, DWORD>		ACTIONS;
	typedef map<DWORD, Action>			RATIOS;
	typedef pair<Action, DWORD>			ACTION_RATIO;

	class ActionRatioCompare 
	{
	public :
		bool			operator () (ACTION_RATIO& l, ACTION_RATIO& r)
		{
			if (l.second < r.second)
			{
				return true;
			}

			return false;
		}
	};

public :
	RatioSelector();
	~RatioSelector() {}

	VOID		Release();

	BOOL		Add(Action action, DWORD ratio);
	BOOL		Remove(Action action);

	VOID		GenerateRatios();

	BOOL		Select(DWORD ratio, Action& selectedAction);
	DWORD		GetTotalRatio();

private :
	bool		m_bGenerated;
	DWORD		m_TotalRatio;
	ACTIONS		m_Actions;
	RATIOS		m_Ratios;
};

template <class Action>
RatioSelector<Action>::RatioSelector() 
: m_bGenerated(true), m_TotalRatio(0)
{
}

template <class Action>
VOID		
RatioSelector<Action>::Release()
{
	m_bGenerated = true;
	m_TotalRatio = 0;
	m_Actions.clear();
	m_Ratios.clear();
}

template <class Action>
BOOL
RatioSelector<Action>::Add(Action action, DWORD ratio)
{

	ASSERT( ratio > 0 );

	ACTIONS::const_iterator iAction = m_Actions.find( action );
	if (iAction != m_Actions.end())
	{
		return FALSE;
	}

	m_Actions[action] = ratio;
	m_bGenerated = false;

	return TRUE;
}


template <class Action>
BOOL
RatioSelector<Action>::Remove(Action action)
{
	ACTIONS::iterator iAction = m_Actions.find( action );
	if (iAction != m_Actions.end())
	{
		m_Actions.erase(iAction);
		m_bGenerated = false;

		return TRUE;
	}

	return FALSE;
}


template <class Action>
VOID
RatioSelector<Action>::GenerateRatios()
{
	m_Ratios.clear();

	// map俊 持绰促.	
	m_TotalRatio = 0;
	ACTIONS::const_iterator itr = m_Actions.begin();
	for (;itr != m_Actions.end(); itr++)
	{
		const Action&	action = itr->first;
		DWORD			ratio  = itr->second;

		m_TotalRatio += ratio;	

		// 角力肺绰 -1鳖瘤啊 action捞 急琶登绰 康开捞促.
		// ex) ratio=20捞搁, 0~19鳖瘤.
		m_Ratios[m_TotalRatio-1] = action;
	}

	m_bGenerated = true;
}


template <class Action>
BOOL
RatioSelector<Action>::Select(DWORD ratio, Action& selectedAction)
{
	if (!m_bGenerated)
	{
		GenerateRatios();
	}

	RATIOS::const_iterator itr = m_Ratios.lower_bound(ratio);

	if (itr==m_Ratios.end())
	{
		return FALSE;
	}

	selectedAction = itr->second;
	return TRUE;
}


template <class Action>
DWORD		
RatioSelector<Action>::GetTotalRatio()		
{ 
	if (!m_bGenerated)
	{
		GenerateRatios();
	}
	
	return m_TotalRatio; 
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?