Esempio n. 1
0
File: map.hpp Progetto: Nikhil14/Map
void Map<T,H>::copy_map(Node*n)
{
    if(n == nullptr)return;
    this->insert(*n->pair_obj); 
    copy_map(n->left);
    copy_map(n->right);
}
  void map_regions_impl(viennagrid::base_mesh<mesh_is_const> const & src_mesh,
                        viennagrid::mesh const & dst_mesh,
                        SegmentIDMapT const & region_id_map)
  {
    typedef viennagrid::base_mesh<mesh_is_const>                                  SrcMeshType;
    typedef viennagrid::mesh                                                      DstMeshType;

    viennagrid::result_of::element_copy_map<>::type copy_map(dst_mesh, false);

    typedef typename viennagrid::result_of::const_cell_range<SrcMeshType>::type   ConstCellRangeType;
    typedef typename viennagrid::result_of::iterator<ConstCellRangeType>::type    ConstCellIteratorType;

    typedef typename viennagrid::result_of::region_range<SrcMeshType>::type       SrcRegionRangeType;
    typedef typename viennagrid::result_of::iterator<SrcRegionRangeType>::type    SrcRegionRangeIterator;
    typedef typename viennagrid::result_of::region_id<SrcMeshType>::type          SrcRegionIDType;
    typedef typename viennagrid::result_of::region_id<DstMeshType>::type          DstRegionIDType;

    typedef typename viennagrid::result_of::element<DstMeshType>::type            CellType;

    std::map<SrcRegionIDType, std::string> region_id_name_map;
    std::map<std::string, DstRegionIDType> region_name_id_map;

    SrcRegionRangeType src_regions(src_mesh);
    for (SrcRegionRangeIterator rit = src_regions.begin(); rit != src_regions.end(); ++rit)
    {
      region_id_name_map[ (*rit).id() ]   = (*rit).get_name();
      region_name_id_map[ (*rit).get_name() ] = (*rit).id();
    }


    ConstCellRangeType cells(src_mesh);
    for (ConstCellIteratorType cit = cells.begin(); cit != cells.end(); ++cit)
    {
      CellType cell = copy_map(*cit );
      std::set<DstRegionIDType> dst_region_ids;

      SrcRegionRangeType element_regions(*cit);
      for (SrcRegionRangeIterator rit = element_regions.begin(); rit != element_regions.end(); ++rit)
      {
        typename SegmentIDMapT::const_iterator dst_region_id_it = region_id_map.find( region_id_name_map[(*rit).id()] );
        if (dst_region_id_it != region_id_map.end())
          dst_region_ids.insert( region_name_id_map[dst_region_id_it->second] );
        else
          dst_region_ids.insert( (*rit).id() );
      }

      for (typename std::set<DstRegionIDType>::const_iterator dst_region_id_it = dst_region_ids.begin(); dst_region_id_it != dst_region_ids.end(); ++dst_region_id_it)
        viennagrid::add( dst_mesh.get_or_create_region(*dst_region_id_it), cell );
    }
  }
Esempio n. 3
0
    PopulationMap<Point,Class>
        LocalSearchFirstFound(const PopulationMap<Point,Class>& orig_map,
                              int iterations) {
    // At least 1 iteration
    assert(iterations > 0);
    int curr_iterations = 0;

    PopulationMap<Point, Class> map(orig_map);

    float curr_quality  = map.EvaluateQuality();

    while (curr_iterations < iterations) {
        PopulationMap<Point, Class> copy_map(map);

        // Get the quality of the modified map
        copy_map.NeighborhoodOperator(true);
        float copy_quality = copy_map.EvaluateQuality();

        // If the quality is better than the previous map, we found a new map
        if (curr_quality < copy_quality) {
            map             = copy_map;
            map.reset();
            curr_iterations = 0;
            curr_quality    = map.EvaluateQuality();
        } else {
            map.SetToPerturb(copy_map.SetToPerturb());
            map.UnusedPointsToToggle(copy_map.UnusedPointsToToggle());
            ++curr_iterations;
        }
    }

    return map;
}
Esempio n. 4
0
void	algo(char before[SIZE_X][SIZE_Y], char after[SIZE_X][SIZE_Y], int turn)
{
	int	i;
	int j;

	i = -1;
	j = -1;
	if (turn == 0)
		turn = -1;
	while (turn != 0)
	{
		while (++i < SIZE_X)
		{
			while (++j < SIZE_Y)
			{
				if (check_around(before, i, j) == 3)
					after[i][j] = 1;
				else if (check_around(before, i, j) == 2 && before[i][j] == 1)
					after[i][j] = 1;
				else
					after[i][j] = 0;
			}
			j = -1;
		}
		i = -1;
		copy_map(after, before);
		clear_map(after);
		ft_putstr("\033[H\033[2J");
		show_map(before);
		usleep(TURN_TIME);
		if (turn != -1)
			--turn;
	}
}
Esempio n. 5
0
File: map.hpp Progetto: Nikhil14/Map
Map<T,H>& Map<T,H>::operator=(const Map<T,H>& other) 
{   
    //std::cout<<"Call"<<'\n';
    if(this!=&other)
    {
        this->delete_all_nodes(this->root);
        copy_map(other.root);
    }
    return *this;
}
Esempio n. 6
0
void GameArea::cur_block_2_map()
{
  copy_map(_copy_map, _cur_map, _row, _col);
  int row = _start_x / _step;
  int col = _start_y / _step;

  // change coordinate
  for (int i = row; i < row + 4; ++i){
    for (int j = col; j < col + 4; ++j) {
      *(_cur_map + i*_col + j) |= *(_cur_map + (i-row)*4 + (j-col));
    }
  }
}
Esempio n. 7
0
void config_cache::load_configs(const std::string& config_path, config& cfg)
{
	// Make sure that we have fake transaction if no real one is going on
	fake_transaction fake;

	if (use_cache_) {
		read_cache(config_path, cfg);
	} else {
		preproc_map copy_map(make_copy_map());
		read_configs(config_path, cfg, copy_map);
		add_defines_map_diff(copy_map);
	}
}
  bool check_hull_topology::run(viennamesh::algorithm_handle &)
  {
    mesh_handle input_mesh = get_required_input<mesh_handle>("mesh");
    mesh_handle output_mesh = make_data<mesh_handle>();

    typedef viennagrid::mesh                                                            MeshType;

    typedef viennagrid::result_of::const_element_range<MeshType>::type                  ConstElementRangeType;
    typedef viennagrid::result_of::iterator<ConstElementRangeType>::type                ConstElementIteratorType;

    typedef viennagrid::result_of::const_coboundary_range<MeshType>::type               ConstCoboundaryElementRangeType;
    typedef viennagrid::result_of::iterator<ConstCoboundaryElementRangeType>::type      ConstCoboundaryElementIteratorType;

    viennagrid::result_of::element_copy_map<>::type copy_map(output_mesh(), true);


    ConstElementRangeType lines( input_mesh(), 1 );
    for (ConstElementIteratorType lit = lines.begin(); lit != lines.end(); ++lit)
    {
      ConstCoboundaryElementRangeType coboundary_triangles( input_mesh(), *lit, 2 );
      if (coboundary_triangles.size() < 2)
      {
        std::cout << "Line " << *lit << " has less than 2 co-boundary triangles" << std::endl;

        for (ConstCoboundaryElementIteratorType ctit = coboundary_triangles.begin(); ctit != coboundary_triangles.end(); ++ctit)
        {
          copy_map(*ctit);
        }
      }

    }

    set_output( "mesh", output_mesh );

    return true;
  }
  void merge_meshes_impl(viennagrid::base_mesh<mesh_is_const> const & src_mesh,
                         viennagrid::mesh const & dst_mesh,
                         double tolerance, bool region_offset)
  {
    viennagrid::result_of::element_copy_map<>::type copy_map(dst_mesh, tolerance, false);

    typedef viennagrid::base_mesh<mesh_is_const>                                    SrcMeshType;

    typedef typename viennagrid::result_of::element<SrcMeshType>::type              CellType;

    typedef typename viennagrid::result_of::const_cell_range<SrcMeshType>::type     ConstCellRangeType;
    typedef typename viennagrid::result_of::iterator<ConstCellRangeType>::type      ConstCellIteratorType;

    typedef typename viennagrid::result_of::region_range<SrcMeshType>::type         SrcRegionRangeType;
    typedef typename viennagrid::result_of::iterator<SrcRegionRangeType>::type      SrcRegionRangeIterator;

    int region_id_offset = dst_mesh.region_count();
    int source_region_count = src_mesh.region_count();

    ConstCellRangeType cells(src_mesh);
    for (ConstCellIteratorType cit = cells.begin(); cit != cells.end(); ++cit)
    {
      CellType cell = copy_map(*cit);

      if (source_region_count <= 1)
        viennagrid::add( dst_mesh.get_or_create_region(region_offset ? region_id_offset : 0), cell );
      else
      {
        SrcRegionRangeType region_range(*cit);
        for (SrcRegionRangeIterator rit = region_range.begin(); rit != region_range.end(); ++rit)
        {
          viennagrid::add( dst_mesh.get_or_create_region((*rit).id() + (region_offset ? region_id_offset : 0)), cell );
        }
      }
    }
  }
Esempio n. 10
0
    PopulationMap<Point,Class>
        LocalSearchFirstFoundRec(const PopulationMap<Point,Class>& map,
                                 float map_quality,
                                 int curr_iterations,
                                 int max_iterations) {
    PopulationMap<Point,Class> copy_map(map);

    if (curr_iterations == 0) {
        return map;
    }

    copy_map.NeighborhoodOperator(false);
    float copy_quality = copy_map.EvaluateQuality();

    return copy_quality > map_quality ?
    LocalSearchFirstFoundRec<Point,Class>(copy_map, copy_quality,
                                          max_iterations, max_iterations) :
    LocalSearchFirstFoundRec<Point,Class>(map, map_quality,
                                          curr_iterations - 1, max_iterations);
}
Esempio n. 11
0
void GameArea::nextBlock()
{
  copy_map(_cur_map, _copy_map, _row, _col);
  this->clear_row();
}
    void operator()(MeshType const & input_mesh,
                    MeshType const & output_mesh,
                    viennagrid_plc plc_output_mesh,
                    PointType const & N)
    {
      typedef viennagrid::result_of::const_element_range<MeshType>::type ConstElementRangeType;
      typedef viennagrid::result_of::iterator<ConstElementRangeType>::type ConstElementRangeIterator;

      viennagrid::result_of::element_copy_map<>::type copy_map(output_mesh, false);

      ConstElementRangeType triangles( input_mesh, 2 );
      for (ConstElementRangeIterator tit = triangles.begin(); tit != triangles.end(); ++tit)
      {
        ElementType v[3];
        PointType p[3];
        double dp[3];

        for (int pi = 0; pi != 3; ++pi)
        {
          v[pi] = viennagrid::vertices(*tit)[pi];
          p[pi] = viennagrid::get_point( v[pi] );

          dp[pi] = viennagrid::inner_prod( p[pi], N );
        }

        if ( !inside(dp[0]) && !inside(dp[1]) && !inside(dp[2]) )
        {
          // all points outside -> ignore
          continue;
        }

        int on_plane_count = 0;
        for (int pi = 0; pi != 3; ++pi)
          if ( on_plane(dp[pi]) )
            ++on_plane_count;

        if (on_plane_count == 3)
          continue;

        if (on_plane_count == 2)
        {
//           std::cout << "!!!!!!!!!!!!!!!!!!!!!!!!  on_plane_count = 2" << std::endl;
          int not_on_plane_index = !on_plane(dp[0]) ? 0 : !on_plane(dp[1]) ? 1 : 2;
          if ( inside(dp[not_on_plane_index]) )
          {
            copy_map(*tit);

            int oi0 = (not_on_plane_index == 0) ? 1 : 0;
            int oi1 = (not_on_plane_index == 2) ? 1 : 2;

            add_line(copy_map(v[oi0]), copy_map(v[oi1]));
          }
//           else
//             std::cout << "   outside -> skipping" << std::endl;
          continue;
        }

        if (on_plane_count == 1)
        {
//           std::cout << "!!!!!!!!!!!!!!!!!!!!!!!!  on_plane_count = 1" << std::endl;
          int on_plane_index = on_plane(dp[0]) ? 0 : on_plane(dp[1]) ? 1 : 2;
          int oi0 = (on_plane_index == 0) ? 1 : 0;
          int oi1 = (on_plane_index == 2) ? 1 : 2;

          if ( !inside(dp[oi0]) && !inside(dp[oi1]) )
            continue;

          if ( inside(dp[oi0]) && inside(dp[oi1]) )
          {
            copy_map(*tit);
            continue;
          }

          // oi0 is inside
          if ( !inside(dp[oi0]) )
            std::swap(oi0, oi1);

          ElementType v_ = get_vertex(output_mesh, N, v[oi0], v[oi1]);
          viennagrid::make_triangle( output_mesh, copy_map(v[on_plane_index]), copy_map(v[oi0]), v_);

          add_line(v_, copy_map(v[on_plane_index]));
          continue;
        }


        if ( inside(dp[0]) && inside(dp[1]) && inside(dp[2])  )
        {
          // all points inside -> copy
          copy_map(*tit);
          continue;
        }

        int pos_count = 0;
        for (int pi = 0; pi != 3; ++pi)
          if ( inside(dp[pi]) )
            ++pos_count;

        int pi = (dp[0]*dp[1] > 0) ? 2 : (dp[1]*dp[2] > 0) ? 0 : 1;
        int oi0 = (pi == 0) ? 1 : 0;
        int oi1 = (pi == 2) ? 1 : 2;

        ElementType v1_ = get_vertex(output_mesh, N, v[pi], v[oi0]);
        ElementType v2_ = get_vertex(output_mesh, N, v[pi], v[oi1]);
        add_line(v1_, v2_);

        if (pos_count == 1)
        {
          viennagrid::make_triangle( output_mesh, copy_map(v[pi]), v1_, v2_);
        }
        else
        {
          viennagrid::make_triangle( output_mesh, copy_map(v[oi0]), copy_map(v[oi1]), v1_);
          viennagrid::make_triangle( output_mesh, copy_map(v[oi1]), v1_, v2_);
        }
      }

      std::vector<viennagrid_int> line_ids;
      std::map<ElementType, viennagrid_int> vertices_on_hyperplane;

      for (LinesOnHyperplaneType::iterator it = lines_on_hyperplane.begin(); it != lines_on_hyperplane.end(); ++it)
      {
        vertices_on_hyperplane.insert( std::make_pair((*it).first, -1) );
        vertices_on_hyperplane.insert( std::make_pair((*it).second, -1) );
      }

      for (std::map<ElementType, viennagrid_int>::iterator vit = vertices_on_hyperplane.begin(); vit != vertices_on_hyperplane.end(); ++vit)
      {
        PointType p = viennagrid::get_point( (*vit).first );
        viennagrid_plc_vertex_create(plc_output_mesh, &p[0], &vit->second);
      }

      for (LinesOnHyperplaneType::iterator it = lines_on_hyperplane.begin(); it != lines_on_hyperplane.end(); ++it)
      {
        viennagrid_int line_id;
        viennagrid_plc_line_create(plc_output_mesh,
                                   vertices_on_hyperplane[(*it).first],
                                   vertices_on_hyperplane[(*it).second],
                                   &line_id);
        line_ids.push_back(line_id);
      }

      viennagrid_plc_facet_create(plc_output_mesh, line_ids.size(), &line_ids[0], NULL);
    }
Esempio n. 13
0
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    
    int block_idx = (int) (mxGetScalar(prhs[1]));
    int translation = (int) (mxGetScalar(prhs[2]));
    int rotation = (int) (mxGetScalar(prhs[3]));
    double* map0 = mxGetPr(prhs[0]);
    int block[4][2];
    int block_pos[2];
    int new_block_pos[2];
    int map_2D[19][12];
    
    double* game_over;
    double* map1;
    
    int i;
    
    plhs[0] =mxCreateDoubleMatrix(19,12, mxREAL); /*resulting board*/
    plhs[1] =mxCreateDoubleMatrix(1,1, mxREAL); /*game over or not*/
    
    map1 = mxGetPr(plhs[0]);
    game_over = mxGetPr(plhs[1]);
    
    /* // default outcomes: */
    game_over[0] = 1;
    copy_map(map1, map0);
    
        
    if(block_idx <1 || block_idx > 7){
        printf("illegal block_idx: %d\n", block_idx);
        return;
    }
    
    if(translation > 10 || translation < 1){
        if(translation>10)
            translation = 10;
        if(translation < 1)
            translation = 1;
    }
    
    if(rotation < 0 || rotation > 3){
        while(rotation < 0)
            rotation+=4;
        while(rotation > 3)
            rotation -= 4;
    }
    
    build_2D_map(map0, map_2D);

/* //    printf("printing before block placement\n"); */
/* //    print_2D_map(map_2D); */

    copy_block(blocks[block_idx-1], block);
        
    game_over[0] = 0;
    
    block_pos[1] = BoxY-3;
    block_pos[0] = (int) (floor(BoxX/2));
    
    /*% rotate action*/
    for (i=1; i<=rotation; ++i){
        rotate_block(block);
    }
    
    
    /* check if game over*/
    if(collision(map_2D, block_pos, block)){
        copy_map(map1, map0);
        game_over[0] = 1;
        return;
    }

    

    
    /*% translate action*/
    while( translation < block_pos[0]){
        new_block_pos[0] = block_pos[0]-1;
        new_block_pos[1] = block_pos[1];
        
        if (!collision(map_2D, new_block_pos, block)){
            block_pos[0] = new_block_pos[0];
        } else {
            break;
        }
    }
    
    while( translation > block_pos[0]){
        new_block_pos[0] = block_pos[0]+1;
        new_block_pos[1] = block_pos[1];
        
        if (!collision(map_2D, new_block_pos, block)){
            block_pos[0] = new_block_pos[0];
        } else {
            break;
        }
    }
    
    /*%drop shape:*/
    new_block_pos[0] = block_pos[0];
    new_block_pos[1] = block_pos[1];
    while(!collision(map_2D, new_block_pos, block)){
        new_block_pos[1] = new_block_pos[1] - 1;
    }
    block_pos[1] = new_block_pos[1]+1;
    
    
    
    place_block_in_map(map_2D, block_pos, block);
/* //    printf("printing after block placement\n"); */
/* //    print_2D_map(map_2D); */
        
    /* % check for filled rows */
    check_map_for_filled_rows(map_2D);
    
/* //    printf("printing after row clearing\n"); */
/* //    print_2D_map(map_2D); */
    
    write_2D_map_to_1D(map_2D, map1);

    game_over[0] = 0;
}
Esempio n. 14
0
void config_cache::read_cache(const std::string& file_path, config& cfg)
{
	static const std::string extension = ".gz";

	std::stringstream defines_string;
	defines_string << file_path;

	bool is_valid = true;

	for(const preproc_map::value_type& d : defines_map_) {
		//
		// Only WESNOTH_VERSION is allowed to be non-empty.
		//
		if((!d.second.value.empty() || !d.second.arguments.empty()) &&
		   d.first != "WESNOTH_VERSION")
		{
			is_valid = false;
			ERR_CACHE << "Invalid preprocessor define: " << d.first << '\n';
			break;
		}

		defines_string << " " << d.first;
	}

	// Do cache check only if define map is valid and
	// caching is allowed.
	const std::string& cache_path = filesystem::get_cache_dir();

	if(is_valid && !cache_path.empty()) {
		// Use a hash for a shorter display of the defines.
		const std::string fname = cache_path + "/" +
								  cache_file_prefix_ +
								  sha1_hash(defines_string.str()).display();
		const std::string fname_checksum = fname + ".checksum" + extension;

		filesystem::file_tree_checksum dir_checksum;

		if(!force_valid_cache_ && !fake_invalid_cache_) {
			try {
				if(filesystem::file_exists(fname_checksum)) {
					config checksum_cfg;

					DBG_CACHE << "Reading checksum: " << fname_checksum << "\n";
					read_file(fname_checksum, checksum_cfg);

					dir_checksum = filesystem::file_tree_checksum(checksum_cfg);
				}
			} catch(config::error&) {
				ERR_CACHE << "cache checksum is corrupt" << std::endl;
			} catch(filesystem::io_exception&) {
				ERR_CACHE << "error reading cache checksum" << std::endl;
			}
		}

		if(force_valid_cache_) {
			LOG_CACHE << "skipping cache validation (forced)\n";
		}

		if(filesystem::file_exists(fname + extension) && (force_valid_cache_ || (dir_checksum == filesystem::data_tree_checksum()))) {
			LOG_CACHE << "found valid cache at '" << fname << extension << "' with defines_map " << defines_string.str() << "\n";
			log_scope("read cache");

			try {
				read_file(fname + extension,cfg);
				const std::string define_file = fname + ".define" + extension;

				if(filesystem::file_exists(define_file)) {
					config_cache_transaction::instance().add_define_file(define_file);
				}

				return;
			} catch(config::error& e) {
				ERR_CACHE << "cache " << fname << extension << " is corrupt. Loading from files: "<< e.message << std::endl;
			} catch(filesystem::io_exception&) {
				ERR_CACHE << "error reading cache " << fname << extension << ". Loading from files" << std::endl;
			} catch (boost::iostreams::gzip_error& e) {
				//read_file -> ... -> read_gz can throw this exception.
				ERR_CACHE << "cache " << fname << extension << " is corrupt. Error code: " << e.error() << std::endl;
			}
		}

		LOG_CACHE << "no valid cache found. Writing cache to '" << fname << extension << " with defines_map "<< defines_string.str() << "'\n";

		// Now we need queued defines so read them to memory
		read_defines_queue();

		preproc_map copy_map(make_copy_map());

		read_configs(file_path, cfg, copy_map);
		add_defines_map_diff(copy_map);

		try {
			write_file(fname + extension, cfg);
			write_file(fname + ".define" + extension, copy_map);

			config checksum_cfg;

			filesystem::data_tree_checksum().write(checksum_cfg);
			write_file(fname_checksum, checksum_cfg);
		} catch(filesystem::io_exception&) {
			ERR_CACHE << "could not write to cache '" << fname << "'" << std::endl;
		}

		return;
	}

	LOG_CACHE << "Loading plain config instead of cache\n";

	preproc_map copy_map(make_copy_map());
	read_configs(file_path, cfg, copy_map);
	add_defines_map_diff(copy_map);
}
Esempio n. 15
0
File: map.hpp Progetto: Nikhil14/Map
Map<T,H>::Map(const Map& copy)
{
    copy_map(copy.root);
}