Exemplo n.º 1
0
static void
test_set(void) {
    char *keys1[] = {"a", "b", "r", "a", "c", "a", "d", "a", "b", "r", "a"};
    char *keys2[] = {"a", "l", "a", "c", "a", "z", "a", "m"};
    SetObject *a = set_new();
    SetObject *b = set_new();
    set_addfrom(a, (void**)keys1, 11);
    set_addfrom(b, (void**)keys2, 8);
    set_print(a); // { 'c', 'd', 'r', 'a', 'b', }
    set_print(b); // { 'c', 'l', 'm', 'a', 'z', }
    set_print(set_rsub(a, b)); //{ 'd', 'b', 'r', }
    set_print(set_ror(a, b)); //{ 'a', 'b', 'c', 'd', 'l', 'm', 'r', 'z', }
    set_print(set_rand(a, b)); //{ 'c', 'a', }
    set_print(set_rxor(a, b)); //{ 'b', 'd', 'l', 'm', 'r', 'z', }
    set_ior(a, b);
    set_print(a);
    set_isub(a, b);
    set_print(a);
    set_ixor(a, b);
    set_print(a);
    set_iand(a, b);
    set_print(a);
    set_free(a);
    set_free(b);
    char *keys3[] = {"a", "b"};
    char *keys4[] = {"a", "b", "d", "c", "e", "f", "g", "h"};
    SetObject *c = set_new();
    SetObject *d = set_new();
    set_addfrom(c, (void**)keys3, 2);
    set_addfrom(d, (void**)keys4, 8);
    set_print(set_xor(c, d));
    set_print(set_and(c, d));
    set_print(set_sub(c, d));
}
Exemplo n.º 2
0
size_t rewire_bipartite(unsigned short *matrix,size_t ncol, size_t nrow,size_t max_iter,size_t verbose,unsigned int seed)
{


	set_rand(seed);
	size_t i,j,kk,n,e=0,rand1,rand2;
	size_t *from;
	size_t a,b,c,d;
	size_t *to;

	for(i=0;i<nrow;++i)
		for(j=0;j<ncol;++j)
            e+=matrix[i*ncol+j];

	do	from=(size_t*)calloc(e,sizeof(size_t));	while(from==NULL);

	do	to=(size_t*)calloc(e,sizeof(size_t));	while(to==NULL);
	kk=0;
	for(i=0;i<nrow;++i)
		for(j=0;j<ncol;++j)
			if(matrix[i*ncol+j]==1)
				{
					from[kk]=i;
					to[kk]=j;
					kk++;
				}
	//GetRNGstate();
	time_t  tin,tfin;
	tin = time (NULL);
	for(n=0;n<max_iter;n++)
	{
		//random rewiring
		if(verbose==1)
    	loadBar( n,  max_iter, 100,  50);
		rand1=(size_t) (unif_rand()*e);
		do  rand2=(size_t) (unif_rand()*e);while (rand1==rand2);
		a=from[rand1];
		c=from[rand2];
		b=to[rand1];
		d=to[rand2];
		if(a!=c && d!=b && matrix[a*ncol+d]==0 && matrix[c*ncol+b]==0)
			{
				to[rand1]=d;
				to[rand2]=b;
				matrix[a*ncol+d]=matrix[c*ncol+b]=1;
				matrix[a*ncol+b]=matrix[c*ncol+d]=0;

			}

	}
	//PutRNGstate();
	tfin = time (NULL);
	if(verbose==1)
    printf("DONE in %d seconds \n",-(tin-tfin));


return 0;

}
Exemplo n.º 3
0
size_t rewire_sparse_bipartite(size_t *from,size_t *to,size_t nc,size_t nr,size_t max_iter,size_t e,size_t verbose,unsigned int seed)
{


	set_rand(seed);
	size_t i,j,kk,n,rand1,rand2;
 	size_t a,b,c,d;
	size_t *index;
	size_t *pos;
	do	index = (size_t*)calloc( (nr+1),sizeof(size_t ));	while(index==NULL);
	do	pos = (size_t*)calloc(e,sizeof(size_t ));	 while(pos==NULL);
	index[0]=0;
  pos[0]=0;
	kk=1;
  j=0;
	for( i=1;i<e;i++)
    {
			if(from[i]!=from[i-1])
        {
					index[kk++]=i;
          j++;
        }
        pos[i]=j;
    }
    index[nr]=e;
	//GetRNGstate();
	time_t  tin,tfin;
	tin = time (NULL);

	for(n=0;n<max_iter;n++)
	{

		//random rewiring
  	if(verbose==1)
			loadBar( n,  max_iter, 100,  50);
		rand1=(size_t) (unif_rand()*e);
		do rand2=(size_t) (unif_rand()*e); while (rand1==rand2);
		a=from[rand1];
		c=from[rand2];
		b=to[rand1];
		d=to[rand2];
		if(a!=c && d!=b &&  check(pos,to,index,rand1,b,rand2,d,e,nr)==1 )
        {
						to[rand1]=d;
            to[rand2]=b;

            //printf("Rewire ok!\n");
        }

	}
	tfin = time (NULL);
	//PutRNGstate();
	if(verbose==1)
		printf("DONE in %d seconds \n",-(tin-tfin));
	return 0;


}
Exemplo n.º 4
0
int main(int argc, char* argv[])
{

	// SDL Event handler
	SDL_Event    event;
	int key_press;

	int game_over, score;
	int game_grid[CELLS] = {0};
	int row_test, last_row, drop_count, lock_in, key_count;
	int first_hold = 1;

	tetromino * falling;
	tetromino * next;
	tetromino * hold;
	tetromino * temp;

	set_rand();
	lock_in = 0;
	drop_count = 1;
	game_over = 0;
	key_count = 0;
	score     = 0;

	falling = tetromino_new(ACTIVE);
	next    = tetromino_new(INACTIVE);
	hold    = tetromino_new(INACTIVE);
	temp    = NULL;
	
	//log = fopen("Log.txt","w");
	//fprintf(log, "Begin Main\n");

	if(init() == 0)
	{//printf(log, "SDL did not initialize.\n" );
		return 0;
	}

	if(load_media() == 0)
	{//printf(log, "Media Loading failed\n" );
		return 0;
	}


	gridRect.x = 0;
	gridRect.y = 0;

	fallingRect.x = 0;
	fallingRect.y = 0;

	prevRect.x = (BLOCK_SIZE*MAXCOL) + PREVIEW_OFFSET;
	prevRect.y = 100;

	scoreRect.x = SCORE_X;
	scoreRect.y = SCORE_Y;

//	Preview = PreviewSurfaces[SQUARE_BLOCK]; // set a dummy default

	//anim = 0;
	//fprintf(log, "Game Loop entry\n" );

	while(game_over == 0)
	{
		/*if(anim ==  0)
			tetromino_drop(falling, game_grid);

		anim = (anim == 200)? 0: anim+1;*/

		drop_count = auto_drop(falling, game_grid, drop_count, score);

		key_press = 100;

		if(SDL_PollEvent(&event) != 0)
		{
			switch(event.type)
			{
				case SDL_QUIT:
					game_over = 1;
				break;

				case SDL_KEYDOWN:
					switch(event.key.keysym.sym)
					{
						case SDLK_UP:
							key_press = UP;
						break;

						case SDLK_DOWN:
							key_press = DOWN;
						break;

						case SDLK_LEFT:
							key_press = LEFT;
						break;

						case SDLK_RIGHT:
							key_press = RIGHT;
						break;

						case SDLK_ESCAPE:
							key_press = ESC;
						break;
						
						case SDLK_SPACE:
							key_press = SPACE;
						break;
					}
				break;
			}
		}

		//if(key_count == 0)
		//{
			switch(key_press)
				{
					case UP:
						tetromino_rotate(falling, game_grid);
					break;
	
					case DOWN:
						tetromino_drop(falling, game_grid);
					break;
	
					case RIGHT:
						tetromino_shift_right(falling, game_grid);
					break;
	
					case LEFT:
						tetromino_shift_left(falling, game_grid);
					break;
	
					case ESC:
					game_over = 1;
					break;
					
					case SPACE: // this still would be nice with visuals...
						if(first_hold == 1)
						{
							temp = hold;
							tetromino_mod(temp);
							hold = falling;
							falling = next;
							tetromino_activate(falling);
							next = temp;
							temp = NULL;
							first_hold = 0;
						}
						else
						{
							temp = falling;
							falling = hold;
							tetromino_activate(falling);
							hold = temp;
							temp = NULL;
						}
					break;
			}
		//}

		key_count = (key_count < KEY_RATE) ? key_count + 1: 0;

		lock_in = (tetromino_lock(falling, game_grid) == 1)? lock_in+1:0;
		
		if(lock_in == 200)
		{
			tetromino_into_grid(falling, game_grid);
			score += falling->shape + 2 * 3;
			score += row_check(game_grid);
			// manage the tetrominos
			temp = falling;
			falling = next;
			tetromino_activate(falling);
			tetromino_mod(temp);
			next = temp;
			temp = NULL;
			lock_in = 0;
		}

		if(grid_row_any(game_grid,0) == 1)
			game_over = 1;

		//blit to screen
		SDL_FillRect(ScreenSurface, 0, 0); // so the images don't trail
		blit_preview(next->shape);	
		blit_grid(game_grid);
		blit_tetromino(falling);
		blit_score(score);
		SDL_UpdateWindowSurface(MainWin);
	}

	game_close();
	free(falling);
	free(next);
	free(hold);
	return 0;
}
Exemplo n.º 5
0
size_t analysis(unsigned short *incidence,size_t ncol,size_t nrow,double *scores,size_t step,size_t max_iter,size_t verbose,unsigned int seed)
{


	set_rand(seed);
	size_t i,j,kk,n,rand1,rand2;
	size_t dim=max_iter+1;
	size_t *from;
  	size_t *to;
  	unsigned short *matrix;

	size_t a,b,c,d,e=0;
	size_t index=1;
  do matrix=(unsigned short *)calloc(nrow*ncol,sizeof(unsigned short)); while(matrix==NULL);
	for(i=0;i<nrow;++i)
	{
		
    for(j=0;j<ncol;++j)
			{
				matrix[i*ncol+j]=incidence[i*ncol+j];
				e+=incidence[i*ncol+j];
			}
  }
	//initialization of score vector overwriting the original
	//do	scores=(double*)calloc(dim,sizeof(double));	while(scores==NULL);
	for(i=0;i<dim;scores[i++]=0.0);
	scores[0]=1.0;
 	do	from=(size_t*)calloc(e,sizeof(size_t));	while(from==NULL);
	do	to=(size_t*)calloc(e,sizeof(size_t));	  while(to==NULL);
 	kk=0;
	for(i=0;i<nrow;++i)
		for(j=0;j<ncol;++j)
			if(matrix[i*ncol+j]==1)
				{
					from[kk]=i;
					to[kk]=j;
					kk++;
				}
	time_t  tin,tfin;
	tin = time (NULL);
	//GetRNGstate();
	for(n=0;n<max_iter;n++)
	{
		//random rewiring
    if(verbose==1)
  		loadBar( n,  max_iter, 100,  50);
		rand1=(size_t) (unif_rand()*e);
		do	rand2=(size_t) (unif_rand()*e);	while (rand1==rand2);
		a=from[rand1];
		c=from[rand2];
		b=to[rand1];
		d=to[rand2];
		//printf("%d %d %d %d %d %d %d %d %d\n",e,a,b,c,d,rand1,rand2,t,n);
		if(a!=c && d!=b && incidence[a*ncol+d]==0 && incidence[c*ncol+b]==0)
			{
				to[rand1]=d;
				to[rand2]=b;
				incidence[a*ncol+d]=incidence[c*ncol+b]=1;
				incidence[a*ncol+b]=incidence[c*ncol+d]=0;

			}

			if(n%step==0)
			(scores)[index++]=similarity(incidence,matrix, ncol,nrow, e);
	}
	tfin = time (NULL);
	//PutRNGstate();
	if(verbose==1)
    printf("DONE in %d seconds \n",-(tin-tfin));

	return (index-1);
}
Exemplo n.º 6
0
size_t rewire_ex(unsigned short *incidence,size_t ncol, size_t nrow,size_t max_iter,size_t verbose,size_t MAXITER,unsigned int seed)
{


	set_rand(seed);

 	size_t i,j,kk,n,rand1,rand2,t=0;
	size_t e=0;
    //copy of the original incidence matrix
	size_t *from;
	size_t *to;
	size_t a,b,c,d;


	for(i=0;i<nrow;i++)
		for(j=0;j<ncol;j++)
			e+=incidence[i*ncol+j];


	e/=2;
	//initialization of score vector overwriting the original TO CHECK
	do from=(size_t*)calloc(e,sizeof(size_t)); while(from==NULL);
	do to=(size_t*)calloc(e,sizeof(size_t));   while(to==NULL);
	kk=0;
	for(i=0;i<nrow;++i)
		for(j=0;j<i;++j)
			if(incidence[i*ncol+j]==1)
				{
					from[kk]=i;
					to[kk]=j;
          kk++;
        }
	time_t  tin,tfin;
  tin = time (NULL);

 	//GetRNGstate();
	for(n=0;n<max_iter;t++)
		{
			if(verbose==1)
				loadBar( n,  max_iter, 100,  50);
		//random rewiring
		  rand1=(size_t) (unif_rand()*e);
      do rand2=(size_t) (unif_rand()*e); while (rand1==rand2);
      a=from[rand1];
      c=from[rand2];
      b=to[rand1];
      d=to[rand2];
	if(t>MAXITER)
			{
				tfin = time (NULL);
 				if(verbose==1)
    		printf("DONE in %d seconds \n",-(tin-tfin));
				//PutRNGstate();
				return (-1);

			}
		//  printf("%d %d %d %d %d %d %d %d %d %d\n ",rand1,rand2,a+1,b+1,c+1,d+1,incidence[a*ncol+d],incidence[c*ncol+b],incidence[a*ncol+c],incidence[d*ncol+b]);
     if(a!=c && b!=d&&  a!=d && c!=b &&
           (	(incidence[a*ncol+d]==0 && incidence[c*ncol+b]==0  ) ||
            (incidence[a*ncol+c]==0 && incidence[d*ncol+b]==0  ) ))
        {
            if(incidence[a*ncol+d]==0 && incidence[c*ncol+b]==0 && incidence[a*ncol+c]==0 && incidence[d*ncol+b]==0 )
            {
                if(unif_rand()>=0)
                {
                    incidence[a*ncol+d]=1;incidence[d*ncol+a]=1;
                    incidence[c*ncol+b]=1;incidence[b*ncol+c]=1;
                    incidence[a*ncol+b]=0;incidence[b*ncol+a]=0;
                    incidence[c*ncol+d]=0;incidence[d*ncol+c]=0;
                    to[rand1]=d;
                    to[rand2]=b;
                    n++;

                }
                else
                {
                    incidence[a*ncol+c]=1;incidence[c*ncol+a]=1;
                    incidence[d*ncol+b]=1;incidence[b*ncol+d]=1;
                    incidence[a*ncol+b]=0;incidence[b*ncol+a]=0;
                    incidence[c*ncol+d]=0;incidence[d*ncol+c]=0;
                    //	from[rand1]=d;
                    to[rand1]=c;
                    from[rand2]=b;
                    to[rand2]=d;

                    n++;

                }
            }
            else
                if(incidence[a*ncol+d]==0 && incidence[c*ncol+b]==0)
                {
                    incidence[a*ncol+d]=1;incidence[d*ncol+a]=1;
                    incidence[c*ncol+b]=1;incidence[b*ncol+c]=1;
                    incidence[a*ncol+b]=0;incidence[b*ncol+a]=0;
                    incidence[c*ncol+d]=0;incidence[d*ncol+c]=0;
                    to[rand1]=d;
                    to[rand2]=b;
                    n++;
                }
                else
                {
                    incidence[a*ncol+c]=1;incidence[c*ncol+a]=1;
                    incidence[d*ncol+b]=1;incidence[b*ncol+d]=1;
                    incidence[a*ncol+b]=0;incidence[b*ncol+a]=0;
                    incidence[c*ncol+d]=0;incidence[d*ncol+c]=0;
                    to[rand1]=c;
                    from[rand2]=b;
                    to[rand2]=d;
                    n++;
                }
        }
    }
	tfin = time (NULL);
 	if(verbose==1)
    printf("DONE in %d seconds \n",-(tin-tfin));
	//PutRNGstate();

	return 0;


}
Exemplo n.º 7
0
size_t analysis_undirected_ex(unsigned short *incidence,size_t ncol, size_t nrow,double *scores,size_t step,size_t max_iter,size_t verbose,size_t MAXITER,unsigned int seed)
{


	set_rand(seed);


	size_t i,j,kk,n,index,rand1,rand2,t=0;
	size_t dim=(size_t)round((double)max_iter/step)+2;
	size_t e=0;
    //copy of the original incidence matrix
    unsigned short *matrix;
	size_t *from;
	size_t *to;
	size_t a,b,c,d;

	do	matrix=(unsigned short *)calloc(nrow*ncol,sizeof(unsigned short));	while(matrix==NULL);
	for(i=0;i<nrow;i++)
		{
			for(j=0;j<ncol;j++)
        {
            matrix[i*ncol+j]=incidence[i*ncol+j];
            e+=incidence[i*ncol+j];
        }
	}
	e/=2;
	//initialization of score vector overwriting the original TO CHECK
	do	from=(size_t*)calloc(e,sizeof(size_t));	while(from==NULL);
	do 	to=(size_t*)calloc(e,sizeof(size_t));    while(to==NULL);
	kk=0;
	for(i=0;i<nrow;++i)
		for(j=0;j<i;++j)
			if(matrix[i*ncol+j]==1)
				{
                from[kk]=i;
                to[kk]=j;
                kk++;
				}
	for(i=0;i<dim;scores[i++]=0.0);
	scores[0]=1.0;
	//RANDOM GENERATOR (MILLISECONDS)
	//GetRNGstate();
	index=1;
	time_t  tin,tfin;
	tin = time (NULL);
	for(n=0;n<max_iter;t++)
	{
		if(verbose==1)
			loadBar( n,  max_iter, 100,  50);
		//random rewiring

		rand1=(size_t) (unif_rand()*e);
		do	rand2=(size_t) (unif_rand()*e);	while (rand1==rand2);
		a=from[rand1];
		c=from[rand2];
		b=to[rand1];
		d=to[rand2];
		if(t>MAXITER)
		{
			tfin = time (NULL);
			//PutRNGstate();
			if(verbose==1)
  	  printf("DONE in %d seconds \n",-(tin-tfin));

			printf("Reached the maximum number admissible of iterations!\n");
			return (index-1);
		}
		//printf("a=%d b=%d c=%d d=%d\n ",a+1,b+1,c+1,d+1);
		if( a!=d && c!=b &&
           (	(incidence[a*ncol+d]==0 && incidence[c*ncol+b]==0  ) ||
            (incidence[a*ncol+c]==0 && incidence[d*ncol+b]==0  ) ))
        {
            if(incidence[a*ncol+d]==0 && incidence[c*ncol+b]==0 && incidence[a*ncol+c]==0 && incidence[d*ncol+b]==0 )
            {
                if(unif_rand()>=0.5)
                {
                    incidence[a*ncol+d]=1;incidence[d*ncol+a]=1;
                    incidence[c*ncol+b]=1;incidence[b*ncol+c]=1;
                    incidence[a*ncol+b]=0;incidence[b*ncol+a]=0;
                    incidence[c*ncol+d]=0;incidence[d*ncol+c]=0;
                    to[rand1]=d;
                    to[rand2]=b;
										n++	; if(n%step==0)
            {
								(scores)[index++]=similarity_undirected(matrix,incidence,ncol,nrow,e);
										//printf("%lf \n",(*scores)[index-1]);
						}
                }
                else
                {
                    incidence[a*ncol+c]=1;incidence[c*ncol+a]=1;
                    incidence[d*ncol+b]=1;incidence[b*ncol+d]=1;
                    incidence[a*ncol+b]=0;incidence[b*ncol+a]=0;
                    incidence[c*ncol+d]=0;incidence[d*ncol+c]=0;
                    //	from[rand1]=d;
                    to[rand1]=c;
                    from[rand2]=b;
                    to[rand2]=d;
                    n++	; if(n%step==0)
            {
								(scores)[index++]=similarity_undirected(matrix,incidence,ncol,nrow,e);
										//printf("%lf \n",(*scores)[index-1]);
						}

                }
            }
            else
                if(incidence[a*ncol+d]==0 && incidence[c*ncol+b]==0)
                {
                    incidence[a*ncol+d]=1;incidence[d*ncol+a]=1;
                    incidence[c*ncol+b]=1;incidence[b*ncol+c]=1;
                    incidence[a*ncol+b]=0;incidence[b*ncol+a]=0;
                    incidence[c*ncol+d]=0;incidence[d*ncol+c]=0;
                    to[rand1]=d;
                    to[rand2]=b;
                    n++	; if(n%step==0)
            {
								(scores)[index++]=similarity_undirected(matrix,incidence,ncol,nrow,e);
										//printf("%lf \n",(*scores)[index-1]);
						}
                }
                else
                {
                    incidence[a*ncol+c]=1;incidence[c*ncol+a]=1;
                    incidence[d*ncol+b]=1;incidence[b*ncol+d]=1;
                    incidence[a*ncol+b]=0;incidence[b*ncol+a]=0;
                    incidence[c*ncol+d]=0;incidence[d*ncol+c]=0;
                    to[rand1]=c;
                    from[rand2]=b;
                    to[rand2]=d;
                   n++	;
						if(n%step==0)
            {
								(scores)[index++]=similarity_undirected(matrix,incidence,ncol,nrow,e);
										//printf("%lf \n",(*scores)[index-1]);
						}
                }
        }



	}

	tfin = time (NULL);
	if(verbose==1)
    printf("DONE in %d seconds \n",-(tin-tfin));

  //PutRNGstate();
	return (index-1);
}
Exemplo n.º 8
0
size_t rewire_sparse_ex(size_t *from, size_t *to,size_t *degree,size_t ncol, size_t nrow,size_t max_iter, size_t e,size_t verbose,size_t MAXITER,unsigned int seed)
{


	set_rand(seed);

 	size_t i,n,rand1,rand2,t=0;
    //copy of the original incidence matrix
	size_t a,b,c,d;
	size_t ad,cb,ac,bd;
	unsigned short *adj;
	size_t *cumdeg;
	do	cumdeg=(size_t *)calloc(nrow,sizeof(size_t)); while(cumdeg==NULL);
    cumdeg[0]=0;
	for(i=1;i<nrow;i++)
		{
			cumdeg[i]=cumdeg[i-1]+degree[i-1]+1;
		}
	adj=(unsigned short *)calloc(cumdeg[nrow-1]+degree[nrow-1]+1,sizeof(unsigned short)); while(adj==NULL);
	for(i=0;i<nrow;i++)
    	adj[cumdeg[i]+degree[i]]= degree[i];
    
	for(i=0;i<e;i++)
		{

			a=from[i];
			b=to[i];
			adj[cumdeg[a]+degree[a]-adj[cumdeg[a]+degree[a]]]=b;
			adj[cumdeg[a]+degree[a]]--;
			adj[cumdeg[b]+degree[b]-adj[cumdeg[b]+degree[b]]]=a;
			adj[cumdeg[b]+degree[b]]--;
    }

  //GetRNGstate();

  time_t  tin,tfin;
	tin = time (NULL);
	for(n=0;n<max_iter;t++)
	{
		if(verbose==1)
			loadBar( n,  max_iter, 100,  50);
		//random rewiring
		   rand1=(size_t) (unif_rand()*e);
        do   rand2=(size_t) (unif_rand()*e); while (rand1==rand2);
        a=from[rand1];
        c=from[rand2];
        b=to[rand1];
        d=to[rand2];

        ad=is_not(a,d,degree,adj,cumdeg);
        cb=is_not(c,b,degree,adj,cumdeg);
        ac=is_not(c,a,degree,adj,cumdeg);
        bd=is_not(b,d,degree,adj,cumdeg);
			if(t>MAXITER)
		{

			tfin = time (NULL);
			if(verbose==1)
		    printf("DONE in %d seconds \n",-(tin-tfin));
		   //PutRNGstate();
			return (-1);




			}
		  // printf("%d %d %d %d %d %d %d %d %d %d \n ",rand1, rand2,a+1,b+1,c+1,d+1,ad,cb,ac,bd);

        if( a!=c && b!=d & a!=d && c!=b && (( ad==1 && cb==1 )|| (ac==1 && bd==1) ))
        {
            if(ad==1 && cb==1 && ac==1 && bd==1 )
            {
                if(unif_rand()>=0.5)
                {

                    to[rand1]=d;
                    to[rand2]=b;
                    sub(a,b,c,d,degree,adj,cumdeg);

                    n++;
                }
                else
                {
                    sub2(a,b,c,d,degree,adj,cumdeg);

                    //	from[rand1]=d;
                    to[rand1]=c;
                    from[rand2]=b;
                    to[rand2]=d;
                               n++;

                }
            }
            else
                if(ad==1 && cb==1 )
                {

                    to[rand1]=d;
                    to[rand2]=b;
                    sub(a,b,c,d,degree,adj,cumdeg);


                    n++;
                }
                else
                {
                    sub2(a,b,c,d,degree,adj,cumdeg);


                    to[rand1]=c;
                    from[rand2]=b;
                    to[rand2]=d;
                    n++;
                }
        }


    }
  tfin = time (NULL);
  if(verbose==1)
 	 printf("DONE in %d seconds \n",-(tin-tfin));
  //PutRNGstate();
	return 0;
}
Exemplo n.º 9
0
int embed(char *infile_path, char *outfile_path, char *orig_outfile_path)
{ //{{{
  SF_INFO	sfinfo;		// struct with info on the samplerate, number of channels, etc
  SNDFILE   	*s_infile;
  SNDFILE   	*s_outfile;
  SNDFILE			*s_orig_out;

  fftw_plan 	*ft_forward;
  fftw_plan 	*ft_reverse;
  double	*time_buffer;
  complex	*freq_buffer;

  void		(*embed_to_signal)(complex *, int);
	int channels;
  
  if(wmark->type == FH_EMBED)
    embed_to_signal = &fh_embed_to_signal;
  else
    embed_to_signal = &ss_embed_to_signal;

  seed_rand(wmark->key_seed);
  set_rand(wmark->len); 

  //
  // Open input file and output file
  // -------------------------------

  // The pre-open SF_INFO should have format = 0, everything else will be set in the open call
  sfinfo.format = 0;
  
  // s_infile = input audio file
  if(!(s_infile = sf_open(infile_path, SFM_READ, &sfinfo))){
    fprintf(stderr,"error opening the following file for reading: %s\n", infile_path);
    return 1;
  }

  // s_outfile = watermaked audio file
  if(!(s_outfile = sf_open(outfile_path, SFM_WRITE, &sfinfo))){
    fprintf(stderr,"error opening the following file for writing: %s\n", outfile_path);
    return 1;
  }

  // s_orig_out = watermaked audio file
  if(!(s_orig_out = sf_open(orig_outfile_path, SFM_WRITE, &sfinfo))){
    fprintf(stderr,"error opening the following file for writing: %s\n", orig_outfile_path);
    return 1;
  }

	channels = sfinfo.channels;
	if(embed_verbose)
		print_sfile_info(sfinfo);

  //
  // Read, process, and write data
  // -----------------------------

  time_buffer = (double *) malloc(sizeof(double) * BUFFER_LEN * channels);
  freq_buffer = (complex *) fftw_malloc(sizeof(complex) * FREQ_LEN * channels);
  
	ft_forward = (fftw_plan *)malloc(sizeof(fftw_plan) * sfinfo.channels);
	ft_reverse = (fftw_plan *)malloc(sizeof(fftw_plan) * sfinfo.channels);

	for(int i = 0; i < channels; i++){
		ft_forward[i] = fftw_plan_dft_r2c_1d(BUFFER_LEN, time_buffer+i*BUFFER_LEN,
																				 freq_buffer+i*FREQ_LEN, FFTW_ESTIMATE);
		ft_reverse[i] = fftw_plan_dft_c2r_1d(BUFFER_LEN, freq_buffer+i*FREQ_LEN,
																			 time_buffer+i*BUFFER_LEN, FFTW_ESTIMATE);
	}

  int bytes_read;

  int counter = 0;
  while(bytes_read = sf_read_double(s_infile, time_buffer, BUFFER_LEN*channels)){
		// embed to orig_out to account for some of the floating point errors
		sf_write_double(s_orig_out, time_buffer, bytes_read);
      
    // only embed in a frame where we read in the full amount for the DFT
		if(bytes_read == BUFFER_LEN*channels){

			// if there are, say, 2 channels a,b, the audio will be stored as
			// a1, b1, a2, b2, ...
			// convert it to a1, a2, ... , b1, b2, ...
			deinterleave_channels(time_buffer, channels);

			for(int i = 0; i < channels; i++)
				fftw_execute(ft_forward[i]);

			// DEBUG print embed read info
			if(counter == embed_debug){
				printf("org: ");
				print_pow_density(freq_buffer, 10);
			}

			embed_to_signal(freq_buffer, FREQ_LEN*channels);

			// DEBUG print embed read info
			if(counter == embed_debug){
				printf("new: ");
				print_pow_density(freq_buffer, 10);
			}

			for(int i = 0; i < channels; i++)
				fftw_execute(ft_reverse[i]);

			// The DFT naturally multiplies every array element by the size of the array, reverse this
			array_div(BUFFER_LEN, time_buffer, BUFFER_LEN*channels);

			interleave_channels(time_buffer, channels);

		} // if(bytes_read == BUFFER_LEN)
		sf_write_double(s_outfile, time_buffer, bytes_read);

			//if(counter == embed_debug){
			//	deinterleave_channels(time_buffer, channels);
			//	for(int i = 0; i < channels; i++)
			//		fftw_execute(ft_forward[i]);
			//		printf("pt:  "); print_pow_density(freq_buffer,10);
			//		printf("cpt: "); print_complex_array(freq_buffer,10);

			//	for(int i = 0; i < channels; i++)
			//		fftw_execute(ft_reverse[i]);
			//	interleave_channels(time_buffer, channels);
			//	array_div(BUFFER_LEN, time_buffer, BUFFER_LEN*channels);
			//}

		counter++;
		embed_iteration++;
	} //while

  //
  // free everything
  // ---------------

	for(int i = 0; i < channels; i++){
		fftw_destroy_plan(ft_forward[i]);
		fftw_destroy_plan(ft_reverse[i]);
	}
	free(ft_forward);
	free(ft_reverse);
  free(time_buffer);
  fftw_free(freq_buffer);
  free_rand();
  sf_close(s_infile);
  sf_close(s_outfile);
  sf_close(s_orig_out);
} //}}}