From 87b8ba6cc6847ff8ff6b6239ca160240d68cedbf Mon Sep 17 00:00:00 2001 From: "HuYingzhuo(hugo/hyzboy)" Date: Wed, 13 Sep 2023 20:06:21 +0800 Subject: [PATCH] added ActiveIDManager.h --- inc/hgl/type/ActiveIDManager.h | 163 +++++++++++++++++++++++++++++++++ src/CMakeLists.txt | 4 +- 2 files changed, 165 insertions(+), 2 deletions(-) create mode 100644 inc/hgl/type/ActiveIDManager.h diff --git a/inc/hgl/type/ActiveIDManager.h b/inc/hgl/type/ActiveIDManager.h new file mode 100644 index 0000000..a26bf17 --- /dev/null +++ b/inc/hgl/type/ActiveIDManager.h @@ -0,0 +1,163 @@ +#pragma once + +#include +#include +#include + +namespace hgl +{ + /** + * 活跃ID管理器 + */ + class ActiveIDManager + { + SortedSets active_list; ///<活跃ID列表 + Stack 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 &GetActiveArray()const{return active_list;} + const DataArray &GetIdleArray()const{return idle_list;} + + public: + + /** + * 创建若干ID,并置于活跌ID列表中。(注:不从闲置列表中获取) + * @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); + } + + /** + * 创建若干ID,并置于闲置ID列表中。(注:无视闲置列表中已存在的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()0) + { + idle_list.Push(active_list.GetData(),count); + + active_list.Clear(); + } + + return count; + } + };//class ActiveIDManager +}//namespace hgl diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2a22fa1..97fb225 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -144,9 +144,9 @@ SOURCE_GROUP("IO\\MemoryStream" FILES ${IO_MEMORY_STREAM_FILES}) SOURCE_GROUP("IO\\File" FILES ${IO_FILE_FILES}) SOURCE_GROUP("IO\\Jave" FILES ${IO_JAVA_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_ARRAY_FILES} ${IO_MEMORY_STREAM_FILES}