用败者树实现求共同的元素(在归并程序上加上筛子)

来源:互联网 发布:js如何产生随机数 编辑:程序博客网 时间:2024/06/11 15:54

 0  1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  999999999 
1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  999999999 
4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  999999999 
9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  999999999 
 9   10   11   12   13   14   15   16   17   18 

 

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

#include  <stdio.h>
#include <stdlib.h>

typedef  struct  wrap_data
{
        int  offset;
        int  path;
        int  *data;
}wrap_data;


int choosevec(int path)
{
        if(path<=4)
        {
                return 4;
        }
        else if (path<=8)
        {
                return 8;
        }
        else if(path<=16)
        {
                return  16;
        }
        else
        {
                return 32;
        }
}

wrap_data **vec;
int  vecsize;

wrap_data  *  up ( int num )
{
        int  i,j,k;
        wrap_data  *first,*second;
        i=num;
        second=vec[i];
        while(i)
        {
                j=i/2;
                first=vec[j];

                if(!first)
                {
                        vec[j]=second;
                        if (!j)
                        {
                                return second;
                        }
                        else
                        {
                                return NULL;
                        }
                }
                if ( first->path==second->path)
                {
                        i=j;
                }
                else if ( *( second->data + second->offset )>  *( first->data + first->offset ))
                {
                        vec[j]=second;
                        second=first;
                        i=j;

                }
                else
                {
                        i=j;
                }
        }
        return  second;
}

int  main()
{
#define  PATH  4
#define  LENGTH  20
#define   MAX_BIG   999999999

        wrap_data *result;
        int i=0,j=0,k=0;
        wrap_data a[PATH]={0};
        int  last_docid=MAX_BIG;
        int  count_docid=0;


        vecsize=2*    choosevec(PATH);
        vec=(wrap_data **)calloc( vecsize ,sizeof (wrap_data*));

        for(i=0;i<PATH;i++)
        {
                a[i].data=(int*) calloc (LENGTH , sizeof (int ));
                a[i].offset=0;
                a[i].path=i;
                for(j=0;j<LENGTH;j++)
                {
                        *(a[i].data+j)=(i*i+j);
                }
                *(a[i].data+LENGTH-1)=MAX_BIG;
        }
        for(i=0;i<PATH;i++)
        {
                for(j=0;j<LENGTH;j++)
                {
                        printf("%d  ", *(a[i].data+j));
                }
                printf("\n");
        }
        k=vecsize/2;
        for(i=0;i<PATH;i++)
        {
                vec[k+i]=&a[i];
        }
        for(i=0;i<PATH;i++)
        {
                result=up(i+k);
                if(!result)
                {
                }
                else
                {
                        break;
                }
        }
        while(result)
        {
                //              printf(" %d  ", *(result->data+result->offset));  
                if (  *(result->data+result->offset) ==  last_docid  )
                {
                        count_docid++;
                        if (count_docid ==  PATH)
                        {
                                printf(" %d  ", *(result->data+result->offset));  
                                last_docid=MAX_BIG;

                        }
                }
                else
                {
                        last_docid=*(result->data+result->offset) ;  
                        count_docid=1;
                }

                //add  a  sed  for  the result  by  chenbing   2011.11.14
                if (result->offset>=LENGTH)
                {
                        break;
                }

                result->offset++; 
                result=up(result->path+k);
        }
        printf("\n");

}

 

原创粉丝点击