Exemplo n.º 1
0
List *Path(World *world, Point *source, Point *destination)
{
  int tentative_gscore;
  List *result = NULL;
  int tries = 0;

  Hashmap *nodes = Hashmap_create(cmp, hash);
  Hashmap *closedset = Hashmap_create(cmp, hash);
  PQueue *openset = PQueue_create(cmp, hash);

  Node *current;
  Node *start = Node_create(source, 0, 0, NULL);
  Hashmap_set(nodes, start, start);

  start->fscore = start->gscore + heuristic_cost_estimate(start->point, destination);
  PQueue_push(openset, start, start->fscore);

  while(!PQueue_empty(openset) &&
	tries < 300) {
    tries++;


    current = PQueue_pop(openset);
    Hashmap_set(closedset, current, current);

    if(POINT_EQ(current->point, destination)) {
      result = reconstruct_path(current);
      break;

    } else {

      List *neighbours = neighbours_list(world, current->point, destination, nodes);

      LIST_FOREACH(neighbours, first, next, cur) {
	Node *neighbour = cur->value;

	if(Hashmap_get(closedset, neighbour) == NULL) {
	  tentative_gscore =  current->gscore + 1;

	  if(!PQueue_contains(openset, neighbour) ||
	     tentative_gscore > neighbour->gscore) {
	    if(!PQueue_contains(openset, neighbour)) {
	      neighbour->came_from = current;
	      neighbour->gscore = tentative_gscore;
	      neighbour->fscore = neighbour->gscore + heuristic_cost_estimate(neighbour->point, destination);
	      PQueue_push(openset, neighbour, neighbour->fscore);
	    }
	  }
	}
      }

      List_destroy(neighbours);
    }
  }
Exemplo n.º 2
0
Arquivo: vm.c Projeto: txus/terrorvm
void VM_start(bstring binary, bstring filename)
{
  STATE = State_new();

  Runtime_init(state);

  VALUE lobby  = Lobby_new(state); // toplevel object
  state->lobby = lobby;
  state->binary = binary;

  BytecodeFile *file = BytecodeFile_new(state, filename);

  int fn_count = DArray_count(file->function_names);
  for(int j=0; j < fn_count; j++) {
    bstring fn_name = (bstring)DArray_at(file->function_names, j);
    Function *fn = (Function*)Hashmap_get(file->functions, fn_name);
    Hashmap_set(state->functions, fn_name, fn);
  }

  bstring main_fn = bfromcstr("0_main");
  CallFrame *top_frame = CallFrame_new(lobby, STATE_FN(main_fn), NULL);
  bdestroy(main_fn);
  top_frame->name = "main";

  Stack_push(FRAMES, top_frame);

  // now we're ready to bootstrap
  State_bootstrap(state);

  // and run the codes!
  VM_run(state);

  State_destroy(state);
}
Exemplo n.º 3
0
static List *neighbours_list(World *world, Point *point, Point *destination, Hashmap *nodes)
{
  List *neighbours = List_create();
  int nx, ny;

  for(nx = point->x - 1; nx <= point->x + 1; nx++) {
    if(nx < 0 || nx >= world->width) continue;
    for(ny = point->y - 1; ny <= point->y + 1; ny++) {
      if(ny < 0 || ny >= world->height ||
	 (ny == point->y && nx == point->x) ||
	 (!World_can_enter(world, nx, ny, point->z) &&
	  !(nx == destination->x && ny == destination->y))) continue;

      Point *p  = Point_create(nx, ny, point->z);
      Node *node = Node_create(p, 0, 0, NULL);

      Node *old_node = Hashmap_get(nodes, node);
      if(old_node) {
	Node_destroy(node);
	node = old_node;
      } else {
	Hashmap_set(nodes, node, node);
      }

      List_push(neighbours, node);
    }
  }

  return neighbours;
}
Exemplo n.º 4
0
char *test_get_set()
{
    int rc = Hashmap_set(map, &test1, &expect1);
    mu_assert(rc == 0, "Failed to set &test1");
    bstring result = Hashmap_get(map, &test1);
    mu_assert(result == &expect1, "Wrong value for test1.");

    rc = Hashmap_set(map, &test2, &expect2);
    mu_assert(rc == 0, "Failed to set test2");
    result = Hashmap_get(map, &test2);
    mu_assert(result == &expect2, "Wrong value for test2.");

    rc = Hashmap_set(map, &test3, &expect3);
    mu_assert(rc == 0, "Failed to set test3");
    result = Hashmap_get(map, &test3);
    mu_assert(result == &expect3, "Wrong value for test3.");

    return NULL;
}
Exemplo n.º 5
0
int main(int argc, char const *argv[])
{
	Hashmap *map = Hashmap_create(NULL, Hashmap_fnv1a_hash);
	char * test1 = "test1";
	char * test2 = "test2";
	char * test3 = "test3";
	char *expect1 = "expect1";
	char *expect2 = "expect2";
	char *expect3 = "expect3";

	int rc1 = Hashmap_set(map, &test1, &expect1);
	int rc2 = Hashmap_set(map, &test2, &expect2);
	int rc3 = Hashmap_set(map, &test3, &expect3);

	int *res = Hashmap_get(map, &test1);

	printf("res = %s\n", (char *)*res);

	return 0;
}
Exemplo n.º 6
0
char *test_get_set()
{
	/*be attention that bsStatic return value only
 * 	can be used as tagstring type  ,the point to which is the bstring
 */
	int rc =Hashmap_set(map , &test1 , &expect1);
	mu_assert(rc == 0 , "failed to set &test1");
	bstring result = Hashmap_get(map , &test1);
	mu_assert(result == &expect1, "wrong value for test1");
	
	rc = Hashmap_set(map , &test2 , &expect2);
	mu_assert(rc == 0 , "failed to set test2");
	result = Hashmap_get(map , &test2);
	mu_assert(result == &expect2 , "wrong values for test2 ");

	rc = Hashmap_set(map , &test3 , &expect3);
	mu_assert(rc == 0 , "failed to set test2");
	result = Hashmap_get(map , &test3);
	mu_assert(result == &expect3 , "wrong values for test2 ");
	
	return NULL;
}
Exemplo n.º 7
0
int Server_create_stat(Hashmap *store, char *name)
{
  bstring b_name = bfromcstr(name);
  Stats *new_stat = Stats_create();
  check(new_stat != NULL, "Failed to create stat");
  int rc = Hashmap_set(store, b_name, new_stat);

  return rc;

error:
  if (b_name) { free(b_name); }
  return -1;
}
Exemplo n.º 8
0
int PQueue_push(PQueue *p_queue, void *item, int priority)
{
  PQueueNode *node = malloc(sizeof(PQueueNode));
  check_mem(node);
  node->item = item;
  node->priority = priority;

  int rc = Hashmap_set(p_queue->map, item, node);
  check(rc != -1, "Failed to set hash");

  rc = BHeap_insert(p_queue->heap, node);
  if(rc == -1) {
    Hashmap_delete(p_queue->map, item);
    sentinel("Failed to push noe");
  }

  return 1;

 error:
  if(node) free(node);
  return -1;
}
Exemplo n.º 9
0
Object*
Hash_new(Object *array_obj) {
  DArray *array = (DArray*)array_obj->value.other;
  int count = DArray_count(array);
  assert(count % 2 == 0 && "Hash element count must be even.");

  Hashmap *hash = Hashmap_create(String_compare, String_hash);

  int i=0;
  for(i=0; i < count; i += 2) {
    Object *key   = retain((Object*)DArray_at(array, i));
    Object *value = retain((Object*)DArray_at(array, i+1));
    assert(key->type == tString && "All hash keys must be strings");
    Hashmap_set(hash, key, value, value->type);
  }

  Object *object      = Object_new();
  object->type        = tHash;
  object->value.other = hash;

  Object_define_native_method(object, bfromcstr("[]"), Primitive_Hash_get, 1);

  return object;
}
Exemplo n.º 10
0
int Object_register_slot(Object *receiver, bstring slot_name, Object *value) {
  int rc = Hashmap_set(receiver->slots, slot_name, value, value->type);
  assert(rc == 0 && "Could not register slot.");
  return rc;
}
Exemplo n.º 11
0
void Object_define_method(Object *object, bstring name, Instruction **instructions, int instructions_count, short arity) {
  Object *fn = Function_new(instructions, instructions_count, arity);
  Hashmap_set(object->slots, name, fn, tFunction);
}
Exemplo n.º 12
0
void Object_define_native_method(Object *object, bstring name, native_fn native_function, short arity) {
  Object *fn = Function_native_new(native_function, arity);
  Hashmap_set(object->slots, name, fn, tFunction);
}