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