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,20 +1,28 @@
#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>
template<typename T> class SortBase namespace hgl
{ {
protected: template<typename T> class SortBase
{
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;
}
/** /**
* *
@ -22,7 +30,7 @@ public:
* @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;
@ -67,11 +75,11 @@ public:
} }
virtual bool sort()=0; //排序 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) void isift(int i,int n)
{ {
int j; int j;
@ -97,7 +105,7 @@ template<typename T> class HeapSort:public SortBase<T>
SortBase<T>::cpy(SortBase<T>::buffer+i,&temp); SortBase<T>::cpy(SortBase<T>::buffer+i,&temp);
} }
public: public:
/** /**
* *
@ -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)
{ {
} }
@ -129,62 +137,63 @@ public:
return(true); return(true);
} }
};//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);
return hs.sort(); return hs.sort();
} }
template<typename T> template<typename T>
bool Sort(T *data,int count) bool Sort(T *data,int count)
{ {
Comparator<T> rnc; Comparator<T> rnc;
HeapSort<T> hs(data,count,&rnc); HeapSort<T> hs(data,count,&rnc);
return hs.sort(); return hs.sort();
} }
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(),
comp); 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();
if(r!=0) if(r!=0)
return r; return r;
return it1.count-it2.count; return it1.count-it2.count;
} }
void BagManage::Sort() void BagManage::Sort()
{ {
Comparator<BagCell> comp_baginfo; Comparator<BagCell> comp_baginfo;
BagCell cell_list[BAG_SLOT_COUNT]; BagCell cell_list[BAG_SLOT_COUNT];
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