Exemplo n.º 1
0
/* Send data through socket
 * @param socket: file descriptor of client
 * @param filename: name of the file
 * @param fdout: file descriptor to write
 *
 * @return: 0 on success. Error code otherwise
 */
int send_data(int socket, char filename[], int fdout) {
    struct stat file_stat;
    char type = END;

    if (stat(filename, &file_stat) < 0)
        return -1;

    if (file_stat.st_mode & S_IFDIR) {
        send_dir(socket, filename, search_root(filename), fdout);
        write(socket, &type, sizeof(char));
    } else if (file_stat.st_mode & S_IFREG || file_stat.st_mode & S_IFLNK) {
        send_file(socket, filename, search_root(filename), fdout);
        write(socket, &type, sizeof(char));
    } else {
        dprintf(fdout, "It can't be sended that kind of file\n");
        dprintf(fdout, "Quitting...\n");
        return -1;
    }
    dprintf(fdout, "\nSe han enviado: - %d directorios\n", DIR_S);
    dprintf(fdout, "                - %d archivos\n", FILES_S);
    dprintf(fdout, "\nTransferencia realizada con éxito.\n");

    write_to(socket, &DIR_S, sizeof(int));
    write_to(socket, &FILES_S, sizeof(int));

    return 0;
}
Exemplo n.º 2
0
static delete_node_return delete_node(compare_t compare, node root, FatPointer<int> item)
{
    delete_node_return __return__;
    /// int search;
    int search = true;
    /// search = search_root(compare, FALSE, root, item, out);
    {
        search_root_return __result__;
        __result__ = search_root(compare, FALSE, root, item);
        __return__.out = __result__.out;
        search = __result__.value;
    }
    /// if(!search)
    if(!search)
    {
        /// return FALSE;
        __return__.value = FALSE;
        return __return__;
    }
    /// search_root(find_min, FALSE, out->children_[RIGHT], NULL, out->children_[RIGHT]);
    {
        search_root_return __result__;
        __result__ = search_root(find_min, FALSE, __return__.out->children_[RIGHT], FatPointer<int>());
        __return__.out->children_[RIGHT] = __result__.out;
    }
    /// out->children_[RIGHT]->children_[LEFT] = out->children_[LEFT];
    __return__.out->children_[RIGHT]->children_[LEFT] = __return__.out->children_[LEFT];
    /// out = out->children_[RIGHT];
    __return__.out = __return__.out->children_[RIGHT];
    /// return TRUE;
    __return__.value = TRUE;
    return __return__;
}
Exemplo n.º 3
0
tree* search_root(tree** root,tree** p)
{

  if((*root)==NULL)return NULL;
  if((*root)->left==*p||(*root)->right==(*p))return *root;
  if((*p)->data<(*root)->data){
        return search_root(&((*root)->left),p);
    }
   if((*p)->data>(*root)->data){
       return search_root(&((*root)->right),p);
    }
}
Exemplo n.º 4
0
static void full_gc(lgc_state_t* gc, int gen)
{
	lgc_list_head_t* head = gc->gen0;
	switch (gen) {
	case 2: lgc_list_combine(head, gc->gen2); //pass to 1
	case 1: lgc_list_combine(head, gc->gen1); //pass to 0
	case 0: break;
	default:
		assert(0);
	}

	search_root(head, gc->gc_root);
	collect_reachable(gc->gc_root, head);
	free_unreachable(gc, head);

	switch(gen) {
	case 2:
		{
			lgc_list_combine(gc->gen2, gc->gc_root);
			gc->state = LGC_GEN2;
		}; break;
	case 1:
		{
			lgc_list_combine(gc->gen2, gc->gc_root);
			gc->state = LGC_GEN1;
		}; break;
	case 0:
		{
			lgc_list_combine(gc->gen1, gc->gc_root);
			gc->state = LGC_GEN0;
		}; break;
	default:;
	}
}
Exemplo n.º 5
0
/*
adiciona a matriz em forma de array na priority queue
checa se ja nao existe a mesma matriz na mesma
*/
void add_priority_queue(heap_node **leaf, int key, int *array, int size) {
    if(leaf == NULL || array == NULL || size < 1) return;

    int i = search_data(search_root(*leaf), array, key, size);
    if(i == 0) return; // array exist in priority queue

    *leaf = (heap_node*) insert_node(leaf, key, array);
}
Exemplo n.º 6
0
int main() {
    double val,root;
    printf("enter number to take square root of: ");
    scanf("%lf", &val);
    
    root = search_root(val);
    printf("The square root is: %lf\n", root);
    return 0;
}
Exemplo n.º 7
0
find_return find(tree _this,FatPointer<int> item)
{
    find_return __return__;
    /// out = this;
    __return__.out = _this;
    /// return search_root(out.compare_, FALSE, out.root_, item, out.root_);
    {
        search_root_return __result__ = search_root(__return__.out.compare_, FALSE, __return__.out.root_, item);
        __return__.out.root_ = __result__.out;
    }
    return __return__;
}
Exemplo n.º 8
0
insert_return insert(tree _this, FatPointer<int> item)
{
    insert_return __return__;
    /// out = this;
    __return__.out = _this;
    /// search_root(out.compare_, TRUE, out.root_, item, out.root_);
    {
        search_root_return __result__ = search_root(__return__.out.compare_, TRUE, __return__.out.root_, item);
        __return__.out.root_ = __result__.out;
    }
    ///
    return __return__;
}
Exemplo n.º 9
0
/*
TODO tentar diminuir o numero de passos
*/
int main(void) {

    int dimension=3;
    int **initial = mount_board("./initial_state.txt");
    if(initial == NULL) return -1;
    int **final = mount_board("./final_state.txt");
    if(final == NULL) return -1;

    heap_node *node = NULL;
    node_queue *queue = NULL;

    solve_puzzle(initial, final, &node, &queue, dimension);

    free_matrix(initial, dimension);
    free_matrix(final, dimension);
    free_priority_queue(search_root(node));
    free_queue(&queue);

    printf("ok\n");

    return 0;
}
Exemplo n.º 10
0
int main()
{
  tree* root=NULL;
  tree* a1=(tree*)malloc(sizeof(tree));
  memset(a1,0,sizeof(tree));
  a1->data=1;

  tree* a2=(tree*)malloc(sizeof(tree));
  memset(a2,0,sizeof(tree));
  a2->data=2;
  
  tree* a3=(tree*)malloc(sizeof(tree));
  memset(a3,0,sizeof(tree));
  a3->data=3;

  tree* a4=(tree*)malloc(sizeof(tree));
  memset(a4,0,sizeof(tree));
  a4->data=4;

  tree* a5=(tree*)malloc(sizeof(tree));
  memset(a5,0,sizeof(tree));
  a5->data=5;
  insert(&root,&a3);
  insert(&root,&a1);
  insert(&root,&a2);
  insert(&root,&a4);
  insert(&root,&a5);
  tree* ptr=NULL;
  ptr=search(&root,1);
  printf("ptr:%p\n",ptr);
  printf("ptr:%d\n",ptr->data);

  tree* p=search_root(&root,&ptr);
  printf("p:%p\n",p);
  printf("p:%d\n",p->data);
  del(&root,2);
  travel(&root);
 return 0;
}
Exemplo n.º 11
0
void del(tree** root,int a)
{
  tree* p=NULL;
  tree* p_root=NULL;
  p=search(&(*root),a);
  printf("del p:%p\n",p);
  p_root=search_root(&(*root),&p);
  printf("del proot:%p\n",p_root);
    if(p==p_root->left)
   {
       p_root->left=NULL;
       if(p->left!=NULL)
       {
           insert(&p_root,&(p->left));
       }
       if(p->right!=NULL)
       {
            insert(&p_root,&(p->right));
       }
      
    }
    if(p==p_root->right)
    {
         p_root->right=NULL;
        if(p->right!=NULL)
        {
            insert(&p_root,&(p->right));
        }
        if(p->left!=NULL)
        {
           insert(&p_root,&(p->left));
        }

    }
 
  free(p);
  p=NULL;
}
Exemplo n.º 12
0
static void search_start( int dice )
{
  int iret;

  history_dice( dice );
  iret = search_root( dice );
  if( iret == -3 )
    {
      out( " This game was already concluded.\n");
      return;
    }
  else if( iret == -2 )
    {
      out( " This game was already concluded.\n");
      return;
    }
  else if( iret == -1 )
    {
      out( " Search() failed.\n" );
      return;
    }
  out_position();
  return;
}
Exemplo n.º 13
0
/*Inizializza la ricerca e gestisce l'iterative deeping*/
unsigned int find_best_move(const int output_mode,const int search_mode) {
	int depth, score, i,c,d,start_depth;
	int alpha, beta;
	MOVE m = {0};

	/*inizializzazioni varie*/
	tree.start_time = time_elapsed();
	tree.stop_time = tree.start_time + tree.delta_time;
	Ply = 0;
	Nodes = 0;
	tree.abort_search = 0;
	start_depth=tree.ponder_start_depth-1;
	alpha = -INF;
	beta = +INF;

	do {
		if (search_mode==CONTINUE_SEARCH) break;
		start_depth=1;
		tree.ponder_start_depth=1;
	for (i=0;i<MAX_PLY;i++) {
		tree.old_pv[i].mi=0;
		tree.k_heuristic[i].m1.mi=0;
		tree.k_heuristic[i].m2.mi=0;
		tree.k_heuristic[i].score1=0;
		tree.k_heuristic[i].score2=0;
		for (c=0;c<MAX_PLY;c++) tree.pv[i][c].mi=0;
	}
	for (i=0;i<256;i++) {
		for (c=0;c<256;c++) {
			tree.h_heuristic[i][c]=0;
		}	
	}
	for (i=0;i<64;i++) {
		for (c=0;c<64;c++) {
			for (d=0;d<256;d++)tree.c_heuristic[i][c][d].mi=0;
		}	
	}
	if (mizar.o.use_hash) reset_tt();
	}while (0);

	
	

	/*Precalcoliamo*/
	Phase(WHITE) = eval_phase(WHITE);
	Phase(BLACK) = eval_phase(BLACK);

	/*Prepariamo il nodo radice*/
	root();
	Nodes=0;

	if (output_mode == IO_CONSOLE)
		printf("ply  score      time      nodes pv\n");

	/*iterative deeping*/ 
	for (depth = start_depth;depth <= tree.max_depth;depth++) {
			
			tree.ponder_start_depth=depth;

			tree.follow_pv = 1;

			score = search_root( alpha, beta, depth);

			if (tree.abort_search)
				break;

			if (score<=alpha || score >=beta) {
				alpha = -INF;
				beta = +INF;
				tree.follow_pv = 1;
				score = search_root( alpha, beta, depth);
				if (tree.abort_search)
				break;
			} 
			else {
				alpha = score - ASPIRATION_W;
				beta = score + ASPIRATION_W;				
				} 

			m.mi = tree.pv[0][0].mi;

			for (i=0;i<MAX_PLY;i++)
				tree.old_pv[i].mi=0;

			sort_root_nodes();

			for (i = 0;i < tree.pv_lenght[0];i++)
				tree.old_pv[i] = tree.pv[0][i];


			if (output_mode == IO_CONSOLE) {
					printf("%3d  %5d  %8d  %9d", depth, score, (time_elapsed() - tree.start_time) / 100, Nodes);
					}

			else if (output_mode == IO_WINBOARD) {
					printf("%d %d %d %d", depth, score, (time_elapsed() - tree.start_time) / 10, Nodes);
					}

			if (output_mode) {
				if (!mizar.o.only_pv) printf(" Phs:%d Phx:%d",Phase(Side),Phase(Xside));
					for (i = 0;i < tree.pv_lenght[0];i++)
						printf(" %s", move2str(tree.old_pv[i]));

					printf("\n");

					fflush(stdout);
					}

			}
	return (m.mi);
	}