cycle-file-dep.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. using namespace boost;
  15. namespace std
  16. {
  17. template < typename T >
  18. std::istream & operator >> (std::istream & in, std::pair < T, T > &p)
  19. {
  20. in >> p.first >> p.second;
  21. return in;
  22. }
  23. }
  24. typedef adjacency_list < listS, // Store out-edges of each vertex in a std::list
  25. vecS, // Store vertex set in a std::vector
  26. directedS // The file dependency graph is directed
  27. > file_dep_graph;
  28. typedef graph_traits < file_dep_graph >::vertex_descriptor vertex_t;
  29. typedef graph_traits < file_dep_graph >::edge_descriptor edge_t;
  30. bool
  31. has_cycle_dfs(const file_dep_graph & g, vertex_t u,
  32. default_color_type * color)
  33. {
  34. color[u] = gray_color;
  35. graph_traits < file_dep_graph >::adjacency_iterator vi, vi_end;
  36. for (boost::tie(vi, vi_end) = adjacent_vertices(u, g); vi != vi_end; ++vi)
  37. if (color[*vi] == white_color) {
  38. if (has_cycle_dfs(g, *vi, color))
  39. return true; // cycle detected, return immediately
  40. } else if (color[*vi] == gray_color) // *vi is an ancestor!
  41. return true;
  42. color[u] = black_color;
  43. return false;
  44. }
  45. bool
  46. has_cycle(const file_dep_graph & g)
  47. {
  48. std::vector < default_color_type > color(num_vertices(g), white_color);
  49. graph_traits < file_dep_graph >::vertex_iterator vi, vi_end;
  50. for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
  51. if (color[*vi] == white_color)
  52. if (has_cycle_dfs(g, *vi, &color[0]))
  53. return true;
  54. return false;
  55. }
  56. int
  57. main(int argc, const char** argv)
  58. {
  59. std::ifstream file_in(argc >= 2 ? argv[1] : "makefile-dependencies.dat");
  60. typedef graph_traits < file_dep_graph >::vertices_size_type size_type;
  61. size_type n_vertices;
  62. file_in >> n_vertices; // read in number of vertices
  63. std::istream_iterator < std::pair < size_type,
  64. size_type > > input_begin(file_in), input_end;
  65. #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
  66. // VC++ has trouble with the edge iterator constructor
  67. file_dep_graph g(n_vertices);
  68. while (input_begin != input_end) {
  69. size_type i, j;
  70. boost::tie(i, j) = *input_begin++;
  71. add_edge(i, j, g);
  72. }
  73. #else
  74. file_dep_graph g(input_begin, input_end, n_vertices);
  75. #endif
  76. std::vector < std::string > name(num_vertices(g));
  77. std::ifstream name_in(argc >= 3 ? argv[2] : "makefile-target-names.dat");
  78. graph_traits < file_dep_graph >::vertex_iterator vi, vi_end;
  79. for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
  80. name_in >> name[*vi];
  81. assert(has_cycle(g) == false);
  82. return 0;
  83. }