removed List<>::Rand,clean codes of List<> and then all realloc in PreAlloc,delete always include delete_count;

This commit is contained in:
HuYingzhuo(hugo/hyzboy) 2023-07-20 14:28:32 +08:00
parent 2d61ee5a50
commit 09eec7e7df
2 changed files with 108 additions and 167 deletions

View File

@ -19,7 +19,7 @@ namespace hgl
if(!items||index<0||index>=count) if(!items||index<0||index>=count)
return(false); return(false);
memcpy(&ti,items+index,sizeof(T)); hgl_cpy<T>(ti,items[index]);
return(true); return(true);
} }
@ -29,7 +29,7 @@ namespace hgl
if(!items||count<=0) if(!items||count<=0)
return(false); return(false);
memcpy(&ti,items,sizeof(T)); hgl_cpy<T>(ti,*items);
return(true); return(true);
} }
@ -39,37 +39,19 @@ namespace hgl
if(!items||count<=0) if(!items||count<=0)
return(false); return(false);
memcpy(&ti,items+count-1,sizeof(T)); hgl_cpy<T>(ti,items[count-1]);
return(true); return(true);
} }
template<typename T> template<typename T>
bool List<T>::Rand(T &ti)const bool List<T>::Set(int index,const T &val)
{ {
if(!items||count<=0) if(!items||index<0||index>=count)
return(false); return(false);
#ifdef _WIN32
memcpy(&ti,items+(rand()%count),sizeof(T));
#else
memcpy(&ti,items+(lrand48()%count),sizeof(T));
#endif
return(true);
}
template<typename T> hgl_cpy<T>(items[index],val);//items[index]=val;
void List<T>::Set(int index,const T &val)
{ return(true);
#ifdef _DEBUG
if(!items||index<0||index>=count)
{
LOG_ERROR(OS_TEXT("List<>::Set(index=")+OSString(index)+OS_TEXT(",T &) error,DataCount=")+OSString(count));
}
else
memcpy(items+index,&val,sizeof(T));//items[index]=val;
#else
if(index>=0&&index<count)
memcpy(items+index,&val,sizeof(T));//items[index]=val;
#endif//_DEBUG
} }
/** /**
@ -79,20 +61,8 @@ namespace hgl
template<typename T> template<typename T>
T *List<T>::Add() T *List<T>::Add()
{ {
if(!items) if(!PreAlloc(count+1))
{ return(nullptr);
count=1;
alloc_count=1;
items=hgl_align_malloc<T>(1);
return items;
}
else if(count+1>alloc_count)
{
alloc_count=power_to_2(count+1);
items=(T *)hgl_align_realloc<T>(items,alloc_count);
}
++count; ++count;
return(items+(count-1)); return(items+(count-1));
@ -106,20 +76,10 @@ namespace hgl
template<typename T> template<typename T>
int List<T>::Add(const T &data) int List<T>::Add(const T &data)
{ {
if(!items) if(!PreAlloc(count+1))
{ return(-1);
count=0;
alloc_count=1;
items=hgl_align_malloc<T>(1);
}
else if(count+1>alloc_count)
{
alloc_count=power_to_2(count+1);
items=(T *)hgl_align_realloc<T>(items,alloc_count); hgl_cpy<T>(items[count],data);//items[count]=data;
}
memcpy(items+count,&data,sizeof(T));//items[count]=data;
return(count++); return(count++);
} }
@ -135,25 +95,15 @@ namespace hgl
{ {
if(n<=0)return(-1); if(n<=0)return(-1);
if(!items) if(!PreAlloc(count+n))
{ return(-2);
count=0;
alloc_count=power_to_2(n);
items=hgl_align_malloc<T>(alloc_count);
}
else if(count+n>alloc_count)
{
alloc_count=power_to_2(count+n);
items=(T *)hgl_align_realloc<T>(items,alloc_count);
}
T *p=items; T *p=items;
int result=count; int result=count;
for(int i=0;i<n;i++) for(int i=0;i<n;i++)
{ {
memcpy(p,&data,sizeof(T));//items[count]=data; hgl_cpy<T>(*p,data);//items[count]=data;
++p; ++p;
} }
@ -170,21 +120,13 @@ namespace hgl
template<typename T> template<typename T>
int List<T>::Add(const T *data,int n) int List<T>::Add(const T *data,int n)
{ {
if(!items) if(!data||n<=0)
{ return(-1);
count=0;
alloc_count=power_to_2(n);
items=hgl_align_malloc<T>(alloc_count); if(!PreAlloc(count+n))
} return(-1);
else if(count+n>alloc_count)
{
alloc_count=power_to_2(count+n);
items=(T *)hgl_align_realloc<T>(items,alloc_count); hgl_cpy<T>(items+count,data,n);
}
memcpy(items+count,data,n*sizeof(T));
int r=count; int r=count;
@ -253,73 +195,84 @@ namespace hgl
} }
/** /**
* , * ()
* @param index * @param start
* @return * @return
*/ */
template<typename T> template<typename T>
bool List<T>::Delete(int index) bool List<T>::DeleteMove(int start,int delete_count)
{
if(count>0&&index>=0&&index<count)
{
--count;
if(index<count)
memcpy(items+index,items+count,sizeof(T)); //将最后一个数据移到当前位置
return(true);
}
else
return(false);
}
/**
*
* @param index
* @return
*/
template<typename T>
bool List<T>::DeleteMove(int index)
{
if(count>0&&index>=0&&index<count)
{
--count;
if(index<count)
memmove(items+index,items+index+1,(count-index)*sizeof(T));
return(true);
}
else
return(false);
}
/**
*
* @param start
* @param number
* @return
*/
template<typename T>
bool List<T>::Delete(int start,int number)
{ {
if(start>=count)return(false); if(start>=count)return(false);
if(start<0) if(start<0)
{ {
number+=start; delete_count+=start;
start=0; start=0;
} }
if(start+number>count) if(start+delete_count>count)
number=count-start; delete_count=count-start;
if(number<=0)return(false); if(delete_count<=0)return(false);
count-=number; const int end_count=count-(start+delete_count);
if(start<count) if(end_count>0)
memmove(items+start,items+start+number,(count-start)*sizeof(T)); hgl_cpy<T>(items+start,items+start+delete_count,end_count);
count-=delete_count;
return(true);
}
/**
* ()
* @param start
* @param delete_count
* @return
*/
template<typename T>
bool List<T>::Delete(int start,int delete_count)
{
if(start>=count)return(false);
if(start<0)
{
delete_count+=start;
start=0;
}
if(start+delete_count>count)
delete_count=count-start;
if(delete_count<=0)return(false);
const int64 end_count=count-(start+delete_count); //删除的数据段后面的数据个数
if(end_count>0)
{
if(end_count<=delete_count) //后面的数据个数比删除的数据个数少,直接整体移动
{
//[------------][***********]
// ^ v
// ^ v
// +<<<<<<<<<<<<<+
hgl_cpy<T>(items+start,items+start+delete_count,end_count);
}
else //后面的数据个数比删除的数据个数多,那就只移动等长的最后一段数据
{
//[---][**********][***]
// ^ v
// ^ v
// +<<<<<<<<<<<<<<<<+
hgl_cpy<T>(items+start,items+(count-delete_count),delete_count);
}
count-=delete_count;
}
//else{后面都没数据了,那就啥都不用干了}
return(true); return(true);
} }
@ -371,16 +324,14 @@ namespace hgl
void List<T>::Exchange(int a,int b) void List<T>::Exchange(int a,int b)
{ {
//T t; //T t;
char t[sizeof(T)]; //char t[sizeof(T)];
// t=items[a]; // t=items[a];
// items[a]=items[b]; // items[a]=items[b];
// items[b]=t; // items[b]=t;
memcpy(&t,items+a,sizeof(T)); hgl_swap(items[a],items[b]);
memcpy(items+a,items+b,sizeof(T));
memcpy(items+b,&t,sizeof(T));
} }
/** /**
@ -389,33 +340,25 @@ namespace hgl
* @param data * @param data
*/ */
template<typename T> template<typename T>
void List<T>::Insert(int index,const T &data) bool List<T>::Insert(int index,const T &data)
{ {
if(index<0)index=0; if(index<0)index=0;
if(index<count) if(index<count)
{ {
if(!items) if(!PreAlloc(count+1))
{ return(false);
alloc_count=1;
items=hgl_align_malloc<T>(alloc_count); hgl_move<T>(items+index+1,items+index,count-index);
}
else if(count+1>alloc_count)
{
alloc_count=power_to_2(count+1);
items=(T *)hgl_align_realloc<T>(items,alloc_count); hgl_cpy<T>(items[index],data);//items[index]=data;
}
memmove(items+index+1,items+index,(count-index)*sizeof(T));
memcpy(items+index,&data,sizeof(T));//items[index]=data;
++count; ++count;
} }
else else
Add(data); Add(data);
return(true);
} }
/** /**
@ -434,16 +377,16 @@ namespace hgl
//T t; //T t;
char t[sizeof(T)]; char t[sizeof(T)];
memcpy(&t,items+index,sizeof(T));//t=items[index]; hgl_cpy<T>(*(T *)&t,items[index]);//t=items[index];
if(index<newindex)memmove(items+index ,items+index+1 ,(newindex-index)*sizeof(T)); if(index<newindex)hgl_move<T>(items+index ,items+index+1 ,newindex-index);
else memmove(items+newindex+1 ,items+newindex ,(index-newindex)*sizeof(T)); else hgl_move<T>(items+newindex+1 ,items+newindex ,index-newindex);
memcpy(items+newindex,&t,sizeof(T));//items[newindex]=t; hgl_cpy<T>(items[newindex],*(T *)&t);//items[newindex]=t;
} }
template<typename T> template<typename T>
bool List<T>::PreMalloc(int new_count) bool List<T>::PreAlloc(int new_count)
{ {
if(alloc_count>=new_count)return(true); if(alloc_count>=new_count)return(true);
@ -457,7 +400,7 @@ namespace hgl
} }
else else
{ {
T *new_items=(T *)hgl_align_realloc<T>(items,alloc_count); T *new_items=hgl_align_realloc<T>(items,alloc_count);
if(!new_items)return(false); if(!new_items)return(false);
@ -477,7 +420,7 @@ namespace hgl
return(true); return(true);
} }
if(!PreMalloc(new_count)) if(!PreAlloc(new_count))
return(false); return(false);
count=new_count; count=new_count;

View File

@ -23,7 +23,7 @@ namespace hgl
int GetAllocCount ()const{return alloc_count;} ///<取得已分配容量 int GetAllocCount ()const{return alloc_count;} ///<取得已分配容量
int GetCount ()const{return count;} ///<取得列表内数据数量 int GetCount ()const{return count;} ///<取得列表内数据数量
virtual bool SetCount (int); ///<设置列表内数据数量 virtual bool SetCount (int); ///<设置列表内数据数量
virtual bool PreMalloc (int); ///<预分配指定数量的数据空间 virtual bool PreAlloc (int); ///<预分配指定数量的数据空间
T * GetData ()const{return items;} ///<提供原始数据项 T * GetData ()const{return items;} ///<提供原始数据项
int GetBytes ()const{return count*sizeof(T);} ///<取得原始数据总字节数 int GetBytes ()const{return count*sizeof(T);} ///<取得原始数据总字节数
@ -51,13 +51,12 @@ namespace hgl
virtual void ClearData(); ///<清除所有数据,但不清空缓冲区 virtual void ClearData(); ///<清除所有数据,但不清空缓冲区
virtual int Find(const T &)const; ///<查找指定数据的索引 virtual int Find(const T &)const; ///<查找指定数据的索引
virtual bool IsExist(const T &flag)const{return Find(flag)!=-1;} ///<确认数据项是否存在 virtual bool IsExist(const T &flag)const{return Find(flag)!=-1;} ///<确认数据项是否存在
virtual bool Delete(int); ///<删除指定索引的数据 virtual bool Delete(int,int=1); ///<删除指定索引的数据
virtual bool Delete(int,int); ///<删除指定索引的数据 virtual bool DeleteMove(int,int=1); ///<删除指定索引的数据,将后面紧邻的数据前移
virtual bool DeleteMove(int); ///<删除指定索引的数据,将后面紧邻的数据前移
virtual bool DeleteByValue(const T &); ///<删除一个指定数据 virtual bool DeleteByValue(const T &); ///<删除一个指定数据
virtual void DeleteByValue(const T *,int); ///<删除一批指定的数据 virtual void DeleteByValue(const T *,int); ///<删除一批指定的数据
virtual void Exchange(int,int); ///<根据索引交换两个数据 virtual void Exchange(int,int); ///<根据索引交换两个数据
virtual void Insert(int,const T &); ///<在指定索引处插入一个数据 virtual bool Insert(int,const T &); ///<在指定索引处插入一个数据
virtual void Move(int,int); ///<移动一个数据到移指索引处 virtual void Move(int,int); ///<移动一个数据到移指索引处
void DeleteClear(); ///<清除所有数据并全部调用delete void DeleteClear(); ///<清除所有数据并全部调用delete
@ -80,8 +79,7 @@ namespace hgl
} }
bool Get(int,T &)const; ///<取得指定索引处的数据 bool Get(int,T &)const; ///<取得指定索引处的数据
void Set(int,const T &); ///<设置指定索引处的数据 bool Set(int,const T &); ///<设置指定索引处的数据
bool Rand(T &)const; ///<随机取得一个数据
virtual bool First(T &)const; ///<取第一个数据 virtual bool First(T &)const; ///<取第一个数据
virtual bool Last(T &)const; ///<取最后一个数据 virtual bool Last(T &)const; ///<取最后一个数据
@ -107,7 +105,7 @@ namespace hgl
if(count<=0)return; if(count<=0)return;
without_list.PreMalloc(count); without_list.PreAlloc(count);
const T *sp=this->GetData(); const T *sp=this->GetData();
@ -161,7 +159,7 @@ namespace hgl
// virtual T * Append(); ///<追加一个数据 // virtual T * Append(); ///<追加一个数据
// virtual T * Insert(int); ///<在指定索引处创建一个数据 // virtual T * Insert(int); ///<在指定索引处创建一个数据
void Insert(int,const ItemPointer &); ///<在指定索引处插入一个数据 bool Insert(int,const ItemPointer &); ///<在指定索引处插入一个数据
virtual void Clear(); ///<清除所有数据 virtual void Clear(); ///<清除所有数据
virtual void ClearData(); ///<清除所有数据,但不清空缓冲区 virtual void ClearData(); ///<清除所有数据,但不清空缓冲区