PushList.h
资源名称:p2p_vod.rar [点击查看]
上传用户:liguizhu
上传日期:2015-11-01
资源大小:2422k
文件大小:4k
源码类别:
P2P编程
开发平台:
Visual C++
- /*
- * Openmysee
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
- */
- #pragma once
- class ReqBlock {
- public:
- UINT blockID; // 编号
- DWORD reqTime; // 开始请求的时间
- bool operator == (const UINT& blockID) const {
- return (this->blockID == blockID);
- };
- };
- class PushList {
- public:
- // 构造函数
- explicit PushList() : m_array(0), m_totalsize(0), m_validsize(0) {
- };
- // 拷贝构造函数
- explicit PushList(const PushList& another) : m_array(NULL), m_totalsize(another.m_totalsize), m_validsize(another.m_validsize) {
- if(m_totalsize) {
- m_array = new ReqBlock[m_totalsize];
- memcpy(m_array, another.m_array, m_validsize*sizeof(ReqBlock));
- }
- };
- virtual ~PushList(void) {
- m_totalsize = 0;
- m_validsize = 0;
- delete [] m_array;
- m_array = NULL;
- }
- // 添加一个块
- bool AddBlock(UINT blockID, DWORD reqTime=0) {
- assert(blockID != UINT_MAX);
- // 查找是否存在重复的块,如果存在就返回false
- if(find(m_array, m_array+m_validsize, blockID) != m_array+m_validsize)
- return false;
- ReqBlock* temp = m_array;
- if(m_validsize == m_totalsize) {
- // 如果没有无效区间可以使用,那么需要增加数组大小
- // 分配一个空间更大的数组,并把区间i之前的数据复制过去
- m_array = new ReqBlock[m_validsize+1];
- memcpy(m_array, temp, m_validsize*sizeof(ReqBlock));
- }
- // 将此块添加到数组末尾
- m_array[m_validsize].blockID = blockID;
- m_array[m_validsize].reqTime = reqTime;
- // 此时m_totalsize和m_validsize尚未增加计数,所以可以用来判断是否增加过区间大小
- if(m_validsize == m_totalsize) {
- // 删除旧的数组,并增加数组大小的计数
- delete [] temp;
- ++m_totalsize;
- }
- ++m_validsize;
- return true;
- };
- // 删除一个块
- bool DelBlock(UINT blockID, DWORD * const reqTime=NULL) {
- assert(blockID != UINT_MAX);
- // 查找是否存在此块,如果不存在就返回false
- ReqBlock* index = find(m_array, m_array+m_validsize, blockID);
- if(index == m_array+m_validsize)
- return false;
- // 获取此块开始请求的时间
- if(reqTime)
- *reqTime = index->reqTime;
- // 删除此块,并将后面的块向前移动
- memcpy(index, index+1, (m_array+m_validsize-index-1)*sizeof(ReqBlock));
- --m_validsize;
- return true;
- };
- // 查找一个block是否存在
- bool FindBlock(const UINT blockID) const {
- // 查找是否存在此块,如果不存在就返回false
- return (find(m_array, m_array+m_validsize, blockID) != m_array+m_validsize);
- };
- // 复制block数组到传入的指针,传入需要的个数,返回实际复制的个数
- void CopyPushList(UINT* targetArray, UINT8& size) const {
- if(!targetArray || size == 0)
- return;
- size = min(size, m_validsize);
- for(UINT8 i = 0; i < size; ++i) {
- targetArray[i] = m_array[i].blockID;
- }
- //memcpy(targetArray, m_array, size*sizeof(UINT));
- };
- // 清空列表
- void Clear() {
- m_validsize = 0;
- };
- // 测试代码,打印出当前数组中的项
- void Print() const {
- printf("nTOTAL: %d, VALID: %d.n", m_totalsize, m_validsize);
- for(UINT8 i = 0; i < m_validsize; ++i) {
- printf("BLOCKID: %d, REQTIME: %d.n", m_array[i].blockID, m_array[i].reqTime);
- }
- };
- // 取得有效元素的个数
- UINT8 GetValidSize() {
- return m_validsize;
- };
- // 取得当前容量
- UINT8 GetTotalSize() {
- return m_totalsize;
- };
- // 取出第一个元素
- void GetFirstBlock(UINT& blockID) {
- if(!m_validsize)
- blockID = UINT_MAX;
- else {
- blockID = m_array[0].blockID;
- memcpy(m_array, m_array+1, (m_validsize-1)*sizeof(ReqBlock));
- --m_validsize;
- }
- };
- private:
- // 正在请求的块,保证无重复
- ReqBlock* m_array;
- // 数组的使用大小
- UINT8 m_totalsize;
- // 从数组头部开始有效元素的个数
- UINT8 m_validsize;
- };