Merge branch 'master' of http://www.hyzgame.com:3000/hyzboy/CMUtil
This commit is contained in:
commit
f3f6fcef88
@ -2,20 +2,19 @@
|
|||||||
#define HGL_ALGORITHM_SORT_INCLUDE
|
#define HGL_ALGORITHM_SORT_INCLUDE
|
||||||
|
|
||||||
#include<hgl/CompOperator.h>
|
#include<hgl/CompOperator.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:
|
|
||||||
|
|
||||||
T *buffer; //数据
|
T *buffer; //数据
|
||||||
int number; //数据个数
|
int number; //数据个数
|
||||||
|
|
||||||
Comparator<T> *comp; //比较函数类
|
Comparator<T> *comp; //比较函数类
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 本类构造函数
|
* 本类构造函数
|
||||||
@ -68,11 +67,11 @@ namespace hgl
|
|||||||
}
|
}
|
||||||
|
|
||||||
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;
|
||||||
@ -98,7 +97,7 @@ namespace hgl
|
|||||||
SortBase<T>::cpy(SortBase<T>::buffer+i,&temp);
|
SortBase<T>::cpy(SortBase<T>::buffer+i,&temp);
|
||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 本类构造函数
|
* 本类构造函数
|
||||||
@ -130,43 +129,62 @@ namespace hgl
|
|||||||
|
|
||||||
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=new Comparator<T>())
|
bool Sort(T *data,int count,Comparator<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(DataArray<T> &list,Comparator<T> *comp=Comparator<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(),
|
return Sort(list.GetData(),
|
||||||
list.GetCount(),
|
list.GetCount(),
|
||||||
comp);
|
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();
|
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);
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
}//namespace hgl
|
|
||||||
#endif//HGL_ALGORITHM_SORT_INCLUDE
|
#endif//HGL_ALGORITHM_SORT_INCLUDE
|
||||||
|
Loading…
x
Reference in New Issue
Block a user