Пример #1
0
struct program_state *program_state_new(struct context *context, struct map *env)
{
    null_check(context);
    struct program_state *state = (struct program_state*)malloc(sizeof(struct program_state));
    state->named_variables = map_copy(env);
    state->all_variables = array_new();
    state->args = array_new();
    stack_push(context->program_stack, state);
    return state;
}
Пример #2
0
void position_copy (Entity target, const Entity source)
{
	const POSITION
		sourcePosition = component_getData (entity_getAs (source, "position"));
	position_setOrientation (target, sourcePosition->orientation);
	position_setDirect (target, sourcePosition->pos, hexPos_platter (sourcePosition->position, 1));
	if (!sourcePosition->position)
		return;
	position_set (target, map_copy (sourcePosition->position));
}
Пример #3
0
bool vis_window_split(Win *original) {
	Win *win = window_new_file(original->vis, original->file, UI_OPTION_STATUSBAR);
	if (!win)
		return false;
	for (size_t i = 0; i < LENGTH(win->modes); i++) {
		if (original->modes[i].bindings)
			win->modes[i].bindings = map_new();
		if (win->modes[i].bindings)
			map_copy(win->modes[i].bindings, original->modes[i].bindings);
	}
	win->file = original->file;
	win->file->refcount++;
	vis_window_syntax_set(win, vis_window_syntax_get(original));
	view_options_set(win->view, view_options_get(original->view));
	view_cursor_to(win->view, view_cursor_get(original->view));
	return true;
}
Пример #4
0
// a + b; in case of intersection, a wins
struct map *map_union(struct map *a, const struct map *b)
{
    if (b == NULL)
        return a;
    if (a == NULL)
        return map_copy(b->context, b);
    struct array *keys = map_keys(b);
    for (int i=0; i<keys->length; i++) {
        const void *key = array_get(keys, i);
        if (!map_has(a, key)) {
            void *key2 = b->copyor(key, a->context);
            void *value = map_get(b, key);
            void *value2 = b->copyor(value, a->context);
            map_insert(a, key2, value2);
        }
    }
    array_del(keys);
    return a;
}
Пример #5
0
st_map_t *map_put (st_map_t *map, const void *key, const void *object)
{
  int n = 0;

  while (n < map->size && map->data[n].key != MAP_FREE_KEY)
        n++;

  if (n == map->size)           /* current map is full */
  {
    int       new_size = map->size + 20;
    st_map_t *map2     = map_create (new_size);

    map_copy (map2, map);
    free (map);
    map = map2;
  }
  map->data[n].key    = (void*) key;
  map->data[n].object = (void*) object;
  return (map);
}
Пример #6
0
int main()
{
	int i,j,k;
	int black=0;
	int tmp;
	srand(time(NULL));
	map=(Grid**)malloc((SIZE+2)*sizeof(Grid*));
	for(i=0;i<SIZE+2;i++)
	{
		map[i]=(Grid*)malloc((SIZE+2)*sizeof(Grid));
	}
	Grid ** testmap;

	for(i=0;i<=SIZE+1;i++)
	{
		for(j=0;j<=SIZE+1;j++)
		{


			if(i==0||j==0||i==SIZE+1||j==SIZE+1)
			{
				map[i][j].brick=1;
				map[i][j].value=5;
			}

			else
			{
				scanf("%d",&tmp);
				map[i][j].pre_process=1;
				map[i][j].x=i;
				map[i][j].y=j;
				map[i][j].heu=0;
				map[i][j].ph=1;
				if(tmp == 6)//只是空格
				{
					map[i][j].brick=0;
					map[i][j].value=1;
					map[i][j].light = 0;
					map[i][j].cover = 0;
				}
				else if(tmp==7)
				{
					map[i][j].brick=0;
					map[i][j].value=0;
					map[i][j].light = 1;
				}
				else
				{
					map[i][j].brick=1;
					black++;
					map[i][j].value=tmp;

				}
			}
			
		}
	}
	int l,r;
	Row_num=0;
	l=0;r=0;
	map = pre_processing(map);
	map=count_heu(map);
	


	for(i=1;i<=SIZE;i++)
	{
		for(j=1;j<=SIZE;j++)
		{
			if(map[i][j].brick==0)
			{
				l=j;
				while(map[i][j].brick==0)
					j++;
				r=j-1;
				Rows[Row_num].start.x=i;
				Rows[Row_num].start.y=l;

				Rows[Row_num].end.x=i;
				Rows[Row_num].end.y=r;
				Rows[Row_num].num=r-l+1;
				Row_num++;
			}
		}

	}
	map = count_prob(map);
	
	
	
	//for(i=0;i<Row_num;i++)
	//	printf("start :(%d, %d), end : (%d, %d), num : %d\n",Rows[i].start.x,Rows[i].start.y,Rows[i].end.x,Rows[i].end.y,Rows[i].num);
	//testmap = map_copy(testmap,map);

	//Answer A;
	//A=pre_processing(testmap);
	//print_map(testmap);
	Grid*** antSOL;
	antSOL = (Grid***)malloc(NUM_ANT*sizeof(Grid**));
	for(i=0;i<NUM_ANT;i++)
		antSOL[i]=NULL;
	int best;
	for(i=0;i<MAX_ITER;i++)
	{
		best=SIZE*SIZE+black;
		//print_ph(map);
		for(k=0;k<NUM_ANT;k++)
		{
			antSOL[k] = map_copy(antSOL[k],map);
			antSOL[k] = buildSol(antSOL[k]);
				//print_map(antSOL[k]);
				//getchar();
			if(cost(antSOL[k])==0)
			{
				printf("solved!!  iteration = %d\n",i);
				print_map(antSOL[k]);
				return 0;
			}
			if(cost(antSOL[k])<best)
			{
				//print_map(antSOL[k]);
				best=cost(antSOL[k]);
			}
		}

		for(k=0;k<NUM_ANT;k++)
		{
			if(cost(antSOL[k])==best)
			{
				map = update_ph(map,antSOL[k]);
				map = count_prob(map);
				//print_ph(map);
				//getchar();
			}
		}

	}
	printf("can't solve\n");
		for(k=0;k<NUM_ANT;k++)
		{
			if(cost(antSOL[k])==best)
			{
				print_map(antSOL[k]);
			}
		}








		
}
Пример #7
0
int main(int argc, char *argv[]) {
	const char *key = "404";
	const int values[3] = { 0, 1, 2 };

	plan_no_plan();

	Map *map = map_new();

	ok(map && map_empty(map), "Creation");
	ok(map_first(map, &key) == NULL && strcmp(key, "404") == 0, "First on empty map");
	ok(map_empty(map_prefix(map, "404")), "Empty prefix map");

	ok(!map_get(map, "404"), "Get non-existing key");
	ok(!map_contains(map, "404"), "Contains non-existing key");
	ok(!map_closest(map, "404") && errno == ENOENT, "Closest non-existing key");

	ok(!map_put(map, "a", NULL) && errno == EINVAL && map_empty(map) && !map_get(map, "a"), "Put NULL value");
	ok(map_put(map, "a", &values[0]) && !map_empty(map) && get(map, "a", &values[0]), "Put 1");
	ok(map_first(map, &key) == &values[0] && strcmp(key, "a") == 0, "First on map with 1 value");
	key = NULL;
	ok(map_first(map_prefix(map, "a"), &key) == &values[0] && strcmp(key, "a") == 0, "First on prefix map");
	ok(map_contains(map, "a"), "Contains existing key");
	ok(map_closest(map, "a") == &values[0], "Closest match existing key");
	ok(!map_put(map, "a", &values[1]) && errno == EEXIST && get(map, "a", &values[0]), "Put duplicate");
	ok(map_put(map, "cafebabe", &values[2]) && get(map, "cafebabe", &values[2]), "Put 2");
	ok(map_put(map, "cafe", &values[1]) && get(map, "cafe", &values[1]), "Put 3");
	key = NULL;
	ok(map_first(map_prefix(map, "cafe"), &key) == &values[1] && strcmp(key, "cafe") == 0, "First on prefix map with multiple suffixes");

	Map *copy = map_new();
	ok(map_copy(copy, map), "Copy");
	ok(!map_empty(copy), "Not empty after copying");
	map_iterate(copy, compare, map);
	map_iterate(map, compare, copy);

	int counter = 0;
	map_iterate(copy, once, &counter);
	ok(counter == 1, "Iterate stop condition");

	ok(!map_get(map, "ca") && !map_closest(map, "ca") && errno == 0, "Closest ambigious");

	int visited[] = { 0, 0, 0 };

	map_iterate(map, visit, &visited);
	ok(visited[0] == 1 && visited[1] == 1 && visited[2] == 1, "Iterate map");

	memset(visited, 0, sizeof visited);
	order_counter = 0;
	map_iterate(map, order, &visited);
	ok(visited[0] == 1 && visited[1] == 2 && visited[2] == 3, "Ordered iteration");

	memset(visited, 0, sizeof visited);
	map_iterate(map_prefix(map, "ca"), visit, &visited);
	ok(visited[0] == 0 && visited[1] == 1 && visited[2] == 1, "Iterate sub map");

	memset(visited, 0, sizeof visited);
	order_counter = 0;
	map_iterate(map_prefix(map, "ca"), order, &visited);
	ok(visited[0] == 0 && visited[1] == 1 && visited[2] == 2, "Ordered sub map iteration");

	ok(map_empty(map_prefix(map, "404")), "Empty map for non-existing prefix");

	ok(!map_delete(map, "404"), "Delete non-existing key");
	ok(map_delete(map, "cafe") == &values[1] && !map_get(map, "cafe"), "Delete existing key");
	ok(map_closest(map, "cafe") == &values[2], "Closest unambigious");
	ok(map_put(map, "cafe", &values[1]) && get(map, "cafe", &values[1]), "Put 3 again");

	map_clear(map);
	ok(map_empty(map), "Empty after clean");

	map_free(map);
	map_free(copy);

	return exit_status();
}