CMExamples/datatype/DataChainTest2.cpp

130 lines
3.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include<hgl/type/DataChain.h>
#include<hgl/type/List.h>
#include<iostream>
#include<iomanip>
#include<random>
using namespace std;
using namespace hgl;
void out_data_chain(DataChain *dc)
{
DataChain::ChainNode *start =dc->GetStartNode();
DataChain::ChainNode *end =dc->GetEndNode();
DataChain::ChainNode *node =start;
cout<<"Data Chain: ";
while(node)
{
cout<<"["<<setw(3)<<node->start<<","<<setw(3)<<node->count<<"]";
if(node==end)
break;
node=node->next;
}
cout<<endl;
}
int os_main(int,os_char **)
{
constexpr const int BLOCK_SIZE=100;
DataChain dc(BLOCK_SIZE); ///数据链管理器
cout<<"DataChain Test"<<endl;
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<int> dis(1,BLOCK_SIZE);
uniform_int_distribution<int> dis_un_count(BLOCK_SIZE/10,BLOCK_SIZE/4);
uniform_int_distribution<int> dis_block_count(1,BLOCK_SIZE/10);
List<DataChain::UserNode *> user_node_list;
int free_count=dc.GetFreeCount();
do
{
int ubc=hgl_min(dis_block_count(gen),free_count);
DataChain::UserNode *un=dc.Acquire(ubc);
if(!un) //失败了
{
cout<<"Acquire UserNode Error! FreeCount "<<free_count<<endl;
break;
}
else
{
free_count-=ubc;
cout<<"Acquire UserNode: ["<<setw(3)<<un->GetStart()<<","<<setw(3)<<un->GetCount()<<"] ";
}
out_data_chain(&dc);
user_node_list.Add(un);
}while(free_count>0);
bernoulli_distribution dis_op(0.5); //使用伯努利分布随机数产生操作类型50%的概率是Acquire50%的概率是Release
const int top_op_count=dis_un_count(gen); //最上层操作次数
int op_run_count=0; //执行成功的次数
do
{
bool op=dis_op(gen); //这次是acquire还是release
int op_count=dis_un_count(gen); //操作数量
if(op) //acquire
{
op_count=hgl_min(op_count,dc.GetFreeCount());
if(op_count<=0)
continue;
DataChain::UserNode *un=dc.Acquire(op_count);
if(un)
{
cout<<"Acquire ["<<setw(3)<<un->GetStart()<<","<<setw(3)<<un->GetCount()<<"] ";
user_node_list.Add(un);
}
else
{
cout<<"Acquire "<<op_count<<" blocks failed!"<<endl;
continue;
}
}
else //release
{
int pos=dis(gen)%user_node_list.GetCount();
DataChain::UserNode *un;
user_node_list.Get(pos,un);
if(dc.Release(un))
{
cout<<"Release ["<<setw(3)<<un->GetStart()<<","<<setw(3)<<un->GetCount()<<"] ";
user_node_list.Delete(pos);
++op_run_count;
}
else
{
cout<<"Release UserNode "<<pos<<" failed!"<<endl;
continue;
}
}
out_data_chain(&dc);
++op_run_count;
}while(op_run_count<top_op_count); //执行成功次数满了才能退出,失败的次数不算
return 0;
}