vertex variable::to_graph(map<string, vertex>& env, map<string,type*> const& inputs, map<string,type*> const& outputs, graph& g) { map<string,vertex>::iterator i = env.find(name); if (i == env.end()) { map<string,type*>::const_iterator j = inputs.find(name); if (j == inputs.end()) { map<string,type*>::const_iterator k = outputs.find(name); if (k == outputs.end()) { std::cerr << "undefined variable " << name << std::endl; exit(-1); return 0; } else { std::cerr << "outputs should not be read before an assignment" << std::endl; exit(-1); } } else { // variable is an input variable vertex_info vi(*(j->second), input, name); set_container_size(name, vi.t); vertex v = g.add_vertex(vi); env[name] = v; return v; } } else { // already in the environment return i->second; } }
void program2graph(vector<stmt*> const& p, map<string,type*>& inputs, map<string,type*>& inouts, map<string,type*>& outputs, graph& g) { map<string, type*>::iterator i; for (i = inouts.begin(); i != inouts.end(); ++i) { inputs.insert(*i); outputs.insert(*i); } for (i = inputs.begin(); i != inputs.end(); i++) { set_container_size(i->first, *(i->second)); } for (i = outputs.begin(); i != outputs.end(); i++) { set_container_size(i->first, *(i->second)); } map<string, vertex> env; for (unsigned int i = 0; i != p.size(); ++i) { stmt* s = p[i]; vertex tmp = s->rhs->to_graph(env, inputs, outputs, g); env[s->lhs] = tmp; } ///// DEBUG #ifdef DEBUG std::ofstream out("lower0.dot"); print_graph(out, g); #endif for (map<string,type*>::iterator i = outputs.begin(); i != outputs.end(); ++i) { vertex def = env[i->first]; vertex_info vi; vi.t = *i->second; vi.op = output; vi.label = i->first; vi.eval = evaluate; vi.trivial = true; string name = i->first; set_container_size(name, vi.t); vertex v = g.add_vertex(vi); g.add_edge(def, v); } // push out types up to operation types for (vertex i = 0; i < g.num_vertices(); i++) { if (g.info(i).op == output) { std::vector<vertex> const& iav = g.inv_adj(i); for (vertex j = 0; j < iav.size(); j++) { if (g.info(iav[j]).t.k == unknown) { g.info(iav[j]).t = g.info(i).t; } } } } }
vertex scalar_in::to_graph(map<string, vertex>& env, map<string,type*> const& inputs, map<string,type*> const& outputs, graph& g) { //type t(scalar, "1"); type t(general); vertex_info vi(t, input, boost::lexical_cast<string>(val), val); return g.add_vertex(vi); }
int find_or_add_op(op_code op, vector<vertex> const& rands, graph& g) { for (unsigned int i = 0; i != g.num_vertices(); ++i) { if (g.info(i).op == op) { if (rands.size() == g.inv_adj(i).size() && includes(rands.begin(), rands.end(), g.inv_adj(i).begin(), g.inv_adj(i).end())) return i; } } int n; if (op == trans && rands.size() > 0 && g.info(rands[0]).label.compare("") != 0) { vertex_info vi(op, g.info(rands[0]).label); n = g.add_vertex(vi); } else { vertex_info vi(op); n = g.add_vertex(vi); } for (unsigned int i = 0; i != rands.size(); ++i) g.add_edge(rands[i], n); return n; }
/* * Given the tokens from one line (split on whitespace), this reads the * p-line from these tokens and initializes the corresponding globals */ void read_problem(const std::vector<std::string>& tokens, graph& g) { if (current_state != COMMENT_SECTION) { throw std::invalid_argument(INV_FMT); } current_state = P_LINE; if(tokens.size() != 4 || tokens[1] != "tw") { throw std::invalid_argument(INV_PROB); } n_vertices = pure_stou(tokens[2]); n_edges = pure_stou(tokens[3]); while (g.add_vertex()+1 < n_vertices) {}; }
// read in 120 cities and their latitude/longitude // cities within limit km of each other are connected by edges void init_graph_from_file(graph& g, const string& filename, double limit) { string line; string city_name; int lat1, lat2, long1, long2; ifstream file_to_read; string part_string; char compass_dir; // open the data file of cities open_for_read(file_to_read, filename.c_str()); size_t count = 0; // keep reading until all cities have been added while (is_more_stuff_there(file_to_read) && count < 120) { string city_name = ""; // build the city name from the file do { file_to_read >> part_string; city_name += part_string + ' '; } while (city_name[city_name.length() - 2] != ':'); city_name = city_name.substr(0, city_name.length() - 2); // grab latitude coordinates file_to_read >> lat1 >> lat2 >> compass_dir; // southern coordinates are negative if (compass_dir == 'S') { lat1 *= -1; lat2 *= -1; } // and grab longitude coordinates file_to_read >> long1 >> long2 >> compass_dir; // western coordinates are negative if (compass_dir == 'W') { long1 *= -1; long2 *= -1; } // create a city vertex in the graph from the data you read vertex city(city_name, lat1, lat2, long1, long2); g.add_vertex(city); ++count; } // now we are done with our file file_to_read.close(); // now we compute the edges that are within the allowed distance vector<vertex>::iterator it1, it2; for (it1 = g.vertices.begin(); it1 != g.vertices.end(); ++it1) { for (it2 = g.vertices.begin(); it2 != g.vertices.end(); ++it2) { if (it1 != it2) { g.add_edge(&*it1, &*it2, limit); } } } }
int load(const int argc, const char *argv[]) { /*************************** * CHECK IF IS FILE EXISTS * ***************************/ if(argc < 2) { fprintf(stdout, "Warning: Resource file not loaded.\nUsage: %s [path_of_file]\n", argv[0]); return 1; } parser fp; if(fp.open(argv[1]) != true) { fprintf(stdout, "Error: File '%s' not found.\n", argv[1]); return 2; } clock_t begin, end; double elapsed_secs = 0; /*************************** * READ FILE * ***************************/ std::cout << "Step 1 of 2: Read a file, add to the vertex of the graph, add to the BST (together) . . . "; begin = clock(); fp.parse( [&] (const std::string &line) { std::string arr[5]; fp.divide_by_delimiter(arr, line, 5, '\t'); /* arr[0] = StateAbb arr[1] = PlaceName arr[2] = longitude arr[3] = latitude arr[4] = DistancetoCapitalCity_km */ std::string city_name = arr[1]; double longitude, latitude; try { longitude = stod(arr[2]); latitude = stod(arr[3]); } catch(const std::invalid_argument &e) { return; /* Invalid line, so we should not add this line */ } /* Add to graph */ int key = g.add_vertex(graphelement(arr[1])); if(key == -1) /* Duplicate city name */ { char *temp = new char[5](); for(int i = 2; ; ++i) /* suffix start with #2 */ { std::snprintf(temp, 5, "%d", i); if((key = g.add_vertex(graphelement(std::string(city_name + " #" + temp)))) != -1) { city_name = city_name + " #" + temp; break; } } delete temp; } /* Add to BST */ bstelement *element = new bstelement(city_name, key, longitude, latitude); bst->add(element); } ); end = clock(); std::cout << "Finished (" << elapsed_secs(begin, end) << "sec)" << std::endl; //bst->printall(); std::cout << "Step 2 of 2: Add the edges to each vertex . . . "; begin = clock(); double lat1, lon1, lat2, lon2; bst->traverse_level_order( [&] (bstnode *node) { bst->binary_search_tree::traverse_level_order( [&] (bstnode *n) { if(10000 >= calc_distance(node->element->latitude, node->element->longitude, n->element->latitude, n->element->longitude)) { if(node->element->latitude < n->element->latitude && node->element->longitude < n->element->longitude) { bool is_connected = false; g.traverse(n->element->value, [&] (graph<graphelement>::node* edgn) { if(edgn->key == node->element->value) is_connected = true; } ); if(! is_connected) g.add_edge(node->element->value, n->element->value); } } } ); } ); end = clock(); std::cout << "Finished (" << elapsed_secs(begin, end) << "sec)" << std::endl; g.print(); }