Exemplo n.º 1
3
int traverseBFS(Graph graph, int start, Dllist close) {
	Dllist node, queue;
	JRB visited;
	int *output;
	int temp;

	int i, n, counter = 0;

	visited = make_jrb();
	queue = new_dllist();
	dll_append(queue, new_jval_i(start));

	while(!dll_empty(queue)) {
		node = dll_first(queue);
		temp = jval_i(node->val);
		dll_delete_node(node);

		if(jrb_find_int(visited, temp) == NULL) {
			counter++;
			// reportFunc(temp);
			jrb_insert_int(visited, temp, new_jval_i(temp));
			
			n = outdegree(graph, temp, output);
			for(i = 0; i < n; i++) {
				if(jrb_find_int(visited, output[i]) == NULL) {
					dll_append(queue, new_jval_i(output[i]));
				}
			}
		}
	}

	return counter;
}
Exemplo n.º 2
0
int main()
{
  JRB myJ = make_jrb();
  JRB nod;
  Jval tmp = new_jval_i(5);
  char *key = "luong";
  jrb_insert_str(myJ, key, tmp);
  tmp = new_jval_i(4);
  jrb_insert_str(myJ, key, tmp);
  jrb_traverse(nod, myJ){
    printf("%d ", jval_i(nod->val));
  }
Exemplo n.º 3
0
int main (int argc, char **argv)
{
  struct msgbuff{
    long mtype;
    pid_t pid;
  }message;

  pid_t pid, npid;
  int i;

  int           msqid;
  key_t         keyx;
  struct msqid_ds msq;

  Dllist q = new_dllist();
  Dllist n;

  keyx = ftok(KEYFILE_PATH, (int)ID);

  msqid = msgget(keyx, 0666 | IPC_CREAT);

  if (msqid == -1) {
    perror("msgget");
    exit(1);
  }

  while(1) { 
    if((msgrcv(msqid, &message, sizeof(pid_t), 1, 0)) ==
       MSGQ_NG){
      perror("msgrcv");
      exit(1);
    }
    pid = message.pid;
    if(pid != 1) {
      if(dll_empty(q)) kill(pid, SIGUSR1);
      else {
        n = dll_first(q);
        npid = jval_i(dll_val(n));
        if(pid == npid) { 
          kill(pid, SIGUSR1);
          dll_delete_node(n);
        } else dll_append(q, new_jval_i(pid));
      }
    } else {
      if(!dll_empty(q)) { 
        n = dll_first(q);
        npid = jval_i(dll_val(n));
        kill(npid, SIGUSR1);
        dll_delete_node(n);
      }
    }
  }

  return 0;
}
Exemplo n.º 4
0
int deepFirstSearch(Graph graph, int start, int stop, Dllist close) {
	Dllist node, stack;
	JRB visited;
	int output[100];
	int temp;

	int i, n;

	visited = make_jrb();
	stack = new_dllist();
	dll_prepend(stack, new_jval_i(start));

	while(!dll_empty(stack)) {
		node = dll_first(stack);
		temp = jval_i(node->val);
		dll_delete_node(node);

		if(jrb_find_int(visited, temp) == NULL) {
			// reportFunc(temp);
			dll_append(close, new_jval_i(temp));
			jrb_insert_int(visited, temp, new_jval_i(temp));
			
			if(compare(temp, stop) == 0) {
				jrb_free_tree(visited);
				free_dllist(stack);
				return 1;
			}

			n = outdegree(graph, temp, output);
			for(i = 0; i < n; i++) {
				if(jrb_find_int(visited, output[i]) == NULL) {
					dll_prepend(stack, new_jval_i(output[i]));
				}
			}
		}
	}

	jrb_free_tree(visited);
	free_dllist(stack);

	return 0;
}
Exemplo n.º 5
0
int UShortestPath(Graph graph, int start, int stop, Dllist close) {
	Dllist node, queue, stackVisit;
	JRB visited;
	int output[100];
	int temp;

	int i, n;

	visited = make_jrb();
	queue = new_dllist();
	stackVisit = new_dllist();
	dll_append(queue, new_jval_i(start));

	while(!dll_empty(queue)) {
		node = dll_first(queue);
		temp = jval_i(node->val);
		dll_delete_node(node);

		if(jrb_find_int(visited, temp) == NULL) {
			jrb_insert_int(visited, temp, new_jval_i(temp));
			dll_prepend(stackVisit, new_jval_i(temp));
			
			if(temp == stop) {
				return solution(graph, start, stop, stackVisit, close);
			}

			n = outdegree(graph, temp, output);
			for(i = 0; i < n; i++) {
				if(jrb_find_int(visited, output[i]) == NULL) {
					dll_append(queue, new_jval_i(output[i]));
				}
			}
		}
	}

	return -1;
}
Exemplo n.º 6
0
void finesleep_sleep(void *a, double t)
{
  Finesleep *fs;
  fd_set r;
  struct timeval tv;
  double newtime;
  JRB ptr;
  double diff;

  fs = (Finesleep *) a;
  if (fs->cheat) {
    newtime = fs->stime + t;
    pthread_mutex_lock(fs->lock);
    ptr = jrb_insert_dbl(fs->tree, newtime, new_jval_i(0));
    diff = newtime - fs->stime;
    pthread_mutex_unlock(fs->lock);
  } else {
    diff = t;
  }
  while (1) {
    tv.tv_sec = diff;
    diff -= tv.tv_sec;
    diff *= 1000000.0;
    tv.tv_usec = diff;
    FD_ZERO(&r);
    FD_SET(fs->fd[0], &r);
    select(fs->fd[0]+1, &r, NULL, NULL, &tv);
    if (fs->cheat) {
      pthread_mutex_lock(fs->lock);
      if (fs->tree->flink->key.d == newtime) {
        jrb_delete_node(ptr);
        fs->stime = newtime;
        pthread_mutex_unlock(fs->lock);
        return;
      } else {
        diff = newtime - fs->tree->flink->key.d;
        pthread_mutex_unlock(fs->lock);
      }
    } else {
      return;
    }
  }
}
Exemplo n.º 7
0
/*
 * insert bid in descending cost
 */
void insert_bid(char *msg)
{
  char *p;
  Bid *b, *tb;
  int i, id;
  Dllist ptr;

  // create a new bid
  b = (Bid *) malloc(sizeof(Bid));
  p = strstr(msg, "B");
  p++;
  b->cost = atoi(p);
  p = strstr(p, "$");
  p++;
  b->numrobot = atoi(p);
  b->coalition = new_dllist();
  for (i = 0; i < b->numrobot; i++) {
    p = strstr(p, "$");
    p++;
    id = atoi(p);
    if (dll_empty(b->coalition)) b->leaderID = id;
    dll_append(b->coalition, new_jval_i(id)); 
  }

  printf("insert bid: %s\n", msg);
  // insert the bid to bidList, increasing cost
  if (dll_empty(bidList)) {
    dll_append(bidList, new_jval_v(b));
  } else {
    dll_traverse(ptr, bidList) {
      tb = (Bid *) jval_v(dll_val(ptr));
      if (tb->cost > b->cost) {
        dll_insert_b(ptr, new_jval_v(b));
        break;
      }
      if (ptr == dll_last(bidList)) {// append it to the last
        dll_append(bidList, new_jval_v(b));
        break;
      } 
    }
  }
Exemplo n.º 8
0
main()
{
  IS is;
  JRB t, tmp;
  int i;
  int j;
  int x;

  j = 0;
  t = make_jrb();
  is = new_inputstruct(NULL);

  while (get_line(is) >= 0) {
    for (i = 0; i < is->NF; i++) {
      jrb_insert_str(t, strdup(is->fields[i]), new_jval_i(j));
      j++;
    }
  }

  jrb_traverse(tmp, t) {
    x = strlen(tmp->key.s);
    printf("%c", tmp->key.s[tmp->val.i%x]);
  }