cycle-file-dep2.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. #include <boost/config.hpp>
  9. #include <fstream>
  10. #include <iostream>
  11. #include <string>
  12. #include <boost/graph/adjacency_list.hpp>
  13. #include <boost/graph/graph_utility.hpp>
  14. // can't do using namespace boost because then
  15. // we get conflict with boost::default_dfs_visitor.
  16. using namespace boost;
  17. namespace std {
  18. template <typename T >
  19. std::istream& operator >> (std::istream & in, std::pair < T, T > &p)
  20. {
  21. in >> p.first >> p.second;
  22. return
  23. in;
  24. }
  25. }
  26. typedef adjacency_list<
  27. listS, // Store out-edges of each vertex in a std::list
  28. vecS, // Store vertex set in a std::vector
  29. directedS // The file dependency graph is directed
  30. > file_dep_graph;
  31. typedef graph_traits<file_dep_graph>::vertex_descriptor vertex_t;
  32. typedef graph_traits<file_dep_graph>::edge_descriptor edge_t;
  33. template < typename Visitor > void
  34. dfs_v1(const file_dep_graph & g, vertex_t u, default_color_type * color,
  35. Visitor vis)
  36. {
  37. color[u] = gray_color;
  38. vis.discover_vertex(u, g);
  39. graph_traits < file_dep_graph >::out_edge_iterator ei, ei_end;
  40. for (boost::tie(ei, ei_end) = out_edges(u, g); ei != ei_end; ++ei) {
  41. if (color[target(*ei, g)] == white_color) {
  42. vis.tree_edge(*ei, g);
  43. dfs_v1(g, target(*ei, g), color, vis);
  44. } else if (color[target(*ei, g)] == gray_color)
  45. vis.back_edge(*ei, g);
  46. else
  47. vis.forward_or_cross_edge(*ei, g);
  48. }
  49. color[u] = black_color;
  50. vis.finish_vertex(u, g);
  51. }
  52. template < typename Visitor > void
  53. generic_dfs_v1(const file_dep_graph & g, Visitor vis)
  54. {
  55. std::vector < default_color_type > color(num_vertices(g), white_color);
  56. graph_traits < file_dep_graph >::vertex_iterator vi, vi_end;
  57. for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi) {
  58. if (color[*vi] == white_color)
  59. dfs_v1(g, *vi, &color[0], vis);
  60. }
  61. }
  62. struct dfs_visitor_default
  63. {
  64. template <typename V, typename G > void
  65. discover_vertex(V, const G &)
  66. {
  67. }
  68. template <typename E, typename G > void
  69. tree_edge(E, const G &)
  70. {
  71. }
  72. template < typename E, typename G > void
  73. back_edge(E, const G &)
  74. {
  75. }
  76. template < typename E, typename G > void
  77. forward_or_cross_edge(E, const G &)
  78. {
  79. }
  80. template < typename V, typename G > void
  81. finish_vertex(V, const G &)
  82. {
  83. }
  84. };
  85. struct cycle_detector : public dfs_visitor_default
  86. {
  87. cycle_detector(bool & cycle):
  88. has_cycle(cycle)
  89. {
  90. }
  91. void
  92. back_edge(edge_t, const file_dep_graph &)
  93. {
  94. has_cycle = true;
  95. }
  96. bool & has_cycle;
  97. };
  98. bool
  99. has_cycle(const file_dep_graph & g)
  100. {
  101. bool has_cycle = false;
  102. cycle_detector vis(has_cycle);
  103. generic_dfs_v1(g, vis);
  104. return has_cycle;
  105. }
  106. int
  107. main(int argc, const char** argv)
  108. {
  109. std::ifstream file_in(argc >= 2 ? argv[1] : "makefile-dependencies.dat");
  110. typedef graph_traits <file_dep_graph >::vertices_size_type size_type;
  111. size_type n_vertices;
  112. file_in >> n_vertices; // read in number of vertices
  113. std::istream_iterator < std::pair < size_type,
  114. size_type > >input_begin(file_in), input_end;
  115. #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
  116. // VC++ has trouble with the edge iterator constructor
  117. file_dep_graph g(n_vertices);
  118. while (input_begin != input_end) {
  119. size_type i, j;
  120. boost::tie(i, j) = *input_begin++;
  121. add_edge(i, j, g);
  122. }
  123. #else
  124. file_dep_graph g(input_begin, input_end, n_vertices);
  125. #endif
  126. std::vector < std::string > name(num_vertices(g));
  127. std::ifstream name_in(argc >= 3 ? argv[2] : "makefile-target-names.dat");
  128. graph_traits < file_dep_graph >::vertex_iterator vi, vi_end;
  129. for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
  130. name_in >> name[*vi];
  131. assert(has_cycle(g) == false);
  132. return 0;
  133. }