int pos;表示文件号
#define HEAP_CONTAIN 150
#define ORDER_TYPE ushort
#define NIL 65535
typedef struct wrap_data
{
ORDER_TYPE data;
int pos;
}wrap_data;
wrap_data min_heap[HEAP_CONTAIN]={0};
int HEAP_COUNT=1;
int left(int i)
{
return 2*i;
}
int right(int i)
{
return 2*i+1;
}
void insert(wrap_data point_a)
//void insert(int a)
{
wrap_data temp;
int pos_temp;
int p, num;
min_heap[HEAP_COUNT]=point_a;
num=HEAP_COUNT;
while(num/2)
{
p=num/2;
if(min_heap[num].data
temp=min_heap[num];min_heap[num]=min_heap[p];min_heap[p]=temp;
}
else
{
break;
}
num=p;
}
HEAP_COUNT++;
if(HEAP_COUNT>HEAP_CONTAIN)HEAP_COUNT=HEAP_CONTAIN;
}
wrap_data pop()
{
wrap_data temp,result=min_heap[1];
int exchange;
int label=1;
min_heap[label]=min_heap[HEAP_COUNT-1];
while(left(label)<=HEAP_COUNT-1)
{
if(right(label)<=HEAP_COUNT-1)
{
if(min_heap [left (label)].data <
min_heap [right (label)].data )
exchange=left (label) ;
else
exchange= right (label);
}
else
exchange=left (label) ;
if(min_heap[exchange].data<=
min_heap[label].data)
{
temp=min_heap[exchange];min_heap[exchange]=min_heap[label];min_heap[label]=temp;
}
else
break;
label=exchange;
}
HEAP_COUNT--;
return result;
}
int empty()
{
return !(HEAP_COUNT-1);
}
bool SplitProc::merge_order(int *array, int file_count)
{
char tmpBuf[1024] = {'\0'};
// MBFile *fmerge_orderfile_all=(MBFile *) calloc ( file_count ,sizeof (MBFile));
MBFile fmerge_orderfile_all[200];
for(int i=0;i
sprintf(tmpBuf,"%s/merge_order.dat.%d",m_pathOfOutput.c_str(), i);
fmerge_orderfile_all[i].fopen(tmpBuf,"rb");
}
MBFile fmerge_order_file;
sprintf(tmpBuf,"%s/merge_order.dat",m_pathOfOutput.c_str());
if( !fmerge_order_file.fopen(tmpBuf,"wb"))
{
return -1;
}
MBFile fmerge_data_file;
sprintf(tmpBuf,"%s/merge_data.dat",m_pathOfOutput.c_str());
if( !fmerge_data_file.fopen(tmpBuf,"wb"))
{
return -1;
}
// wrap_data *result;
wrap_data result;
int i=0,j=0;
// wrap_data a[PATH]={0};
// wrap_data *merge_heap=(wrap_data *)calloc ( file_count ,sizeof (wrap_data));
ushort value;
for(i=0;i
fmerge_orderfile_all[i].fread(&value ,sizeof (ushort),1);
result.pos=i;
result.data=value;
insert( result);
}
while(!empty())
{
result=pop();
ushort fileno=(ushort)result.pos;
printf("{%d %d} ",result.data,fileno);
if ( result.data==NIL)
{
break;
}
else
{
fmerge_order_file.fwrite( & fileno ,sizeof (ushort),1);
fmerge_data_file.fwrite( & result.data ,sizeof (ushort),1);
if(fmerge_orderfile_all[fileno].fread(&value ,sizeof (ushort),1)!=1)
{
result.pos=fileno;
result.data=NIL;
}
else
{
result.pos=fileno;
result.data=value;
}
insert( result);
}
}
for(int i=0;i
fmerge_orderfile_all[i].fclose();
}
fmerge_data_file.fclose();
fmerge_order_file.fclose();
// free (fmerge_orderfile_all);
}