updated codes of SortBase

This commit is contained in:
hyzboy 2024-12-12 13:21:34 +08:00
parent a7f4afab80
commit 6582b635c9

View File

@ -1,10 +1,11 @@
#ifndef HGL_ALGORITHM_SORT_INCLUDE #pragma once
#define HGL_ALGORITHM_SORT_INCLUDE
#include<hgl/CompOperator.h> #include<hgl/Comparator.h>
#include<hgl/type/List.h> #include<hgl/type/List.h>
#include<string.h> #include<string.h>
namespace hgl
{
template<typename T> class SortBase template<typename T> class SortBase
{ {
protected: protected:
@ -12,17 +13,24 @@ protected:
T *buffer; //数据 T *buffer; //数据
int number; //数据个数 int number; //数据个数
Comparator<T> *comp; //比较函数类 ItemComparator<T> *comp; //比较函数类
public: public:
SortBase(ItemComparator<T> *c)
{
buffer=nullptr;
number=0;
comp=c;
}
/** /**
* *
* @param buf * @param buf
* @param n * @param n
* @param c * @param c
*/ */
SortBase(T *buf,int n,Comparator<T> *c) SortBase(T *buf,int n,ItemComparator<T> *c)
{ {
buffer =buf; buffer =buf;
number =n; number =n;
@ -105,7 +113,7 @@ public:
* @param n * @param n
* @param c * @param c
*/ */
HeapSort(T *buf,int n,Comparator<T> *c=new Comparator<T>()):SortBase<T>(buf,n,c) HeapSort(T *buf,int n,ItemComparator<T> *c=new ItemComparator<T>()):SortBase<T>(buf,n,c)
{ {
} }
@ -132,7 +140,7 @@ public:
};//class HeapSort:public SortBase<T> };//class HeapSort:public SortBase<T>
template<typename T> template<typename T>
bool Sort(T *data,int count,Comparator<T> *comp) bool Sort(T *data,int count,ItemComparator<T> *comp)
{ {
HeapSort<T> hs(data,count,comp); HeapSort<T> hs(data,count,comp);
@ -151,7 +159,7 @@ bool Sort(T *data,int count)
template<typename T> template<typename T>
bool Sort(hgl::DataArray<T> &list,Comparator<T> *comp) bool Sort(hgl::DataArray<T> &list,ItemComparator<T> *comp)
{ {
return Sort(list.GetData(), return Sort(list.GetData(),
list.GetCount(), list.GetCount(),
@ -161,14 +169,14 @@ bool Sort(hgl::DataArray<T> &list,Comparator<T> *comp)
template<typename T> template<typename T>
bool Sort(hgl::DataArray<T> &list) bool Sort(hgl::DataArray<T> &list)
{ {
Comparator<T> rnc; ItemComparator<T> rnc;
return Sort<T>(list,&rnc); return Sort<T>(list,&rnc);
} }
/* /*
//仅实现模拟虚拟成员函数即可,无需整个类重载 //仅实现模拟虚拟成员函数即可,无需整个类重载
template<> int Comparator<BagCell>::compare(const BagCell &it1,const BagCell &it2) const template<> int ItemComparator<BagCell>::compare(const BagCell &it1,const BagCell &it2) const
{ {
int r=it1.GetItemID()-it2.GetItemID(); int r=it1.GetItemID()-it2.GetItemID();
@ -187,4 +195,5 @@ void BagManage::Sort()
hgl::Sort<BagCell>(cell_list,BAG_SLOT_COUNT,&comp_baginfo); hgl::Sort<BagCell>(cell_list,BAG_SLOT_COUNT,&comp_baginfo);
} }
*/ */
#endif//HGL_ALGORITHM_SORT_INCLUDE }//namespace hgl