inline std::map<std::string, typename boost::graph_traits<Graph>::vertex_descriptor> read_weighted_graph(Graph& g, NameMap nm, WeightMap wm, InputStream& is) { typedef typename boost::graph_traits<Graph>::vertex_descriptor Vertex; typedef typename boost::graph_traits<Graph>::edge_descriptor Edge; std::map<std::string, Vertex> verts; for(std::string line; std::getline(is, line); ) { if(line.empty()) continue; std::size_t i = line.find_first_of(','); std::size_t j = line.find_first_of(',', i + 1); std::string first(line, 0, i); std::string second(line, i + 1, j - i - 1); std::string prob(line, j + 1); // convert the probability to a float std::stringstream ss(prob); float p; ss >> p; // add the vertices to the graph Vertex u = add_named_vertex(g, nm, first, verts); Vertex v = add_named_vertex(g, nm, second, verts); // add the edge and set the weight Edge e = add_edge(u, v, g).first; put(wm, e, p); } return verts; }
inline std::map<std::string, typename boost::graph_traits<Graph>::vertex_descriptor> read_graph(Graph& g, NameMap nm, InputStream& is) { typedef typename boost::graph_traits<Graph>::vertex_descriptor Vertex; std::map<std::string, Vertex> verts; for(std::string line; std::getline(is, line); ) { if(line.empty()) continue; std::size_t index = line.find_first_of(','); std::string first(line, 0, index); std::string second(line, index + 1); Vertex u = add_named_vertex(g, nm, first, verts); Vertex v = add_named_vertex(g, nm, second, verts); add_edge(u, v, g); } return verts; }
boost::adjacency_list< //!OCLINT Sure it is too long, but this is just a fun function boost::vecS, boost::vecS, boost::directedS, boost::property<boost::vertex_name_t,std::string> > create_tutorial_subchapters_graph() noexcept { auto g = create_empty_directed_named_vertices_graph(); add_named_vertex("Creating an empty directed graph", g); add_named_vertex("Creating an empty undirected graph", g); add_named_vertex("Add a vertex with a property", g); add_named_vertex("Getting the vertices' properties", g); add_named_vertex("Creating a non-empty directed graph", g); add_named_vertex("Creating a non-empty undirected graph", g); add_named_vertex("Has a vertex with a certain property", g); add_named_vertex("Find a vertex by its property", g); add_named_vertex("Get a vertex its property", g); add_named_vertex("Set a vertex its property", g); add_named_vertex("Set all vertices' properties", g); add_named_vertex("Save the graph with those properties", g); add_named_vertex("Load a directed graph with those properties from file", g); add_named_vertex("Load an undirected graph with those properties from file", g); { const std::string s = "Creating an empty directed graph"; add_edge_between_named_vertices(s, std::string("Creating an empty undirected graph"), g); add_edge_between_named_vertices(s, std::string("Add a vertex with a property"), g); } { const std::string s = "Creating an empty undirected graph"; add_edge_between_named_vertices(s, std::string("Add a vertex with a property"), g); } { const std::string s = "Add a vertex with a property"; add_edge_between_named_vertices(s, std::string("Getting the vertices' properties"), g); add_edge_between_named_vertices(s, std::string("Creating a non-empty directed graph"), g); add_edge_between_named_vertices(s, std::string("Creating a non-empty undirected graph"), g); } { const std::string s = "Getting the vertices' properties"; add_edge_between_named_vertices(s, std::string("Creating a non-empty directed graph"), g); add_edge_between_named_vertices(s, std::string("Creating a non-empty undirected graph"), g); } { const std::string s = "Creating a non-empty directed graph"; add_edge_between_named_vertices(s, std::string("Has a vertex with a certain property"), g); add_edge_between_named_vertices(s, std::string("Find a vertex by its property"), g); add_edge_between_named_vertices(s, std::string("Save the graph with those properties"), g); } { const std::string s = "Creating a non-empty undirected graph"; add_edge_between_named_vertices(s, std::string("Has a vertex with a certain property"), g); add_edge_between_named_vertices(s, std::string("Find a vertex by its property"), g); add_edge_between_named_vertices(s, std::string("Save the graph with those properties"), g); } { const std::string s = "Has a vertex with a certain property"; add_edge_between_named_vertices(s, std::string("Find a vertex by its property"), g); } { const std::string s = "Find a vertex by its property"; add_edge_between_named_vertices(s, std::string("Get a vertex its property"), g); } { const std::string s = "Get a vertex its property"; add_edge_between_named_vertices(s, std::string("Set a vertex its property"), g); } { const std::string s = "Set a vertex its property"; add_edge_between_named_vertices(s, std::string("Set all vertices' properties"), g); } { const std::string s = "Set all vertices' properties"; add_edge_between_named_vertices(s, std::string("Save the graph with those properties"), g); } { const std::string s = "Save the graph with those properties"; add_edge_between_named_vertices( s, std::string("Load a directed graph with those properties from file"), g ); add_edge_between_named_vertices( s, std::string("Load an undirected graph with those properties from file"), g ); } return g; }