update ResPool<> Stats function, add KeyExistActive,KeyExistIdle,GetRefCount functions, add comment in all functions.
This commit is contained in:
@@ -18,6 +18,9 @@ namespace hgl
|
||||
}
|
||||
};//template<typename T> struct RefData
|
||||
|
||||
/**
|
||||
* 附带引用计数的资源池列表
|
||||
*/
|
||||
template<typename K,typename V> class ResPool
|
||||
{
|
||||
protected:
|
||||
@@ -37,36 +40,49 @@ namespace hgl
|
||||
/**
|
||||
* 根据key列表统计数据
|
||||
* @param key_list 要扫描的key列表
|
||||
* @param in_active_list 有多少个key是活跃的
|
||||
* @param in_idle_list 有多少个key是闲置的
|
||||
* @param out_list active/idle中都不存在的key有多少个
|
||||
* @param idle_left_list 不在active/idle中,但可以从idle中释放的个数
|
||||
* @param in_active_count 有多少个key是活跃的
|
||||
* @param in_idle_count 有多少个key是闲置的
|
||||
* @param out_count active/idle中都不存在的key有多少个
|
||||
* @param idle_left_count 不在active/idle中,但可以从idle中释放的个数
|
||||
*/
|
||||
void Stats(const List<K> &key_list,List<K> &in_active_list,List<K> &in_idle_list,List<K> &out_list,List<K> &idle_left_list)
|
||||
void Stats(const List<K> &key_list,int *in_active_count,int *in_idle_count,int *out_count,int *idle_left_count)
|
||||
{
|
||||
in_active_list.ClearData();
|
||||
in_idle_list.ClearData();
|
||||
out_list.ClearData();
|
||||
idle_left_list.ClearData();
|
||||
*in_active_count=0;
|
||||
*in_idle_count=0;
|
||||
*out_count=0;
|
||||
*idle_left_count=0;
|
||||
|
||||
const K *kp=key_list.GetData();
|
||||
for(int i=0;i<key_list.GetCount();i++)
|
||||
{
|
||||
if(active_items.KeyExist(*kp))
|
||||
in_active_list.Add(*kp);
|
||||
++(*in_active_count);
|
||||
else
|
||||
if(idle_items.KeyExist(*kp))
|
||||
in_idle_list.Add(*kp);
|
||||
++(*in_idle_count);
|
||||
else
|
||||
out_list.Add(*kp);
|
||||
++(*out_count);
|
||||
|
||||
++kp;
|
||||
}
|
||||
|
||||
idle_items.WithoutList(idle_left_list,in_idle_list);
|
||||
*idle_left_count=idle_items.GetCount()-(*in_idle_count);
|
||||
}
|
||||
|
||||
bool KeyExist(const K &key)
|
||||
/**
|
||||
* 确定指定key是否在活跃列表
|
||||
*/
|
||||
bool KeyExistActive(const K &key)const{return active_items.KeyExist(key);}
|
||||
|
||||
/**
|
||||
* 确定指定key是否在闲置列表
|
||||
*/
|
||||
bool KeyExistIdle(const K &key)const{return idle_items.KeyExist(key);}
|
||||
|
||||
/**
|
||||
* 确定指定key是否在列表中(包括活跃列表和闲置列表)
|
||||
*/
|
||||
bool KeyExist(const K &key)const
|
||||
{
|
||||
if(active_items.KeyExist(key))
|
||||
return(true);
|
||||
@@ -77,6 +93,29 @@ namespace hgl
|
||||
return(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定key数据的引用计数
|
||||
* @param key 要查询的key
|
||||
* @return >0 在活跃列表中
|
||||
* @return =0 在闲置列表中
|
||||
* @return <0 不存在
|
||||
*/
|
||||
int GetRefCount(const K &key)const
|
||||
{
|
||||
ActiveItem *ai;
|
||||
|
||||
if(active_items.Get(key,ai)) //在活跃列表中找
|
||||
return ai->ref_count;
|
||||
|
||||
if(idle_items.KeyExist(key))
|
||||
return 0;
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据key获取指定值(并增加引用引数)
|
||||
*/
|
||||
bool Get(const K &key,V &value)
|
||||
{
|
||||
int pos;
|
||||
@@ -100,11 +139,17 @@ namespace hgl
|
||||
return(false);
|
||||
}
|
||||
|
||||
/**
|
||||
* 外部增加一个值
|
||||
*/
|
||||
void Add(const K &key,V &value)
|
||||
{
|
||||
active_items.Add(key,new ActiveItem(value));
|
||||
}
|
||||
|
||||
/**
|
||||
* 释放一个值的使用(并释放引用计数)
|
||||
*/
|
||||
void Release(const K &key)
|
||||
{
|
||||
int pos;
|
||||
|
Reference in New Issue
Block a user