您的位置:首页 > 职场人生

通向码农的道路(运镖系统,推荐算法)

2015-01-27 11:50 162 查看
#include <iostream>

#include <map>

#include <list>

#include <deque>

#include <vector>

#include <string.h>

#define Param 1000000

using namespace std;

template <typename T>

class SafeQueue

{

public:

SafeQueue()

:m_size(0)

{

}

~SafeQueue()

{

}

T front()

{

if (m_queue.empty())

{

return T(0);

}

else

{

return m_queue.front();

}

}

T pop()

{

if (!m_queue.empty())

{

T ret = m_queue.front();

m_queue.pop_front();

--m_size;

return ret;

}

else

{

return T(0);

}

}

void push(T val)

{

m_queue.push_back(val);

++m_size;

}

long size()

{

return m_size;

}

private:

list <T> m_queue;

int m_size;

};

template<typename T>

class ObjectPool_

{

public:

ObjectPool_( int initCount, int maxCount)

: m_initCount(initCount), m_maxCount(maxCount)

{

for ( int i = 0; i != initCount; ++i)

{

push(new T);

}

}

~ObjectPool_()

{

T *pVal = NULL;

while ((pVal = m_pool.pop()) != NULL)

{

delete pVal;

}

}

T* pop()

{

T *pVal = m_pool.pop();

if (pVal == NULL)

{

pVal = new T;

}

pVal->reset();

return pVal;

}

void push(T *pVal)

{

if (pVal != NULL)

{

pVal->reset();

m_pool.push(pVal);

if (m_pool.size() > m_maxCount)

{

delete m_pool.pop();

}

}

}

private:

SafeQueue<T*> m_pool;

int m_initCount;

int m_maxCount;

};

struct DeliverInfo

{

void Init(int &_playerlv,int &_quanliy,int &_battleforce,int &_ID)

{

playerlv = _playerlv;

quanliy = _quanliy;

battleforce = _battleforce;

ID = _ID;

}

void reset()

{

memset(this,0,sizeof(DeliverInfo));

}

int playerlv;

int quanliy;

int battleforce;

int ID;

};

ObjectPool_<DeliverInfo> m_DeliverPool(3000,4000);

map<int, DeliverInfo*> m_AllDeliverData;

static const int DeliverDeltaBattleForce = 20000;
//战斗力 间隔

static const int DeliverDeltaLv = 20;
//等级 间隔

static const int DeliverMaxLvSize = 3;

static const int DeliverMaxSize = 255;

static const int DeliverMaxDevierCount = 255;

class Test

{

public:

~Test()

{

m_AllDeliverData.clear();

}

Test()

{

for(int i = 0; i < DeliverMaxSize; i++)

{

DequeInfo info;

FastFind.push_back(info);

}

for(int i = 0; i < DeliverMaxSize; i++)

{

DequeInfo info;

FastFindHigh.push_back(info);

}

}

struct DequeInfo

{

DequeInfo()

{

index = 0;

}

deque<DeliverInfo*> deq;

int index;

void Add(DeliverInfo* i)

{

deq.push_back(i);

}

bool Remove(DeliverInfo* i)

{

bool isFind = false;

for (deque<DeliverInfo*>::iterator it = deq.begin(); it != deq.end(); it++)

{

if (*it == i)

{

it = deq.erase(it);

isFind = true;

}

}

return isFind;

}

int GetSize()

{

return deq.size();

}

int Random()

{

//index在于 每次进行刷新镖车匹配对应时候,保证不一致!

if (deq.size() == 0)

return -1;

if (index >= (int)deq.size())

{

index = 0;

int id = deq[index]->ID;

index++;

return id;

}

else

{

int id = deq[index]->ID;

index ++;

return id;

}

}

};

vector<DequeInfo>
FastFind; //普通镖车
按战斗力 分布

vector<DequeInfo>
FastFindHigh; //高级镖车 按玩家等级 分布

int m_DeliverId[4];
//运送ID时间

void SetDeliverID( int i, int id)

{

if (i > 4 || i < 0)

return;

m_DeliverId[i] = id;

}

// 快速查找队列中 添加 对应的镖车

int GetBattleForceIndex(int battleForce)

{

int x = battleForce / DeliverDeltaBattleForce;

if ( x >= DeliverMaxSize)

x = DeliverMaxSize -1;

return x;

}

// 快速查找队列中 添加 对应的镖车

void AddInFastFind(DeliverInfo* info)

{

if (info != NULL)

{

//通过配置的机器人数据,品质判断如果品质不等于4,就通过战斗力来匹配推荐数据,如果品质等于4就通过等级来匹配数据

if (info->quanliy != 4)

{

int x = GetBattleForceIndex(info->battleforce);

FastFind[x].Add(info);

}

else

{

int x = GetLevelIndex(info->playerlv);

FastFindHigh[x].Add(info);

}

}

}

int GetLevelIndex(int lv)

{

int x = lv / DeliverDeltaLv;

if ( x >= DeliverMaxLvSize)

x = DeliverMaxLvSize -1;

return x;

}

void RandomDevierID_Function(int x, int deltaValue, int type, vector<int>& vec, int needSize, int expertID)

{

if (type == 0)

{

if (x < 0 || x > DeliverMaxSize)

return;

int size = FastFind[x].GetSize();

if (size <= 0)

{

RandomDevierID_Function1(deltaValue, x, type, vec, needSize, expertID);

}

else

{

for (int i = 0; i < size; i++)

{

int getid = FastFind[x].Random();

if (getid != expertID)

vec.push_back(getid);

if (vec.size() >= needSize)

break;

}

//如果取完之后 仍然小于 要取得数量,继续递归

if (vec.size() < needSize)

{

RandomDevierID_Function1(deltaValue, x, type, vec, needSize, expertID);

}

}

}

else if (type == 1)

{

if ( x < 0 || x > DeliverMaxLvSize)

return;

int size = FastFindHigh[x].GetSize();

if (size <= 0)//这个格子没有数据 , 直接进入递归

{

RandomDevierID_Function1(deltaValue, x, type, vec, needSize, expertID);

}

else

{

for (int i = 0; i < size; i++)

{

int getid = FastFindHigh[x].Random();

if (getid != expertID)

vec.push_back(getid);

if (vec.size() >= needSize)

break;

}

//如果取完之后 仍然小于 要取得数量,继续递归

if (vec.size()< needSize)

{

RandomDevierID_Function1(deltaValue, x, type, vec, needSize, expertID);

}

}

}

}

//递归辅助函数, 越来越小和越来越大

void RandomDevierID_Function1(int deltaValue, int x, int type, vector<int>& vec, int needSize, int expertID)

{

if (deltaValue == 0)

{

RandomDevierID_Function(x , 1, type, vec, needSize, expertID);

RandomDevierID_Function(x-1 , -1, type, vec, needSize, expertID);

}

else if (deltaValue == 1)

{

RandomDevierID_Function(x+1 , 1, type, vec, needSize, expertID);

}

else if (deltaValue == -1)

{

RandomDevierID_Function(x-1 , -1, type, vec, needSize, expertID);

}

}

// 快速查找队列中 移除 对应的镖车

bool RemoveInFastFind(DeliverInfo* info)

{

if (info != NULL)

{

if (info->quanliy != 4)

{

int x = info->battleforce / DeliverDeltaBattleForce;

if ( x >= DeliverMaxSize)

x = DeliverMaxSize -1;

if(x < 0)

x = 0;

for (deque<DeliverInfo*>::iterator it = FastFind[x].deq.begin(); it != FastFind[x].deq.end(); it++)

{

if (*it == info)

{

it = FastFind[x].deq.erase(it);

return true;

}

}

}

else

{

int x = info->playerlv / DeliverDeltaLv;

if ( x >= DeliverMaxLvSize)

x = DeliverMaxLvSize -1;

for (deque<DeliverInfo*>::iterator it = FastFindHigh[x].deq.begin(); it != FastFindHigh[x].deq.end(); it++)

{

if (*it == info)

{

it = FastFindHigh[x].deq.erase(it);

return true;

}

}

}

}

return false;

}

void RandomDevierID(int ID)

{

vector<int> ids;//存储于玩家自身相近的镖车数据,用于展示在前台界面显示

int expertID = ID;//在匹配数据的时候,加入玩家自身的ID,避免匹配到自己的数据

int level = Param;

int x = GetLevelIndex(level);

RandomDevierID_Function(x, 0, 1, ids, 1, expertID);

int battleForce = Param;

int y = GetBattleForceIndex(battleForce);

if (ids.size() == 1)

RandomDevierID_Function(y, 0, 0, ids, 3, expertID);

else

RandomDevierID_Function(y, 0, 0, ids, 4, expertID);

for (size_t i = 0; i < ids.size(); i++)

{

SetDeliverID(i, ids[i]);

}

}

void Begin()

{

for(int i = 1000000; i < 1002000; i++)

{

DeliverInfo *deliver = m_DeliverPool.pop();//启动进程时候,已经开辟出了3000个对象,此时从list这个结构冲,拉出一个数据

deliver->Init(i,i,i,i);

m_AllDeliverData.insert(make_pair(i,deliver));//把所有的数据加入到一个map中,这个目的在于镖车数据在结束(通过time判定)查找的对应镖车ID

AddInFastFind(deliver);//将数据加入到一个vector<> + deque结构当中,vecotr存储区间范围,deque存储具体数据, 比如镖车数据为60 61 62 63 当我除以/2 都在30这个区间!! 那么vector[30]中就存储60 61 62 63 的数据

}

}

void End(DeliverInfo* info)

{

m_DeliverPool.push(info);

}

DeliverInfo* GetDeliverInfo(int id)

{

map<int, DeliverInfo*>::iterator it =m_AllDeliverData.find(id);

if (it != m_AllDeliverData.end())

return it->second;

return NULL;

}

};

int main()

{

Test test;

test.Begin(); //开始new出2000个机器人数据!!!

DeliverInfo *data = test.GetDeliverInfo(Param);//测试下镖车中有没有100000这个ID数据

if(data == NULL)

return 0;

test.RandomDevierID(data->ID);

for(int i = 0 ; i < 4;i++)

{

cout<<test.m_DeliverId[i]<<endl;

}

//在镖车数据中删除玩家数据,并回收到对象池

for(map<int, DeliverInfo*>::iterator it = m_AllDeliverData.begin(); it != m_AllDeliverData.end(); ++it)

{

test.RemoveInFastFind(it->second);

test.End(it->second);

it = m_AllDeliverData.erase(it);//erase一定要注意it =

break;

}

//在进程结束时候手动delete数据

for(map<int, DeliverInfo*>::iterator it = m_AllDeliverData.begin(); it != m_AllDeliverData.end(); ++it)

{

delete it->second;

}

//进程结束,对象池析构函数自动释放内存

return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: