Пример #1
0
void queue_test()
{
  int i;
  int item;
  int length = 128;

  Queue *queue = make_queue(length);
  assert(queue_empty(queue));
  for (i=0; i<length-1; i++) {
    queue_push(queue, i);
  }
  assert(queue_full(queue));
  for (i=0; i<10; i++) {
    item = queue_pop(queue);
    assert(i == item);
  }
  assert(!queue_empty(queue));
  assert(!queue_full(queue));
  for (i=0; i<10; i++) {
    queue_push(queue, i);
  }
  assert(queue_full(queue));
  for (i=0; i<10; i++) {
    item = queue_pop(queue);
  }  
  assert(item == 19);
}
int main(void) {
  int buf[MAX_BUF] = {0,};

  if( !input("input.txt", buf) ) {
    fprintf(stderr, "ERROR: file open error or buffer overflow\n");
    delete_all_memory();
    exit(EXIT_FAILURE);
  }
  if(!make_stack(buf)) {
    fprintf(stderr, "ERROR: memory allocation failed\n");
    delete_all_memory();
    exit(EXIT_FAILURE);
  }
  if(!make_queue(buf)) {
    fprintf(stderr, "ERROR: memory allocation failed\n");
    delete_all_memory();
    exit(EXIT_FAILURE);
  }
  if(!make_circle(buf)) {
    fprintf(stderr, "ERROR: memory allocation failed or Queue size overflow\n");
    delete_all_memory();
    exit(EXIT_FAILURE);
  }
  output();

  exit(EXIT_SUCCESS);
}
Пример #3
0
Level_Order_Tree_Iterator_Impl<T>::Level_Order_Tree_Iterator_Impl (Tree<T> &tree)
  // You fill in here.
  :queue_(make_queue(Options::instance()->queue_type())), rvalue()
{
  // You fill in here.
  if(!tree.is_null())
    {
      queue_->enqueue(tree);
      rvalue = queue_->front();
    }
}
Пример #4
0
int main(int argc,char  ** argv) {
    if(argc!=3) {
        printf("ERROR in call one of the client\n");
        exit(-1);
    }
    cln_num=atoi(argv[2]);
    num_cl=atoi(argv[1]);
    make_queue();

    return 0;
}
Пример #5
0
static void number_connected_verts(struct mds* m, mds_id v,
    struct mds_tag* tag, mds_id* label)
{
  struct queue q;
  struct mds_set adj[2];
  int i;
  adj[0].n = adj[1].n = 0;
  if (!visit(m, tag, label, v))
    return;
  make_queue(&q, m->n[MDS_VERTEX]);
  push_queue(&q, v);
  while ( ! queue_empty(&q)) {
    v = pop_queue(&q);
    mds_get_adjacent(m, v, 1, &adj[1]);
    adj[0].n = adj[1].n;
    for (i = 0; i < adj[1].n; ++i)
      adj[0].e[i] = other_vert(m, adj[1].e[i], v);
    for (i = 0; i < adj[0].n; ++i)
      if (visit(m, tag, label, adj[0].e[i]))
        push_queue(&q, adj[0].e[i]);
  }
  free_queue(&q);
}
Пример #6
0
void 
carmen_conventional_dynamic_program(int goal_x, int goal_y) 
{
  double *utility_ptr;
  int index;
  double max_val, min_val;
  int num_expanded;
  int done;

  struct timeval start_time, end_time;
  int delta_sec, delta_usec;
  static double last_time, cur_time, last_print;
  
  queue state_queue;
  state_ptr current_state;

  if (costs == NULL)
    return;

  gettimeofday(&start_time, NULL);

  cur_time = carmen_get_time();
  if ((int)(cur_time - last_print) > 10) {
    carmen_verbose("Time since last DP: %d secs, %d usecs\n", (int)(cur_time - last_time),
		   ((int)((cur_time-last_time)*1e6)) % 1000000);
    last_print = cur_time;
  }
  last_time = cur_time;

  if (utility == NULL) {
    utility = (double *)calloc(x_size*y_size, sizeof(double));
    carmen_test_alloc(utility);
  }
  
  utility_ptr = utility;
  for (index = 0; index < x_size * y_size; index++) 
    *(utility_ptr++) = -1;

  if (is_out_of_map(goal_x, goal_y))
    return;

  max_val = -MAXDOUBLE;
  min_val = MAXDOUBLE; 
  done = 0;

  state_queue = make_queue();

  current_state = carmen_conventional_create_state(goal_x, goal_y, 0);
  max_val = MAX_UTILITY;
  *(utility_value(goal_x, goal_y)) = max_val;
  add_neighbours_to_queue(goal_x, goal_y, state_queue);    
  num_expanded = 1;
  
  while ((current_state = pop_queue(state_queue)) != NULL) {
    num_expanded++;
    if (current_state->cost <= *(utility_value(current_state->x, current_state->y)))
      add_neighbours_to_queue(current_state->x, current_state->y, state_queue);
    if (current_state->cost < min_val)
      min_val = current_state->cost;
    free(current_state);
  }

  delete_queue(&state_queue);

  gettimeofday(&end_time, NULL);

  delta_usec = end_time.tv_usec - start_time.tv_usec;
  delta_sec = end_time.tv_sec - start_time.tv_sec;
  if (delta_usec < 0) {
    delta_sec--;
    delta_usec += 1000000;
  }

  //  carmen_warn("Elasped time for dp: %d secs, %d usecs\n", delta_sec, delta_usec);
}
Пример #7
0
/***
 *  share_add()
 *      Add a share: NET SHARE netname[=resource[;resource...]]
 *
 *  Args:
 *      name - netname=resource string
 *      pass - password
 *      type - 0: unknown, STYPE_PRINTQ: printQ, STYPE_DEVICE: comm
 *
 *  Returns:
 *      nothing - success
 *      exit(2) - command failed
 */
VOID share_add(TCHAR * name, TCHAR * pass, int type)
{
    USHORT          err;                /* API return status */
    TCHAR     *     resource;
    TCHAR FAR *     ptr;
    struct share_info_2 FAR * share_entry;
    ULONG           setType;
    TCHAR                    disk_dev_buf[4];

//
// The redir doesn't have to be running on NT for the server to work
//

    start_autostart(txt_SERVICE_FILE_SRV);

    share_entry =
        (struct share_info_2 FAR *) MGetBuffer(BIG_BUFFER_SIZE);

    /*
     * If name isn't IPC$ and we're on a peer server, then set the
     * mac_uses to MAX_PEER_USERS
     */
    if (stricmpf(name,IPC_DOLLAR) && (QueryServerType() == PEER_BASE_VER))
        share_entry->shi2_max_uses = MAX_PEER_USERS;
    else
        share_entry->shi2_max_uses = (DWORD) SHI_USES_UNLIMITED;

    share_entry->shi2_permissions = ACCESS_ALL & (~ACCESS_PERM); /* default */
    share_entry->shi2_remark = 0L;

    /* Find netname and resource. We determine a value for resource rather  */
    /* strangely due to problems caused by _tcschr() returning a FAR char * */
    /* and resource needing to be a NEAR char *.                                                    */

    if (ptr = _tcschr(name, '='))
    {
        *ptr = NULLC;
        resource = name + _tcslen(name) + 1;

        /* if use specified path for IPC$ or ADMIN$, barf! */
        if (!stricmpf(name, ADMIN_DOLLAR) || !stricmpf(name, IPC_DOLLAR))
            ErrorExit(APE_CannotShareSpecial) ;
    }
    else
        resource = NULL;

    /* Check here for 8.3 FAT name compliance. If the name is not FAT 8.3,  */
    /* warn the admin and query as to whether he wants to continue.         */
    /* (fix for bug 818)                                                    */

    {
        ULONG        Type;

        if (I_MNetPathType(NULL, name, &Type, INPT_FLAGS_OLDPATHS))
            if ( !YorN(APE2_SHARE_MSG_NONFAT, TRUE) )
                return;
    }

    COPYTOARRAY(share_entry->shi2_netname, name);

#ifndef NTENV
    if (type == STYPE_PRINTQ)
    {
        share_entry->shi2_path = name;
        share_entry->shi2_type = STYPE_PRINTQ;
        make_queue(share_entry->shi2_netname, resource);
        share_entry->shi2_permissions = ACCESS_CREATE; /* default */
    }
    else
#endif
    if (type == STYPE_DEVICE)
    {
        share_entry->shi2_type = STYPE_DEVICE;
        share_entry->shi2_path = resource;
        share_entry->shi2_permissions = ACCESS_CREATE | ACCESS_WRITE |
                                        ACCESS_READ;
    }
    else if (resource == NULL)
    {
        /* Here must have  IPC$ or ADMIN$.  Assume the parser got it right  */
        if (! stricmpf(share_entry->shi2_netname, ADMIN_DOLLAR))
        {
            share_entry->shi2_type = STYPE_DISKTREE;
            share_entry->shi2_path = NULL;
        }
        else
        {
            share_entry->shi2_type = STYPE_IPC;
            share_entry->shi2_path = NULL;
        }
    }
    else
    {
        /* Disk or Spooled thing? */

        if (I_MNetPathType(NULL, resource, &setType, 0L))
            /*  resource has already been typed successfully
             *  by the call to I_NetListCanon, so this error
             *  must mean that we have a LIST.
             */
            setType = ITYPE_DEVICE_LPT;

        if (setType == ITYPE_DEVICE_DISK)
        {
            strncpyf(disk_dev_buf,resource,3);
            _tcscpy(disk_dev_buf+2, TEXT("\\"));
            share_entry->shi2_path = (TCHAR FAR *)disk_dev_buf;
            share_entry->shi2_type = STYPE_DISKTREE;
        }
        else
#ifndef NTENV
        if (setType == ITYPE_PATH_ABSD)
#endif
        {
            share_entry->shi2_type = STYPE_DISKTREE;
            share_entry->shi2_path = resource;
        }
#ifndef NTENV
        else
        {
            /* assume it is a list of print devices */
            make_queue(share_entry->shi2_netname, resource);
            share_entry->shi2_type = STYPE_PRINTQ;
            share_entry->shi2_path = name;
            share_entry->shi2_permissions = ACCESS_CREATE; /* default */
        }
#endif
    }

    COPYTOARRAY(share_entry->shi2_passwd, TEXT(""));

    share_munge(share_entry);

    if ((share_entry->shi2_type == STYPE_DISKTREE) && resource)
    {
        TCHAR dev[DEVLEN+1] ;
  
        dev[0] = *resource ;
        dev[1] = TEXT(':') ;
        dev[2] = TEXT('\\') ;
        dev[3] = 0 ;

        if (GetDriveType(dev) == DRIVE_REMOTE)
            ErrorExit(APE_BadResource) ;
    }

    if (err = MNetShareAdd(NULL,
                          2,
                          (LPBYTE)share_entry,
                          LITTLE_BUFFER_SIZE))
        ErrorExit(err);

    InfoPrintInsTxt(APE_ShareSuccess, share_entry->shi2_netname);
    NetApiBufferFree((TCHAR FAR *) share_entry);
}
Пример #8
0
Level_Order_Tree_Iterator_Impl<T>::Level_Order_Tree_Iterator_Impl()
  // You fill in here.
  :queue_(make_queue(Options::instance()->queue_type())), rvalue(0, false)
{
}
static void search(carmen_roadmap_vertex_t *start_node, 
		   carmen_roadmap_t *roadmap)
{
  int num_expanded;
  state_ptr current_state;
  int i, j;
  double *fwd_utilities, *open_list;
  int *parent_ptrs;
  carmen_roadmap_vertex_t *node_list;
  carmen_roadmap_edge_t *edges;
  double min_neighbour_utility;
  int min_neighbour, neighbour_id;
  int min_edge;
  double best_utility, utility;
  int best_neighbour;
  queue the_state_queue = NULL;

  the_state_queue = make_queue();

  node_list = (carmen_roadmap_vertex_t *)(roadmap->nodes->list);

  fwd_utilities = (double *)calloc(roadmap->nodes->length, sizeof(double));
  carmen_test_alloc(fwd_utilities);
  for (i = 0; i < roadmap->nodes->length; i++)
    fwd_utilities[i] = FLT_MAX;

  open_list = (double *)calloc(roadmap->nodes->length, sizeof(double));
  carmen_test_alloc(open_list);
  for (i = 0; i < roadmap->nodes->length; i++)
    open_list[i] = -1;

  parent_ptrs = (int *)calloc(roadmap->nodes->length, sizeof(int));
  carmen_test_alloc(parent_ptrs);
  for (i = 0; i < roadmap->nodes->length; i++)
    parent_ptrs[i] = -1;

  push_state(start_node->id, start_node->id, 0, 0, the_state_queue);
  fwd_utilities[start_node->id] = 0;

  node_list[roadmap->goal_id].utility = 0;
  num_expanded = 0;

  while ((current_state = pop_queue(the_state_queue)) != NULL) {
    num_expanded++;
    if (0)
    carmen_warn("Popped %d\n", current_state->id);
    fwd_utilities[current_state->id] = 
      fwd_utilities[current_state->parent_id] + current_state->cost;
    parent_ptrs[current_state->id] = current_state->parent_id;
    open_list[current_state->id] = -2;
    assert (fwd_utilities[current_state->id] < 1e5);
    if (current_state->id == roadmap->goal_id) {
      if (node_list[roadmap->goal_id].edges->length == 0)
	construct_edges(roadmap, roadmap->goal_id);
      empty_queue(the_state_queue);
    } else {
      add_neighbours_to_queue(roadmap, current_state->id,
			      roadmap->goal_id, fwd_utilities, open_list,
			      the_state_queue);
    }

    free(current_state);
  }

  carmen_warn("Num Expanded %d\n", num_expanded);

  if (fwd_utilities[roadmap->goal_id] > FLT_MAX/2)
    carmen_warn("PROBLEM\n");
  else if (0) {
    node_list[roadmap->goal_id].utility = 0;
    i = roadmap->goal_id;
    while (i != start_node->id) {
      edges = (carmen_roadmap_edge_t *)(node_list[i].edges->list);
      assert (node_list[i].edges->length > 0);
      min_neighbour_utility = FLT_MAX;      
      min_neighbour = -1;
      min_edge = -1;
      for (j = 0; j < node_list[i].edges->length; j++) {
	if (edges[j].cost > 1e5 || edges[j].blocked)
	  continue;
	neighbour_id = edges[j].id;
	assert (fwd_utilities[neighbour_id] >= 0);
	if (fwd_utilities[neighbour_id] < min_neighbour_utility) {
	  min_neighbour_utility = fwd_utilities[neighbour_id];
	  min_neighbour = neighbour_id;
	  min_edge = j;
	}
      }
      assert (min_neighbour >= 0);
      assert (min_neighbour_utility < fwd_utilities[i]);
      assert(node_list[min_neighbour].utility > FLT_MAX/2 ||
	     node_list[min_neighbour].utility ==
	     node_list[i].utility + edges[min_edge].cost);      

      if (node_list[min_neighbour].utility > FLT_MAX/2) {
	node_list[min_neighbour].utility = node_list[i].utility + 
	  edges[min_edge].cost;

	assert(node_list[i].utility < FLT_MAX/2);
	assert(fwd_utilities[i] < FLT_MAX/2);
	assert(fwd_utilities[min_neighbour] < FLT_MAX/2);
	assert(node_list[min_neighbour].utility < 1e5);
      }
      assert (node_list[i].utility < node_list[min_neighbour].utility);
      //      carmen_warn("%d %d %f %f\n", node_list[i].x, node_list[i].y, node_list[i].utility, FLT_MAX/2);

      i = min_neighbour;
      assert(node_list[i].utility < FLT_MAX/2);
    }
  } else {
    node_list[roadmap->goal_id].utility = 0;
    i = roadmap->goal_id;
    while (i != start_node->id) {
      min_neighbour = parent_ptrs[i];
      assert (min_neighbour >= 0);

      min_neighbour_utility = fwd_utilities[min_neighbour];
      assert (min_neighbour_utility < fwd_utilities[i]);

      edges = (carmen_roadmap_edge_t *)(node_list[i].edges->list);
      assert (node_list[i].edges->length > 0);
      for (min_edge = 0; min_edge < node_list[i].edges->length; min_edge++) {
	if (edges[min_edge].id == min_neighbour)
	  break;
      }
      assert (min_edge < node_list[i].edges->length);
      assert(node_list[min_neighbour].utility > FLT_MAX/2 ||
	     fabs(node_list[min_neighbour].utility  - 
		  (node_list[i].utility + edges[min_edge].cost)) < 1e6);      

      if (node_list[min_neighbour].utility > FLT_MAX/2) {
	carmen_roadmap_edge_t *edges2;
	edges2 = (carmen_roadmap_edge_t *)node_list[min_neighbour].edges->list;
	node_list[min_neighbour].utility = node_list[i].utility + 
	  edges[min_edge].cost;
	best_neighbour = -1;
	best_utility = FLT_MAX;
	for (j = 0; j < node_list[min_neighbour].edges->length; j++) {
	  if (edges2[j].cost > 1e5 || edges2[j].blocked)
	    continue;      
	  if (node_list[edges2[j].id].utility > FLT_MAX/2)
	    continue;
	  utility = node_list[edges2[j].id].utility;
	  if (utility < best_utility) {
	    best_utility = utility;
	    best_neighbour = edges2[j].id;
	  }
	}
	
	if (best_neighbour < 0)
	  continue;
	assert (best_utility < FLT_MAX/2);
	assert (node_list[best_neighbour].utility < 
		node_list[min_neighbour].utility);

	assert(node_list[i].utility < FLT_MAX/2);
	assert(fwd_utilities[i] < FLT_MAX/2);
	assert(fwd_utilities[min_neighbour] < FLT_MAX/2);
	assert(node_list[min_neighbour].utility < 1e5);
      }
      assert (node_list[i].utility < node_list[min_neighbour].utility);
      //      carmen_warn("%d %d %f %f\n", node_list[i].x, node_list[i].y, node_list[i].utility, FLT_MAX/2);

      i = min_neighbour;
      assert(node_list[i].utility < FLT_MAX/2);
    }
  }

  /* Check to make sure that no node is a local minimum. The only node that's
     allowed to be a local minimum is the goal.
   */
  for (i = 0; i < roadmap->nodes->length; i++) {
    if (i == roadmap->goal_id)
      continue;
    edges = (carmen_roadmap_edge_t *)(node_list[i].edges->list);

    if (node_list[i].utility > FLT_MAX/2)
      continue;

    best_neighbour = -1;
    best_utility = FLT_MAX;
    for (j = 0; j < node_list[i].edges->length; j++) {
      if (edges[j].cost > 1e5 || edges[j].blocked)
	continue;      
      if (node_list[edges[j].id].utility > FLT_MAX/2)
	continue;
      utility = node_list[edges[j].id].utility;
      if (utility < best_utility) {
	best_utility = utility;
	best_neighbour = edges[j].id;
      }
    }

    if (best_neighbour < 0)
      continue;
    assert (best_utility < FLT_MAX/2);
    assert (node_list[best_neighbour].utility < node_list[i].utility);
  }

  free(fwd_utilities);
  delete_queue(&the_state_queue);
}
Пример #10
0
Shared *make_shared()
{
  Shared *shared = check_malloc(sizeof(Shared));
  shared->queue = make_queue(128);
  return shared;
}