topo-sort-file-dep2.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 &
  19. 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 topo_visitor : public dfs_visitor_default
  86. {
  87. topo_visitor(vertex_t * &order) : topo_order(order)
  88. {
  89. }
  90. void
  91. finish_vertex(vertex_t u, const file_dep_graph &)
  92. {
  93. *--topo_order = u;
  94. }
  95. vertex_t*& topo_order;
  96. };
  97. void
  98. topo_sort(const file_dep_graph & g, vertex_t * topo_order)
  99. {
  100. topo_visitor vis(topo_order);
  101. generic_dfs_v1(g, vis);
  102. }
  103. int
  104. main(int argc, const char** argv)
  105. {
  106. std::ifstream file_in(argc >= 2 ? argv[1] : "makefile-dependencies.dat");
  107. typedef graph_traits<file_dep_graph>::vertices_size_type size_type;
  108. size_type n_vertices;
  109. file_in >> n_vertices; // read in number of vertices
  110. std::istream_iterator < std::pair < size_type,
  111. size_type > >input_begin(file_in), input_end;
  112. #if defined(BOOST_MSVC) && BOOST_MSVC <= 1300
  113. // VC++ can't handle the iterator constructor
  114. file_dep_graph g(n_vertices);
  115. while (input_begin != input_end) {
  116. size_type i, j;
  117. boost::tie(i, j) = *input_begin++;
  118. add_edge(i, j, g);
  119. }
  120. #else
  121. file_dep_graph g(input_begin, input_end, n_vertices);
  122. #endif
  123. std::vector < std::string > name(num_vertices(g));
  124. std::ifstream name_in(argc >= 2 ? argv[1] : "makefile-target-names.dat");
  125. graph_traits < file_dep_graph >::vertex_iterator vi, vi_end;
  126. for (boost::tie(vi, vi_end) = vertices(g); vi != vi_end; ++vi)
  127. name_in >> name[*vi];
  128. std::vector < vertex_t > order(num_vertices(g));
  129. topo_sort(g, &order[0] + num_vertices(g));
  130. for (size_type i = 0; i < num_vertices(g); ++i)
  131. std::cout << name[order[i]] << std::endl;
  132. return 0;
  133. }