diff --git a/inc/hgl/type/AccumMemoryManager.h b/inc/hgl/type/AccumMemoryManager.h new file mode 100644 index 0000000..79cc16e --- /dev/null +++ b/inc/hgl/type/AccumMemoryManager.h @@ -0,0 +1,64 @@ +#pragma once + +#include + +namespace hgl +{ + /** + * 累计内存管理
+ * 用于不断分配固定容量的内存块,但不动态调整,最后统一释放的情况。 + */ + class AccumMemoryManager + { + public: + + struct Block + { + int64 offset; + int64 size; + }; + + private: + + int64 total_bytes=0; ///<总字节数 + + DataArray block_list; + DataArray data_array; ///<数据 + + public: + + AccumMemoryManager()=default; + ~AccumMemoryManager()=default; + + const int64 GetTotalBytes()const{return total_bytes;} + const int64 GetBlockCount()const{return block_list.GetCount();} + + const void *Acquire(const int64 size) + { + if(size<=0)return(nullptr); + + Block b; + + b.offset=total_bytes; + b.size=size; + + data_array.AddCount(size); + + return data_array.GetPointer(b.offset); + } + + void Clear() + { + total_bytes=0; + block_list.Clear(); + data_array.Clear(); + } + + void Free() + { + total_bytes=0; + block_list.Free(); + data_array.Free(); + } + };//class AccumMemoryManager +}//namespace diff --git a/inc/hgl/type/ArrayItemProcess.h b/inc/hgl/type/ArrayItemProcess.h index 4931218..817ff5e 100644 --- a/inc/hgl/type/ArrayItemProcess.h +++ b/inc/hgl/type/ArrayItemProcess.h @@ -35,14 +35,14 @@ namespace hgl /** * 鏌ユ壘鏁版嵁鍦ㄦ棤搴忛樀鍒椾腑鐨勪綅缃 */ - template static int FindDataPositionInArray(const T *data_list,const int count,const T &data) + template static int FindDataPositionInArray(const T *data_list,const int64 count,const T &data) { if(!data_list)return(-1); if(count<=0)return(-1); const T *p=data_list; - for(int i=0;i static int FindDataPositionInArray(const T &data_list,const O &data) + template static int64 FindDataPositionInArray(const T &data_list,const O &data) { return FindDataPositionInArray(data_list.GetData(),data_list.GetCount(),data); } @@ -62,10 +62,10 @@ namespace hgl /** * 鏌ユ壘鏁版嵁鍦ㄦ湁搴忛樀鍒椾腑鐨勪綅缃 */ - template static int FindDataPositionInSortedArray(const T *data_array,const int count,const T &flag) + template static int64 FindDataPositionInSortedArray(const T *data_array,const int64 count,const T &flag) { - int left=0,right=count-1; //浣跨敤left,right鑰屼笉浣跨敤min,max鏄负浜嗚浠g爜鑳藉鏇村ソ鐨勯槄璇汇 - int mid; + int64 left=0,right=count-1; //浣跨敤left,right鑰屼笉浣跨敤min,max鏄负浜嗚浠g爜鑳藉鏇村ソ鐨勯槄璇汇 + int64 mid; while(left<=right) { @@ -91,7 +91,7 @@ namespace hgl return(-1); } - template static int FindDataPositionInSortedArray(const T &data_array,const O &flag) + template static int64 FindDataPositionInSortedArray(const T &data_array,const O &flag) { return FindDataPositionInSortedArray(data_array.GetData(),data_array.GetCount(),flag); } @@ -104,10 +104,10 @@ namespace hgl * @param flag 瑕佹煡鎵剧殑鏁版嵁 * @return 杩欎釜鏁版嵁鏄惁宸茬粡瀛樺湪 */ - template static bool FindInsertPositionInSortedArray(int *pos,const T *data_array,const int count,const T &flag) + template static bool FindInsertPositionInSortedArray(int64 *pos,const T *data_array,const int64 count,const T &flag) { - int left=0,right=count-1; - int mid; + int64 left=0,right=count-1; + int64 mid; while(left<=right) { @@ -182,7 +182,7 @@ namespace hgl return(false); } - template static bool FindInsertPositionInSortedArray(int *pos,const T &data_array,const O &flag) + template static bool FindInsertPositionInSortedArray(int64 *pos,const T &data_array,const O &flag) { return FindInsertPositionInSortedArray(pos,data_array.GetData(),data_array.GetCount(),flag); } diff --git a/inc/hgl/type/ArrayRearrangeHelper.h b/inc/hgl/type/ArrayRearrangeHelper.h index cb60f1a..6eaaa30 100644 --- a/inc/hgl/type/ArrayRearrangeHelper.h +++ b/inc/hgl/type/ArrayRearrangeHelper.h @@ -8,24 +8,24 @@ namespace hgl */ class ArrayRearrangeHelper { - int data_count; ///<鏁版嵁鎬婚噺 - int left_count; ///<鍓╀綑鏁伴噺 - int data_offset; ///<褰撳墠璁块棶鍋忕Щ + int64 data_count; ///<鏁版嵁鎬婚噺 + int64 left_count; ///<鍓╀綑鏁伴噺 + int64 data_offset; ///<褰撳墠璁块棶鍋忕Щ - int field_count; //鍒嗘鏁伴噺 + int64 field_count; ///<鍒嗘鏁伴噺 struct Field { - int start; - int count; + int64 start; + int64 count; }; Field *field_list; - int field_index; + int64 field_index; public: - ArrayRearrangeHelper(int dc,int fc) + ArrayRearrangeHelper(int64 dc,int64 fc) { data_count=dc; left_count=dc; @@ -45,7 +45,7 @@ namespace hgl /** * 娣诲姞涓涓垎娈 */ - bool AddField(int count) + bool AddField(int64 count) { if(count<0)return(false); if(count>left_count)return(false); @@ -63,9 +63,9 @@ namespace hgl /** * 娣诲姞澶氫釜鍒嗘 */ - bool AddField(const std::initializer_list &count_list) + bool AddField(const std::initializer_list &count_list) { - for(const int count:count_list) + for(const int64 count:count_list) if(!AddField(count)) return(false); @@ -100,7 +100,7 @@ namespace hgl * 閲嶆柊鎺掑垪鏁版嵁鍒颁竴涓柊鐨勬暟缁勪腑 */ template - bool Rearrange(T *new_array,const T *old_array,const int *index) + bool Rearrange(T *new_array,const T *old_array,const int64 *index) { if(!Finish()) return(false); @@ -108,7 +108,7 @@ namespace hgl T *p=new_array; Field *f; - for(int i=0;i=field_index) return(false); @@ -125,20 +125,23 @@ namespace hgl } template - bool Rearrange(T *new_array,const T *old_array,const std::initializer_list &index) + bool Rearrange(T *new_array,const T *old_array,const std::initializer_list &index) { return Rearrange(new_array,old_array,index.begin()); } };//class ArrayRearrangeHelper template - inline bool ArrayRearrange(T *new_array,const T *old_array,const int count,const std::initializer_list &field_list,const std::initializer_list &index) + inline bool ArrayRearrange( T *new_array, + const T *old_array,const int64 count, + const std::initializer_list &field_list, + const std::initializer_list &index) { - int field_count=(int)field_list.size(); + int64 field_count=(int)field_list.size(); - int total=0; + int64 total=0; - for(const int fc:field_list)total+=fc; + for(const int64 fc:field_list)total+=fc; if(total=count)?nullptr:items+n; } - const bool ReadAt(T &obj,int const index)const + const bool ReadAt(T &obj,int64 const index)const { if(index<0||index>=count)return(false); @@ -172,7 +172,7 @@ namespace hgl return(true); } - const bool ReadAt(T *obj,int const start,const int num)const + const bool ReadAt(T *obj,const int64 start,const int64 num)const { if(!obj||start<0||start+num>count)return(false); @@ -180,7 +180,7 @@ namespace hgl return(true); } - const bool WriteAt(const T &obj,const int index) + const bool WriteAt(const T &obj,const int64 index) { if(index<0||index>=count)return(false); @@ -188,7 +188,7 @@ namespace hgl return(true); } - const bool WriteAt(const T *obj,const int start,const int num) + const bool WriteAt(const T *obj,const int64 start,const int64 num) { if(!obj||start<0||start+num>count)return(false); @@ -214,7 +214,7 @@ namespace hgl void operator = (const std::initializer_list &l) { - SetCount((int)l.size()); + SetCount((int64)l.size()); hgl_cpy(items,l.begin(),count); } @@ -225,7 +225,7 @@ namespace hgl * @param delete_count 瑕佸垹闄ょ殑鏁版嵁椤规暟閲 * @return 鏄惁鎴愬姛 */ - bool Delete(int start,int delete_count=1) + bool Delete(int64 start,int64 delete_count=1) { if(!items)return(false); if(start>=count)return(false); @@ -276,7 +276,7 @@ namespace hgl * @param start 瑕佸垹闄ょ殑鏁版嵁椤圭殑绱㈠紩鍊 * @return 鏄惁鎴愬姛 */ - bool DeleteMove(int start,int delete_count=1) + bool DeleteMove(int64 start,int64 delete_count=1) { if(!items)return(false); if(start>=count)return(false); @@ -292,7 +292,7 @@ namespace hgl if(delete_count<=0)return(false); - const int end_count=count-(start+delete_count); + const int64 end_count=count-(start+delete_count); if(end_count>0) hgl_cpy(items+start,items+start+delete_count,end_count); @@ -307,7 +307,7 @@ namespace hgl * @param a 绗竴涓暟鎹殑浣嶇疆 * @param b 绗簩涓暟鎹殑浣嶇疆 */ - void Exchange(int a,int b) + void Exchange(int64 a,int64 b) { hgl_swap(items[a],items[b]); } @@ -318,7 +318,7 @@ namespace hgl * @param old_index 鏃х殑浣嶇疆 * @param move_number 瑕佺Щ鍔ㄧ殑鏁版嵁涓暟 */ - bool Move(int new_index,int old_index,int move_number=1) + bool Move(int64 new_index,int64 old_index,int64 move_number=1) { if(!items)return(false); if(new_index==old_index)return(false); @@ -333,7 +333,7 @@ namespace hgl if(move_number<=0)return(false); //鐩存帴鍒涘缓鏂扮紦鍐插尯澶嶅埗杩囧幓锛屾斁寮冩棫缂撳啿鍖 - const int new_alloc_count=power_to_2(count); + const int64 new_alloc_count=power_to_2(count); T *new_items=hgl_align_malloc(new_alloc_count); bool result; @@ -543,7 +543,7 @@ namespace hgl * @param find_count 瑕佹煡鎵剧殑鏈澶ф暟閲 * @return <0 鏈壘鍒版垨鍏跺畠閿欒 */ - const int Find(const T &data,const int start=0,int find_count=-1)const + const int64 Find(const T &data,const int64 start=0,int64 find_count=-1)const { if(!items||count<=0||start<0||start>=count)return(-1); @@ -556,7 +556,7 @@ namespace hgl /** * 鍦ㄦ寚瀹氫綅缃彃鍏ユ暟鎹 */ - bool Insert(int pos,const T *data,const int data_number) + bool Insert(int64 pos,const T *data,const int64 data_number) { if(!data||data_number<=0) return(false); @@ -566,7 +566,7 @@ namespace hgl if(count+data_number>alloc_count) { - int new_alloc_count=power_to_2(alloc_count+data_number); + int64 new_alloc_count=power_to_2(alloc_count+data_number); T *new_items=hgl_align_malloc(new_alloc_count); @@ -591,7 +591,7 @@ namespace hgl return(true); } - bool Insert(int pos,const T &data) + bool Insert(int64 pos,const T &data) { return Insert(pos,&data,1); } @@ -602,14 +602,14 @@ namespace hgl void WithoutList(DataArray &result_list,const DataArray &without_list) { result_list.Clear(); - const int count=this->GetCount(); + const int64 count=this->GetCount(); if(count<=0)return; result_list.Clear(); result_list.PreAlloc(count); - int result=0; + int64 result=0; T *p=result_list.items; @@ -626,8 +626,8 @@ namespace hgl result_list.SetCount(result); } - //int Intersection (SortedSets &result,const SortedSets &sets); ///<鍙栧緱涓庢寚瀹氬悎闆嗙殑浜ら泦 - //int Intersection (const SortedSets &set); ///<鍙栧緱涓庢寚瀹氬悎闆嗙殑浜ら泦鏁伴噺 + //int64 Intersection (SortedSets &result,const SortedSets &sets); ///<鍙栧緱涓庢寚瀹氬悎闆嗙殑浜ら泦 + //int64 Intersection (const SortedSets &set); ///<鍙栧緱涓庢寚瀹氬悎闆嗙殑浜ら泦鏁伴噺 ///** // * 鍙栧緱涓庢寚瀹氫氦闆唅s鐨勫悎闆嗭紝浣嗘帓鏂s鍚堥泦涓殑鏁版嵁 @@ -636,9 +636,9 @@ namespace hgl // * @param cs 姹傛帓鏂ョ殑鍚堥泦 // * @return 缁撴灉鏁伴噺 // */ - //int Intersection (SortedSets &result,const SortedSets &is,const SortedSets &cs); + //int64 Intersection (SortedSets &result,const SortedSets &is,const SortedSets &cs); - //int Difference (const SortedSets &is); ///<姹傚樊闆嗘暟閲 + //int64 Difference (const SortedSets &is); ///<姹傚樊闆嗘暟閲 ///** // * 姹傚綋鍓嶅悎闆嗕笌鍙︿竴涓暟鎹泦鐨勪氦闆 @@ -646,7 +646,7 @@ namespace hgl // * @param list 瑕佽绠椾氦闆嗙殑鏁版嵁闆 // * @return 浜ら泦鏁伴噺 // */ - //int Intersection(SortedSets &result,const SortedSets &list) + //int64 Intersection(SortedSets &result,const SortedSets &list) //{ // if(data_list.GetCount()<=0) // return(0); @@ -663,7 +663,7 @@ namespace hgl // return result.GetCount(); //} - //int Intersection(const SortedSets &list) + //int64 Intersection(const SortedSets &list) //{ // if(data_list.GetCount()<=0) // return(0); @@ -671,10 +671,10 @@ namespace hgl // if(list.GetCount()<=0) // return(0); - // int count=0; + // int64 count=0; // T *obj=data_list.GetData(); - // for(int i=0;i &result,const SortedSets &il,const SortedSets &cl) + //int64 Intersection(SortedSets &result,const SortedSets &il,const SortedSets &cl) //{ // if(data_list.GetCount()<=0) // return(0); @@ -694,7 +694,7 @@ namespace hgl // return(0); // T *obj=data_list.GetData(); - // for(int i=0;i &clear_sets); ///<娓呴櫎鎸囧畾鍚堥泦涓墍鏈夋暟鎹 + //int64 Clear (const SortedSets &clear_sets); ///<娓呴櫎鎸囧畾鍚堥泦涓墍鏈夋暟鎹 };//template class DataArray }//namespace hgl diff --git a/inc/hgl/type/SortedSets.h b/inc/hgl/type/SortedSets.h index 4adb95e..5cf9bf5 100644 --- a/inc/hgl/type/SortedSets.h +++ b/inc/hgl/type/SortedSets.h @@ -14,15 +14,15 @@ namespace hgl DataArray data_list; - bool FindPos(const T &flag,int &pos)const ///<鏌ユ壘鏁版嵁濡傛灉鎻掑叆鍚庯紝浼氭墍鍦ㄧ殑浣嶇疆锛岃繑鍥炴槸鍚﹀瓨鍦ㄨ繖涓暟鎹 + bool FindPos(const T &flag,int64 &pos)const ///<鏌ユ壘鏁版嵁濡傛灉鎻掑叆鍚庯紝浼氭墍鍦ㄧ殑浣嶇疆锛岃繑鍥炴槸鍚﹀瓨鍦ㄨ繖涓暟鎹 {return FindInsertPositionInSortedArray(&pos,data_list,flag);} - int FindPos(const T &flag)const{int pos;return FindPos(flag,pos)?pos:-1;} ///<鏌ユ壘鏁版嵁濡傛灉鎻掑叆鍚庯紝浼氭墍鍦ㄧ殑浣嶇疆 + int64 FindPos(const T &flag)const{int64 pos;return FindPos(flag,pos)?pos:-1;} ///<鏌ユ壘鏁版嵁濡傛灉鎻掑叆鍚庯紝浼氭墍鍦ㄧ殑浣嶇疆 public: //灞炴 T * GetData ()const{return data_list.GetData();} ///<鍙栧緱鏁版嵁鎸囬拡 - int GetCount ()const{return data_list.GetCount();} ///<鍙栧緱鏁版嵁鎬婚噺 + int64 GetCount ()const{return data_list.GetCount();} ///<鍙栧緱鏁版嵁鎬婚噺 const bool IsEmpty ()const{return data_list.IsEmpty();} ///<纭鍒楄〃鏄惁涓虹┖ @@ -40,15 +40,15 @@ namespace hgl SortedSets()=default; virtual ~SortedSets()=default; - void SetCount (int count){data_list.SetCount(count);} ///<鎸囧畾鏁版嵁鏁伴噺锛屼竴鑸敤浜庢壒閲忓姞杞藉墠鐨勫鐞 - void PreAlloc (int count){data_list.Alloc(count);} ///<棰勫垎閰嶆寚瀹氭暟閲忕殑鏁版嵁绌洪棿 + void SetCount (int64 count){data_list.SetCount(count);} ///<鎸囧畾鏁版嵁鏁伴噺锛屼竴鑸敤浜庢壒閲忓姞杞藉墠鐨勫鐞 + void PreAlloc (int64 count){data_list.Alloc(count);} ///<棰勫垎閰嶆寚瀹氭暟閲忕殑鏁版嵁绌洪棿 /** * 鏌ユ壘鏁版嵁鏄惁瀛樺湪 * @param flag 鏁版嵁 * @return 鏁版嵁鎵鍦ㄧ储寮曪紝-1琛ㄧず涓嶅瓨鍦 */ - int Find (const T &flag)const + int64 Find (const T &flag)const { return FindDataPositionInSortedArray(data_list,flag); } @@ -61,7 +61,7 @@ namespace hgl * @return 鎻掑叆鐨勪綅缃 * @reutrn -1 鏁版嵁宸插瓨鍦紝娣诲姞澶辫触 */ - int Add (const T &data) + int64 Add (const T &data) { if(data_list.GetCount()<=0) { @@ -73,7 +73,7 @@ namespace hgl } else { - int pos; + int64 pos; if(FindPos(data,pos)) return(-1); //鏁版嵁宸插瓨鍦 @@ -87,7 +87,7 @@ namespace hgl /* * 娣诲姞涓鎵规暟鎹 */ - int Add (T *dl,const int count) + int64 Add (T *dl,const int64 count) { if(!dl||count<=0)return -1; @@ -97,10 +97,10 @@ namespace hgl data_list.Alloc(data_list.GetCount()+count); { - int pos; - int result=0; + int64 pos; + int64 result=0; - for(int i=0;i=data_list.GetCount()) return(false);