List删除不合理设计的[]操作符重载

This commit is contained in:
hyzboy 2018-11-28 20:57:08 +08:00
parent 5c3caf4111
commit b872105f75
6 changed files with 64 additions and 68 deletions

View File

@ -51,21 +51,6 @@ namespace hgl
return(true);
}
template<typename T>
T &List<T>::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<typename T>
void List<T>::Set(int index,const T &val)
{

View File

@ -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; ///<取最后一个数据

View File

@ -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<typename F,typename T,typename DataPair>
bool _Map<F,T,DataPair>::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<count;i++)
{
int index=Find(*fp);
if(index!=-1)
{
data_pool.Release(data_list[index]);
data_list.DeleteMove(index);
++total;
}
if(DeleteBySerial(Find(*fp)))
++total;
++fp;
}
@ -369,14 +359,7 @@ namespace hgl
template<typename F,typename T,typename DataPair>
bool _Map<F,T,DataPair>::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<typename F,typename T,typename DataPair>
bool _Map<F,T,DataPair>::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);
}
/**

View File

@ -4,7 +4,6 @@
#include<hgl/type/List.h>
#include<hgl/type/Pair.h>
#include<hgl/type/Pool.h>
#include<hgl/LogInfo.h>
#include<hgl/thread/RWLock.h>
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<F,T *,DataPair> 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 <Clear> 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 <Clear> 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

View File

@ -127,8 +127,13 @@ namespace hgl
{
int n=Active.GetCount();
T *p=Active.GetData();
while(n--)
Clear(Active[n]);
{
Clear(*p);
++p;
}
Active.Clear();
}

View File

@ -1,4 +1,4 @@
#ifndef HGL_POOL_INCLUDE
#ifndef HGL_POOL_INCLUDE
#define HGL_POOL_INCLUDE
#include<hgl/type/List.h>
@ -47,8 +47,6 @@ namespace hgl
virtual void ClearInactive(); ///<清除所有空闲的
virtual void ClearAll(); ///<清除所有的
virtual T operator[](int n){return Active[n];}
};//template<typename T> class Pool
template<typename T> class MTPool:public Pool<T> ///多线程数据池
@ -188,6 +186,13 @@ namespace hgl
using Pool<T *>::Pool;
virtual ~_ObjectPool(){Pool<T *>::ClearAll();}
virtual bool Release(T *obj) override ///<释放一个数据
{
if(!obj)return(true);
return Pool<T *>::Release(obj);
}
};//template<typename T> class _ObjectPool
template<typename T> class ObjectPool:public _ObjectPool<T> ///对象池