1.use Free() instead of Clear in DataArray<>,List<>,Stack<>,Queue<>...

2.rewrite Stack<>
This commit is contained in:
HuYingzhuo(hugo/hyzboy) 2023-08-01 15:38:24 +08:00
parent dff19d4be9
commit 2a43dc894a
12 changed files with 122 additions and 308 deletions

View File

@ -107,10 +107,10 @@ namespace hgl
virtual ~DataArray() virtual ~DataArray()
{ {
Clear(); Free();
} }
void Clear() void Free()
{ {
SAFE_FREE(items); SAFE_FREE(items);
@ -118,7 +118,7 @@ namespace hgl
alloc_count=0; alloc_count=0;
} }
void ClearData() void Clear()
{ {
count=0; count=0;
} }
@ -594,12 +594,12 @@ namespace hgl
*/ */
void WithoutList(DataArray<T> &result_list,const DataArray<T> &without_list) void WithoutList(DataArray<T> &result_list,const DataArray<T> &without_list)
{ {
result_list.ClearData(); result_list.Clear();
const int count=this->GetCount(); const int count=this->GetCount();
if(count<=0)return; if(count<=0)return;
result_list.ClearData(); result_list.Clear();
result_list.PreAlloc(count); result_list.PreAlloc(count);
int result=0; int result=0;

View File

@ -60,7 +60,7 @@ namespace hgl
void Clear() void Clear()
{ {
stop_list.Clear(); stop_list.Free();
dirty=true; dirty=true;
} }

View File

@ -45,7 +45,7 @@ namespace hgl
List(const List<T> &lt){operator=(lt);} ///<本类构造函数 List(const List<T> &lt){operator=(lt);} ///<本类构造函数
List(const std::initializer_list<T> &lt){operator=(lt);} List(const std::initializer_list<T> &lt){operator=(lt);}
virtual ~List(){Clear();} ///<本类析构函数 virtual ~List(){Free();} ///<本类析构函数
/** /**
* *
@ -115,8 +115,8 @@ namespace hgl
int Add(const List<T> &l){return Add(l.items,l.count);} ///<增加一批数据 int Add(const List<T> &l){return Add(l.items,l.count);} ///<增加一批数据
virtual void Clear(){data_array.Clear();} ///<清除所有数据 virtual void Free(){data_array.Free();} ///<清除所有数据
virtual void ClearData(){data_array.ClearData();} ///<清除所有数据,但不清空缓冲区 virtual void ClearData(){data_array.Clear();} ///<清除所有数据,但不清空缓冲区
virtual int Find(const T &data)const{return data_array.Find(data);} ///<查找指定数据的索引 virtual int Find(const T &data)const{return data_array.Find(data);} ///<查找指定数据的索引
virtual bool IsExist(const T &flag)const{return Find(flag)!=-1;} ///<确认数据项是否存在 virtual bool IsExist(const T &flag)const{return Find(flag)!=-1;} ///<确认数据项是否存在

View File

@ -480,7 +480,7 @@ namespace hgl
void _Map<K,V,KVData>::Clear() void _Map<K,V,KVData>::Clear()
{ {
data_pool.ClearAll(); data_pool.ClearAll();
data_list.Clear(); data_list.Free();
} }
/** /**

View File

@ -18,7 +18,7 @@ namespace hgl
public: //方法 public: //方法
using List<T *>::List; using List<T *>::List;
virtual ~ObjectList(){Clear();} virtual ~ObjectList(){Free();}
public: public:
@ -30,10 +30,10 @@ namespace hgl
return List<T *>::Insert(index,obj); return List<T *>::Insert(index,obj);
} }
virtual void Clear() override ///<清除所有数据 virtual void Free() override ///<清除所有数据
{ {
ClearData(); ClearData();
List<T *>::Clear(); List<T *>::Free();
} }
virtual void ClearData() override ///<清除所有数据,但不清空缓冲区 virtual void ClearData() override ///<清除所有数据,但不清空缓冲区

View File

@ -44,7 +44,7 @@ namespace hgl
Pool(){alloc_count=0;history_max=0;max_count=0;} Pool(){alloc_count=0;history_max=0;max_count=0;}
virtual ~Pool()=default; virtual ~Pool()=default;
virtual void PreAlloc(int,bool set_to_max=false); ///<预分配空间 virtual void PreAlloc(int,bool set_to_max=false); ///<预分配空间
virtual int SetMaxCount(int); ///<设定最大数量限制 virtual int SetMaxCount(int); ///<设定最大数量限制

View File

@ -79,7 +79,7 @@ namespace hgl
* *
*/ */
template<typename T> template<typename T>
void Queue<T>::Clear() void Queue<T>::Free()
{ {
if(max_count==0) if(max_count==0)
if(cur_count) if(cur_count)
@ -233,7 +233,7 @@ namespace hgl
{ {
if(ori.cur_count==0)return; if(ori.cur_count==0)return;
Clear(); Free();
max_count=ori.cur_count; max_count=ori.cur_count;
cur_count=ori.cur_count; cur_count=ori.cur_count;
@ -259,7 +259,7 @@ namespace hgl
while(n--) while(n--)
delete Queue<T *>::items[n]; delete Queue<T *>::items[n];
Queue<T *>::Clear(); Queue<T *>::Free();
} }
} }
#endif//HGL_QUEUE_CPP #endif//HGL_QUEUE_CPP

View File

@ -47,7 +47,7 @@ namespace hgl
bool IsExist (const T &data)const{return this->Find(data)!=-1;} ///<确认是否有这个数据 bool IsExist (const T &data)const{return this->Find(data)!=-1;} ///<确认是否有这个数据
bool Delete (int); ///<删除队列中指定编号的数据 bool Delete (int); ///<删除队列中指定编号的数据
void Clear (); ///<清除所有数据 void Free (); ///<清除所有数据
void ClearData (); ///<清除所有数据,但不释放内存 void ClearData (); ///<清除所有数据,但不释放内存
bool GetItem (int n,T &ti) ///<取得指定项数据 bool GetItem (int n,T &ti) ///<取得指定项数据

View File

@ -1,261 +0,0 @@
#ifndef HGL_STACK_CPP
#define HGL_STACK_CPP
#include<hgl/log/LogInfo.h>
namespace hgl
{
/**
*
* @param m m的值不为0使
*/
template<typename T>
Stack<T>::Stack(int m)
{
cur_count=0;
if(m)
{
max_count=m;
items=hgl_align_malloc<T>(max_count);
}
else
max_count=0;
alloc_count=max_count;
}
template<typename T>
Stack<T>::~Stack()
{
if(cur_count||max_count)hgl_free(items);
}
/**
*
*/
template<typename T>
void Stack<T>::SetMaxCount(int m)
{
if(m<=0)return;
if(alloc_count==0)
{
alloc_count=power_to_2(m);
items=hgl_align_malloc<T>(alloc_count);
}
else
if(alloc_count<m)
{
alloc_count=power_to_2(m);
items=(T *)hgl_align_realloc<T>(items,alloc_count);
}
max_count=m;
if(cur_count>=max_count)cur_count=max_count-1;
}
template<typename T>
bool Stack<T>::SetCount(int c)
{
if(c<0)return(false);
if(c>max_count)
return(false);
cur_count=c;
return(true);
}
/**
*
*/
template<typename T>
void Stack<T>::Clear()
{
if(max_count==0)
if(cur_count)
{
hgl_free(items);
alloc_count=0;
}
cur_count=0;
}
template<typename T>
bool Stack<T>::GetItem(int n,T &data)
{
if(n<0||n>=cur_count)
{
LOG_ERROR(OS_TEXT("从堆栈中按索引<") + OSString(n) + OS_TEXT(">取数据,超出正常范围<")+OSString(cur_count) + OS_TEXT(">"));
return(false);
}
data=items[n];
return(true);
}
/**
* 访
* @param t
* @return
*/
template<typename T>
bool Stack<T>::Peek(T &t)
{
if(cur_count)
{
memcpy(&t,items+(cur_count-1),sizeof(T));
// t=items[cur_count-1];
return(true);
}
else
return(false);
}
/**
*
* @param t
* @return
*/
template<typename T>
bool Stack<T>::Pop(T &t)
{
if(cur_count)
{
// t=items[--cur_count];
cur_count--;
memcpy(&t,items+cur_count,sizeof(T));
if(max_count==0)
{
if(cur_count==0)
{
hgl_free(items);
alloc_count=0;
}
//else
//items=(T *)hgl_realloc(items,cur_count*sizeof(T));
}
return(true);
}
else
return(false);
}
/**
*
* @param data
* @return true
* @return false
*/
template<typename T>
bool Stack<T>::Push(T &data)
{
if(max_count)
{
if(cur_count>=max_count)return(false);
}
else
{
if(cur_count)
{
if(cur_count+1>alloc_count)
{
alloc_count=power_to_2(cur_count+1);
items=(T *)hgl_align_realloc<T>(items,alloc_count);
}
}
else
{
items=hgl_align_malloc<T>(1);
alloc_count=1;
}
}
// items[cur_count++]=data;
memcpy(items+cur_count,&data,sizeof(T));
cur_count++;
return(true);
}
/**
*
* @param data
* @param data_count
* @return true
* @return false
*/
template<typename T>
bool Stack<T>::Push(T *data,int data_count)
{
if(max_count)
{
if(cur_count>=max_count)return(false);
}
else
{
if(cur_count)
{
if(cur_count+data_count>alloc_count)
{
alloc_count=power_to_2(cur_count+data_count);
items=(T *)hgl_align_realloc<T>(items,alloc_count);
}
}
else
{
items=hgl_align_malloc<T>(data_count);
alloc_count=data_count;
}
}
memcpy(items+cur_count,data,data_count*sizeof(T));
cur_count+=data_count;
return(true);
}
template<typename T>
void Stack<T>::operator =(const Stack<T> &ori)
{
if(ori.cur_count==0)return;
Clear();
max_count=ori.cur_count;
cur_count=ori.cur_count;
if(max_count==0)
alloc_count=cur_count;
else
alloc_count=max_count;
items=hgl_align_malloc<T>(alloc_count);
memcpy(items,ori.items,alloc_count*sizeof(T));
}
}//namespace hgl
namespace hgl
{
template<typename T>
void StackObject<T>::Clear()
{
for(int i=0;i<this->cur_count;i++)
delete this->items[i];
Stack<T *>::Clear();
}
}//namespace hgl
#endif//HGL_STACK_CPP

View File

@ -1,7 +1,6 @@
#ifndef HGL_STACK_INCLUDE #pragma once
#define HGL_STACK_INCLUDE
#include<hgl/type/DataType.h> #include<hgl/type/DataArray.h>
namespace hgl namespace hgl
{ {
/** /**
@ -13,47 +12,123 @@ namespace hgl
{ {
protected: protected:
int max_count; DataArray<T> data_array;
int alloc_count;
int cur_count;
T *items;
public: //属性 public: //属性
int GetCount () const{return cur_count;} ///<取得堆栈中数据的个数 const int GetAllocCount ()const{return data_array.GetAllocCount();} ///<取得已分配容量
bool SetCount (int c); ///<直接设置堆栈中数据的个数 const int GetCount ()const{return data_array.GetCount();} ///<取得列表内数据数量
virtual bool SetCount (int count){return data_array.SetCount(count);} ///<设置列表内数据数量
virtual bool PreAlloc (int count){return data_array.Alloc(count);} ///<预分配指定数量的数据空间
int GetMaxCount () const{return max_count;} ///<取得堆栈中的最大数据个数 const bool IsEmpty ()const{return data_array.IsEmpty();} ///<确认列表是否为空
void SetMaxCount (int); ///<设置堆栈中的最大数据个数
T * GetData () {return items;} ///<取得原始数据 T * GetData ()const{return data_array.GetData();} ///<提供原始数据项
int GetBytes ()const{return data_array.GetBytes();} ///<取得原始数据总字节数
T * begin ()const{return data_array.begin();}
T * end ()const{return data_array.end();}
public:
DataArray<T> & GetArray() {return data_array;}
const DataArray<T> &GetArray()const {return data_array;}
operator DataArray<T> & () {return data_array;}
operator const DataArray<T> & ()const {return data_array;}
public: //方法 public: //方法
Stack(int=0); Stack()=default;
virtual ~Stack(); virtual ~Stack()=default;
bool Peek(T &); ///<尝试访问一个数据 virtual bool Push(T *data,int count) ///<压入多个数据
virtual bool Pop(T &); ///<弹出一个数据 {
bool Push(T &); ///<压入一个数据 if(!data||count<=0)return(false);
bool Push(T *,int); ///<压入多个数据
virtual void Clear(); ///<清除所有数据 int offset=data_array.GetCount();
bool GetItem(int,T &); data_array.AddCount(count);
virtual void operator =(const Stack<T> &); data_array.WriteAt(data,offset,count);
return(true);
}
virtual bool Push(T &data){return Push(&data,1)==1;} ///<压入一个数据
virtual bool Peek(T &data) ///<尝试访问一个数据
{
if(data_array.GetCount()<=0)return(false);
return data_array.ReadAt(data,data_array.GetCount()-1);
}
virtual bool Pop(T &data) ///<弹出一个数据
{
if(data_array.GetCount()<=0)return(false);
if(!data_array.ReadAt(data,data_array.GetCount()-1))
return(false);
data_array.AddCount(-1);
return(true);
}
virtual void Clear(){data_array.Clear();} ///<清除所有数据
virtual void Free(){data_array.Free();} ///<清除所有数据并释放内存
virtual void operator =(const DataArray<T> &da) ///<复制一个堆栈
{
data_array.SetCount(da.GetCount());
data_array.WriteAt(da.GetData(),0,da.GetCount());
}
virtual void operator =(const Stack<T> &s){this->operator=(s.data_array);}
};//template<typename T> class Stack };//template<typename T> class Stack
template<typename T> class StackObject:public Stack<T *> ///堆栈对象 template<typename T> class ObjectStack:public Stack<T *> ///堆栈对象
{ {
protected:
virtual void DeleteObject(T *obj){if(obj)delete obj;}
public: public:
using Stack<T *>::Stack; using Stack<T *>::Stack;
virtual ~StackObject(){Clear();}; virtual ~ObjectStack() override {Free();}
void Clear(); virtual bool Push(T *obj)
};//template<typename T> class StackObject {
if(!obj)return(false);
return Stack<T *>::Push(obj);
}
virtual T *Pop()
{
T *obj;
if(!Stack<T *>::Pop(obj))
return(nullptr);
return obj;
}
void Clear()
{
for(T *obj:data_array)
DeleteObject(obj);
data_array.Clear();
}
void Free()
{
ObjectStack<T>::Clear();
data_array.Free();
}
};//template<typename T> class ObjectStack
}//namespace hgl }//namespace hgl
#include<hgl/type/Stack.cpp>
#endif//HGL_STACK_INCLUDE

View File

@ -138,7 +138,7 @@ namespace hgl
/** /**
* *
*/ */
void Clear(){Items.Clear();} ///<删除列表中的所有字符串 void Clear(){Items.ClearData();} ///<删除列表中的所有字符串
/** /**
* *

View File

@ -96,7 +96,7 @@ namespace hgl
++log; ++log;
} }
log_list->Clear(); log_list->Free();
} }
}//namespace logger }//namespace logger