PiecePicker.cpp
资源名称:GGBT.rar [点击查看]
上传用户:lds876
上传日期:2013-05-25
资源大小:567k
文件大小:5k
源码类别:
P2P编程
开发平台:
Visual C++
- // PiecePicker.cpp: implementation of the CPiecePicker class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "testbt.h"
- #include "PiecePicker.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- void showlong(vector<long> vInsterest);
- void showvector(vector<vector<long> > vInsterest);
- long randlong(long b, long e);
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CPiecePicker::CPiecePicker(long lPiecesCount)
- {
- m_lPiecesCount = lPiecesCount;
- m_vInterests.push_back(vector<long>());
- for (long i=0; i<m_lPiecesCount; i++)
- {
- m_vNumInterests.push_back(0L);
- m_vPosInterest.push_back(i);
- m_vInterests[0].push_back(i);
- }
- m_bGotany = false;
- }
- CPiecePicker::~CPiecePicker()
- {
- }
- void CPiecePicker::got_have(long index)
- {
- assert(index < m_vNumInterests.size() && index >= 0 );
- if (m_vNumInterests[index] == -1)
- return;
- long lval = m_vInterests[m_vNumInterests[index]][m_vPosInterest[index]];
- assert(lval == index);
- vector<long>& vInsterest = m_vInterests[m_vNumInterests[index]];
- long lPos = m_vPosInterest[index];
- vInsterest[lPos] = vInsterest[vInsterest.size() - 1];
- m_vPosInterest[vInsterest[vInsterest.size() - 1]] = lPos;
- vInsterest.erase(vInsterest.begin() + (vInsterest.size() - 1));
- m_vNumInterests[index]++;
- if (m_vInterests.size() <= m_vNumInterests[index])
- m_vInterests.push_back(vector<long>());
- m_vPosInterest[index] = m_vInterests[m_vNumInterests[index]].size();
- m_vInterests[m_vNumInterests[index]].push_back(index);
- }
- void CPiecePicker::lost_have(long index)
- {
- assert(index < m_vNumInterests.size() && index >= 0 );
- if (m_vNumInterests[index] == -1)
- return;
- vector<long>& vInsterest = m_vInterests[m_vNumInterests[index]];
- long lPos = m_vPosInterest[index];
- vInsterest[lPos] = vInsterest[vInsterest.size() - 1];
- m_vPosInterest[vInsterest[vInsterest.size() - 1]] = lPos;
- vInsterest.erase(vInsterest.begin() + (vInsterest.size() - 1));
- m_vNumInterests[index]--;
- assert(m_vNumInterests[index] >= 0);
- m_vPosInterest[index] = m_vInterests[m_vNumInterests[index]].size();
- m_vInterests[m_vNumInterests[index]].push_back(index);
- }
- void CPiecePicker::came_in(long index)
- {
- assert(index < m_vNumInterests.size() && index >= 0 );
- if (m_vNumInterests[index] == -1)
- return;
- vector<long>& vInsterest = m_vInterests[m_vNumInterests[index]];
- long lPos = m_vPosInterest[index];
- vInsterest[lPos] = vInsterest[vInsterest.size() - 1];
- m_vPosInterest[vInsterest[vInsterest.size() - 1]] = lPos;
- vInsterest.erase(vInsterest.begin() + (vInsterest.size() - 1));
- m_vNumInterests[index] = -1;
- m_vFixed.push_back(index);
- }
- void CPiecePicker::complete(long index)
- {
- m_bGotany = true;
- came_in(index);
- vector<long>::iterator iter = find(m_vFixed.begin(), m_vFixed.end(), index);
- if (iter != m_vFixed.end())
- m_vFixed.erase(iter);
- }
- vector<long> CPiecePicker::GetResult()
- {
- vector<long> vRet = m_vFixed;
- // showlong(vRet);
- // showvector(m_vInterests);
- for (int i=1; i<m_vInterests.size(); i++)
- for (int j=0; j<m_vInterests[i].size(); j++)
- {
- long x = randlong(j, m_vInterests[i].size()-1);
- if (x != j)
- {
- long last = m_vInterests[i][j];
- m_vInterests[i][j] = m_vInterests[i][x];
- m_vInterests[i][x] = last;
- m_vPosInterest[m_vInterests[i][x]] = x;
- m_vPosInterest[m_vInterests[i][j]] = j;
- }
- vRet.push_back(m_vInterests[i][j]);
- }
- // showvector(m_vInterests);
- return vRet;
- }
- long randlong(long b, long e)
- {
- assert(e >= b);
- return rand()%(e-b+1) + b;
- }
- //////////////////////////////////////////////////////
- // following for test
- void showlong(vector<long> vInsterest)
- {
- TRACE("rnarray:{");
- for (int i=0; i<vInsterest.size(); i++)
- {
- TRACE("%d,", vInsterest[i]);
- }
- TRACE("}rn");
- }
- void showvector(vector<vector<long> > vInsterest)
- {
- TRACE("rntree:{");
- for (int i=0; i<vInsterest.size(); i++)
- {
- TRACE("{");
- for (int j=0; j<vInsterest[i].size(); j++)
- {
- TRACE("%d,", vInsterest[i][j]);
- }
- TRACE("}, ");
- }
- TRACE("}rn");
- }
- void PiecePickerTest()
- {
- long l = randlong(0, 10);
- l = randlong(0, 10);
- l = randlong(0, 10);
- l = randlong(0, 0);
- l = randlong(0, 0);
- /*
- CPiecePicker p(10);
- p.got_have(0);
- p.got_have(2);
- p.GetResult();
- p.got_have(4);
- p.got_have(6);
- p.came_in(1);
- p.came_in(1);
- p.came_in(3);
- p.came_in(0);
- p.came_in(6);
- for (int i=0; i<100; i++)
- {
- vector<long> v = p.GetResult();
- showlong(v);
- }
- return;
- //*/
- /*
- CPiecePicker p(10);
- p.got_have(0);
- p.got_have(2);
- p.got_have(4);
- p.got_have(6);
- p.lost_have(2);
- p.lost_have(6);
- p.GetResult();
- //*/
- //*
- CPiecePicker p(10);
- p.complete(8);
- p.got_have(0);
- p.got_have(2);
- p.got_have(2);
- p.got_have(2);
- p.got_have(3);
- p.got_have(3);
- p.got_have(3);
- p.got_have(4);
- p.got_have(6);
- // p.lost_have(2);
- // p.lost_have(2);
- p.lost_have(6);
- // p.complete(2);
- for (int i=0; i<100; i++)
- {
- vector<long> v = p.GetResult();
- showlong(v);
- }
- return;
- //*/
- /*
- CPiecePicker p(1);
- p.got_have(0);
- p.complete(0);
- p.GetResult();
- p.got_have(0);
- p.lost_have(0);
- //*/
- }