内存限制,数字重复出现位图排序,每个数字限定最多出现10次
#include<iostream>
#include<fstream>
#include<string>
#include"time.h"
using namespace std;
#define N 10000
#define NGROUP 5//一个数需要保存两个信息,1bit数值信息,4bit个数信息
#define BITSTEP 32
#define MASK 0x1F
#define shift 5
#define shiftnum 3//4bit保存一个数字出现的次数
#define MEMORY 100//内存限制
#define N_S (MEMORY*8/NGROUP) //按照数字范围处理,每趟能处理的数字范围,即每趟能处理多少个数
#define COUNT (N/N_S+1) //需要多少趟
//#define COUNT(N,MEMORY) ((N)/((MEMORY) * 8) + 1)
//#define NNUM_S (NNUM/COUNT)
int bit[N_S/BITSTEP+1];//保存数字
int bitnum[N_S*4/BITSTEP+1];//保存数字出现的次数
/****************
数字没出现一次计数加1,当出现次数超过15次时,报错
*/
void add(int i)
{
int tmp=bitnum[i>>shiftnum]&(0xF<<((i&0x7)*4));
tmp=tmp>>(((i&0x7)*4));
if(tmp==0xF)
{
cerr<<"num over flow: "<<i<<endl;
exit(1);
}
bitnum[i>>shiftnum]+=(1<<((i&0x7)*4));
cout<<"num "<<i<<"count "<<bitnum[i>>shiftnum]<<endl;
}
/************************
返回对应数字i出现的次数
*/
int num(int i)
{
int tmp=bitnum[i>>shiftnum]&(0xF<<((i&0x7)*4));
return tmp>>((i&0x7)*4);
}
void clr(int i,int * bit_temp)
{
bit_temp[i>>shift]&=~(1<<(i&MASK));
}
void set(int i)
{
bit[i>>shift]|=(1<<(i&MASK));
}
int test(int i)
{
int tmp=bit[i>>shift]&(1<<(i&MASK));
return tmp>>(i&MASK);
}
int main(void)
{
string ifile,ofile;
ifstream infile;
ofstream outfile;
int a;
cin>>ifile;
cin>>ofile;
//cout<<COUNT(N,MEMORY)<<endl;
//cout<<(N/2)<<endl;
// NN=N/COUNT;
cout<<COUNT<<endl;
time_t str_time=time(&str_time);
char * str=ctime(&str_time);
//cout<<"str_time"<<str<<endl;
infile.open(ifile.c_str());
outfile.open(ofile.c_str());
if(!infile)
{
cerr<<"error: can not open file :"<<ifile<<endl;
return -1;
}
if(!outfile)
{
cerr<<"can not open outfile :"<<ofile<<endl;
return -1;
}
for(int k=0;k<COUNT;k++)
{
cout<<"round "<<k<<endl;
infile.clear();//清掉EOF
infile.seekg(0,ios::beg);//回到文件头
outfile.seekp(0,ios::cur);
for(int i=0;i<N_S;i++){ //位图向量与位图计数向量均清零
// infile<<i<<endl;
clr(i,bit);
}
for(i=0;i<N_S*4;i++)
{
clr(i,bitnum);
}
while(infile>>a)
{
int count_num=0;
//cout<<a<<endl;
if(a>=k*N_S&&a<N_S+k*N_S){
cout<<"num "<<a<<endl;
//cout<<a<<endl;
a=a-N_S*k;
// cout<<a<<endl;
set(a);
add(a);
}
// cout<<bit[a>>shift]<<endl;
}
time_t net_time=time(&net_time);
char * net=ctime(&net_time);
double difnet=difftime(str_time,net_time);
//cout<<"round"<<k<<endl;
//cout<<"net_time"<<net<<difnet<<endl;
for(i=0;i<N_S;i++)
{
int num_tmp=num(i);
cout<<"test!!!num "<<i<<"num_count "<<num_tmp<<endl;
for(int j=0;j<num_tmp;j++)
{
if(test(i)==1)
//cout<<test(i)<<endl;
outfile<<i+k*N_S<<endl;
}
}
}
time_t end_time=time(&end_time);
char * end=ctime(&end_time);
double dif=difftime(str_time,end_time);
//double end_time=_getsystime(&t);
// cout<<"end_time"<<end<<dif<<endl;
}