Beispiel #1
0
    virtual void execute(cmd_context& ctx) {
        if (m_target == 0) {
            throw cmd_exception("invalid query command, argument expected");
        }
        datalog::context& dlctx = m_dl_ctx->get_dl_context();
        set_background(ctx);        
        dlctx.updt_params(m_params);
        unsigned timeout   = m_params.get_uint(":timeout", UINT_MAX);
        cancel_eh<datalog::context> eh(dlctx);
        lbool status = l_undef;
        {
            scoped_ctrl_c ctrlc(eh);
            scoped_timer timer(timeout, &eh);
            cmd_context::scoped_watch sw(ctx);
            try {
                status = dlctx.query(m_target);
            }
            catch (z3_error & ex) {
                throw ex;
            }
            catch (z3_exception& ex) {
                ctx.regular_stream() << "(error \"query failed: " << ex.msg() << "\")" << std::endl;
            }
            dlctx.cleanup();
        }
        switch (status) {
        case l_false:
            ctx.regular_stream() << "unsat\n";
            print_certificate(ctx);
            break;
        case l_true: 
            ctx.regular_stream() << "sat\n";
            print_answer(ctx);
            print_certificate(ctx);
            break;
        case l_undef: 
            ctx.regular_stream() << "unknown\n";
            switch(dlctx.get_status()) {
            case datalog::INPUT_ERROR:
                break;
                
            case datalog::MEMOUT:
                ctx.regular_stream() << "memory bounds exceeded\n";
                break;

            case datalog::TIMEOUT:
                ctx.regular_stream() << "timeout\n";
                break;
                
            case datalog::OK: 
                break;
            default:
                UNREACHABLE();
            }
            break;
        }
        print_statistics(ctx);
        m_target = 0;
    }
Beispiel #2
0
static void doit(int n, int t)
{
     int *A;
     int *B;

     A = malloc(n * sizeof(int));
     B = malloc((t + 1) * sizeof(int));
     answer = malloc(t * sizeof(int));

     B[0] = 0;
     best_so_far = INFTY;
     search(n, t, A, B + 1, t);

     print_answer(n, t);

     free(A); free(B); free(answer);
}
Beispiel #3
0
void		show_ways(t_map *start, t_antfarm *infos)
{
	int		i;
	int		nbr_ways;

	nbr_ways = 0;
	i = 0;
	while (i < start->nbr_links)
	{
		if (start->links[i]->is_way > 0)
			nbr_ways++;
		i++;
	}
	if (nbr_ways == 0)
	{
		ft_printf("ERROR\n");
		return ;
	}
	print_answer(infos, start, nbr_ways);
}
Beispiel #4
0
/**
 * Activity on our incoming socket.  Read data from the
 * incoming connection.
 *
 * @param cls
 */
static void
do_udp_read (void *cls)
{
  //struct GNUNET_NAT_Test *tst = cls;
  unsigned char reply_buf[1024];
  ssize_t rlen;
  struct sockaddr_in answer;
  const struct GNUNET_SCHEDULER_TaskContext *tc;

  ltask4 = NULL;
  tc = GNUNET_SCHEDULER_get_task_context ();
  if ( (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) ||
       (! GNUNET_NETWORK_fdset_isset (tc->read_ready,
                                      lsock4)) )
  {
    fprintf (stderr,
             "Timeout waiting for STUN response\n");
    stop();
  }
  rlen = GNUNET_NETWORK_socket_recv (lsock4,
                                     reply_buf,
                                     sizeof (reply_buf));
  memset (&answer,
          0,
          sizeof(struct sockaddr_in));
  if (GNUNET_OK !=
      GNUNET_NAT_stun_handle_packet (reply_buf,
                                     rlen,
                                     &answer))
  {
    fprintf (stderr,
             "Unexpected UDP packet, trying to read more\n");
    ltask4 = GNUNET_SCHEDULER_add_read_net (TIMEOUT,
                                            lsock4,
                                            &do_udp_read, NULL);
    return;
  }
  ret = 0;
  print_answer (&answer);
  stop ();
}
Beispiel #5
0
int solve(Field f, Position p, int flip, int rotate, int i, int nowscore) {
  if (i >= number_of_stones) {
    return 0;
  }
  int u = i;

  if (flip == 1) {
    u += fliped;
  }

  u += rot[rotate / 90];

  std::vector<Position> next_positions;
  int score = 0;

  if (score = put_stone(f, p, u, next_positions)) {
    score += nowscore;
    if (score > max_score) {
      max_score = score;
      max_score_field = f;
      fprintf(stderr, "max score %d!\n", score);
    }
    f.answer[i] = make_answer(p, flip, rotate);
    printf("score: %d\n", score);
    dump_field(f);
    print_answer(f.answer);
    int rot_buf[] = {0, 90, 180, 270};
    for (auto && f_p : next_positions) {
      for (int j = i + 1; j < number_of_stones; ++j) {
        for (auto && s_p : stones[j].fills) {
          for (int k = 0; k < 4; ++k) {
            solve(f, Position{f_p.y - s_p.y, f_p.x - s_p.x}, 0, rot_buf[k], j, score);
            solve(f, Position{f_p.y - s_p.y, f_p.x - s_p.x}, 1, rot_buf[k], j, score);
          }
        }
      }
    }
  }

  return 0;
}
Beispiel #6
0
static void search(int n, int t, int *A, int *B, int depth)
{
     if (depth == 0) {
	  int i, tc;
	  for (i = 0; i < n; ++i)
	       A[i] = INFTY;
	  A[0] = 0;		/* always free */
	  for (i = 1; i <= t; ++i)
	       A[B[-i]] = ldcost;

	  tc = optimize(n, A);
	  if (tc < best_so_far) {
	       best_so_far = tc;
	       for (i = 1; i <= t; ++i)
		    answer[t - i] = B[-i];
	       if (verbose)
		    print_answer(n, t);
	  }
     } else {
	  for (B[0] = B[-1] + 1; B[0] < n; ++B[0])
	       search(n, t, A, B + 1, depth - 1);
     }
}
/*
	MAIN()
	------
*/
int main(int argc, char *argv[])
{
    static char *seperators = " ";
    char *file, *token, *where_to, *filename;			// *start;
    char **term_list, **first, **last, **current;
    ANT_link_extract_term *link_index, *index_term;
    long terms_in_index, current_docid, param, file_number;
    long lowercase_only, first_param;
    long is_utf8_token, cmp, is_substring = FALSE;				// token_len
    char *command;
    ANT_directory_iterator_object file_object;

    char buffer[1024 * 1024];

    if (argc < 3)
        exit(printf("Usage:%s [-chinese] [-lowercase] <index> <file_to_link> ...\n", argv[0]));

    first_param = 1;
    lowercase_only = FALSE;
    chinese = FALSE;

    for (param = 1; param < argc; param++)
    {
        if (*argv[param] == '-')
        {
            command = argv[param] + 1;
            if (strcmp(command, "lowercase") == 0)
            {
                lowercase_only = TRUE;
                ++first_param;
            }
            else if (strcmp(command, "chinese") == 0)
            {
                chinese = TRUE;
                ++first_param;
            }
            else
                exit(printf("Unknown parameter:%s\n", argv[param]));
        }
    }

    link_index = read_index(argv[first_param], &terms_in_index);

    file_number = 1;
    for (param = first_param + 1; param < argc; param++)
    {
        ANT_directory_iterator_recursive disk(argv[param]);  // make the recursive pattern matching as for default files reading
        if (disk.first(&file_object) == NULL)
            file = filename = NULL;
        else
        {
            filename = file_object.filename;
            file = ANT_disk::read_entire_file(filename);
        }
        while (file != NULL)
        {
            current_docid = get_doc_id(file);
            if (current_docid > 0)
            {
//			printf("ID:%d\n", current_docid);
                string_clean(file, lowercase_only, TRUE);

                current = term_list = new char *[strlen(file)];		// this is the worst case by far
                if (chinese)
                    create_utf8_token_list(file, term_list);
                else
                {
                    for (token = strtok(file, seperators); token != NULL; token = strtok(NULL, seperators))
                        *current++ = token;
                    *current = NULL;
                }


                for (first = term_list; *first != NULL; first++)
                {
//				fprintf(stderr, "%s\n", *first);
                    where_to = buffer;
                    for (last = first; *last != NULL; last++)
                    {
                        if (where_to == buffer)
                        {
                            strcpy(buffer, *first);
                            where_to = buffer + strlen(buffer);
                            if (chinese)
                            {
                                if ((*first[0] & 0x80) && isutf8(*first))
                                    is_utf8_token = TRUE;
                                else
                                    is_utf8_token = FALSE;
                            }
                        }
                        else
                        {
                            if (!chinese)
                                *where_to++ = ' ';
                            strcpy(where_to, *last);
                            where_to += strlen(*last);
                        }

                        *where_to = '\0';

                        index_term = find_term_in_list(buffer, link_index, terms_in_index);

                        if (index_term == NULL)
                            break;		// we're after the last term in the list so can stop because we can't be a substring

                        if (chinese)
                        {
                            is_substring = FALSE;
                            cmp = utf8_token_compare(buffer, index_term->term, &is_substring);
                        }
                        else
                            cmp = string_compare(buffer, index_term->term);

                        if (cmp == 0)		// we're a term in the list
                        {
                            index_term->total_occurences++;
                            if (index_term->last_docid != current_docid)
                            {
                                index_term->last_docid = current_docid;
                                index_term->docs_containing_term++;
                            }
                        }
                        else
                        {
                            if (chinese)
                                cmp = is_substring == TRUE ? 0 : 1;
                            else
                                cmp = memcmp(buffer, index_term->term, strlen(buffer));
                            if  (cmp != 0)
                                break;		// we're a not a substring so we can't find a longer term
                        }
                    }
                }
                if (chinese)
                    free_utf8_token_list(term_list);
                delete [] term_list;
                delete [] file;

                if (file_number % 1000 == 0)
                    fprintf(stderr, "Files processed:%d\n", file_number);
                file_number++;
            }
            else
                fprintf(stderr, "Error reading file %s\n", filename);
            //filename = disk.get_next_filename();
            if (disk.next(&file_object) == NULL)
                file = filename = NULL;
            else
            {
                filename = file_object.filename;
                file = ANT_disk::read_entire_file(filename);
            }
        }
    }

    print_answer(link_index, terms_in_index);

    fprintf(stderr, "%s Completed\n", argv[0]);

    return 0;
}