added ActiveIDManager.h

This commit is contained in:
HuYingzhuo(hugo/hyzboy) 2023-09-13 20:06:21 +08:00
parent 43e328c7aa
commit 87b8ba6cc6
2 changed files with 165 additions and 2 deletions

View File

@ -0,0 +1,163 @@
#pragma once
#include<hgl/type/DataArray.h>
#include<hgl/type/Stack.h>
#include<hgl/type/SortedSets.h>
namespace hgl
{
/**
* ID管理器
*/
class ActiveIDManager
{
SortedSets<int> active_list; ///<活跃ID列表
Stack<int> idle_list; ///<闲置ID列表
int id_count;
private:
bool Create(int *id_list,int count)
{
if(!id_list||count<=0)return(false);
for(int i=id_count;i<id_count+count;i++)
{
*id_list=i;
++id_list;
}
id_count+=count;
return(true);
}
public:
ActiveIDManager()
{
id_count=0;
}
~ActiveIDManager()=default;
void Alloc(int c)
{
active_list.PreAlloc(c);
idle_list.PreAlloc(c);
}
int GetActiveCount ()const{return active_list.GetCount();}
int GetIdleCount ()const{return idle_list.GetCount();}
int GetTotalCount ()const{return active_list.GetCount()+idle_list.GetCount();}
int GetHistoryMaxId ()const{return id_count;}
const DataArray<int> &GetActiveArray()const{return active_list;}
const DataArray<int> &GetIdleArray()const{return idle_list;}
public:
/**
* IDID列表中)
* @return
*/
int CreateActive(int *id,int count=1)
{
if(!id||count<=0)return(0);
if(!Create(id,count))return(0);
return active_list.Add(id,count);
}
/**
* IDID列表中(ID)
* @return
*/
int CreateIdle(int count=1)
{
if(count<=0)return(0);
const int new_count=idle_list.GetCount()+count;
idle_list.PreAlloc(new_count);
int *end=idle_list.end();
if(!Create(end,count))return(0);
idle_list.SetCount(new_count);
return(count);
}
/**
* ID数据(Idle中取,Active列表)
*/
bool Get(int *id,int count=1)
{
if(!id||count<=0)return(false);
if(!idle_list.Pop(id,count))
return(false);
active_list.Add(id,count);
return(true);
}
/**
* ID数据(Idle中取Active列表)
*/
bool GetOrCreate(int *id,int count=1)
{
if(!id||count<=0)return(false);
if(idle_list.GetCount()<count)
CreateIdle(count-idle_list.GetCount());
return Get(id,count);
}
/**
* ID数据(Active列表中取出Idle列表中)
*/
int Release(int *id,int count=1)
{
if(!id||count<=0)return(false);
int result=0;
while(count--)
{
if(active_list.Delete(*id))
{
idle_list.Push(*id);
++result;
}
++id;
}
return result;
}
/**
* ID
*/
int ReleaseAllActive()
{
const int count=active_list.GetCount();
if(count>0)
{
idle_list.Push(active_list.GetData(),count);
active_list.Clear();
}
return count;
}
};//class ActiveIDManager
}//namespace hgl

View File

@ -144,7 +144,7 @@ SOURCE_GROUP("IO\\MemoryStream" FILES ${IO_MEMORY_STREAM_FILES})
SOURCE_GROUP("IO\\File" FILES ${IO_FILE_FILES}) SOURCE_GROUP("IO\\File" FILES ${IO_FILE_FILES})
SOURCE_GROUP("IO\\Jave" FILES ${IO_JAVA_FILES}) SOURCE_GROUP("IO\\Jave" FILES ${IO_JAVA_FILES})
SOURCE_GROUP("IO\\Text" FILES ${IO_TEXT_FILES}) SOURCE_GROUP("IO\\Text" FILES ${IO_TEXT_FILES})
SOURCE_GROUP("ID\\StringList" FILES ${IO_STRING_LIST_FILES}) SOURCE_GROUP("IO\\StringList" FILES ${IO_STRING_LIST_FILES})
SET(IO_SOURCE_FILES ${IO_BASE_FILES} SET(IO_SOURCE_FILES ${IO_BASE_FILES}
${IO_DATA_FILES} ${IO_DATA_FILES}