helper.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // (C) Copyright Andrew Sutton 2007
  2. //
  3. // Use, modification and distribution are subject to the
  4. // Boost Software License, Version 1.0 (See accompanying file
  5. // LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_GRAPH_EXAMPLE_HELPER_HPP
  7. #define BOOST_GRAPH_EXAMPLE_HELPER_HPP
  8. #include <string>
  9. #include <sstream>
  10. #include <map>
  11. #include <algorithm>
  12. #include <boost/graph/properties.hpp>
  13. template <typename Graph, typename NameMap, typename VertexMap>
  14. typename boost::graph_traits<Graph>::vertex_descriptor
  15. add_named_vertex(Graph& g, NameMap nm, const std::string& name, VertexMap& vm)
  16. {
  17. typedef typename boost::graph_traits<Graph>::vertex_descriptor Vertex;
  18. typedef typename VertexMap::iterator Iterator;
  19. Vertex v;
  20. Iterator iter;
  21. bool inserted;
  22. boost::tie(iter, inserted) = vm.insert(make_pair(name, Vertex()));
  23. if(inserted) {
  24. // The name was unique so we need to add a vertex to the graph
  25. v = add_vertex(g);
  26. iter->second = v;
  27. put(nm, v, name); // store the name in the name map
  28. }
  29. else {
  30. // We had alread inserted this name so we can return the
  31. // associated vertex.
  32. v = iter->second;
  33. }
  34. return v;
  35. }
  36. template <typename Graph, typename NameMap, typename InputStream>
  37. inline std::map<std::string, typename boost::graph_traits<Graph>::vertex_descriptor>
  38. read_graph(Graph& g, NameMap nm, InputStream& is)
  39. {
  40. typedef typename boost::graph_traits<Graph>::vertex_descriptor Vertex;
  41. std::map<std::string, Vertex> verts;
  42. for(std::string line; std::getline(is, line); ) {
  43. if(line.empty()) continue;
  44. std::size_t index = line.find_first_of(',');
  45. std::string first(line, 0, index);
  46. std::string second(line, index + 1);
  47. Vertex u = add_named_vertex(g, nm, first, verts);
  48. Vertex v = add_named_vertex(g, nm, second, verts);
  49. add_edge(u, v, g);
  50. }
  51. return verts;
  52. }
  53. template <typename Graph, typename InputStream>
  54. inline std::map<std::string, typename boost::graph_traits<Graph>::vertex_descriptor>
  55. read_graph(Graph& g, InputStream& is)
  56. {
  57. typedef typename boost::graph_traits<Graph>::vertex_descriptor Vertex;
  58. typedef boost::null_property_map<Vertex, std::string> NameMap;
  59. return read_graph(g, NameMap(), is);
  60. }
  61. template <typename Graph, typename NameMap, typename WeightMap, typename InputStream>
  62. inline std::map<std::string, typename boost::graph_traits<Graph>::vertex_descriptor>
  63. read_weighted_graph(Graph& g, NameMap nm, WeightMap wm, InputStream& is)
  64. {
  65. typedef typename boost::graph_traits<Graph>::vertex_descriptor Vertex;
  66. typedef typename boost::graph_traits<Graph>::edge_descriptor Edge;
  67. std::map<std::string, Vertex> verts;
  68. for(std::string line; std::getline(is, line); ) {
  69. if(line.empty()) continue;
  70. std::size_t i = line.find_first_of(',');
  71. std::size_t j = line.find_first_of(',', i + 1);
  72. std::string first(line, 0, i);
  73. std::string second(line, i + 1, j - i - 1);
  74. std::string prob(line, j + 1);
  75. // convert the probability to a float
  76. std::stringstream ss(prob);
  77. float p;
  78. ss >> p;
  79. // add the vertices to the graph
  80. Vertex u = add_named_vertex(g, nm, first, verts);
  81. Vertex v = add_named_vertex(g, nm, second, verts);
  82. // add the edge and set the weight
  83. Edge e = add_edge(u, v, g).first;
  84. put(wm, e, p);
  85. }
  86. return verts;
  87. }
  88. template <typename Graph, typename WeightMap, typename InputStream>
  89. inline std::map<std::string, typename boost::graph_traits<Graph>::vertex_descriptor>
  90. read_weighted_graph(Graph& g, WeightMap wm, InputStream& is)
  91. {
  92. typedef typename boost::graph_traits<Graph>::vertex_descriptor Vertex;
  93. typedef boost::null_property_map<Vertex, std::string> NameMap;
  94. return read_weighted_graph(g, NameMap(), wm, is);
  95. }
  96. #endif