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
#define HGL_ALGORITHM_SORT_INCLUDE
#pragma once
#include<hgl/CompOperator.h>
#include<hgl/Comparator.h>
#include<hgl/type/List.h>
#include<string.h>
template<typename T> class SortBase
namespace hgl
{
protected:
template<typename T> class SortBase
{
protected:
T *buffer; //数据
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 c
*/
SortBase(T *buf,int n,Comparator<T> *c)
SortBase(T *buf,int n,ItemComparator<T> *c)
{
buffer =buf;
number =n;
@ -67,11 +75,11 @@ public:
}
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;
@ -97,7 +105,7 @@ template<typename T> class HeapSort:public SortBase<T>
SortBase<T>::cpy(SortBase<T>::buffer+i,&temp);
}
public:
public:
/**
*
@ -105,7 +113,7 @@ public:
* @param n
* @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);
}
};//class HeapSort:public SortBase<T>
};//class HeapSort:public SortBase<T>
template<typename T>
bool Sort(T *data,int count,Comparator<T> *comp)
{
template<typename T>
bool Sort(T *data,int count,ItemComparator<T> *comp)
{
HeapSort<T> hs(data,count,comp);
return hs.sort();
}
}
template<typename T>
bool Sort(T *data,int count)
{
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)
{
template<typename T>
bool Sort(hgl::DataArray<T> &list,ItemComparator<T> *comp)
{
return Sort(list.GetData(),
list.GetCount(),
comp);
}
}
template<typename T>
bool Sort(hgl::DataArray<T> &list)
{
Comparator<T> rnc;
template<typename T>
bool Sort(hgl::DataArray<T> &list)
{
ItemComparator<T> 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();
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);
}
*/
#endif//HGL_ALGORITHM_SORT_INCLUDE
}
*/
}//namespace hgl