new Queue<>, test OK!
This commit is contained in:
@@ -23,12 +23,12 @@ template<typename T> class Comparator
|
||||
public:
|
||||
|
||||
/**
|
||||
* 比较函数,需被特例化或派生实现
|
||||
* 比较函数,需被特例化或派生实现. 正确情况下此函数不应该会被调用
|
||||
*/
|
||||
virtual int compare(const T &a,const T &b)const
|
||||
{
|
||||
return 0; // 如 return(a-b); ,但这个函数正确情况下不应该会被调用
|
||||
}
|
||||
virtual int compare(const T &a,const T &b)const;
|
||||
//{
|
||||
// return 0; // 如 return(a-b);
|
||||
//}
|
||||
|
||||
/**
|
||||
* 交换函数
|
||||
|
@@ -539,11 +539,18 @@ namespace hgl
|
||||
/**
|
||||
* 查找数据在数组中的位置
|
||||
* @param data 要查找的数据
|
||||
* @param start 开始查找的位置
|
||||
* @param find_count 要查找的最大数量
|
||||
* @return <0 未找到或其它错误
|
||||
*/
|
||||
const int Find(const T &data)const
|
||||
const int Find(const T &data,const int start=0,int find_count=-1)const
|
||||
{
|
||||
return FindDataPositionInArray<T>(items,count,data);
|
||||
if(!items||count<=0||start<0||start>=count)return(-1);
|
||||
|
||||
if(find_count<0||find_count>count-start)
|
||||
find_count=count;
|
||||
|
||||
return FindDataPositionInArray<T>(items+start,find_count,data);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -6,10 +6,11 @@ namespace hgl
|
||||
/**
|
||||
* 原生数据生命周期处理回调
|
||||
*/
|
||||
template<typename T> struct RawLifetimeCallback
|
||||
template<typename T> struct DataLifetimeCallback
|
||||
{
|
||||
virtual bool Create(T *){return true;}
|
||||
virtual void Clear(T *){return;}
|
||||
virtual void Clear(T *){};
|
||||
virtual void Clear(T *dst,int count){};
|
||||
|
||||
virtual void Copy(T *dst,T *src,int count)
|
||||
{
|
||||
@@ -23,20 +24,20 @@ namespace hgl
|
||||
|
||||
public: //活跃事件
|
||||
|
||||
virtual bool OnCreate (T *){return true;} ///<被创建
|
||||
virtual bool OnActive (T *){return true;} ///<切换到激活
|
||||
virtual bool OnIdle (T *){return true;} ///<切换到空闲
|
||||
virtual bool OnReuse (T *){return true;} ///<被重用(空间被重用,旧数据需要清除)
|
||||
virtual bool OnRelease (T *){return true;} ///<被释放
|
||||
virtual bool OnActive (T *,int count=1){return true;} ///<切换到激活
|
||||
virtual bool OnIdle (T *,int count=1){return true;} ///<切换到空闲
|
||||
virtual bool OnClear (T *,int count=1){return true;} ///<被释放
|
||||
};
|
||||
|
||||
/**
|
||||
* 对像生命周期处理回调
|
||||
*/
|
||||
template<typename T> struct ObjectLifetimeCallback:public RawLifetimeCallback<T *>
|
||||
template<typename T> struct ObjectLifetimeCallback:public DataLifetimeCallback<T *>
|
||||
{
|
||||
virtual bool Create(T **) override=0;
|
||||
virtual void Clear(T **) override=0;
|
||||
|
||||
virtual void Clear(T **obj,int count) override=0;
|
||||
};//struct ObjectLifetimeCallback
|
||||
|
||||
/**
|
||||
@@ -46,5 +47,17 @@ namespace hgl
|
||||
{
|
||||
virtual bool Create(T **obj) override {*obj=new T;return(true);}
|
||||
virtual void Clear(T **obj) override {if(obj&&*obj)delete *obj;}
|
||||
|
||||
virtual void Clear(T **obj,int count) override
|
||||
{
|
||||
if(!obj||count<=0)return;
|
||||
|
||||
for(int i=0;i<count;i++)
|
||||
{
|
||||
delete *obj;
|
||||
*obj=nullptr;
|
||||
++obj;
|
||||
}
|
||||
}
|
||||
};//struct DefaultObjectLifetimeCallback
|
||||
}//namespace hgl
|
||||
|
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include<hgl/type/DataArray.h>
|
||||
#include<hgl/type/LifetimeCallback.h>
|
||||
namespace hgl
|
||||
{
|
||||
/**
|
||||
@@ -41,7 +42,7 @@ namespace hgl
|
||||
data_array[1].GetAllocCount();} ///<取得已分配的数据数量
|
||||
|
||||
const int GetCount ()const{return data_array[0].GetCount()+
|
||||
data_array[1].GetCount();} ///<取得列表内数据数量
|
||||
data_array[1].GetCount()-read_offset;}///<取得列表内数据数量
|
||||
|
||||
virtual bool PreAlloc (int count) ///<预分配数据空间
|
||||
{
|
||||
@@ -53,6 +54,14 @@ namespace hgl
|
||||
|
||||
const bool IsEmpty ()const{return GetCount()==0;} ///<确认列表是否为空
|
||||
|
||||
const bool IsExist (const T &data)const
|
||||
{
|
||||
if(data_array[read_index].Find(data,read_offset)!=-1)
|
||||
return(true);
|
||||
|
||||
return data_array[write_index].Find(data)!=-1;
|
||||
}
|
||||
|
||||
public: //方法
|
||||
|
||||
Queue()
|
||||
@@ -100,7 +109,7 @@ namespace hgl
|
||||
|
||||
virtual bool Pop (T &data) ///<弹出一个数据
|
||||
{
|
||||
if(data_array[read_index].GetCount()>=read_offset)
|
||||
if(data_array[read_index].GetCount()<=read_offset)
|
||||
{
|
||||
if(data_array[write_index].GetCount()<=0)
|
||||
return(false);
|
||||
@@ -108,37 +117,51 @@ namespace hgl
|
||||
data_array[read_index].Clear();
|
||||
|
||||
SwapIndex();
|
||||
|
||||
data_array[read_index].ReadAt(data,0);
|
||||
|
||||
++read_offset;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
data_array[read_index].ReadAt(data,read_offset);
|
||||
|
||||
++read_offset;
|
||||
}
|
||||
|
||||
return(true);
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
virtual void Clear (){data_array[0].Clear();data_array[1].Clear();} ///<清除所有数据
|
||||
virtual void Free (){data_array[0].Free();data_array[1].Free();} ///<清除所有数据并释放内存
|
||||
virtual void Clear (DataLifetimeCallback<T> *dlc=nullptr) ///<清除所有数据
|
||||
{
|
||||
if(dlc)
|
||||
{
|
||||
if(data_array[read_index].GetCount()>read_offset) //还有没读完的,需要清掉
|
||||
|
||||
dlc->Clear(data_array[read_index].GetData()+read_offset,
|
||||
data_array[read_index].GetCount()-read_offset);
|
||||
}
|
||||
|
||||
data_array[0].Clear();
|
||||
data_array[1].Clear();
|
||||
}
|
||||
|
||||
virtual void Free (DataLifetimeCallback<T> *dlc=nullptr) ///<清除所有数据并释放内存
|
||||
{
|
||||
Clear(dlc);
|
||||
|
||||
data_array[0].Free();
|
||||
data_array[1].Free();
|
||||
}
|
||||
};//template<typename T> class Queue
|
||||
|
||||
template<typename T> class ObjectQueue:public Queue<T *> ///对象队列
|
||||
{
|
||||
protected:
|
||||
|
||||
virtual void DeleteObject(T *obj){if(obj)delete obj;}
|
||||
DefaultObjectLifetimeCallback<T> default_olc;
|
||||
|
||||
public:
|
||||
|
||||
using Queue<T *>::Queue;
|
||||
virtual ~ObjectQueue(){Free();}
|
||||
virtual ~ObjectQueue()
|
||||
{
|
||||
Free();
|
||||
}
|
||||
|
||||
virtual bool Push(T *obj)
|
||||
{
|
||||
@@ -157,21 +180,17 @@ namespace hgl
|
||||
return obj;
|
||||
}
|
||||
|
||||
void Clear()
|
||||
void Clear(ObjectLifetimeCallback<T> *olc=nullptr)
|
||||
{
|
||||
for(T *obj:data_array[0])
|
||||
DeleteObject(obj);
|
||||
if(!olc)
|
||||
olc=&default_olc;
|
||||
|
||||
for(T *obj:data_array[1])
|
||||
DeleteObject(obj);
|
||||
|
||||
data_array[0].Clear();
|
||||
data_array[1].Clear();
|
||||
Queue<T *>::Clear(olc);
|
||||
}
|
||||
|
||||
void Free()
|
||||
void Free(ObjectLifetimeCallback<T> *olc=nullptr)
|
||||
{
|
||||
ObjectQueue<T>::Clear();
|
||||
ObjectQueue<T>::Clear(olc);
|
||||
|
||||
data_array[0].Free();
|
||||
data_array[1].Free();
|
||||
|
Reference in New Issue
Block a user