reachable-loop-head.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //=======================================================================
  2. // Copyright 2001 Jeremy G. Siek, Andrew Lumsdaine, Lie-Quan Lee,
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See
  5. // accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //=======================================================================
  8. /*
  9. IMPORTANT!!!
  10. ~~~~~~~~~~~~
  11. This example uses interfaces that have been deprecated and removed from Boost.Grpah.
  12. Someone needs to update it, as it does NOT compile.
  13. */
  14. #include <boost/config.hpp>
  15. #include <iostream>
  16. #include <fstream>
  17. #include <boost/graph/adjacency_list.hpp>
  18. #include <boost/graph/depth_first_search.hpp>
  19. #include <boost/graph/graphviz.hpp>
  20. #include <boost/graph/copy.hpp>
  21. int
  22. main(int argc, char *argv[])
  23. {
  24. if (argc < 3) {
  25. std::cerr << "usage: reachable-loop-head.exe <in-file> <out-file>"
  26. << std::endl;
  27. return -1;
  28. }
  29. using namespace boost;
  30. GraphvizDigraph g;
  31. read_graphviz(argv[1], g);
  32. graph_traits < GraphvizDigraph >::vertex_descriptor loop_head = 1;
  33. typedef color_traits < default_color_type > Color;
  34. std::vector < default_color_type >
  35. reachable_from_head(num_vertices(g), Color::white());
  36. default_color_type c;
  37. depth_first_visit(g, loop_head, default_dfs_visitor(),
  38. make_iterator_property_map(reachable_from_head.begin(),
  39. get(vertex_index, g), c));
  40. property_map<GraphvizDigraph, vertex_attribute_t>::type
  41. vattr_map = get(vertex_attribute, g);
  42. graph_traits < GraphvizDigraph >::vertex_iterator i, i_end;
  43. for (boost::tie(i, i_end) = vertices(g); i != i_end; ++i)
  44. if (reachable_from_head[*i] != Color::white()) {
  45. vattr_map[*i]["color"] = "gray";
  46. vattr_map[*i]["style"] = "filled";
  47. }
  48. std::ofstream loops_out(argv[2]);
  49. #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
  50. // VC++ has trouble with the get_property() functions
  51. loops_out << "digraph G {\n"
  52. << "size=\"3,3\"\n"
  53. << "ratio=\"fill\"\n"
  54. << "shape=\"box\"\n";
  55. graph_traits<GraphvizDigraph>::vertex_iterator vi, vi_end;
  56. for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) {
  57. loops_out << *vi << "[";
  58. for (std::map<std::string,std::string>::iterator ai = vattr_map[*vi].begin();
  59. ai != vattr_map[*vi].end(); ++ai) {
  60. loops_out << ai->first << "=" << ai->second;
  61. if (next(ai) != vattr_map[*vi].end())
  62. loops_out << ", ";
  63. }
  64. loops_out<< "]";
  65. }
  66. property_map<GraphvizDigraph, edge_attribute_t>::type
  67. eattr_map = get(edge_attribute, g);
  68. graph_traits<GraphvizDigraph>::edge_iterator ei, ei_end;
  69. for (boost::tie(ei, ei_end) = edges(g); ei != ei_end; ++ei) {
  70. loops_out << source(*ei, g) << " -> " << target(*ei, g) << "[";
  71. std::map<std::string,std::string>& attr_map = eattr_map[*ei];
  72. for (std::map<std::string,std::string>::iterator eai = attr_map.begin();
  73. eai != attr_map.end(); ++eai) {
  74. loops_out << eai->first << "=" << eai->second;
  75. if (next(eai) != attr_map.end())
  76. loops_out << ", ";
  77. }
  78. loops_out<< "]";
  79. }
  80. loops_out << "}\n";
  81. #else
  82. get_property(g, graph_graph_attribute)["size"] = "3,3";
  83. get_property(g, graph_graph_attribute)["ratio"] = "fill";
  84. get_property(g, graph_vertex_attribute)["shape"] = "box";
  85. write_graphviz(loops_out, g,
  86. make_vertex_attributes_writer(g),
  87. make_edge_attributes_writer(g),
  88. make_graph_attributes_writer(g));
  89. #endif
  90. return EXIT_SUCCESS;
  91. }