From 2a43dc894a5f9a4cec13b20f46ba77acaccc3f94 Mon Sep 17 00:00:00 2001 From: "HuYingzhuo(hugo/hyzboy)" Date: Tue, 1 Aug 2023 15:38:24 +0800 Subject: [PATCH] 1.use Free() instead of Clear in DataArray<>,List<>,Stack<>,Queue<>... 2.rewrite Stack<> --- inc/hgl/type/DataArray.h | 10 +- inc/hgl/type/Gradient.h | 2 +- inc/hgl/type/List.h | 6 +- inc/hgl/type/Map.cpp | 2 +- inc/hgl/type/ObjectList.h | 6 +- inc/hgl/type/Pool.h | 2 +- inc/hgl/type/Queue.cpp | 6 +- inc/hgl/type/Queue.h | 2 +- inc/hgl/type/Stack.cpp | 261 -------------------------------------- inc/hgl/type/Stack.h | 129 +++++++++++++++---- inc/hgl/type/StringList.h | 2 +- src/Log/Loginfo.cpp | 2 +- 12 files changed, 122 insertions(+), 308 deletions(-) delete mode 100644 inc/hgl/type/Stack.cpp diff --git a/inc/hgl/type/DataArray.h b/inc/hgl/type/DataArray.h index 3eb55e8..5aabde9 100644 --- a/inc/hgl/type/DataArray.h +++ b/inc/hgl/type/DataArray.h @@ -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 &result_list,const DataArray &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; diff --git a/inc/hgl/type/Gradient.h b/inc/hgl/type/Gradient.h index d73e402..549068a 100644 --- a/inc/hgl/type/Gradient.h +++ b/inc/hgl/type/Gradient.h @@ -60,7 +60,7 @@ namespace hgl void Clear() { - stop_list.Clear(); + stop_list.Free(); dirty=true; } diff --git a/inc/hgl/type/List.h b/inc/hgl/type/List.h index 9ccddcc..ce3a4d0 100644 --- a/inc/hgl/type/List.h +++ b/inc/hgl/type/List.h @@ -45,7 +45,7 @@ namespace hgl List(const List <){operator=(lt);} ///<本类构造函数 List(const std::initializer_list <){operator=(lt);} - virtual ~List(){Clear();} ///<本类析构函数 + virtual ~List(){Free();} ///<本类析构函数 /** * 向列表中添加一个空数据 @@ -115,8 +115,8 @@ namespace hgl int Add(const List &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;} ///<确认数据项是否存在 diff --git a/inc/hgl/type/Map.cpp b/inc/hgl/type/Map.cpp index 8688b8d..d0bdc46 100644 --- a/inc/hgl/type/Map.cpp +++ b/inc/hgl/type/Map.cpp @@ -480,7 +480,7 @@ namespace hgl void _Map::Clear() { data_pool.ClearAll(); - data_list.Clear(); + data_list.Free(); } /** diff --git a/inc/hgl/type/ObjectList.h b/inc/hgl/type/ObjectList.h index 20eebd9..f5ff2fc 100644 --- a/inc/hgl/type/ObjectList.h +++ b/inc/hgl/type/ObjectList.h @@ -18,7 +18,7 @@ namespace hgl public: //方法 using List::List; - virtual ~ObjectList(){Clear();} + virtual ~ObjectList(){Free();} public: @@ -30,10 +30,10 @@ namespace hgl return List::Insert(index,obj); } - virtual void Clear() override ///<清除所有数据 + virtual void Free() override ///<清除所有数据 { ClearData(); - List::Clear(); + List::Free(); } virtual void ClearData() override ///<清除所有数据,但不清空缓冲区 diff --git a/inc/hgl/type/Pool.h b/inc/hgl/type/Pool.h index feef0fc..e6546aa 100644 --- a/inc/hgl/type/Pool.h +++ b/inc/hgl/type/Pool.h @@ -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); ///<设定最大数量限制 diff --git a/inc/hgl/type/Queue.cpp b/inc/hgl/type/Queue.cpp index a04b973..754980d 100644 --- a/inc/hgl/type/Queue.cpp +++ b/inc/hgl/type/Queue.cpp @@ -79,7 +79,7 @@ namespace hgl * 清除队列中的所有数据 */ template - void Queue::Clear() + void Queue::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::items[n]; - Queue::Clear(); + Queue::Free(); } } #endif//HGL_QUEUE_CPP diff --git a/inc/hgl/type/Queue.h b/inc/hgl/type/Queue.h index 2b6c2b3..31a130e 100644 --- a/inc/hgl/type/Queue.h +++ b/inc/hgl/type/Queue.h @@ -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) ///<取得指定项数据 diff --git a/inc/hgl/type/Stack.cpp b/inc/hgl/type/Stack.cpp deleted file mode 100644 index 75d41d9..0000000 --- a/inc/hgl/type/Stack.cpp +++ /dev/null @@ -1,261 +0,0 @@ -#ifndef HGL_STACK_CPP -#define HGL_STACK_CPP - -#include -namespace hgl -{ - /** - * 本类构造函数 - * @param m 如果m的值不为0,则表示使用固定的堆栈大小。固定大小的堆栈会在一开始即分配好内存。 - */ - template - Stack::Stack(int m) - { - cur_count=0; - - if(m) - { - max_count=m; - - items=hgl_align_malloc(max_count); - } - else - max_count=0; - - alloc_count=max_count; - } - - template - Stack::~Stack() - { - if(cur_count||max_count)hgl_free(items); - } - - /** - * 修改堆栈的最大值 - */ - template - void Stack::SetMaxCount(int m) - { - if(m<=0)return; - - if(alloc_count==0) - { - alloc_count=power_to_2(m); - items=hgl_align_malloc(alloc_count); - } - else - if(alloc_count(items,alloc_count); - } - - max_count=m; - - if(cur_count>=max_count)cur_count=max_count-1; - } - - template - bool Stack::SetCount(int c) - { - if(c<0)return(false); - - if(c>max_count) - return(false); - - cur_count=c; - return(true); - } - - /** - * 清除堆栈中的所有数据 - */ - template - void Stack::Clear() - { - if(max_count==0) - if(cur_count) - { - hgl_free(items); - alloc_count=0; - } - - cur_count=0; - } - - template - bool Stack::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 - bool Stack::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 - bool Stack::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 - bool Stack::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(items,alloc_count); - } - } - else - { - items=hgl_align_malloc(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 - bool Stack::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(items,alloc_count); - } - } - else - { - items=hgl_align_malloc(data_count); - - alloc_count=data_count; - } - } - - memcpy(items+cur_count,data,data_count*sizeof(T)); - cur_count+=data_count; - - return(true); - } - - template - void Stack::operator =(const Stack &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(alloc_count); - - memcpy(items,ori.items,alloc_count*sizeof(T)); - } -}//namespace hgl - -namespace hgl -{ - template - void StackObject::Clear() - { - for(int i=0;icur_count;i++) - delete this->items[i]; - - Stack::Clear(); - } -}//namespace hgl -#endif//HGL_STACK_CPP diff --git a/inc/hgl/type/Stack.h b/inc/hgl/type/Stack.h index 9820753..5c7c7f0 100644 --- a/inc/hgl/type/Stack.h +++ b/inc/hgl/type/Stack.h @@ -1,7 +1,6 @@ -#ifndef HGL_STACK_INCLUDE -#define HGL_STACK_INCLUDE +#pragma once -#include +#include namespace hgl { /** @@ -13,47 +12,123 @@ namespace hgl { protected: - int max_count; - int alloc_count; - int cur_count; - T *items; + DataArray 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 & GetArray() {return data_array;} + const DataArray &GetArray()const {return data_array;} + + operator DataArray & () {return data_array;} + operator const DataArray & ()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 &); + 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 &da) ///<复制一个堆栈 + { + data_array.SetCount(da.GetCount()); + + data_array.WriteAt(da.GetData(),0,da.GetCount()); + } + + virtual void operator =(const Stack &s){this->operator=(s.data_array);} };//template class Stack - template class StackObject:public Stack ///堆栈对象 + template class ObjectStack:public Stack ///堆栈对象 { + protected: + + virtual void DeleteObject(T *obj){if(obj)delete obj;} + public: using Stack::Stack; - virtual ~StackObject(){Clear();}; + virtual ~ObjectStack() override {Free();} - void Clear(); - };//template class StackObject + virtual bool Push(T *obj) + { + if(!obj)return(false); + + return Stack::Push(obj); + } + + virtual T *Pop() + { + T *obj; + + if(!Stack::Pop(obj)) + return(nullptr); + + return obj; + } + + void Clear() + { + for(T *obj:data_array) + DeleteObject(obj); + + data_array.Clear(); + } + + void Free() + { + ObjectStack::Clear(); + + data_array.Free(); + } + };//template class ObjectStack }//namespace hgl -#include -#endif//HGL_STACK_INCLUDE diff --git a/inc/hgl/type/StringList.h b/inc/hgl/type/StringList.h index 7ff3478..101417b 100644 --- a/inc/hgl/type/StringList.h +++ b/inc/hgl/type/StringList.h @@ -138,7 +138,7 @@ namespace hgl /** * 清除所有字符串 */ - void Clear(){Items.Clear();} ///<删除列表中的所有字符串 + void Clear(){Items.ClearData();} ///<删除列表中的所有字符串 /** * 删除指定的字符串 diff --git a/src/Log/Loginfo.cpp b/src/Log/Loginfo.cpp index 3aec9a7..87a313c 100644 --- a/src/Log/Loginfo.cpp +++ b/src/Log/Loginfo.cpp @@ -96,7 +96,7 @@ namespace hgl ++log; } - log_list->Clear(); + log_list->Free(); } }//namespace logger