1.use Free() instead of Clear in DataArray<>,List<>,Stack<>,Queue<>...
2.rewrite Stack<>
This commit is contained in:
parent
dff19d4be9
commit
2a43dc894a
@ -107,10 +107,10 @@ namespace hgl
|
||||
|
||||
virtual ~DataArray()
|
||||
{
|
||||
Clear();
|
||||
Free();
|
||||
}
|
||||
|
||||
void Clear()
|
||||
void Free()
|
||||
{
|
||||
SAFE_FREE(items);
|
||||
|
||||
@ -118,7 +118,7 @@ namespace hgl
|
||||
alloc_count=0;
|
||||
}
|
||||
|
||||
void ClearData()
|
||||
void Clear()
|
||||
{
|
||||
count=0;
|
||||
}
|
||||
@ -594,12 +594,12 @@ namespace hgl
|
||||
*/
|
||||
void WithoutList(DataArray<T> &result_list,const DataArray<T> &without_list)
|
||||
{
|
||||
result_list.ClearData();
|
||||
result_list.Clear();
|
||||
const int count=this->GetCount();
|
||||
|
||||
if(count<=0)return;
|
||||
|
||||
result_list.ClearData();
|
||||
result_list.Clear();
|
||||
result_list.PreAlloc(count);
|
||||
|
||||
int result=0;
|
||||
|
@ -60,7 +60,7 @@ namespace hgl
|
||||
|
||||
void Clear()
|
||||
{
|
||||
stop_list.Clear();
|
||||
stop_list.Free();
|
||||
dirty=true;
|
||||
}
|
||||
|
||||
|
@ -45,7 +45,7 @@ namespace hgl
|
||||
List(const List<T> <){operator=(lt);} ///<本类构造函数
|
||||
List(const std::initializer_list<T> <){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);} ///<增加一批数据
|
||||
|
||||
virtual void Clear(){data_array.Clear();} ///<清除所有数据
|
||||
virtual void ClearData(){data_array.ClearData();} ///<清除所有数据,但不清空缓冲区
|
||||
virtual void Free(){data_array.Free();} ///<清除所有数据
|
||||
virtual void ClearData(){data_array.Clear();} ///<清除所有数据,但不清空缓冲区
|
||||
|
||||
virtual int Find(const T &data)const{return data_array.Find(data);} ///<查找指定数据的索引
|
||||
virtual bool IsExist(const T &flag)const{return Find(flag)!=-1;} ///<确认数据项是否存在
|
||||
|
@ -480,7 +480,7 @@ namespace hgl
|
||||
void _Map<K,V,KVData>::Clear()
|
||||
{
|
||||
data_pool.ClearAll();
|
||||
data_list.Clear();
|
||||
data_list.Free();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -18,7 +18,7 @@ namespace hgl
|
||||
public: //方法
|
||||
|
||||
using List<T *>::List;
|
||||
virtual ~ObjectList(){Clear();}
|
||||
virtual ~ObjectList(){Free();}
|
||||
|
||||
public:
|
||||
|
||||
@ -30,10 +30,10 @@ namespace hgl
|
||||
return List<T *>::Insert(index,obj);
|
||||
}
|
||||
|
||||
virtual void Clear() override ///<清除所有数据
|
||||
virtual void Free() override ///<清除所有数据
|
||||
{
|
||||
ClearData();
|
||||
List<T *>::Clear();
|
||||
List<T *>::Free();
|
||||
}
|
||||
|
||||
virtual void ClearData() override ///<清除所有数据,但不清空缓冲区
|
||||
|
@ -44,7 +44,7 @@ namespace hgl
|
||||
Pool(){alloc_count=0;history_max=0;max_count=0;}
|
||||
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); ///<设定最大数量限制
|
||||
|
||||
|
@ -79,7 +79,7 @@ namespace hgl
|
||||
* 清除队列中的所有数据
|
||||
*/
|
||||
template<typename T>
|
||||
void Queue<T>::Clear()
|
||||
void Queue<T>::Free()
|
||||
{
|
||||
if(max_count==0)
|
||||
if(cur_count)
|
||||
@ -233,7 +233,7 @@ namespace hgl
|
||||
{
|
||||
if(ori.cur_count==0)return;
|
||||
|
||||
Clear();
|
||||
Free();
|
||||
|
||||
max_count=ori.cur_count;
|
||||
cur_count=ori.cur_count;
|
||||
@ -259,7 +259,7 @@ namespace hgl
|
||||
while(n--)
|
||||
delete Queue<T *>::items[n];
|
||||
|
||||
Queue<T *>::Clear();
|
||||
Queue<T *>::Free();
|
||||
}
|
||||
}
|
||||
#endif//HGL_QUEUE_CPP
|
||||
|
@ -47,7 +47,7 @@ namespace hgl
|
||||
bool IsExist (const T &data)const{return this->Find(data)!=-1;} ///<确认是否有这个数据
|
||||
bool Delete (int); ///<删除队列中指定编号的数据
|
||||
|
||||
void Clear (); ///<清除所有数据
|
||||
void Free (); ///<清除所有数据
|
||||
void ClearData (); ///<清除所有数据,但不释放内存
|
||||
|
||||
bool GetItem (int n,T &ti) ///<取得指定项数据
|
||||
|
@ -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
|
@ -1,7 +1,6 @@
|
||||
#ifndef HGL_STACK_INCLUDE
|
||||
#define HGL_STACK_INCLUDE
|
||||
#pragma once
|
||||
|
||||
#include<hgl/type/DataType.h>
|
||||
#include<hgl/type/DataArray.h>
|
||||
namespace hgl
|
||||
{
|
||||
/**
|
||||
@ -13,47 +12,123 @@ namespace hgl
|
||||
{
|
||||
protected:
|
||||
|
||||
int max_count;
|
||||
int alloc_count;
|
||||
int cur_count;
|
||||
T *items;
|
||||
DataArray<T> data_array;
|
||||
|
||||
public: //属性
|
||||
|
||||
int GetCount () const{return cur_count;} ///<取得堆栈中数据的个数
|
||||
bool SetCount (int c); ///<直接设置堆栈中数据的个数
|
||||
const int GetAllocCount ()const{return data_array.GetAllocCount();} ///<取得已分配容量
|
||||
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;} ///<取得堆栈中的最大数据个数
|
||||
void SetMaxCount (int); ///<设置堆栈中的最大数据个数
|
||||
const bool IsEmpty ()const{return data_array.IsEmpty();} ///<确认列表是否为空
|
||||
|
||||
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: //方法
|
||||
|
||||
Stack(int=0);
|
||||
virtual ~Stack();
|
||||
Stack()=default;
|
||||
virtual ~Stack()=default;
|
||||
|
||||
bool Peek(T &); ///<尝试访问一个数据
|
||||
virtual bool Pop(T &); ///<弹出一个数据
|
||||
bool Push(T &); ///<压入一个数据
|
||||
bool Push(T *,int); ///<压入多个数据
|
||||
virtual bool Push(T *data,int count) ///<压入多个数据
|
||||
{
|
||||
if(!data||count<=0)return(false);
|
||||
|
||||
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 StackObject:public Stack<T *> ///堆栈对象
|
||||
template<typename T> class ObjectStack:public Stack<T *> ///堆栈对象
|
||||
{
|
||||
protected:
|
||||
|
||||
virtual void DeleteObject(T *obj){if(obj)delete obj;}
|
||||
|
||||
public:
|
||||
|
||||
using Stack<T *>::Stack;
|
||||
virtual ~StackObject(){Clear();};
|
||||
virtual ~ObjectStack() override {Free();}
|
||||
|
||||
void Clear();
|
||||
};//template<typename T> class StackObject
|
||||
virtual bool Push(T *obj)
|
||||
{
|
||||
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
|
||||
#include<hgl/type/Stack.cpp>
|
||||
#endif//HGL_STACK_INCLUDE
|
||||
|
@ -138,7 +138,7 @@ namespace hgl
|
||||
/**
|
||||
* 清除所有字符串
|
||||
*/
|
||||
void Clear(){Items.Clear();} ///<删除列表中的所有字符串
|
||||
void Clear(){Items.ClearData();} ///<删除列表中的所有字符串
|
||||
|
||||
/**
|
||||
* 删除指定的字符串
|
||||
|
@ -96,7 +96,7 @@ namespace hgl
|
||||
++log;
|
||||
}
|
||||
|
||||
log_list->Clear();
|
||||
log_list->Free();
|
||||
}
|
||||
}//namespace logger
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user