2019-08-19 19:19:58 +08:00
|
|
|
|
#ifndef HGL_COMP_OPERATOR_INCLUDE
|
|
|
|
|
#define HGL_COMP_OPERATOR_INCLUDE
|
|
|
|
|
|
2023-07-24 14:23:07 +08:00
|
|
|
|
#include<hgl/TypeFunc.h>
|
2019-08-19 19:19:58 +08:00
|
|
|
|
namespace hgl
|
|
|
|
|
{
|
|
|
|
|
#define CompOperator(name,compfunc) const bool operator > (name i)const {return compfunc(i)>0;} \
|
|
|
|
|
const bool operator < (name i)const {return compfunc(i)<0;} \
|
|
|
|
|
const bool operator >=(name i)const {return compfunc(i)>=0;}\
|
|
|
|
|
const bool operator <=(name i)const {return compfunc(i)<=0;}\
|
|
|
|
|
const bool operator ==(name i)const {return compfunc(i)==0;}\
|
|
|
|
|
const bool operator !=(name i)const {return compfunc(i)!=0;}
|
|
|
|
|
|
|
|
|
|
#define CompOperatorMemcmp(name) int _Comp(name data)const{return memcmp(this,&data,sizeof(name));} \
|
|
|
|
|
CompOperator(name,_Comp)
|
2024-05-28 23:03:57 +08:00
|
|
|
|
|
|
|
|
|
#define CompOperatorMemcmpPointer(name) int _Comp(const name *data)const{return memcmp(this,data,sizeof(name));} \
|
|
|
|
|
CompOperator(const name *,_Comp)
|
2023-07-28 20:11:16 +08:00
|
|
|
|
}//namespace hgl
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 比较处理模板基类
|
|
|
|
|
*/
|
|
|
|
|
template<typename T> class Comparator ///比较处理模板基类
|
|
|
|
|
{
|
|
|
|
|
public:
|
2023-07-24 14:23:07 +08:00
|
|
|
|
|
|
|
|
|
/**
|
2023-08-09 16:51:51 +08:00
|
|
|
|
* 比较函数,需被特例化或派生实现. 正确情况下此函数不应该会被调用
|
2023-07-24 14:23:07 +08:00
|
|
|
|
*/
|
2023-08-09 16:51:51 +08:00
|
|
|
|
virtual int compare(const T &a,const T &b)const;
|
|
|
|
|
//{
|
|
|
|
|
// return 0; // 如 return(a-b);
|
|
|
|
|
//}
|
2023-07-28 20:11:16 +08:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 交换函数
|
|
|
|
|
*/
|
|
|
|
|
virtual void exchange(T &a,T &b)
|
2023-07-24 14:23:07 +08:00
|
|
|
|
{
|
2023-07-28 20:11:16 +08:00
|
|
|
|
hgl::hgl_swap(a,b);
|
|
|
|
|
}
|
2023-07-24 14:23:07 +08:00
|
|
|
|
|
2023-07-28 20:11:16 +08:00
|
|
|
|
/**
|
|
|
|
|
* 复制数据
|
2023-07-24 14:23:07 +08:00
|
|
|
|
*/
|
2023-07-28 20:11:16 +08:00
|
|
|
|
virtual void cpy(T *t,T *s)
|
|
|
|
|
{
|
|
|
|
|
memcpy(t,s,sizeof(T));
|
|
|
|
|
}
|
|
|
|
|
};//class Comparator
|
2023-07-24 14:23:07 +08:00
|
|
|
|
|
2023-07-28 20:11:16 +08:00
|
|
|
|
//针对原生类型的特例化版本,做适当加速
|
|
|
|
|
#define COMPARATOR_ORIGIN_TYPE(type) template<> class Comparator<type> \
|
|
|
|
|
{ \
|
|
|
|
|
public: \
|
|
|
|
|
int compare(const type &a,const type &b)const{return a-b;} \
|
|
|
|
|
void exchange(type &a,type &b){type t;t=a;a=b;b=t;} \
|
|
|
|
|
void cpy(type *t,type *s){*t=*s;} \
|
|
|
|
|
};
|
2023-07-24 14:23:07 +08:00
|
|
|
|
|
2023-07-28 20:11:16 +08:00
|
|
|
|
COMPARATOR_ORIGIN_TYPE(hgl::int8)
|
|
|
|
|
COMPARATOR_ORIGIN_TYPE(hgl::int16)
|
|
|
|
|
COMPARATOR_ORIGIN_TYPE(hgl::int32)
|
|
|
|
|
COMPARATOR_ORIGIN_TYPE(hgl::int64)
|
2023-07-24 14:23:07 +08:00
|
|
|
|
|
2023-07-28 20:11:16 +08:00
|
|
|
|
COMPARATOR_ORIGIN_TYPE(hgl::uint8)
|
|
|
|
|
COMPARATOR_ORIGIN_TYPE(hgl::uint16)
|
|
|
|
|
COMPARATOR_ORIGIN_TYPE(hgl::uint32)
|
|
|
|
|
COMPARATOR_ORIGIN_TYPE(hgl::uint64)
|
2023-07-24 14:23:07 +08:00
|
|
|
|
|
2023-07-28 20:11:16 +08:00
|
|
|
|
COMPARATOR_ORIGIN_TYPE(float)
|
|
|
|
|
COMPARATOR_ORIGIN_TYPE(double)
|
2023-07-24 14:23:07 +08:00
|
|
|
|
|
2023-07-28 20:11:16 +08:00
|
|
|
|
#if __cpp_char8_t
|
|
|
|
|
COMPARATOR_ORIGIN_TYPE(char8_t)
|
|
|
|
|
#endif//
|
2023-07-24 14:23:07 +08:00
|
|
|
|
|
2023-07-28 20:11:16 +08:00
|
|
|
|
COMPARATOR_ORIGIN_TYPE(char)
|
|
|
|
|
COMPARATOR_ORIGIN_TYPE(wchar_t)
|
2023-07-24 14:23:07 +08:00
|
|
|
|
|
2023-07-28 20:11:16 +08:00
|
|
|
|
#if HGL_OS != HGL_OS_Windows
|
|
|
|
|
COMPARATOR_ORIGIN_TYPE(u16char)
|
|
|
|
|
#endif//HGL_OS != HGL_OS_Windows
|
2023-07-24 14:23:07 +08:00
|
|
|
|
|
2023-07-28 20:11:16 +08:00
|
|
|
|
COMPARATOR_ORIGIN_TYPE(char32_t)
|
|
|
|
|
#undef COMPARATOR_ORIGIN_TYPE
|
2023-07-24 14:23:07 +08:00
|
|
|
|
|
2019-08-19 19:19:58 +08:00
|
|
|
|
#endif//HGL_COMP_OPERATOR_INCLUDE
|