2024-12-05 13:38:29 +08:00
|
|
|
|
#pragma once
|
2019-08-19 19:19:58 +08:00
|
|
|
|
|
2024-12-05 13:38:29 +08:00
|
|
|
|
#include<string.h>
|
2024-05-28 23:03:57 +08:00
|
|
|
|
|
2024-12-05 13:38:29 +08:00
|
|
|
|
namespace hgl
|
2023-07-28 20:11:16 +08:00
|
|
|
|
{
|
2023-07-24 14:23:07 +08:00
|
|
|
|
/**
|
2024-12-05 13:38:29 +08:00
|
|
|
|
* 比较处理模板基类
|
2023-07-24 14:23:07 +08:00
|
|
|
|
*/
|
2024-12-05 13:38:29 +08:00
|
|
|
|
template<typename T> class Comparator ///比较处理模板基类
|
2023-07-24 14:23:07 +08:00
|
|
|
|
{
|
2024-12-05 13:38:29 +08:00
|
|
|
|
public:
|
2023-07-24 14:23:07 +08:00
|
|
|
|
|
2024-12-05 13:38:29 +08:00
|
|
|
|
/**
|
|
|
|
|
* 比较函数,需被特例化或派生实现. 正确情况下此函数不应该会被调用
|
2023-07-24 14:23:07 +08:00
|
|
|
|
*/
|
2024-12-05 13:38:29 +08:00
|
|
|
|
virtual const int compare(const T &other)const=0;
|
|
|
|
|
|
|
|
|
|
const bool operator > (const T &i)const{return compare(i)>0;}
|
|
|
|
|
const bool operator < (const T &i)const{return compare(i)<0;}
|
|
|
|
|
const bool operator >=(const T &i)const{return compare(i)>=0;}
|
|
|
|
|
const bool operator <=(const T &i)const{return compare(i)<=0;}
|
|
|
|
|
const bool operator ==(const T &i)const{return compare(i)==0;}
|
|
|
|
|
const bool operator !=(const T &i)const{return compare(i)!=0;}
|
|
|
|
|
};//class Comparator
|
|
|
|
|
|
|
|
|
|
template<typename T> class ComparatorData:public Comparator<T>
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
const int compare(const T &other)const override final
|
|
|
|
|
{
|
|
|
|
|
return memcmp(this,&other,sizeof(T));
|
|
|
|
|
}
|
|
|
|
|
};//class ComparatorData
|
2024-12-12 13:20:31 +08:00
|
|
|
|
|
|
|
|
|
template<typename T> class ItemComparator
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
|
|
static const int compare(const T &a,const T &b);
|
|
|
|
|
|
|
|
|
|
static void cpy(T *dst,const T *src)
|
|
|
|
|
{
|
|
|
|
|
memcpy(dst,src,sizeof(T));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void exchange(T &a,T &b)
|
|
|
|
|
{
|
|
|
|
|
T temp;
|
|
|
|
|
cpy(&temp,&a);
|
|
|
|
|
cpy(&a,&b);
|
|
|
|
|
cpy(&b,&temp);
|
|
|
|
|
}
|
|
|
|
|
};
|
2024-12-05 13:38:29 +08:00
|
|
|
|
}//namespace hgl
|