This commit is contained in:
HuYingzhuo(hugo/hyzboy) 2023-07-31 17:13:56 +08:00
commit f3f6fcef88

View File

@ -2,20 +2,19 @@
#define HGL_ALGORITHM_SORT_INCLUDE
#include<hgl/CompOperator.h>
#include<hgl/type/List.h>
#include<string.h>
namespace hgl
template<typename T> class SortBase
{
template<typename T> class SortBase
{
protected:
protected:
T *buffer; //数据
int number; //数据个数
Comparator<T> *comp; //比较函数类
public:
public:
/**
*
@ -68,11 +67,11 @@ namespace hgl
}
virtual bool sort()=0; //排序
};//struct SortBase
};//struct SortBase
//堆排序
template<typename T> class HeapSort:public SortBase<T>
{
//堆排序
template<typename T> class HeapSort:public SortBase<T>
{
void isift(int i,int n)
{
int j;
@ -98,7 +97,7 @@ namespace hgl
SortBase<T>::cpy(SortBase<T>::buffer+i,&temp);
}
public:
public:
/**
*
@ -130,43 +129,62 @@ namespace hgl
return(true);
}
};//class HeapSort:public SortBase<T>
};//class HeapSort:public SortBase<T>
template<typename T>
bool Sort(T *data,int count,Comparator<T> *comp=new Comparator<T>())
{
template<typename T>
bool Sort(T *data,int count,Comparator<T> *comp)
{
HeapSort<T> hs(data,count,comp);
return hs.sort();
}
}
template<typename T>
bool Sort(DataArray<T> &list,Comparator<T> *comp=Comparator<T>())
{
template<typename T>
bool Sort(T *data,int count)
{
Comparator<T> rnc;
HeapSort<T> hs(data,count,&rnc);
return hs.sort();
}
template<typename T>
bool Sort(hgl::DataArray<T> &list,Comparator<T> *comp)
{
return Sort(list.GetData(),
list.GetCount(),
comp);
}
}
template<typename T>
bool Sort(hgl::DataArray<T> &list)
{
Comparator<T> rnc;
return Sort<T>(list,&rnc);
}
/*
//仅实现模拟虚拟成员函数即可,无需整个类重载
template<> int Comparator<BagCell>::compare(const BagCell &it1,const BagCell &it2) const
{
//仅实现模拟虚拟成员函数即可,无需整个类重载
template<> int Comparator<BagCell>::compare(const BagCell &it1,const BagCell &it2) const
{
int r=it1.GetItemID()-it2.GetItemID();
if(r!=0)
return r;
return it1.count-it2.count;
}
}
void BagManage::Sort()
{
void BagManage::Sort()
{
Comparator<BagCell> comp_baginfo;
BagCell cell_list[BAG_SLOT_COUNT];
hgl::Sort<BagCell>(cell_list,BAG_SLOT_COUNT,&comp_baginfo);
}
*/
}//namespace hgl
}
*/
#endif//HGL_ALGORITHM_SORT_INCLUDE