newly DataLifecycleManager in Map/ObjectManage/Queue/Stack
This commit is contained in:
parent
1fe8f51faa
commit
bf8be47d3a
@ -9,7 +9,7 @@ namespace hgl
|
||||
* @return 数据所在索引,-1表示不存在
|
||||
*/
|
||||
template<typename K,typename V,typename KVData>
|
||||
int _Map<K,V,KVData>::Find(const K &flag)const
|
||||
int MapTemplate<K,V,KVData>::Find(const K &flag)const
|
||||
{
|
||||
int left=0,right=data_list.GetCount()-1; //使用left,right而不使用min,max是为了让代码能够更好的阅读。
|
||||
int mid;
|
||||
@ -42,7 +42,7 @@ namespace hgl
|
||||
}
|
||||
|
||||
template<typename K,typename V,typename KVData>
|
||||
bool _Map<K,V,KVData>::FindPos(const K &flag,int &pos)const
|
||||
bool MapTemplate<K,V,KVData>::FindPos(const K &flag,int &pos)const
|
||||
{
|
||||
int left=0,right=data_list.GetCount()-1;
|
||||
int mid;
|
||||
@ -125,7 +125,7 @@ namespace hgl
|
||||
}
|
||||
|
||||
template<typename K,typename V,typename KVData>
|
||||
int _Map<K,V,KVData>::FindByValue(const V &data)const
|
||||
int MapTemplate<K,V,KVData>::FindByValue(const V &data)const
|
||||
{
|
||||
const int count=data_list.GetCount();
|
||||
|
||||
@ -150,7 +150,7 @@ namespace hgl
|
||||
* @return 新创建好的数据结构
|
||||
*/
|
||||
template<typename K,typename V,typename KVData>
|
||||
KVData *_Map<K,V,KVData>::Add(const K &flag,const V &data)
|
||||
KVData *MapTemplate<K,V,KVData>::Add(const K &flag,const V &data)
|
||||
{
|
||||
KVData *dp=new KVData;
|
||||
|
||||
@ -175,7 +175,7 @@ namespace hgl
|
||||
* @param obj 数据
|
||||
*/
|
||||
template<typename K,typename V,typename KVData>
|
||||
void _Map<K,V,KVData>::Add(KVData *obj)
|
||||
void MapTemplate<K,V,KVData>::Add(KVData *obj)
|
||||
{
|
||||
data_list.Insert(FindPos(obj->key),obj);
|
||||
}
|
||||
@ -185,7 +185,7 @@ namespace hgl
|
||||
* 比如定义了Map<int ID,struct DATA>,直接返回DATA需要复制会消耗一些时间,直接返回DATA *会更好一些
|
||||
*/
|
||||
template<typename K,typename V,typename KVData>
|
||||
V *_Map<K,V,KVData>::GetValuePointer(const K &key)const
|
||||
V *MapTemplate<K,V,KVData>::GetValuePointer(const K &key)const
|
||||
{
|
||||
int index=Find(key);
|
||||
|
||||
@ -205,7 +205,7 @@ namespace hgl
|
||||
* @return 数据序号,<0表示失败
|
||||
*/
|
||||
template<typename K,typename V,typename KVData>
|
||||
int _Map<K,V,KVData>::GetValueAndSerial(const K &flag,V &data) const
|
||||
int MapTemplate<K,V,KVData>::GetValueAndSerial(const K &flag,V &data) const
|
||||
{
|
||||
int index=Find(flag);
|
||||
|
||||
@ -220,7 +220,7 @@ namespace hgl
|
||||
}
|
||||
|
||||
template<typename K,typename V,typename KVData>
|
||||
bool _Map<K,V,KVData>::Check(const K &key,const V &value) const
|
||||
bool MapTemplate<K,V,KVData>::Check(const K &key,const V &value) const
|
||||
{
|
||||
int index=Find(key);
|
||||
|
||||
@ -240,7 +240,7 @@ namespace hgl
|
||||
* @return 是否取得成功
|
||||
*/
|
||||
template<typename K,typename V,typename KVData>
|
||||
bool _Map<K,V,KVData>::GetBySerial(int index,K &f,V &t) const
|
||||
bool MapTemplate<K,V,KVData>::GetBySerial(int index,K &f,V &t) const
|
||||
{
|
||||
if(index<0||index>=data_list.GetCount())return(false);
|
||||
|
||||
@ -262,7 +262,7 @@ namespace hgl
|
||||
* @return 是否取得成功
|
||||
*/
|
||||
template<typename K,typename V,typename KVData>
|
||||
bool _Map<K,V,KVData>::GetKey(int index,K &f)
|
||||
bool MapTemplate<K,V,KVData>::GetKey(int index,K &f)
|
||||
{
|
||||
if(index<0||index>=data_list.GetCount())return(false);
|
||||
|
||||
@ -283,7 +283,7 @@ namespace hgl
|
||||
* @return 是否取得成功
|
||||
*/
|
||||
template<typename K,typename V,typename KVData>
|
||||
bool _Map<K,V,KVData>::GetValue(int index,V &t)
|
||||
bool MapTemplate<K,V,KVData>::GetValue(int index,V &t)
|
||||
{
|
||||
if(index<0||index>=data_list.GetCount())return(false);
|
||||
|
||||
@ -303,7 +303,7 @@ namespace hgl
|
||||
* @param t 数据
|
||||
*/
|
||||
template<typename K,typename V,typename KVData>
|
||||
bool _Map<K,V,KVData>::SetValueBySerial(int index,V &t)
|
||||
bool MapTemplate<K,V,KVData>::SetValueBySerial(int index,V &t)
|
||||
{
|
||||
if(index<0||index>=data_list.GetCount())return(false);
|
||||
|
||||
@ -319,7 +319,7 @@ namespace hgl
|
||||
* @return 是否成功
|
||||
*/
|
||||
template<typename K,typename V,typename KVData>
|
||||
bool _Map<K,V,KVData>::Delete(const K &flag,V &data)
|
||||
bool MapTemplate<K,V,KVData>::Delete(const K &flag,V &data)
|
||||
{
|
||||
int index=Find(flag);
|
||||
|
||||
@ -342,7 +342,7 @@ namespace hgl
|
||||
* @return 是否成功
|
||||
*/
|
||||
template<typename K,typename V,typename KVData>
|
||||
bool _Map<K,V,KVData>::DeleteByKey(const K &flag)
|
||||
bool MapTemplate<K,V,KVData>::DeleteByKey(const K &flag)
|
||||
{
|
||||
return DeleteAt(Find(flag));
|
||||
}
|
||||
@ -354,7 +354,7 @@ namespace hgl
|
||||
* @return 是否成功
|
||||
*/
|
||||
template<typename K,typename V,typename KVData>
|
||||
int _Map<K,V,KVData>::DeleteByKey(const K *fp,const int count)
|
||||
int MapTemplate<K,V,KVData>::DeleteByKey(const K *fp,const int count)
|
||||
{
|
||||
if(!fp||count<=0)return(0);
|
||||
|
||||
@ -378,7 +378,7 @@ namespace hgl
|
||||
* @return 是否成功
|
||||
*/
|
||||
template<typename K,typename V,typename KVData>
|
||||
bool _Map<K,V,KVData>::DeleteByValue(const V &data)
|
||||
bool MapTemplate<K,V,KVData>::DeleteByValue(const V &data)
|
||||
{
|
||||
return DeleteAt(FindByValue(data));
|
||||
}
|
||||
@ -389,7 +389,7 @@ namespace hgl
|
||||
* @return 是否成功
|
||||
*/
|
||||
template<typename K,typename V,typename KVData>
|
||||
bool _Map<K,V,KVData>::DeleteAt(int index)
|
||||
bool MapTemplate<K,V,KVData>::DeleteAt(int index)
|
||||
{
|
||||
if(index<0
|
||||
||index>=data_list.GetCount())return(false);
|
||||
@ -407,7 +407,7 @@ namespace hgl
|
||||
* @return 是否成功
|
||||
*/
|
||||
template<typename K,typename V,typename KVData>
|
||||
bool _Map<K,V,KVData>::DeleteAt(int start,int number)
|
||||
bool MapTemplate<K,V,KVData>::DeleteAt(int start,int number)
|
||||
{
|
||||
KVData **dp=data_list.GetData()+start;
|
||||
|
||||
@ -426,7 +426,7 @@ namespace hgl
|
||||
* @param data 新的数据内容
|
||||
*/
|
||||
template<typename K,typename V,typename KVData>
|
||||
bool _Map<K,V,KVData>::ChangeOrAdd(const K &flag,const V &data)
|
||||
bool MapTemplate<K,V,KVData>::ChangeOrAdd(const K &flag,const V &data)
|
||||
{
|
||||
int result;
|
||||
KVData *dp;
|
||||
@ -467,7 +467,7 @@ namespace hgl
|
||||
* @param return 是否更改成功
|
||||
*/
|
||||
template<typename K,typename V,typename KVData>
|
||||
bool _Map<K,V,KVData>::Change(const K &flag,const V &data)
|
||||
bool MapTemplate<K,V,KVData>::Change(const K &flag,const V &data)
|
||||
{
|
||||
KVData *dp=GetObjectFromList(data_list,Find(flag));
|
||||
|
||||
@ -482,7 +482,7 @@ namespace hgl
|
||||
* 清除所有数据
|
||||
*/
|
||||
template<typename K,typename V,typename KVData>
|
||||
void _Map<K,V,KVData>::Free()
|
||||
void MapTemplate<K,V,KVData>::Free()
|
||||
{
|
||||
data_pool.Clear();
|
||||
data_list.Free();
|
||||
@ -492,14 +492,14 @@ namespace hgl
|
||||
* 清除所有数据,但不释放内存
|
||||
*/
|
||||
template<typename K,typename V,typename KVData>
|
||||
void _Map<K,V,KVData>::Clear()
|
||||
void MapTemplate<K,V,KVData>::Clear()
|
||||
{
|
||||
data_pool.ReleaseActive();
|
||||
data_list.Clear();
|
||||
}
|
||||
|
||||
template<typename K,typename V,typename KVData>
|
||||
void _Map<K,V,KVData>::operator=(const ThisClass &ftd)
|
||||
void MapTemplate<K,V,KVData>::operator=(const ThisClass &ftd)
|
||||
{
|
||||
Free();
|
||||
|
||||
@ -529,7 +529,7 @@ namespace hgl
|
||||
}
|
||||
|
||||
template<typename K,typename V,typename KVData>
|
||||
void _Map<K,V,KVData>::Enum(void (*enum_func)(const K &,V &))
|
||||
void MapTemplate<K,V,KVData>::Enum(void (*enum_func)(const K &,V &))
|
||||
{
|
||||
const int count=data_list.GetCount();
|
||||
|
||||
@ -547,7 +547,7 @@ namespace hgl
|
||||
}
|
||||
|
||||
template<typename K,typename V,typename KVData>
|
||||
void _Map<K,V,KVData>::EnumKey(void (*enum_func)(const K &))
|
||||
void MapTemplate<K,V,KVData>::EnumKey(void (*enum_func)(const K &))
|
||||
{
|
||||
const int count=data_list.GetCount();
|
||||
|
||||
@ -565,7 +565,7 @@ namespace hgl
|
||||
}
|
||||
|
||||
template<typename K,typename V,typename KVData>
|
||||
void _Map<K,V,KVData>::EnumAllValue(void (*enum_func)(V &))
|
||||
void MapTemplate<K,V,KVData>::EnumAllValue(void (*enum_func)(V &))
|
||||
{
|
||||
const int count=data_list.GetCount();
|
||||
|
||||
@ -583,7 +583,7 @@ namespace hgl
|
||||
}
|
||||
|
||||
template<typename K,typename V,typename KVData>
|
||||
void _Map<K,V,KVData>::EnumValue(bool (*enum_func)(V &))
|
||||
void MapTemplate<K,V,KVData>::EnumValue(bool (*enum_func)(V &))
|
||||
{
|
||||
const int count=data_list.GetCount();
|
||||
|
||||
@ -605,7 +605,7 @@ namespace hgl
|
||||
* 统计出所有在in_list中出现的数据,产生的结果写入with_list
|
||||
*/
|
||||
template<typename K,typename V,typename KVData>
|
||||
void _Map<K,V,KVData>::WithList(_Map<K,V,KVData>::KVDataList &with_list,const List<K> &in_list)
|
||||
void MapTemplate<K,V,KVData>::WithList(MapTemplate<K,V,KVData>::KVDataList &with_list,const List<K> &in_list)
|
||||
{
|
||||
with_list.Clear();
|
||||
const int count=this->GetCount();
|
||||
@ -629,7 +629,7 @@ namespace hgl
|
||||
* 统计出所有没有出现在in_list中的数据,产生的结果写入without_list
|
||||
*/
|
||||
template<typename K,typename V,typename KVData>
|
||||
void _Map<K,V,KVData>::WithoutList(_Map<K,V,KVData>::KVDataList &without_list,const List<K> &in_list)
|
||||
void MapTemplate<K,V,KVData>::WithoutList(MapTemplate<K,V,KVData>::KVDataList &without_list,const List<K> &in_list)
|
||||
{
|
||||
without_list.Clear();
|
||||
const int count=this->GetCount();
|
||||
|
@ -1,5 +1,4 @@
|
||||
#ifndef HGL_MAP_INCLUDE
|
||||
#define HGL_MAP_INCLUDE
|
||||
#pragma once
|
||||
|
||||
#include<hgl/type/List.h>
|
||||
#include<hgl/type/Pair.h>
|
||||
@ -16,11 +15,11 @@ namespace hgl
|
||||
/**
|
||||
* 索引数据模板
|
||||
*/
|
||||
template<typename K,typename V,typename KVData> class _Map
|
||||
template<typename K,typename V,typename KVData> class MapTemplate
|
||||
{
|
||||
protected:
|
||||
|
||||
using ThisClass=_Map<K,V,KVData>;
|
||||
using ThisClass=MapTemplate<K,V,KVData>;
|
||||
|
||||
using KVDataPool=ObjectPool<KVData>;
|
||||
using KVDataList=List<KVData *>;
|
||||
@ -35,8 +34,8 @@ namespace hgl
|
||||
|
||||
public: //方法
|
||||
|
||||
_Map()=default;
|
||||
virtual ~_Map()=default;
|
||||
MapTemplate()=default;
|
||||
virtual ~MapTemplate()=default;
|
||||
|
||||
const int GetCount()const{return data_list.GetCount();} ///<取得数据总量
|
||||
const bool IsEmpty()const{return data_list.IsEmpty();} ///<是否为空
|
||||
@ -141,9 +140,9 @@ namespace hgl
|
||||
|
||||
void WithList(KVDataList &with_list,const List<K> &in_list); ///<统计出所有在in_list中出现的数据,产生的结果写入with_list
|
||||
void WithoutList(KVDataList &without_list,const List<K> &in_list); ///<统计出所有没有出现在in_list中的数据,产生的结果写入without_list
|
||||
};//class _Map
|
||||
};//class MapTemplate
|
||||
|
||||
template<typename K,typename V> class Map:public _Map<K,V,KeyValue<K,V> >
|
||||
template<typename K,typename V> class Map:public MapTemplate<K,V,KeyValue<K,V> >
|
||||
{
|
||||
public:
|
||||
|
||||
@ -161,21 +160,19 @@ namespace hgl
|
||||
return result;
|
||||
}
|
||||
|
||||
template<typename K,typename V,typename KVData> class _ObjectMap:public _Map<K,V *,KVData>
|
||||
template<typename K,typename V,typename KVData> class ObjectMapTemplate:public MapTemplate<K,V *,KVData>
|
||||
{
|
||||
protected:
|
||||
|
||||
typedef _Map<K,V *,KVData> SuperClass;
|
||||
typedef MapTemplate<K,V *,KVData> SuperClass;
|
||||
|
||||
ObjectLifecycleManager<V> *olc;
|
||||
ObjectLifecycleManager<V> default_olc;
|
||||
ObjectLifecycleManager<V> *olm;
|
||||
|
||||
void DeleteObject(KVData *ds)
|
||||
{
|
||||
if(!ds)return;
|
||||
|
||||
if(olc)
|
||||
olc->Clear(&(ds->value));
|
||||
olm->Clear(&(ds->value));
|
||||
}
|
||||
|
||||
void DeleteObject(int index)
|
||||
@ -185,21 +182,16 @@ namespace hgl
|
||||
|
||||
public:
|
||||
|
||||
_ObjectMap()
|
||||
ObjectMapTemplate(ObjectLifecycleManager<V> *_olm)
|
||||
{
|
||||
olc=&default_olc;
|
||||
olm=_olm;
|
||||
}
|
||||
|
||||
virtual ~_ObjectMap()
|
||||
virtual ~ObjectMapTemplate()
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
|
||||
virtual void SetDataLifetimeCallback(ObjectLifecycleManager<V> *cb) ///<设定数据生命周期回调函数
|
||||
{
|
||||
olc=cb;
|
||||
}
|
||||
|
||||
/**
|
||||
* 断开一个数据
|
||||
* @param flag 要断开的数据标识
|
||||
@ -349,9 +341,19 @@ namespace hgl
|
||||
else
|
||||
return nullptr;
|
||||
};
|
||||
};//class _ObjectMap
|
||||
};//class ObjectMapTemplate
|
||||
|
||||
template<typename K,typename V> using ObjectMap=_ObjectMap<K,V,KeyValue<K,V *> >;
|
||||
template<typename K,typename V> class ObjectMap:public ObjectMapTemplate<K,V,KeyValue<K,V *>>
|
||||
{
|
||||
protected:
|
||||
|
||||
ObjectLifecycleManager<V> DefaultOLM;
|
||||
|
||||
public:
|
||||
|
||||
ObjectMap():ObjectMapTemplate(&DefaultOLM){};
|
||||
virtual ~ObjectMap()=default;
|
||||
};//template<typename K,typename V> class ObjectMap:public ObjectMapTemplate<K,V,KeyValue<K,V *>>
|
||||
}//namespace hgl
|
||||
|
||||
#include<hgl/type/Map.cpp>
|
||||
#endif//HGL_MAP_INCLUDE
|
||||
|
@ -26,7 +26,7 @@ namespace hgl
|
||||
|
||||
protected:
|
||||
|
||||
_Map<K,V *,KVObject> items;
|
||||
MapTemplate<K,V *,KVObject> items;
|
||||
|
||||
ObjectLifecycleManager<V> *olm; ///<数据生命周期回调函数
|
||||
|
||||
|
@ -171,11 +171,11 @@ namespace hgl
|
||||
|
||||
template<typename T> class ObjectQueue:public QueueTemplate<T *> ///对象队列
|
||||
{
|
||||
DefaultObjectLifetimeCallback<T> default_olc;
|
||||
ObjectLifecycleManager<T> DefaultOLM;
|
||||
|
||||
public:
|
||||
|
||||
ObjectQueue():QueueTemplate(&default_olc){}
|
||||
ObjectQueue():QueueTemplate(&DefaultOLM){}
|
||||
virtual ~ObjectQueue() override { Free(); }
|
||||
|
||||
virtual bool Push(T *obj)
|
||||
@ -192,27 +192,19 @@ namespace hgl
|
||||
return Queue<T *>::Push(obj_list,count);
|
||||
}
|
||||
|
||||
//virtual T *Pop()
|
||||
//{
|
||||
// T *obj;
|
||||
|
||||
// if(!Queue<T *>::Pop(obj))
|
||||
// return(nullptr);
|
||||
|
||||
// return obj;
|
||||
//}
|
||||
|
||||
void Clear(DataLifecycleManager<T *> *olc=nullptr)
|
||||
T *Pop()
|
||||
{
|
||||
if(!olc)
|
||||
olc=&default_olc;
|
||||
T *obj;
|
||||
|
||||
Queue<T *>::Clear(olc);
|
||||
if(!Queue<T *>::Pop(obj))
|
||||
return(nullptr);
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
void Free(DataLifecycleManager<T *> *olc=nullptr)
|
||||
void Free()
|
||||
{
|
||||
ObjectQueue<T>::Clear(olc);
|
||||
ObjectQueue<T>::Clear();
|
||||
|
||||
this->data_array[0].Free();
|
||||
this->data_array[1].Free();
|
||||
|
@ -1,16 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include<hgl/type/DataArray.h>
|
||||
#include<hgl/type/LifetimeCallback.h>
|
||||
#include<hgl/type/LifecycleManager.h>
|
||||
namespace hgl
|
||||
{
|
||||
/**
|
||||
* Stack模板类用于保存一个先进后出、后进先出的数据堆栈
|
||||
*/
|
||||
template<typename T> class Stack ///堆栈顺序数据访问类
|
||||
template<typename T> class StackTemplate ///堆栈顺序数据访问类
|
||||
{
|
||||
protected:
|
||||
|
||||
DataLifecycleManager<T> *dlm;
|
||||
|
||||
DataArray<T> data_array;
|
||||
|
||||
public: //属性
|
||||
@ -38,8 +40,8 @@ namespace hgl
|
||||
|
||||
public: //方法
|
||||
|
||||
Stack()=default;
|
||||
virtual ~Stack()=default;
|
||||
StackTemplate(DataLifecycleManager<T> *_dlm){dlm=_dlm;}
|
||||
virtual ~StackTemplate()=default;
|
||||
|
||||
virtual bool Push(T *data,int count) ///<压入多个数据
|
||||
{
|
||||
@ -87,15 +89,14 @@ namespace hgl
|
||||
|
||||
public:
|
||||
|
||||
virtual void Clear (DataLifecycleManager<T> *dlm=nullptr) ///<清除所有数据
|
||||
virtual void Clear () ///<清除所有数据
|
||||
{
|
||||
if(dlm)
|
||||
dlm->Clear(data_array.GetData(),data_array.GetCount());
|
||||
|
||||
data_array.Clear();
|
||||
}
|
||||
|
||||
virtual void Free (DataLifecycleManager<T> *dlm=nullptr) ///<清除所有数据并释放内存
|
||||
virtual void Free () ///<清除所有数据并释放内存
|
||||
{
|
||||
Clear(dlm);
|
||||
data_array.Free();
|
||||
@ -109,15 +110,27 @@ namespace hgl
|
||||
}
|
||||
|
||||
virtual void operator =(const Stack<T> &s){this->operator=(s.data_array);}
|
||||
};//template<typename T> class Stack
|
||||
};//template<typename T> class StackTemplate
|
||||
|
||||
template<typename T> class ObjectStack:public Stack<T *> ///堆栈对象
|
||||
template<typename T> class Stack:public StackTemplate<T>
|
||||
{
|
||||
DefaultObjectLifetimeCallback<T> default_olc;
|
||||
protected:
|
||||
|
||||
DataLifecycleManager<T> DefaultDLM;
|
||||
|
||||
public:
|
||||
|
||||
using Stack<T *>::Stack;
|
||||
Stack():StackTemplate(&DefaultDLM){};
|
||||
virtual ~Stack()=default;
|
||||
};//template<typename T> class Stack:public StackTemplate<T>
|
||||
|
||||
template<typename T> class ObjectStack:public StackTemplate<T *> ///堆栈对象
|
||||
{
|
||||
ObjectLifecycleManager<T> DefaultOLM;
|
||||
|
||||
public:
|
||||
|
||||
ObjectStack():StackTemplate(&DefaultOLM){}
|
||||
virtual ~ObjectStack() override {Free();}
|
||||
|
||||
virtual bool Push(T *obj)
|
||||
@ -127,7 +140,7 @@ namespace hgl
|
||||
return Stack<T *>::Push(obj);
|
||||
}
|
||||
|
||||
virtual T *Pop()
|
||||
T *Pop()
|
||||
{
|
||||
T *obj;
|
||||
|
||||
@ -137,17 +150,9 @@ namespace hgl
|
||||
return obj;
|
||||
}
|
||||
|
||||
void Clear(ObjectLifecycleManager<T> *olc=nullptr)
|
||||
void Free()
|
||||
{
|
||||
if(!olc)
|
||||
olc=&default_olc;
|
||||
|
||||
Stack<T *>::Clear(olc);
|
||||
}
|
||||
|
||||
void Free(ObjectLifecycleManager<T> *olc=nullptr)
|
||||
{
|
||||
ObjectStack<T>::Clear(olc);
|
||||
ObjectStack<T>::Clear();
|
||||
|
||||
this->data_array.Free();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user