From b872105f75c8b9b685e7ac47a3fca094dd7fa699 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Wed, 28 Nov 2018 20:57:08 +0800 Subject: [PATCH] =?UTF-8?q?List=E5=88=A0=E9=99=A4=E4=B8=8D=E5=90=88?= =?UTF-8?q?=E7=90=86=E8=AE=BE=E8=AE=A1=E7=9A=84[]=E6=93=8D=E4=BD=9C?= =?UTF-8?q?=E7=AC=A6=E9=87=8D=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/hgl/type/List.cpp | 15 ----------- inc/hgl/type/List.h | 1 - inc/hgl/type/Map.cpp | 61 +++++++++++++++++-------------------------- inc/hgl/type/Map.h | 37 ++++++++++++++++++-------- inc/hgl/type/Pool.cpp | 7 ++++- inc/hgl/type/Pool.h | 11 +++++--- 6 files changed, 64 insertions(+), 68 deletions(-) diff --git a/inc/hgl/type/List.cpp b/inc/hgl/type/List.cpp index 0ea5843e..e6fcdfe5 100644 --- a/inc/hgl/type/List.cpp +++ b/inc/hgl/type/List.cpp @@ -51,21 +51,6 @@ namespace hgl return(true); } - template - T &List::operator[](int index)const ///<操作符重载取得指定索引处的数据 - { - if(!items||index<0||index>=count) - { - static T *null_ptr=nullptr; - - LOG_ERROR(OS_TEXT("List<>::operator(index=")+OSString(index)+OS_TEXT(") error,DataCount=")+OSString(count)); - - return(*null_ptr); - } - - return items[index]; - } - template void List::Set(int index,const T &val) { diff --git a/inc/hgl/type/List.h b/inc/hgl/type/List.h index 394e9329..806d4e74 100644 --- a/inc/hgl/type/List.h +++ b/inc/hgl/type/List.h @@ -70,7 +70,6 @@ namespace hgl void Set(int,const T &); ///<设置指定索引处的数据 bool Rand(T &)const; ///<随机取得一个数据 - virtual T &operator[](int n)const; ///<操作符重载取得指定索引处的数据 virtual bool Begin(T &)const; ///<取第一个数据 virtual bool End(T &)const; ///<取最后一个数据 diff --git a/inc/hgl/type/Map.cpp b/inc/hgl/type/Map.cpp index 2effe73c..be99c48b 100644 --- a/inc/hgl/type/Map.cpp +++ b/inc/hgl/type/Map.cpp @@ -208,9 +208,12 @@ namespace hgl { int index=Find(flag); - if(index==-1)return(false); + DataPair *obj=GetObject(data_list,index); - data=data_list[index]->right; + if(!obj) + return(false); + + data=obj->right; return(true); } @@ -299,9 +302,10 @@ namespace hgl { int index=Find(flag); - if(index==-1)return(false); + DataPair *dp=GetObject(data_list,index); - DataPair *dp=data_list[index]; + if(!dp) + return(false); data=dp->right; @@ -319,14 +323,7 @@ namespace hgl template bool _Map::DeleteByKey(const F &flag) { - int index=Find(flag); - - if(index==-1)return(false); - - data_pool.Release(data_list[index]); - data_list.DeleteMove(index); - - return(true); + return DeleteBySerial(Find(flag)); } /** @@ -344,15 +341,8 @@ namespace hgl for(int i=0;i bool _Map::DeleteByValue(const T &data) { - int index=FindByValue(data); - - if(index==-1)return(false); - - data_pool.Release(data_list[index]); - data_list.DeleteMove(index); - - return(true); + return DeleteBySerial(FindByValue(data)); } /** @@ -390,7 +373,7 @@ namespace hgl if(index<0 ||index>=data_list.GetCount())return(false); - data_pool.Release(data_list[index]); + data_pool.Release(GetObject(data_list,index)); data_list.DeleteMove(index); return(true); @@ -427,7 +410,12 @@ namespace hgl int result; if(FindPos(flag,result)) - data_list[result]->right=data; + { + DataPair *dp=GetObject(data_list,result); + + if(dp) + dp->right=data; + } else { DataPair *ds=data_pool.Acquire(); @@ -448,14 +436,13 @@ namespace hgl template bool _Map::Change(const F &flag,const T &data) { - int result=Find(flag); + DataPair *dp=GetObject(data_list,Find(flag)); - if(result==-1) - return(false); + if(!dp) + return(false); - data_list[result]->right=data; - - return(true); + dp->right=data; + return(true); } /** diff --git a/inc/hgl/type/Map.h b/inc/hgl/type/Map.h index 8bb26c84..86217b4d 100644 --- a/inc/hgl/type/Map.h +++ b/inc/hgl/type/Map.h @@ -4,7 +4,6 @@ #include #include #include -#include #include namespace hgl { @@ -93,7 +92,7 @@ namespace hgl return count; } - IDItem *GetItem(int n)const{return data_list[n];} ///<取指定序号的数据 + IDItem *GetItem(int n)const{return GetObject(data_list,n);} ///<取指定序号的数据 bool Get(int,F &,T &)const; ///<取指定序号的数据 bool GetKey(int,F &)const; ///<取指定序号的索引 bool GetValue(int,T &)const; ///<取指定序号的数据 @@ -134,8 +133,16 @@ namespace hgl typedef _Map SuperClass; virtual void DeleteObject(const F &,T *)=0; ///<删除一个数据 - void DeleteObject(DataPair *ds){DeleteObject(ds->left,ds->right);} - void DeleteObject(int index){DeleteObject(this->data_list[index]);} + void DeleteObject(DataPair *ds) + { + if(!ds)return; + DeleteObject(ds->left,ds->right); + } + + void DeleteObject(int index) + { + DeleteObject(GetObject(this->data_list,index)); + } public: @@ -145,7 +152,7 @@ namespace hgl if(SuperClass::GetCount()>0) { //LOG_ERROR(u"这是一个严重的程序设计错误,会产生纯虚函数调用,请在派生类的析构函数中调用Clear函数以清除数据。"); - LOG_ERROR(OS_TEXT("This is a serious design errors, will produce the pure virtual function call, please call in the destructor of the derived class the function to clear the data.")); + //LOG_ERROR(OS_TEXT("This is a serious design errors, will produce the pure virtual function call, please call in the destructor of the derived class the function to clear the data.")); } } @@ -252,7 +259,10 @@ namespace hgl { DeleteObject(index); - this->data_list[index]->second=data; + DataPair *dp=GetObject(this->data_list,index); + + if(dp) + dp->right=data; } else { @@ -274,8 +284,12 @@ namespace hgl { DeleteObject(index); - this->data_list[index]->second=data; + DataPair *dp=GetObject(this->data_list,index); + if(!dp) + return(false); + + dp->right=data; return(true); } else @@ -313,11 +327,12 @@ namespace hgl T *operator[](const F &index)const { - const int pos=this->Find(index); + auto *obj=GetObject(this->data_list,this->Find(index)); - if(pos==-1)return(nullptr); - - return this->data_list[pos]->right; + if(obj) + return obj->right; + else + return nullptr; }; };//class MapObject }//namespace hgl diff --git a/inc/hgl/type/Pool.cpp b/inc/hgl/type/Pool.cpp index 2bec56d1..58bc7bf8 100644 --- a/inc/hgl/type/Pool.cpp +++ b/inc/hgl/type/Pool.cpp @@ -127,8 +127,13 @@ namespace hgl { int n=Active.GetCount(); + T *p=Active.GetData(); + while(n--) - Clear(Active[n]); + { + Clear(*p); + ++p; + } Active.Clear(); } diff --git a/inc/hgl/type/Pool.h b/inc/hgl/type/Pool.h index ef09df44..92146352 100644 --- a/inc/hgl/type/Pool.h +++ b/inc/hgl/type/Pool.h @@ -1,4 +1,4 @@ -#ifndef HGL_POOL_INCLUDE +#ifndef HGL_POOL_INCLUDE #define HGL_POOL_INCLUDE #include @@ -47,8 +47,6 @@ namespace hgl virtual void ClearInactive(); ///<清除所有空闲的 virtual void ClearAll(); ///<清除所有的 - - virtual T operator[](int n){return Active[n];} };//template class Pool template class MTPool:public Pool ///多线程数据池 @@ -188,6 +186,13 @@ namespace hgl using Pool::Pool; virtual ~_ObjectPool(){Pool::ClearAll();} + + virtual bool Release(T *obj) override ///<释放一个数据 + { + if(!obj)return(true); + + return Pool::Release(obj); + } };//template class _ObjectPool template class ObjectPool:public _ObjectPool ///对象池