adjusted folder of examples.

This commit is contained in:
2023-07-21 11:11:24 +08:00
parent d76f259125
commit 34cc1c1b46
16 changed files with 79 additions and 46 deletions

135
datatype/CollectionTest.cpp Normal file
View File

@@ -0,0 +1,135 @@
#include<hgl/type/Collection.h>
//#include<hgl/type/List.h>
#include<iostream>
using namespace hgl;
using namespace std;
using TEST_TYPE =char;
using MyEnumerator=ElementEnumerator<TEST_TYPE>;
using MyCheck =CheckElementEqual<TEST_TYPE>;
constexpr size_t UNIT_BYTES =sizeof(TEST_TYPE);
void out(const Collection &c)
{
MyEnumerator me(&c);
#if 0
const TEST_TYPE *str=me.begin();
const size_t count=me.size();
for(size_t i=0;i<count;i++)
{
std::cout<<*str<<' ';
++str;
}
#else
for(const TEST_TYPE &value:me)
std::cout<<value<<" ";
#endif//
std::cout<<std::endl;
}
void out3(const Collection &c)
{
const char *str=(char *)c.begin();
const size_t count=c.GetCount();
for(size_t i=0;i<count;i++)
{
std::cout<<str[0]<<str[1]<<str[2]<<' ';
str+=3;
}
std::cout<<std::endl;
}
void main(int,char **)
{
std::cout<<"test one byte Collection."<<std::endl<<std::endl;
{
Collection cu(UNIT_BYTES);
for(TEST_TYPE i=0;i<10;i++)
{
cu.AddValue<TEST_TYPE>('A'+i);
out(cu);
}
std::cout<<"insert 'A' data at first."<<std::endl;
cu.Insert(0,"A");
out(cu);
std::cout<<"indexOf(E) is "<<cu.indexOfValue('E')<<std::endl;
std::cout<<"remove second data."<<std::endl;
cu.RemoveAt(1);out(cu);
std::cout<<"remove all 'A'"<<std::endl;
cu.RemoveValue('A');out(cu);
std::cout<<"remove 3 items from third"<<std::endl;
cu.RemoveAt(2,3);out(cu);
{
Collection del_cu(UNIT_BYTES);
CheckElementEqual<TEST_TYPE> cee;
del_cu.AddValue<TEST_TYPE>('I');
del_cu.AddValue<TEST_TYPE>('F');
std::cout<<"remove all 'I' and 'F'"<<std::endl;
cu.RemoveCollection(del_cu,&cee);out(cu);
}
std::cout<<"exchanged two elements, they at 0 and 2"<<std::endl;
cu.Exchange(0,2);out(cu);
}
std::cout<<std::endl;
std::cout<<"test three bytes Collection."<<std::endl<<std::endl;
{
Collection cu3(3);
for(TEST_TYPE i=0;i<10;i++)
{
char data[3];
data[0]='A'+i;
data[1]='a'+i;
data[2]='0'+i;
cu3.Add(data);
out3(cu3);
}
cu3.Insert(0,0);out3(cu3);
std::cout<<"indexOf(Ee4) is "<<cu3.indexOf("Ee4")<<std::endl;
std::cout<<"remove second data."<<std::endl;
cu3.RemoveAt(1);out3(cu3);
std::cout<<"remove all 'Aa0'"<<std::endl;
cu3.Remove("Aa0");out3(cu3);
std::cout<<"remove 3 items from third"<<std::endl;
cu3.RemoveAt(2,3);out3(cu3);
{
Collection del_cu(3);
CheckElementMemcmp cee(3);
del_cu.Add("Ii8");
del_cu.Add("Ff5");
std::cout<<"remove all 'Ii8' and 'Ff5'"<<std::endl;
cu3.RemoveCollection(del_cu,&cee);out3(cu3);
}
std::cout<<"exchanged two elements, they at 0 and 2"<<std::endl;
cu3.Exchange(0,2);out3(cu3);
}
}

View File

@@ -0,0 +1,94 @@
/**
* DataArray测试
*
* DataArray是List模板的数据储存类它也提供一些通用的简单处理。
*/
#include<hgl/type/DataArray.h>
#include<iostream>
#include<iomanip>
using namespace hgl;
DataArray<int> da_list;
void restart()
{
da_list.SetCount(10); //设定里面有10个值
for(int i=0;i<10;i++)
da_list[i]=i; //设置值
}
void output(const char *str)
{
const int count=da_list.GetCount();
std::cout<<std::setw(16)<<str<<"["<<std::setw(2)<<count<<"] ";
const int *p=da_list.data();
for(int i=0;i<count;i++)
{
if(i>0)
std::cout<<","<<*p;
else
std::cout<<*p;
++p;
}
std::cout<<std::endl;
restart();
}
int main(int,char **)
{
restart();
output("origin ");
da_list.Delete(0,3); //删除从0号位置开始的3个值
output("delete(0,3)");
da_list.Delete(0,5); //删除从0号位置开始的5个值
output("delete(0,5)");
da_list.Delete(3,2); //删除从3号位置开始的2个值
output("delete(3,2)");
da_list.Delete(5,3); //删除从5号位置开始的3个值
output("delete(5,3)");
da_list.Delete(5,5); //删除从5号位置开始的5个值
output("delete(5,5)");
da_list.DeleteMove(0,3); //删除从0号位置开始的3个值
output("delete move(0,3)");
da_list.Move(10,0,2); //从0号位置开始的2个值移动到10号位置
output("move(10,0,2)");
da_list.Move(5,0,2); //从0号位置开始的2个值移动到5号位置
output("move(5,0,2)");
da_list.Move(0,7,3); //从7号位置开始的3个值移动到0号位置
output("move(0,7,3)");
da_list.Move(2,7,3); //从7号位置开始的3个值移动到2号位置
output("move(2,7,3)");
da_list.Move(0,5,3); //从5号位置开始的3个值移动到0号位置
output("move(0,5,3)");
da_list.Move(10,5,3); //从5号位置开始的3个值移动到10号位置
output("move(10,5,3)");
da_list.Move(2,5,3); //从5号位置开始的3个值移动到2号位置
output("move(2,5,3)");
da_list.Move(7,2,3); //从2号位置开始的3个值移动到7号位置
output("move(7,2,3)");
return(0);
}

View File

@@ -0,0 +1,81 @@
#include<hgl/math/HalfFloat.h>
#include<iostream>
#include<cstdlib>
#include<cmath>
using namespace hgl;
using namespace std;
static float origin_float[4];
void InitFloat4()
{
//随机产生4个f32浮点数
{
srand(time(nullptr));
uint8 *p=(uint8 *)origin_float;
for(uint i=0;i<16;i++)
p[i]=rand()%0xFF;
}
for(uint i=0;i<4;i++)
{
origin_float[i]=abs(origin_float[i]);
}
}
void OutputFloat4(const char *hint,const float *f)
{
cout<<hint<<" float4: "<<f[0]<<","<<f[1]<<","<<f[2]<<","<<f[3]<<endl;
}
void OutputHalfFloat(const char *hint,const half_float *hf)
{
cout<<hint<<" half_float: "<<hf[0]<<","<<hf[1]<<","<<hf[2]<<","<<hf[3]<<endl;
}
void half_float_test()
{
OutputFloat4("origin",origin_float);
half_float hf_fast[4];
half_float hf_std[4];
float_to_half(hf_fast,origin_float,4);
Float32toFloat16(hf_std,origin_float,4);
for(uint i=0;i<4;i++)
hf_std[i]&=0x7FFF; //去掉符号位
OutputHalfFloat("fast",hf_fast);
OutputHalfFloat("std ",hf_std);
}
void split_float_test()
{
bool sign_bit;
uint exponent;
uint mantissa;
float result;
for(uint i=0;i<4;i++)
{
SplitFloat32(sign_bit,exponent,mantissa,origin_float[i]);
result=MergeFloat32(sign_bit,exponent,mantissa);
cout<<"origin: "<<origin_float[i]<<" result: "<<result<<" sign_bit:"<<sign_bit<<" exponent:"<<exponent<<" mantissa:"<<mantissa<<endl;
}
}
void main(int,char **)
{
InitFloat4();
half_float_test();
split_float_test();
}

View File

@@ -0,0 +1,12 @@
template<typename KEY,typename VALUE>
struct DataPair
{
KEY key;
VALUE value;
};
template<typename KEY,typename VALUE>
class UnorderedMap
{
ObjectList<DataPair<KEY,VALUE>> data_list;
};

View File

@@ -0,0 +1,8 @@
struct Person
{
char name[128];
bool sex;
int age;
};
template<typename T> Table

34
datatype/Size2Test.cpp Normal file
View File

@@ -0,0 +1,34 @@
#include<hgl/type/Size2.h>
#include<iostream>
using namespace hgl;
using namespace std;
Size2i si_screen,si_canvas,
si_finish,si_gap;
void output(const char *str,float scale)
{
cout<<endl<<str<<" scale "<<scale<<endl;
si_finish=si_canvas*scale;
cout<<" finish size: "<<si_finish.width<<","<<si_finish.height<<endl;
//si_gap=(si_finish-si_screen)/2;
si_gap=(si_canvas*scale-si_screen)/2; //写这行而不是用上一行,是为了验证操作符重载的正确性
cout<<" gap size: "<<si_gap.width<<","<<si_gap.height<<endl;
}
void main()
{
si_screen.Set(1920,1200);
si_canvas.Set(1280,720);
cout<<"screen size: "<<si_screen.width<<","<<si_screen.height<<endl;
cout<<"canvas size: "<<si_canvas.width<<","<<si_canvas.height<<endl;
output("min",si_screen.alcScale(si_canvas,false));
output("max",si_screen.alcScale(si_canvas,true));
}

View File

@@ -0,0 +1,95 @@
#include<hgl/type/SplitString.h>
#include<hgl/type/MergeString.h>
#include<iostream>
using namespace hgl;
using namespace std;
void Output(UTF8StringList &sl,const char *str)
{
cout<<str<<": ";
for(int i=0;i<sl.GetCount();i++)
cout<<"\""<<sl[i].c_str()<<"\" ";
cout<<endl;
sl.Clear();
}
int main(int,char **)
{
UTF8StringList sl;
UTF8String str="hello game world!";
{
UTF8String left,right;
SpliteByString(str, UTF8String("game"), &left, &right);
cout<<"SpliteByString"<<endl;
cout << "left: \"" << left.c_str() <<"\"" << endl;
cout << "right: \""<<right.c_str() <<"\"" << endl;
}
{
SplitToStringListBySpace(sl,str);
Output(sl,"SplitToStringListBySpace");
}
{
SplitToStringListByChar(sl,str,' ');
Output(sl,"SplitToStringListByChar");
}
{
str="Hello\rgame,\nhello\r\nworld!";
SplitToStringListByEnter(sl,str);
Output(sl,"SplitToStringListByEnter");
}
{
str="Hello, Game. World!";
SplitToStringListByCodes(sl,str);
Output(sl,"SplitToStringListByCodes");
}
{
str="123,456 789-000";
SplitToStringListByDigit(sl,str);
Output(sl,"SplitToStringListByDigit");
}
{
str="0x123,0x456 0x789-0x000";
SplitToStringListByXDigit(sl,str);
Output(sl,"SplitToStringListByXDigit");
}
{
str="123.456 789 0.123 3.1415 .5";
SplitToStringListByFloat(sl,str);
Output(sl,"SplitToStringListByFloat");
}
{
str="#include<hello.h>\n"
"#include\"world.h\"\n";
SplitToStringListByChars(sl,str,UTF8String("<>\"\n"));
Output(sl,"SplitToStringListByChars");
}
}

View File

@@ -0,0 +1,4 @@
#include"RAM_TestClass.h"
HGL_RUNTIME_ASSET_DECLARATION(InstanceID,Instance)
HGL_RUNTIME_ASSET_DECLARATION(PhysicalDeviceID,PhysicalDevice)

View File

@@ -0,0 +1,55 @@
#pragma once
#include"RuntimeAssetManager.h"
#include<hgl/type/String.h>
#include<hgl/type/SortedSets.h>
#include<iostream>
using namespace hgl;
using InstanceID =uint64;
using PhysicalDeviceID =uint64;
struct Instance:public RuntimeAsset<InstanceID,Instance>
{
SortedSets<PhysicalDeviceID> physical_devices;
public:
using RuntimeAsset::RuntimeAsset;
~Instance()
{
std::cout<<"~Instance("<<GetID()<<")"<<std::endl;
}
void AddPhysicalDevice(PhysicalDeviceID pd_id)
{
physical_devices.Add(pd_id);
}
const SortedSets<PhysicalDeviceID> &GetPhysicalDevices()const{return physical_devices;}
};
struct PhysicalDevice:public RuntimeAsset<PhysicalDeviceID,PhysicalDevice>
{
InstanceID inst_id;
AnsiString device_name;
public:
using RuntimeAsset::RuntimeAsset;
~PhysicalDevice()
{
std::cout<<"~PhysicalDevice("<<GetID()<<")"<<std::endl;
}
bool Init(const AnsiString &name,const InstanceID &iid)
{
device_name=name;
inst_id=iid;
return(true);
}
};

View File

@@ -0,0 +1,92 @@
#pragma once
#include<hgl/type/ObjectManage.h>
using namespace hgl;
template<typename K,typename V> struct RuntimeAssetManager:public ObjectManage<K,V>
{
public:
bool Add(V *v)
{
if(!v)return(false);
return ObjectManage<K,V>::Add(v->GetID(),v);
}
};
template<typename K,typename V> struct RuntimeAsset
{
public:
using RAMClass=RuntimeAssetManager<K,V>;
private:
static RuntimeAssetManager<K,V> RAM;
private:
K RuntimeAssetID;
public:
const K GetID()const{return RuntimeAssetID;}
public:
RuntimeAsset(K id)
{
RuntimeAssetID=id;
}
virtual ~RuntimeAsset()=default;
bool Init()
{
return(true);
}
public:
static uint GetInstanceCount()
{
return RAM.GetCount();
}
static RuntimeAssetManager<K,V> &GetRAM()
{
return RAM;
}
template<typename ...ARGS>
static V *CreateInstance(const K &id,ARGS...args)
{
V *obj=new V(id);
if(!obj)return(nullptr);
if(!obj->Init(args...))
{
delete obj;
return(nullptr);
}
RAM.Add(obj);
return obj;
}
static V *GetInstance(const K &id)
{
return RAM.Get(id);
}
void ReleaseInstance()
{
RAM.Release(RuntimeAssetID);
delete this;
}
};
#define HGL_RUNTIME_ASSET_DECLARATION(RA_ID_TYPE,RA_CLASS) RuntimeAssetManager<RA_ID_TYPE,RA_CLASS> RuntimeAsset<RA_ID_TYPE,RA_CLASS>::RAM;

View File

@@ -0,0 +1,61 @@
#include<iostream>
#include<iomanip>
#include<random>
#include"RAM_TestClass.h"
using namespace hgl;
void CreateTestObject()
{
srand(time(nullptr));
uint32_t ic=rand()%10+1;
uint32_t pc=rand()%10+1;
std::cout<<"Instance Count: "<<ic<<std::endl;
std::cout<<"PhysicalDevice Count: "<<pc<<std::endl;
Instance **ii=new Instance *[ic];
PhysicalDevice **pd=new PhysicalDevice *[pc];
for(uint32_t i=0;i<ic;i++)
ii[i]=Instance::CreateInstance(i);
for(uint32_t i=0;i<pc;i++)
{
uint32_t iid=rand()%ic;
PhysicalDevice *pd=PhysicalDevice::CreateInstance(i,"PD"+AnsiString::numberOf(i),iid);
ii[iid]->AddPhysicalDevice(pd->GetID());
}
delete[] pd;
delete[] ii;
}
void OutputTestObject()
{
const auto &ram=Instance::GetRAM();
for(int i=0;i<ram.GetCount();i++)
{
Instance *inst=Instance::GetInstance(i);
const auto &pd_set=inst->GetPhysicalDevices();
std::cout<<"Instance "<<inst->GetID()<<" have "<<pd_set.GetCount()<<" physical device."<<std::endl;
for(const auto &pd_id:pd_set)
{
PhysicalDevice *pd=PhysicalDevice::GetInstance(pd_id);
std::cout<<" PhysicalDevice "<<pd->GetID()<<" name: "<<pd->device_name.c_str()<<std::endl;
}
}
}
void main()
{
CreateTestObject();
OutputTestObject();
}