From 6582b635c9b93284c29dd7e1bc6dc5ecf59a8705 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Thu, 12 Dec 2024 13:21:34 +0800 Subject: [PATCH] updated codes of SortBase --- inc/hgl/util/sort/Sort.h | 321 ++++++++++++++++++++------------------- 1 file changed, 165 insertions(+), 156 deletions(-) diff --git a/inc/hgl/util/sort/Sort.h b/inc/hgl/util/sort/Sort.h index f423ae5..368cc75 100644 --- a/inc/hgl/util/sort/Sort.h +++ b/inc/hgl/util/sort/Sort.h @@ -1,190 +1,199 @@ -#ifndef HGL_ALGORITHM_SORT_INCLUDE -#define HGL_ALGORITHM_SORT_INCLUDE +#pragma once -#include +#include #include #include -template class SortBase +namespace hgl { -protected: - - T *buffer; //数据 - int number; //数据个数 - - Comparator *comp; //比较函数类 - -public: - - /** - * 本类构造函数 - * @param buf 数据缓冲区 - * @param n 数据个数 - * @param c 数据大小比较类 - */ - SortBase(T *buf,int n,Comparator *c) + template class SortBase { - buffer =buf; - number =n; - comp =c; - } + protected: - virtual ~SortBase()=default; + T *buffer; //数据 + int number; //数据个数 - int GetCount()const - { - return number; - } + ItemComparator *comp; //比较函数类 - int compare(const T &a,const T &b) - { - return comp->compare(a,b); - } + public: - virtual int compare_by_index(int a,int b) - { - return comp->compare(buffer[a],buffer[b]); - } - - virtual void exchange(T &a,T &b) - { - comp->exchange(a,b); - } - - virtual void exchane_by_index(int a,int b) //交换两个数据 - { - comp->exchange(buffer[a],buffer[b]); - } - - virtual void cpy(T *dst,T *src) - { - comp->cpy(dst,src); - } - - virtual void cpy_by_index(int dst,int src) - { - comp->cpy(buffer+dst,buffer+src); - } - - virtual bool sort()=0; //排序 -};//struct SortBase - -//堆排序 -template class HeapSort:public SortBase -{ - void isift(int i,int n) - { - int j; - T temp; - - SortBase::cpy(&temp,SortBase::buffer+i); - - j=2*(i+1)-1; - - while(j<=n) + SortBase(ItemComparator *c) { - if((j::compare_by_index(j,j+1)<0))j++; + buffer=nullptr; + number=0; + comp=c; + } - if(SortBase::compare(temp,SortBase::buffer[j])<0) + /** + * 本类构造函数 + * @param buf 数据缓冲区 + * @param n 数据个数 + * @param c 数据大小比较类 + */ + SortBase(T *buf,int n,ItemComparator *c) + { + buffer =buf; + number =n; + comp =c; + } + + virtual ~SortBase()=default; + + int GetCount()const + { + return number; + } + + int compare(const T &a,const T &b) + { + return comp->compare(a,b); + } + + virtual int compare_by_index(int a,int b) + { + return comp->compare(buffer[a],buffer[b]); + } + + virtual void exchange(T &a,T &b) + { + comp->exchange(a,b); + } + + virtual void exchane_by_index(int a,int b) //交换两个数据 + { + comp->exchange(buffer[a],buffer[b]); + } + + virtual void cpy(T *dst,T *src) + { + comp->cpy(dst,src); + } + + virtual void cpy_by_index(int dst,int src) + { + comp->cpy(buffer+dst,buffer+src); + } + + virtual bool sort()=0; //排序 + };//struct SortBase + + //堆排序 + template class HeapSort:public SortBase + { + void isift(int i,int n) + { + int j; + T temp; + + SortBase::cpy(&temp,SortBase::buffer+i); + + j=2*(i+1)-1; + + while(j<=n) { - SortBase::cpy_by_index(i,j); - i=j; - j=2*(i+1)-1; + if((j::compare_by_index(j,j+1)<0))j++; + + if(SortBase::compare(temp,SortBase::buffer[j])<0) + { + SortBase::cpy_by_index(i,j); + i=j; + j=2*(i+1)-1; + } + else j=n+1; } - else j=n+1; + + SortBase::cpy(SortBase::buffer+i,&temp); } - SortBase::cpy(SortBase::buffer+i,&temp); - } + public: -public: - - /** - * 本类构造函数 - * @param buf 数据缓冲区 - * @param n 数据个数 - * @param c 数据大小比较类 - */ - HeapSort(T *buf,int n,Comparator *c=new Comparator()):SortBase(buf,n,c) - { - } - - bool sort() - { - if(!SortBase::buffer||SortBase::number<2||!SortBase::comp) - return(false); - - int i; - int mm=SortBase::number>>1; - - for(i=mm-1;i>=0;i--) - isift(i,SortBase::number-1); - - for(i=SortBase::number-1;i>=1;i--) + /** + * 本类构造函数 + * @param buf 数据缓冲区 + * @param n 数据个数 + * @param c 数据大小比较类 + */ + HeapSort(T *buf,int n,ItemComparator *c=new ItemComparator()):SortBase(buf,n,c) { - SortBase::exchane_by_index(0,i); - - isift(0,i-1); } - return(true); + bool sort() + { + if(!SortBase::buffer||SortBase::number<2||!SortBase::comp) + return(false); + + int i; + int mm=SortBase::number>>1; + + for(i=mm-1;i>=0;i--) + isift(i,SortBase::number-1); + + for(i=SortBase::number-1;i>=1;i--) + { + SortBase::exchane_by_index(0,i); + + isift(0,i-1); + } + + return(true); + } + };//class HeapSort:public SortBase + + template + bool Sort(T *data,int count,ItemComparator *comp) + { + HeapSort hs(data,count,comp); + + return hs.sort(); } -};//class HeapSort:public SortBase -template -bool Sort(T *data,int count,Comparator *comp) -{ - HeapSort hs(data,count,comp); + template + bool Sort(T *data,int count) + { + Comparator rnc; - return hs.sort(); -} + HeapSort hs(data,count,&rnc); -template -bool Sort(T *data,int count) -{ - Comparator rnc; - - HeapSort hs(data,count,&rnc); - - return hs.sort(); -} + return hs.sort(); + } -template -bool Sort(hgl::DataArray &list,Comparator *comp) -{ - return Sort(list.GetData(), - list.GetCount(), - comp); -} + template + bool Sort(hgl::DataArray &list,ItemComparator *comp) + { + return Sort(list.GetData(), + list.GetCount(), + comp); + } -template -bool Sort(hgl::DataArray &list) -{ - Comparator rnc; + template + bool Sort(hgl::DataArray &list) + { + ItemComparator rnc; - return Sort(list,&rnc); -} + return Sort(list,&rnc); + } -/* -//仅实现模拟虚拟成员函数即可,无需整个类重载 -template<> int Comparator::compare(const BagCell &it1,const BagCell &it2) const -{ - int r=it1.GetItemID()-it2.GetItemID(); + /* + //仅实现模拟虚拟成员函数即可,无需整个类重载 + template<> int ItemComparator::compare(const BagCell &it1,const BagCell &it2) const + { + int r=it1.GetItemID()-it2.GetItemID(); - if(r!=0) - return r; + if(r!=0) + return r; - return it1.count-it2.count; -} + return it1.count-it2.count; + } -void BagManage::Sort() -{ - Comparator comp_baginfo; + void BagManage::Sort() + { + Comparator comp_baginfo; - BagCell cell_list[BAG_SLOT_COUNT]; + BagCell cell_list[BAG_SLOT_COUNT]; + + hgl::Sort(cell_list,BAG_SLOT_COUNT,&comp_baginfo); + } + */ +}//namespace hgl - hgl::Sort(cell_list,BAG_SLOT_COUNT,&comp_baginfo); -} -*/ -#endif//HGL_ALGORITHM_SORT_INCLUDE