use newly Comparator<>

This commit is contained in:
hyzboy 2024-12-05 13:38:29 +08:00
parent 680b7717fc
commit 6e1f32f38e
7 changed files with 48 additions and 110 deletions

View File

@ -132,7 +132,7 @@ namespace hgl
return (uint16)CharCodePage::NONE; return (uint16)CharCodePage::NONE;
} }
struct CharSet struct CharSet:public Comparator<CharSet>
{ {
uint16 codepage; uint16 codepage;
CharSetName charset; CharSetName charset;
@ -160,8 +160,10 @@ namespace hgl
strcpy(charset,CHAR_SET_NAME_MAX_LENGTH,cs.charset); strcpy(charset,CHAR_SET_NAME_MAX_LENGTH,cs.charset);
} }
int _Comp(const CharSet &data)const{return (size_t)codepage-(size_t)data.codepage;} \ const int compare(const CharSet &other)const override
CompOperator(const CharSet &,_Comp) {
return (size_t)codepage-(size_t)other.codepage;
}
};//struct CharacterSet };//struct CharacterSet
inline CharSet::CharSet(const uint16 ccp) inline CharSet::CharSet(const uint16 ccp)

View File

@ -1,89 +1,36 @@
#ifndef HGL_COMP_OPERATOR_INCLUDE #pragma once
#define HGL_COMP_OPERATOR_INCLUDE
#include<string.h>
#include<hgl/TypeFunc.h>
namespace hgl 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;}\ template<typename T> class Comparator ///比较处理模板基类
const bool operator ==(name i)const {return compfunc(i)==0;}\ {
const bool operator !=(name i)const {return compfunc(i)!=0;} public:
#define CompOperatorMemcmp(name) int _Comp(name data)const{return memcmp(this,&data,sizeof(name));} \
CompOperator(name,_Comp)
#define CompOperatorMemcmpPointer(name) int _Comp(const name *data)const{return memcmp(this,data,sizeof(name));} \
CompOperator(const name *,_Comp)
}//namespace hgl
/**
*
*/
template<typename T> class Comparator ///比较处理模板基类
{
public:
/** /**
* . * .
*/ */
virtual int compare(const T &a,const T &b)const; virtual const int compare(const T &other)const=0;
//{
// return 0; // 如 return(a-b);
//}
/** 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;}
virtual void exchange(T &a,T &b) 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>
{ {
hgl::hgl_swap(a,b); public:
}
/** const int compare(const T &other)const override final
*
*/
virtual void cpy(T *t,T *s)
{ {
memcpy(t,s,sizeof(T)); return memcmp(this,&other,sizeof(T));
} }
};//class Comparator };//class ComparatorData
}//namespace hgl
//针对原生类型的特例化版本,做适当加速
#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;} \
};
COMPARATOR_ORIGIN_TYPE(hgl::int8)
COMPARATOR_ORIGIN_TYPE(hgl::int16)
COMPARATOR_ORIGIN_TYPE(hgl::int32)
COMPARATOR_ORIGIN_TYPE(hgl::int64)
COMPARATOR_ORIGIN_TYPE(hgl::uint8)
COMPARATOR_ORIGIN_TYPE(hgl::uint16)
COMPARATOR_ORIGIN_TYPE(hgl::uint32)
COMPARATOR_ORIGIN_TYPE(hgl::uint64)
COMPARATOR_ORIGIN_TYPE(float)
COMPARATOR_ORIGIN_TYPE(double)
#if __cpp_char8_t
COMPARATOR_ORIGIN_TYPE(char8_t)
#endif//
COMPARATOR_ORIGIN_TYPE(char)
COMPARATOR_ORIGIN_TYPE(wchar_t)
#if HGL_OS != HGL_OS_Windows
COMPARATOR_ORIGIN_TYPE(u16char)
#endif//HGL_OS != HGL_OS_Windows
COMPARATOR_ORIGIN_TYPE(char32_t)
#undef COMPARATOR_ORIGIN_TYPE
#endif//HGL_COMP_OPERATOR_INCLUDE

View File

@ -6,7 +6,7 @@
namespace hgl namespace hgl
{ {
template<typename SC> class ConstString template<typename SC> class ConstString:public Comparator<ConstString<SC>>
{ {
const SC *str; const SC *str;
int length; int length;
@ -24,18 +24,16 @@ namespace hgl
const SC *GetString()const{return str;} const SC *GetString()const{return str;}
const int GetLength()const{return length;} const int GetLength()const{return length;}
const int Comp(const ConstString &cs)const const int Comp(const ConstString &cs)const override
{ {
if(length<cs.length)return(-1); if(length<cs.length)return(-1);
if(length>cs.length)return( 1); if(length>cs.length)return( 1);
return memcmp(str,cs.str,length); return memcmp(str,cs.str,length);
} }
CompOperator(const ConstString &,Comp)
}; };
template<typename SC> struct ConstStringView template<typename SC> struct ConstStringView:public Comparator<ConstStringView<SC>>
{ {
DataArray<SC> *str_data; DataArray<SC> *str_data;
@ -58,14 +56,12 @@ namespace hgl
return str_data->GetData()+offset; return str_data->GetData()+offset;
} }
int Comp(const ConstStringView<SC> &csv)const const int compare(const ConstStringView<SC> &csv)const override
{ {
if(length!=csv.length)return(length-csv.length); if(length!=csv.length)return(length-csv.length);
return hgl::strcmp(GetString(),csv.GetString(),length); return hgl::strcmp(GetString(),csv.GetString(),length);
} }
CompOperator(const ConstStringView &,Comp)
}; };
template<typename SC> class ConstStringSet template<typename SC> class ConstStringSet

View File

@ -17,7 +17,7 @@ namespace hgl
* *
* Hours,Minutes,Seconds中任何一个值进行加减时11:30使Minutes+=555512:25 * Hours,Minutes,Seconds中任何一个值进行加减时11:30使Minutes+=555512:25
*/ */
class Time ///时间类 class Time:public Comparator<Time> ///时间类
{ {
int32 gmt_off; ///<当前时区与UTC时间的差值 int32 gmt_off; ///<当前时区与UTC时间的差值
@ -79,9 +79,7 @@ namespace hgl
Time &operator = (const Time &); Time &operator = (const Time &);
const int Comp(const Time &)const; const int compare(const Time &)const override;
CompOperator(const Time &,Comp);
void Sync(const double=0); ///<和系统时间同步 void Sync(const double=0); ///<和系统时间同步
@ -94,7 +92,7 @@ namespace hgl
* *
* Year,Month,Day中任意一个值进行修改时1981-4-17,使Day+=4004001982-5-21 * Year,Month,Day中任意一个值进行修改时1981-4-17,使Day+=4004001982-5-21
*/ */
class Date ///日期类 class Date:public Comparator<Date> ///日期类
{ {
int32 year; int32 year;
int8 month; int8 month;
@ -141,9 +139,7 @@ namespace hgl
Date &operator = (const Date &); Date &operator = (const Date &);
const int Comp(const Date &)const; const int compare(const Date &)const override;
CompOperator(const Date &,Comp);
void Sync(const double=0); ///<和系统日期同步 void Sync(const double=0); ///<和系统日期同步

View File

@ -1,4 +1,4 @@
#pragma once #pragma once
#include<hgl/type/ConstStringSet.h> #include<hgl/type/ConstStringSet.h>
#include<typeinfo> #include<typeinfo>
@ -12,7 +12,7 @@ namespace hgl
* ID+<br> * ID+<br>
* *
*/ */
template<typename SC,int CLASS_COUNTER> class OrderedIDName template<typename SC,int CLASS_COUNTER> class OrderedIDName:public Comparator<OrderedIDName<SC,CLASS_COUNTER>>
{ {
public: public:
@ -85,12 +85,7 @@ namespace hgl
public: public:
const int Comp(const OrderedIDName &oin)const{return GetID()-oin.GetID();} const int compare(const OrderedIDName &oin)const override{return GetID()-oin.GetID();}
const int Comp(const OrderedIDName *oin)const{return GetID()-oin->GetID();}
CompOperator(const OrderedIDName &,Comp)
CompOperator(const OrderedIDName *,Comp)
};//class IDName };//class IDName
#define DefineIDName(name,type) using name=OrderedIDName<type,__COUNTER__>; //使用__COUNTER__是为了让typeid()不同 #define DefineIDName(name,type) using name=OrderedIDName<type,__COUNTER__>; //使用__COUNTER__是为了让typeid()不同

View File

@ -11,7 +11,7 @@ namespace hgl
/** /**
* *
*/ */
template<typename T> class String ///字符串基类 template<typename T> class String:public Comparator<String<T>> ///字符串基类
{ {
protected: protected:
@ -1312,8 +1312,10 @@ namespace hgl
#undef BASE_STRING_NUMBER_OPERATOR_ADD #undef BASE_STRING_NUMBER_OPERATOR_ADD
CompOperator(const T *,Comp); const int compare(const SelfClass &str)const override
CompOperator(const SelfClass &,Comp); {
return Comp(str);
}
};//template<typename T> class String };//template<typename T> class String
//这种重载用于value+str的情况 //这种重载用于value+str的情况

View File

@ -90,7 +90,7 @@ namespace hgl
return(*this); return(*this);
} }
const int Time::Comp(const Time &t)const const int Time::compare(const Time &t)const
{ {
if(hours!=t.hours) if(hours!=t.hours)
return hours-t.hours; return hours-t.hours;
@ -220,7 +220,7 @@ namespace hgl
return(*this); return(*this);
} }
const int Date::Comp(const Date &t)const const int Date::compare(const Date &t)const
{ {
if(year!=t.year) if(year!=t.year)
return year-t.year; return year-t.year;