This commit is contained in:
2023-07-28 20:17:09 +08:00
22 changed files with 20912 additions and 327 deletions

26
inc/hgl/util/Crypt.h Normal file
View File

@@ -0,0 +1,26 @@
#ifndef HGL_ALGORITHM_CRYPT_INCLUDE
#define HGL_ALGORITHM_CRYPT_INCLUDE
#include<hgl/type/DataType.h>
namespace hgl //校验/加密算法
{
namespace io
{
class OutputStream;
}//namespace io
namespace crypt
{
void OverflowEncrypt(void *, void *, int, void *, int); ///<溢出加密(轻度加密)
void OverflowDecrypt(void *, void *, int, void *, int); ///<溢出解密
void AesEncrypt(uint8 data[16], uint8 *key, int keysize); ///<AES加密
void AesDecrypt(uint8 data[16], uint8 *key, int keysize); ///<AES解密
void RC4Encrypt(uint8 *data, int datasize, uint8 *key, int keysize); ///<RC4加密
bool base64_encode(io::OutputStream *os,const uchar *input,size_t len);
bool base64_decode(io::OutputStream *os,const uchar *input,size_t len);
}//namespace crypt
}//namespace hgl
#endif//HGL_ALGORITHM_CRYPT_INCLUDE

View File

@@ -0,0 +1,88 @@
#pragma once
#include<hgl/type/StrChar.h>
namespace hgl
{
namespace util
{
/**
* CSV字段拆分工具<br>
* 支持逗号分隔与tab分隔以及使用引号包裹的字符串
*/
template<typename T> class CSVFieldSplite
{
const T *str;
int str_length;
const T *sp;
const T *end;
public:
CSVFieldSplite()
{
str=nullptr;
str_length=0;
sp=nullptr;
end=nullptr;
}
CSVFieldSplite(const T *s,const int length){Start(s,length);}
~CSVFieldSplite()=default;
void Start(const T *s,const int length)
{
str=s;
str_length=length;
sp=str;
end=str+str_length;
}
const T *next_field(int *len)
{
if(!len)return(nullptr);
if(sp>=end)return(nullptr);
if(*sp==','||*sp=='\t')
{
*len=0;
++sp;
return sp;
}
const T *result;
if(*sp=='"')
{
++sp;
const T *ep=hgl::strchr(sp,T('"'));
if(!ep)
return nullptr;
result=sp;
*len=ep-sp;
sp=ep+2;
return result;
}
else
{
result=sp;
const T *ep=sp+1;
while(*ep!=','&&*ep!='\t'&&ep<end)
++ep;
*len=ep-sp;
sp=ep+1;
return result;
}
}
};//class CSVFieldSplite
}//namespace util
}//namespace hgl

View File

@@ -0,0 +1,126 @@
#pragma once
#include<hgl/io/TextOutputStream.h>
#include<initializer_list>
namespace hgl
{
namespace util
{
/**
* CSV输出控制
*/
template<typename T> class CSVOutput
{
io::TextOutputStream *tos;
uint FieldCount;
T FieldsTerminatedChar;
T EnclosedChar;
T NullStringField[2];
protected:
uint write_count;
void NextField()
{
--write_count;
if(!write_count)
{
write_count=FieldCount;
tos->WriteLineEnd();
}
else
tos->WriteChars(&FieldsTerminatedChar,1);
}
public:
CSVOutput( io::TextOutputStream *os, //文本输出流
const uint field_count, //字段数量
const T fields_terminated_char=T(','), //字段分隔符
const T enclosed_char=T('"')) //字符串包裹字符
{
tos=os;
FieldCount =field_count;
FieldsTerminatedChar=fields_terminated_char;
EnclosedChar =enclosed_char;
NullStringField[0] =enclosed_char;
NullStringField[1] =enclosed_char;
write_count =FieldCount;
}
~CSVOutput()=default;
void WriteString(const T *str,const int len)
{
if(!str||!*str||len<=0)
{
tos->WriteChars(NullStringField,2);
}
else
{
tos->WriteChars(&EnclosedChar,1);
tos->WriteChars(str,len);
tos->WriteChars(&EnclosedChar,1);
}
NextField();
}
void WriteString(const String<T> &str)
{
WriteString(str.c_str(),str.Length());
}
void WriteStringList(const StringList<T> &sl)
{
for(const String<T> &str:sl)
WriteString(str);
}
void WriteStringList(const std::initializer_list<String<T>> &sl)
{
for(const String<T> &str:sl)
WriteString(str);
}
void WriteStringList(const std::initializer_list<const T *> &sl)
{
for(const T *str:sl)
WriteString(str);
}
template<typename I>
void WriteInteger(const I &value)
{
tos->WriteString(String<T>::numberOf(value));
NextField();
}
template<typename F>
void WriteFloat(const F &value,const uint frac)
{
tos->WriteString(String<T>::floatOf(value,frac));
NextField();
}
};//class CSVOutput
using UTF8CSVOutput=CSVOutput<u8char>;
using UTF16CSVOutput=CSVOutput<u16char>;
using UTF32CSVOutput=CSVOutput<u32char>;
inline CSVOutput<u8char> * CreateUTF8CSVOutput (io::TextOutputStream * tos,const uint fc,const u8char ftc=U8_TEXT (','),const u8char ec=U8_TEXT ('"')){return(new UTF8CSVOutput (tos,fc,ftc,ec));}
inline CSVOutput<u16char> * CreateUTF16CSVOutput (io::TextOutputStream * tos,const uint fc,const u16char ftc=U16_TEXT(','),const u16char ec=U16_TEXT('"')){return(new UTF16CSVOutput(tos,fc,ftc,ec));}
inline CSVOutput<u16char> * CreateUTF32CSVOutput (io::TextOutputStream * tos,const uint fc,const u32char ftc=U32_TEXT(','),const u32char ec=U32_TEXT('"')){return(new UTF16CSVOutput(tos,fc,ftc,ec));}
}//namespace util
}//namespace hgl

View File

@@ -0,0 +1,63 @@
#pragma once
#include<hgl/util/csv/CSVOutput.h>
namespace hgl
{
namespace io
{
class OutputStream;
class TextOutputStream;
}//namespace io
namespace util
{
template<typename T> class CSVOutputStream
{
io::OutputStream *os;
io::TextOutputStream *tos;
CSVOutput<T> *csv;
public:
CSVOutputStream(io::OutputStream *_os,io::TextOutputStream *_tos,CSVOutput<T> *_csv)
{
os=_os;
tos=_tos;
csv=_csv;
}
~CSVOutputStream()
{
delete csv;
delete tos;
delete os;
}
void WriteString(const T *str,const int len){csv->WriteString(str,len);}
void WriteString(const String<T> &str){csv->WriteString(str);}
void WriteStringList(const StringList<T> &sl){return csv->WriteStringList(sl);}
void WriteStringList(const std::initializer_list<String<T>> &sl){return csv->WriteStringList(sl);}
void WriteStringList(const std::initializer_list<const T *> &sl){return csv->WriteStringList(sl);}
template<typename I>
void WriteInteger(const I &value){csv->WriteInteger(value);}
template<typename F>
void WriteFloat(const F &value,const uint frac){csv->WriteFloat(value,frac);}
};//template<typename T> class CSVOutputStream
template<typename T> inline CSVOutputStream<T> *CreateCSVOutputToStream(io::OutputStream *os,io::TextOutputStream *tos,const uint field_count,const T fields_terminated_char=T(','),const T enclosed_char=T('"'))
{
CSVOutput<T> *csv=new CSVOutput<T>(tos,field_count,fields_terminated_char,enclosed_char);
return(new CSVOutputStream<T>(os,tos,csv));
}
CSVOutputStream<u8char> *CreateCSVOutputToUTF8File(const OSString &filename,const uint field_count,const u8char fields_terminated_char=U8_TEXT(','),const u8char enclosed_char=U8_TEXT('"'));
CSVOutputStream<u16char> *CreateCSVOutputToUTF16LEFile(const OSString &filename,const uint field_count,const u16char fields_terminated_char=U16_TEXT(','),const u16char enclosed_char=U16_TEXT('"'));
}//namespace util
}//namespace hgl

View File

@@ -0,0 +1,62 @@
#pragma once
#include<hgl/io/FileInputStream.h>
#include<hgl/io/TextInputStream.h>
#include<hgl/util/csv/CSVFieldSplite.h>
namespace hgl
{
namespace util
{
template<typename T> class CSVParseCallback
{
public:
virtual bool OnLine(util::CSVFieldSplite<T> &csv)=0;
};
template<typename T> class CSVTextParse:public io::TextInputStream::ParseCallback<T>
{
util::CSVFieldSplite<T> splite;
CSVParseCallback<T> *callback;
public:
CSVTextParse(CSVParseCallback<T> *pcb)
{
callback=pcb;
}
bool OnLine(const T *text,const int length) override
{
if(!text||!*text||length<=0)
return(true);
splite.Start(text,length);
return callback->OnLine(splite);
}
};//class CSVTextParse
template<typename T> inline bool ParseCSV(io::InputStream *is,CSVParseCallback<T> *pcb)
{
io::TextInputStream tis(is);
CSVTextParse<T> parse(pcb);
tis.SetParseCallback<T>(&parse);
return tis.Run()>0;
}
template<typename T> inline bool ParseCSVFile(const OSString &filename,CSVParseCallback<T> *pcb)
{
io::OpenFileInputStream fis(filename);
if(!fis)
return false;
return ParseCSV<T>(fis,pcb);
}
}//namespace util
}//namespace hgl

View File

@@ -6,6 +6,13 @@ namespace hgl
{
namespace util
{
constexpr const uint TGA_IMAGE_TYPE_COLOR_MAP =1;
constexpr const uint TGA_IMAGE_TYPE_TRUE_COLOR =2;
constexpr const uint TGA_IMAGE_TYPE_GRAYSCALE =3;
constexpr const uint TGA_DIRECTION_LOWER_LEFT =0;
constexpr const uint TGA_DIRECTION_UPPER_LEFT =1;
#pragma pack(push,1)
struct TGAHeader
{

View File

@@ -0,0 +1,30 @@
#pragma once
#include<hgl/util/plist/PAttrib.h>
#include<hgl/io/LoadStringList.h>
namespace hgl
{
using namespace io;
/**
* 从文本文件中加载PList
* @param filename 文件名
* @param pa_map 属性列表
* @return 是否加载成功
*/
template<typename C> static int LoadFromTextFile(const OSString &filename,PAttribMap<C> &pa_map)
{
StringList<C> sl;
if(LoadStringListFromTextFile(sl,filename)<=0)
return(false);
int n=sl.GetCount();
while(n--)
Add(pa_map,sl[n]);
return(true);
}
}//namespace hgl

View File

@@ -1,7 +1,7 @@
#pragma once
#include<hgl/type/BaseString.h>
#include<hgl/type/StringList.h>
#include<hgl/type/String.h>
#include<hgl/io/LoadStringList.h>
#include<hgl/type/Map.h>
#include<hgl/io/FileOutputStream.h>
#include<hgl/io/TextOutputStream.h>
@@ -17,21 +17,21 @@ namespace hgl
PAttribBase()=default;
virtual ~PAttribBase()=default;
virtual const bool ParseFromString(const BaseString<C> &str)=0;
virtual BaseString<C> MakeToString()const=0;
virtual const bool ParseFromString(const String<C> &str)=0;
virtual String<C> MakeToString()const=0;
};
template<typename C,typename T> class PAttrib:public PAttribBase<C>
{
protected:
BaseString<C> name;
String<C> name;
T value;
T default_value;
public:
PAttrib(const BaseString<C> &n,const T &v)
PAttrib(const String<C> &n,const T &v)
{
name=n;
value=v;
@@ -40,8 +40,8 @@ namespace hgl
virtual ~PAttrib()=default;
virtual const bool ParseFromString(const BaseString<C> &str)=0;
virtual BaseString<C> MakeToString()const=0;
virtual const bool ParseFromString(const String<C> &str)=0;
virtual String<C> MakeToString()const=0;
virtual const T &Get(){return value;}
virtual void Set(const T &v){value=v;}
@@ -55,7 +55,7 @@ namespace hgl
public:
PNumberAttrib(const BaseString<C> &n,const T &dv,const T &min_v,const T &max_v):PAttrib<C,T>(n,dv)
PNumberAttrib(const String<C> &n,const T &dv,const T &min_v,const T &max_v):PAttrib<C,T>(n,dv)
{
min_value=min_v;
max_value=max_v;
@@ -63,7 +63,7 @@ namespace hgl
virtual ~PNumberAttrib()=default;
const bool ParseFromString(const BaseString<C> &str) override
const bool ParseFromString(const String<C> &str) override
{
if(ToNumber(str,this->value))
{
@@ -75,16 +75,57 @@ namespace hgl
return(false);
}
BaseString<C> MakeToString() const override
String<C> MakeToString() const override
{
return BaseString<C>(this->value);
return String<C>::numberOf(this->value);
}
};//class PNumberAttrib:public PAttrib<C,uint>
template<typename C> using PIntAttrib =PNumberAttrib<C,int >;
template<typename C> using PUintAttrib =PNumberAttrib<C,uint >;
template<typename C> using PFloatAttrib =PNumberAttrib<C,float >;
template<typename C> using PDoubleAttrib=PNumberAttrib<C,double >;
template<typename C,typename T> class PFloatNumberAttrib:public PAttrib<C,T>
{
protected:
T min_value,max_value;
uint frac;
public:
PFloatNumberAttrib(const String<C> &n,const T &dv,const T &min_v,const T &max_v,const int f):PAttrib<C,T>(n,dv)
{
min_value=min_v;
max_value=max_v;
frac=f;
}
virtual ~PFloatNumberAttrib()=default;
const bool ParseFromString(const String<C> &str) override
{
if(ToNumber(str,this->value))
{
if(this->value>=min_value&&this->value<=max_value)
return(true);
}
this->value=this->default_value;
return(false);
}
void SetFrac(uint f){frac=f;}
String<C> MakeToString() const override
{
return String<C>::floatOf(this->value,frac);
}
};//class PFloatNumberAttrib:public PAttrib<C,uint>
template<typename C> using PFloatAttrib =PFloatNumberAttrib<C,float >;
template<typename C> using PDoubleAttrib=PFloatNumberAttrib<C,double >;
template<typename C> class PBoolAttrib:public PAttrib<C,bool>
{
@@ -92,7 +133,7 @@ namespace hgl
using PAttrib<C,bool>::PAttrib;
const bool ParseFromString(const BaseString<C> &str)
const bool ParseFromString(const String<C> &str)
{
if(str.ToBool(this->value))
return(true);
@@ -101,36 +142,87 @@ namespace hgl
return(false);
}
BaseString<C> MakeToString() const override
String<C> MakeToString() const override
{
return(this->value?"true":"false");
}
};
template<typename C> class PStringAttrib:public PAttrib<C,BaseString<C>>
template<typename C> class PStringAttrib:public PAttrib<C,String<C>>
{
public:
using PAttrib<C,BaseString<C>>::PAttrib;
using PAttrib<C,String<C>>::PAttrib;
const bool ParseFromString(const BaseString<C> &str) override
const bool ParseFromString(const String<C> &str) override
{
this->value=str;
return(true);
}
BaseString<C> MakeToString() const override
String<C> MakeToString() const override
{
return this->value;
}
};
template<typename C> using PAttribMap=Map<String<C>,PAttribBase<C> *>;
/**
* 向属性列表中写入一个属性
*/
template<typename C> static bool Add(PAttribMap<C> &pa_map,const String<C> &str)
{
String<C> name;
C *value;
int off;
if(str.Length()<2)return(false);
if(((off=str.FindChar(C('\t')))==-1)
&&((off=str.FindChar(C(' '))) ==-1)
&&((off=str.FindChar(C('='))) ==-1)
&&((off=str.FindChar(C(':'))) ==-1))
return(false);
name.Strcpy(str,off);
off++;
value=str.c_str()+off;
while(true)
{
if(*value == C('\t')
||*value == C('=')
||*value == C(' ')
||*value == C(':'))
{
value++;
continue;
}
break;
}
PAttribBase<C> *attr=GetListObject(pa_map,name);
if(attr)
attr->ParseFromString(value);
return(true);
}
template<typename C> class PAttribSet
{
using PString=BaseString<C>;
using PString=String<C>;
using PStringList=StringList<PString>;
Map<BaseString<C>,PAttribBase<C> *> pa_map;
PAttribMap<C> pa_map;
public:
operator PAttribMap<C> &() {return pa_map;}
operator const PAttribMap<C> &()const {return pa_map;}
public:
@@ -144,6 +236,25 @@ namespace hgl
return obj;
}
/**
* 创建一个浮点数属性
* @param name 属性名称
* @param dv 默认值
* @param min_v 最小值
* @param max_v 最大值
* @param frac 小数点后位数
* @return 属性对象
*/
template<typename T>
PFloatNumberAttrib<C,T> *CreateFloatAttrib(const PString &name,const T &dv,const T &min_v,const T &max_v,const int frac)
{
PFloatNumberAttrib<C,T> *obj=new PFloatNumberAttrib<C,T>(name,dv,min_v,max_v,frac);
pa_map.Add(name,obj);
return obj;
}
PBoolAttrib<C> *CreateBoolAttrib(const PString &name,const bool &dv)
{
PBoolAttrib<C> *obj=new PBoolAttrib<C>(name,dv);
@@ -169,110 +280,22 @@ namespace hgl
return pa_map.Add(name,attr);
}
PAttribBase<C> *Get(const PString &name){return GetObject(pa_map,name);}
bool Add(const PString &str)
{
return Add(pa_name,str);
}
PAttribBase<C> *Get(const PString &name){return GetListObject(pa_map,name);}
void Delete(const PString &name){pa_map.DeleteByKey(name);}
void Clear(){pa_map.Clear();}
void ClearData(){pa_map.ClearData();}
void Enum(void (*enum_func)(const BaseString<C> &key,PAttribBase<C> *value))
void Enum(void (*enum_func)(const String<C> &key,PAttribBase<C> *value))
{
pa_map.Enum(enum_func);
};
public:
/**
* 保存到文本文件中
*/
template<ByteOrderMask BOM>
bool SaveToTextFile(const OSString &filename,const PString &gap_ch=PString("\t")) ///<保存列表到文件
{
FileOutputStream fos;
EndianTextOutputStream<BOM> tos(&fos);
if(!fos.CreateTrunc(filename))return(false);
tos.WriteBOM();
const int count=pa_map.GetCount();
auto **pa_obj=pa_map.GetDataList();
for(int i=0;i<count;i++)
{
tos.WriteString((*pa_obj)->left);
tos.WriteString(gap_ch);
tos.WriteString((*pa_obj)->right->MakeToString());
tos.WriteLineEnd();
++pa_obj;
}
return(true);
}
private:
bool Add(const PString &str) ///<向列表中增加一项
{
PString name;
C *value;
int off;
if(str.Length()<2)return(false);
if(((off=str.FindChar(C('\t')))==-1)
&&((off=str.FindChar(C(' '))) ==-1)
&&((off=str.FindChar(C('='))) ==-1)
&&((off=str.FindChar(C(':'))) ==-1))
return(false);
name.Strcpy(str,off);
off++;
value=str.c_str()+off;
while(true)
{
if(*value == C('\t')
||*value == C('=')
||*value == C(' ')
||*value == C(':'))
{
value++;
continue;
}
break;
}
PAttribBase<C> *attr=Get(name);
if(attr)
attr->ParseFromString(value);
return(true);
}
public:
/**
* 从文本文件中加载
*/
virtual bool LoadFromTextFile(const OSString &filename) ///<从文件中加载列表
{
PStringList sl;
if(LoadStringListFromTextFile(sl,filename)<=0)
return(false);
int n=sl.GetCount();
while(n--)
Add(sl[n]);
return(true);
}
};//template<typename C> class PAttribSet
using UTF8PAttribSet =PAttribSet<char>;

View File

@@ -1,128 +0,0 @@
#ifndef HGL_PLIST_INCLUDE
#define HGL_PLIST_INCLUDE
#include<hgl/type/Map.h>
#include<hgl/type/LoadStringList.h>
#include<hgl/io/FileOutputStream.h>
#include<hgl/io/TextOutputStream.h>
namespace hgl
{
using namespace io;
/**
* 属性列表,类似INI的管理类
*/
template<typename C> class PList:public Map<String<C>,String<C>> ///属性列表
{
public:
using PString=String<C>;
using PStringList=StringList<C>;
using PMap=Map<PString,PString>;
protected:
void ReadData(const PStringList &sl)
{
int n=sl.GetCount();
while(n--)
Add(sl[n]);
}
public:
virtual ~PList()=default;
virtual bool Add(const PString &key) ///<向列表中增加一项
{
PString name;
C *value;
int off;
if(key.Length()<2)return(false);
if(((off=key.FindChar(C('\t')))==-1)
&&((off=key.FindChar(C(' ' )))==-1)
&&((off=key.FindChar(C('=' )))==-1)
&&((off=key.FindChar(C(':' )))==-1))
return(false);
name.Strcpy(key,off);
off++;
value=key.c_str()+off;
while(true)
{
if(*value == C('\t')
||*value == C('=')
||*value == C(' ')
||*value == C(':'))
value++;
else
{
PString str=value;
PMap::Add(name,str);
return(true);
}
}
}//bool PList::Add
virtual bool Add(const PString &key,const PString &value) ///<向列表中增加一项
{
return PMap::Add(key,value);
}
/**
* 从文本文件中加载
*/
virtual bool LoadFromTextFile(const OSString &filename,const CharSet &cs=OSCharSet) ///<从文件中加载列表
{
PStringList sl;
if(LoadStringListFromTextFile(sl,filename,cs)<=0)
return(false);
ReadData(sl);
return(true);
}
/**
* 保存到文本文件中
*/
template<ByteOrderMask BOM>
bool SaveToTextFile(const OSString &filename,const C &gap_ch='\t') ///<保存列表到文件
{
FileOutputStream fos;
EndianTextOutputStream<BOM> tos(&fos);
if(!fos.CreateTrunc(filename))return(false);
PString gap_str=PString::charOf(gap_ch);
int n=this->data_list.GetCount();
tos.WriteBOM();
while(n--)
{
PString f,s;
if(GetBySerial(n,f,s))
tos.WriteLine(f+gap_str+s);
}
return(true);
}
};//class PList
using UTF8PList =PList<char >;
using UTF16PList=PList<u16char>;
using WidePList =PList<wchar_t>;
using OSPList =PList<os_char>;
}//namespace hgl
#endif//HGL_PLIST_INCLUDE

View File

@@ -0,0 +1,42 @@
#pragma once
#include<hgl/util/plist/PAttrib.h>
#include<hgl/io/FileOutputStream.h>
#include<hgl/io/TextOutputStream.h>
namespace hgl
{
using namespace io;
/**
* 保存PList到文本文件中
* @param filename 文件名
* @param pa_map 属性列表
* @param gap_ch 分隔符
* @return 保存的属性数量
*/
template<typename C,ByteOrderMask BOM> static int SaveToTextFile(const OSString &filename,const PAttribMap<C> &pa_map,const String<C> &gap_ch=String<C>("\t"))
{
FileOutputStream fos;
EndianTextOutputStream<BOM> tos(&fos);
if(!fos.CreateTrunc(filename))return(-1);
tos.WriteBOM();
const int count=pa_map.GetCount();
auto **pa_obj=pa_map.GetDataList();
for(int i=0;i<count;i++)
{
tos.WriteString((*pa_obj)->key);
tos.WriteString(gap_ch);
tos.WriteString((*pa_obj)->value->MakeToString());
tos.WriteLineEnd();
++pa_obj;
}
return(count);
}
}//namespace hgl

View File

@@ -1,7 +1,6 @@
#ifndef HGL_ALGORITHM_SORT_INCLUDE
#define HGL_ALGORITHM_SORT_INCLUDE
#include<hgl/TypeFunc.h>
#include<hgl/CompOperator.h>
#include<hgl/type/List.h>
#include<string.h>
@@ -70,103 +69,102 @@ public:
virtual bool sort()=0; //排序
};//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;
T temp;
SortBase<T>::cpy(&temp,SortBase<T>::buffer+i);
j=2*(i+1)-1;
while(j<=n)
{
int j;
T temp;
if((j<n)&&(SortBase<T>::compare_by_index(j,j+1)<0))j++;
SortBase<T>::cpy(&temp,SortBase<T>::buffer+i);
j=2*(i+1)-1;
while(j<=n)
if(SortBase<T>::compare(temp,SortBase<T>::buffer[j])<0)
{
if((j<n)&&(SortBase<T>::compare_by_index(j,j+1)<0))j++;
if(SortBase<T>::compare(temp,SortBase<T>::buffer[j])<0)
{
SortBase<T>::cpy_by_index(i,j);
i=j;
j=2*(i+1)-1;
}
else j=n+1;
SortBase<T>::cpy_by_index(i,j);
i=j;
j=2*(i+1)-1;
}
SortBase<T>::cpy(SortBase<T>::buffer+i,&temp);
else j=n+1;
}
public:
/**
* 本类构造函数
* @param buf 数据缓冲区
* @param n 数据个数
* @param c 数据大小比较类
*/
HeapSort(T *buf,int n,Comparator<T> *c=new Comparator<T>()):SortBase<T>(buf,n,c)
{
}
bool sort()
{
if(!SortBase<T>::buffer||SortBase<T>::number<2||!SortBase<T>::comp)
return(false);
int i;
int mm=SortBase<T>::number>>1;
for(i=mm-1;i>=0;i--)
isift(i,SortBase<T>::number-1);
for(i=SortBase<T>::number-1;i>=1;i--)
{
SortBase<T>::exchane_by_index(0,i);
isift(0,i-1);
}
return(true);
}
};//class HeapSort:public SortBase<T>
template<typename T>
bool Sort(T *data,int count,Comparator<T> *comp=new Comparator<T>())
{
HeapSort<T> hs(data,count,comp);
return hs.sort();
SortBase<T>::cpy(SortBase<T>::buffer+i,&temp);
}
template<typename T>
bool Sort(hgl::List<T> &list,Comparator<T> *comp=Comparator<T>())
public:
/**
* 本类构造函数
* @param buf 数据缓冲区
* @param n 数据个数
* @param c 数据大小比较类
*/
HeapSort(T *buf,int n,Comparator<T> *c=new Comparator<T>()):SortBase<T>(buf,n,c)
{
return Sort(list.GetData(),
list.GetCount(),
comp);
}
bool sort()
{
if(!SortBase<T>::buffer||SortBase<T>::number<2||!SortBase<T>::comp)
return(false);
int i;
int mm=SortBase<T>::number>>1;
for(i=mm-1;i>=0;i--)
isift(i,SortBase<T>::number-1);
for(i=SortBase<T>::number-1;i>=1;i--)
{
SortBase<T>::exchane_by_index(0,i);
isift(0,i-1);
}
return(true);
}
};//class HeapSort:public SortBase<T>
template<typename T>
bool Sort(T *data,int count,Comparator<T> *comp=new Comparator<T>())
{
HeapSort<T> hs(data,count,comp);
return hs.sort();
}
template<typename T>
bool Sort(hgl::DataArray<T> &list,Comparator<T> *comp=Comparator<T>())
{
return Sort(list.GetData(),
list.GetCount(),
comp);
}
/*
//仅实现模拟虚拟成员函数即可,无需整个类重载
template<> int Comparator<BagCell>::compare(const BagCell &it1,const BagCell &it2) const
{
int r=it1.GetItemID()-it2.GetItemID();
//仅实现模拟虚拟成员函数即可,无需整个类重载
template<> int Comparator<BagCell>::compare(const BagCell &it1,const BagCell &it2) const
{
int r=it1.GetItemID()-it2.GetItemID();
if(r!=0)
return r;
if(r!=0)
return r;
return it1.count-it2.count;
}
return it1.count-it2.count;
}
void BagManage::Sort()
{
Comparator<BagCell> comp_baginfo;
void BagManage::Sort()
{
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);
}
*/
#endif//HGL_ALGORITHM_SORT_INCLUDE